Another day yet another assignment, which is why I am here. I come with 2 problems.
1. I cannot for the life of me figure out the printf function.
I want to do be able to do this:
Number Type Amount
1 type 2.00
2 type 12.00
Well right now, my program is doing this because I am only using println.
Number Type Amount
1 type 2.00
2 type 12.00
If I try to use printf, a lot of errors in green are popping out, i mean a lot.
2. I am currently working on a bank program and has a Transaction object array, which contains stuff for a bank transaction but suddenly I was asked to use Inheritance, so now, that Transaction class is now a parent class to Check. If the transaction is a check ( meaning the customer is going to cash a check ), I must use the Check class and not the Transaction. My problem is, how can I use the derived Check class if necessary without disrupting my Transaction object array? Codes attached.
Any input would be really appreciated.
Check class:
public class Check extends Transaction
{
private int checkNumber;
public Check(int tId, double tAmt, int tCount, int checkNumber)
{
super(tId, tAmt, tCount);
this.checkNumber = checkNumber;
}
public int getCheckNumber()
{
return checkNumber;
}
public void setCheckNumber(int checkNumber)
{
this.checkNumber = checkNumber;
}
}
Transaction class:
public class Transaction
{
int transNumber;
int transId;
double transAmt;
public Transaction(int num, int id, double amt)
{
transNumber = num;
transId = id;
transAmt = amt;
}
}
CheckingAccount:
import java.text.DecimalFormat;
public class CheckingAccount extends Account
{
private Transaction[] arrayOfTransactions;
private int transCount;
private double balance;
private double totalServiceCharge;
DecimalFormat a = new DecimalFormat("###,###.00");
public CheckingAccount(String name,double initialBalance)
{
super(name,initialBalance);
balance = initialBalance;
arrayOfTransactions = new Transaction[100];
transCount = 0;
}
public double getBalance()
{
return balance;
}
public void setBalance(double transAmt, int tCode)
{
if(tCode == 1)
{
balance = Main.processCheck(transAmt);
}
else if(tCode == 2)
{
balance = Main.processDeposit(transAmt);
}
}
public double getServiceCharge()
{
return totalServiceCharge;
}
public void setServiceCharge(double currentServiceCharge)
{
totalServiceCharge = totalServiceCharge + currentServiceCharge;
}
public int getTrans()
{
return transCount;
}
private void increaseSize()
{
Transaction[] temp = new Transaction[arrayOfTransactions.length * 2];
for (int cd = 0; cd < arrayOfTransactions.length; cd++)
{
temp[cd] = arrayOfTransactions[cd];
}
arrayOfTransactions = temp;
}
public void addTrans(int tempNum, int tempId, double tempAmt)
{
if (transCount == arrayOfTransactions.length)
{
increaseSize();
}
arrayOfTransactions[transCount] = new Transaction (tempNum,tempId,tempAmt);
transCount++;
}
public void printAll()
{
System.out.println("Transaction list for");
System.out.println("Name: " + getName());
System.out.println("ID" + " " + "Type" + " " + "Amount");
for( int i = 0 ; i < transCount ; i++ )
{
if(arrayOfTransactions[i].transId == 1 )
{
System.out.println(arrayOfTransactions[i].transNumber + " " + "check" + " " + arrayOfTransactions[i].transAmt);
}
if(arrayOfTransactions[i].transId == 2 )
{
System.out.println(arrayOfTransactions[i].transNumber + " " + "deposit" + " " + arrayOfTransactions[i].transAmt);
}
if(arrayOfTransactions[i].transId == 3 )
{
System.out.println(arrayOfTransactions[i].transNumber + " " + "svc.chrg." + " " + arrayOfTransactions[i].transAmt);
}
}
}
public void printChecks()
{
System.out.println("Checks cashed:");
System.out.println("ID" + " " + "Amount");
for( int i = 0 ; i < transCount ; i++ )
{
if(arrayOfTransactions[i].transId == 1)
{
System.out.println(arrayOfTransactions[i].transNumber + " " + arrayOfTransactions[i].transAmt);
}
}
}
public void printDeposits()
{
System.out.println("Deposits made:");
System.out.println("ID" + " " + "Amount");
for( int i = 0 ; i < transCount ; i++ )
{
if(arrayOfTransactions[i].transId == 2)
{
System.out.println(arrayOfTransactions[i].transNumber + " " + arrayOfTransactions[i].transAmt);
}
}
}
}
And the main:
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import javax.swing.JFrame;
public class Main
{
public static CheckingAccount c;
public static Transaction t;
public static JFrame frame;
public static boolean oneTimeCharge = false;
public static int tempCounter = 0;
public static void main (String[] args)
{
String temp1,tempName;
double tempBalance;
tempName = JOptionPane.showInputDialog("Enter the account name:");
temp1 = JOptionPane.showInputDialog("Enter initial balance: ");
tempBalance = Double.parseDouble(temp1);
c = new CheckingAccount( tempName,tempBalance );
frame = new JFrame ("Checking Account Actions");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
EOptionsPanel panel = new EOptionsPanel();
frame.getContentPane().add (panel);
frame.pack();
frame.setLocationRelativeTo(frame.getRootPane());
frame.setVisible(true);
}
public static void message(int tempTransCode, double tempTransAmt)
{
String temp3;
DecimalFormat a = new DecimalFormat("###,###.00");
int check = 1;
int deposit = 2;
int serviceCharge = 3;
double serviceChargeChe = 0.15;
double serviceChargeDep = 0.10;
double oneTimeChargeFee = 5.00;
double belowZeroCharge = 10.00;
if( tempTransCode == 1 && c.getBalance() >= 500 )
{
temp3 = c.getName() + "'s Account" + "\nTransaction: Check in amount of $" + a.format(tempTransAmt) + "\nCurrent Balance: $" +
a.format(c.getBalance()) + "\nService Charge: Check -- charge $0.15" + "\nTotal service charge: $" +
a.format(c.getServiceCharge());
c.addTrans(tempCounter,check,tempTransAmt);
tempCounter++;
c.addTrans(tempCounter,serviceCharge,serviceChargeChe);
tempCounter++;
JOptionPane.showMessageDialog(null,temp3);
}
if( tempTransCode == 2 && c.getBalance() >= 500 )
{
temp3 = c.getName() + "'s Account" + "\nTransaction: Deposit in amount of $" + a.format(tempTransAmt) + "\nCurrent Balance: $" +
a.format(c.getBalance()) + "\nService Charge: Deposit -- charge $0.10" + "\nTotal service charge: $" +
a.format(c.getServiceCharge());
c.addTrans(tempCounter,deposit,tempTransAmt);
tempCounter++;
c.addTrans(tempCounter,serviceCharge,serviceChargeDep);
tempCounter++;
JOptionPane.showMessageDialog(null,temp3);
}
if( tempTransCode == 1 && c.getBalance() < 500 && c.getBalance() >= 50 && oneTimeCharge == true )
{
temp3 = c.getName() + "'s Account" + "\nTransaction: Check in amount of $" + a.format(tempTransAmt) + "\nCurrent Balance: $" +
a.format(c.getBalance()) + "\nService Charge: Check -- charge $0.15" + "\nTotal service charge: $" +
a.format(c.getServiceCharge());
c.addTrans(tempCounter,check,tempTransAmt);
tempCounter++;
c.addTrans(tempCounter,serviceCharge,serviceChargeChe);
tempCounter++;
JOptionPane.showMessageDialog(null,temp3);
}
if( tempTransCode == 2 && c.getBalance() < 500 && c.getBalance() >= 50 && oneTimeCharge == true )
{
temp3 = c.getName() + "'s Account" + "\nTransaction: Deposit in amount of $" + a.format(tempTransAmt) + "\nCurrent Balance: $" +
a.format(c.getBalance()) + "\nService Charge: Deposit -- charge $0.10" + "\nTotal service charge: $" +
a.format(c.getServiceCharge());
c.addTrans(tempCounter,deposit,tempTransAmt);
tempCounter++;
c.addTrans(tempCounter,serviceCharge,serviceChargeDep);
tempCounter++;
JOptionPane.showMessageDialog(null,temp3);
}
if( tempTransCode == 1 && c.getBalance() < 500 && oneTimeCharge == false )
{
oneTimeCharge = true;
c.setServiceCharge( 5.00 );
temp3 = c.getName() + "'s Account" + "\nTransaction: Check in amount of $" + a.format(tempTransAmt) + "\nCurrent Balance: $" +
a.format(c.getBalance()) + "\nService Charge: Check -- charge $0.15" + "\nService Charge: Below $500 -- charge $5.00" +
"\nTotal service charge: $" + a.format(c.getServiceCharge());
c.addTrans(tempCounter,check,tempTransAmt);
tempCounter++;
c.addTrans(tempCounter,serviceCharge,serviceChargeChe);
tempCounter++;
c.addTrans(tempCounter,serviceCharge,oneTimeChargeFee);
tempCounter++;
JOptionPane.showMessageDialog(null,temp3);
}
if( tempTransCode == 1 && c.getBalance() < 0 )
{
c.setServiceCharge( 10.00 );
temp3 = c.getName() + "'s Account" + "\nTransaction: Check in amount of $" + a.format(tempTransAmt) + "\nCurrent Balance: $" +
a.format(c.getBalance()) + "\nService Charge: Check -- charge $0.15" +"\nWarning: Balance below $50.00" +
"\nService Charge: Below $0.00 -- charge $10.00" +
"\nTotal service charge: $" + a.format(c.getServiceCharge());
c.addTrans(tempCounter,check,tempTransAmt);
tempCounter++;
c.addTrans(tempCounter,serviceCharge,serviceChargeChe);
tempCounter++;
c.addTrans(tempCounter,serviceCharge,belowZeroCharge);
tempCounter++;
JOptionPane.showMessageDialog(null,temp3);
}
if( tempTransCode == 2 && c.getBalance() < 0 )
{
c.setServiceCharge( 10.00 );
temp3 = c.getName() + "'s Account" + "\nTransaction: Deposit in amount of $" + a.format(tempTransAmt) + "\nCurrent Balance: $" +
a.format(c.getBalance()) + "\nService Charge: Deposit -- charge $0.10" +"\nWarning: Balance below $50.00" +
"\nService Charge: Below $0.00 -- charge $10.00" +
"\nTotal service charge: $" + a.format(c.getServiceCharge());
c.addTrans(tempCounter,deposit,tempTransAmt);
tempCounter++;
c.addTrans(tempCounter,serviceCharge,serviceChargeDep);
tempCounter++;
c.addTrans(tempCounter,serviceCharge,belowZeroCharge);
tempCounter++;
JOptionPane.showMessageDialog(null,temp3);
}
if( tempTransCode == 1 && c.getBalance() < 50 && c.getBalance() >= 0 )
{
temp3 = c.getName() + "'s Account" + "\nTransaction: Check in amount of $" + a.format(tempTransAmt) + "\nCurrent Balance: $" +
a.format(c.getBalance()) + "\nService Charge: Check -- charge $0.15" +"\nWarning: Balance below $50.00" +
"\nTotal service charge: $" + a.format(c.getServiceCharge());
c.addTrans(tempCounter,check,tempTransAmt);
tempCounter++;
c.addTrans(tempCounter,serviceCharge,serviceChargeChe);
tempCounter++;
JOptionPane.showMessageDialog(null,temp3);
}
if( tempTransCode == 2 && c.getBalance() < 50 && c.getBalance() >= 0 )
{
temp3 = c.getName() + "'s Account" + "\nTransaction: Deposit in amount of $" + a.format(tempTransAmt) + "\nCurrent Balance: $" +
a.format(c.getBalance()) + "\nService Charge: Deposit -- charge $0.10" +"\nWarning: Balance below $50.00" +
"\nTotal service charge: $" + a.format(c.getServiceCharge());
c.addTrans(tempCounter,deposit,tempTransAmt);
tempCounter++;
c.addTrans(tempCounter,serviceCharge,serviceChargeDep);
tempCounter++;
JOptionPane.showMessageDialog(null,temp3);
}
}
public static int getTransCode()
{
String temp1;
int tempTrans;
temp1 = JOptionPane.showInputDialog("Enter trans code: ");
tempTrans = Integer.parseInt(temp1);
return tempTrans;
}
public static double getTransAmt()
{
String temp1;
double tempTransAmt;
temp1 = JOptionPane.showInputDialog("Enter trans amount: ");
tempTransAmt = Double.parseDouble(temp1);
return tempTransAmt;
}
public static double processCheck(double transAmt)
{
double temp1;
temp1 = c.getBalance();
temp1 = temp1 - transAmt;
c.setServiceCharge( 0.15 );
return temp1;
}
public static double processDeposit(double transAmt)
{
double temp1;
temp1 = c.getBalance();
temp1 = temp1 + transAmt;
c.setServiceCharge( 0.10 );
return temp1;
}
public static void transactionMaker()
{
frame.setVisible(false);
int tempTransCode;
double tempTransAmt, tempFinalBalance;
String temp2;
DecimalFormat a = new DecimalFormat("###,###.00");
tempTransCode = getTransCode();
if( tempTransCode == 0 )
{
tempFinalBalance = c.getBalance() - c.getServiceCharge();
temp2 = c.getName() + "'s Account" + "\nTransaction: End" + "\nCurrent Balance: $" + a.format(c.getBalance()) + "\nTotal Service Charge: $" +
a.format(c.getServiceCharge()) + "\nFinal Balance: $" + a.format(tempFinalBalance);
JOptionPane.showMessageDialog(null,temp2);
System.exit(0);
}
else
{
tempTransAmt = getTransAmt();
c.setBalance( tempTransAmt, tempTransCode );
message(tempTransCode,tempTransAmt);
frame.setVisible(true);
}
}
public static void getPrintAll()
{
c.printAll();
}
public static void getPrintChecks()
{
c.printChecks();
}
public static void getPrintDeposits()
{
c.printDeposits();
}
}
That's not all the files but the rest are just for the radio buttons and stuff.