So the program is supposed to display the box like this (for LIST ALL TRANSACTIONS)
List all transactions: ID Type Amount
0 check 50.00
1 svr.chg 5.15
2 deposit 40.00
3 svr.chg 0.10
the box displays other transactions; it saves the old and new ones and display them one by one.
however, service charge doesn't show. instead, it keeps displaying the new ones and does not display the old ones.
do i have to make another method to do this? so this is how my program displays the box:
List all transactions:
ID Type Amount
0 check 50.00
0 svr.chg 5.25
1 deposit 40.00
1 svr.chg 5.20
(check amount 50 -> deposit amount -> 40)
also, i can't figure out how to display the number in order, instead of displaying in 0-0 1-1 2-2 and so on.
i know i have to add something more and edit here and there, but i just can't figure out.
here's my code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.ArrayList;
import javax.swing.*;
public class CheckingAccountActions extends JPanel
{
public static double initialBalance, transactionAmount;
public static int firstTime = 0;
public static int transactionCode;
static CheckingAccount ca;
static DecimalFormat dollar;
static Transaction t;
public static int check = 1, deposit = 2, serviceCharge = 3;
private JLabel message;
private JRadioButton transaction, listTrans, checks, deposits;
ArrayList transList;
public CheckingAccountActions()
{
initialBalance = initialBalance();
ca = new CheckingAccount(initialBalance);
dollar = new DecimalFormat("#,###.00");
message = new JLabel ("Choose an action: ");
message.setFont (new Font ("Helvetica", Font.BOLD, 24));
transaction = new JRadioButton("Entering a Transaction");
transaction.setBackground (Color.yellow);
listTrans = new JRadioButton("Listing All Transactions");
listTrans.setBackground(Color.yellow);
checks = new JRadioButton("Listing All Checks");
checks.setBackground(Color.yellow);
deposits = new JRadioButton("Listing All Deposits");
deposits.setBackground(Color.yellow);
ButtonGroup group = new ButtonGroup();
group.add(transaction);
group.add(listTrans);
group.add(checks);
group.add(deposits);
CheckingAccountActionsListener listener = new CheckingAccountActionsListener();
transaction.addActionListener(listener);
listTrans.addActionListener(listener);
checks.addActionListener(listener);
deposits.addActionListener(listener);
add(message);
add(transaction);
add(listTrans);
add(checks);
add(deposits);
setBackground(Color.YELLOW);
setPreferredSize (new Dimension(250, 180));
}
private class CheckingAccountActionsListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
int i = 0;
if(source==transaction)
{
transactionCode = getTransCode();
switch(transactionCode)
{
case 1:
transactionAmount = getTransAmt();
processCheck(transactionAmount);
break;
case 2:
transactionAmount = getTransAmt();
processDeposit(transactionAmount);
break;
case 0:
JOptionPane.showMessageDialog(null, "Transaction: End\n"
+ "Current Balance: $" + ca.getBalance() + "\n"
+ "Total Service Charge: $" + dollar.format(ca.getServiceCharge()) + "\n"
+ "Final Balance: $"
+ dollar.format(ca.getBalance() - ca.getServiceCharge()));
break;
default:
JOptionPane.showMessageDialog(null, "Invalid Choice. Enter Again.");
}
t = new Transaction(ca.getTransCount(),transactionCode,transactionAmount);
ca.addTrans(t);
JOptionPane.showMessageDialog(null, transList.get(i));
}
else if(source==listTrans)
{
int nums;
double amount, service;
String line, message = "";
line = "List All Transactions" + "\n"
+ "ID Type Amount" + "\n";
for(int index = 0; index < ca.getSize(); index++)
{
nums = ca.getTrans(index).getTransNumber();
switch(ca.getTrans(index).getTransId())
{
case 1:
message = "Check ";
break;
case 2:
message = "Deposits ";
break;
case 3:
message = "Svr.Chrg";
break;
}
amount = ca.getTrans(index).getTransAmount();
service = getServiceCharge();
line += String.format("%-10d %7s %10.2f", nums,message,amount) + "\n" ;
line += String.format("%-10d %7s %10.2f", nums,"Svr.Chrg",service) + "\n";
}
JTextArea text = new JTextArea(line);
text.setBorder(null);
text.setOpaque(false);
text.setFont(new Font("Monospaced", Font.PLAIN, 14));
JOptionPane.showMessageDialog(null, text);
}
else if(source==checks)
{
int nums = 0;
double amount = 0.0;
String line = "Checks made: \n" + "ID \t Amount \n";
for(int index = 0; index<ca.getSize(); index++)
{
if(ca.getTrans(index).getTransId()==1)
{
nums = ca.getTrans(index).getTransNumber();
amount = ca.getTrans(index).getTransAmount();
}
line+=String.format("%-10d %10.2f", nums,amount)+"\n";
}
JTextArea text = new JTextArea(line);
text.setBorder(null);
text.setOpaque(false);
text.setFont(new Font("Monospaced", Font.PLAIN, 14) );
JOptionPane.showMessageDialog(null, text);
}
else if(source==deposits)
{
int nums = 0;
double amount = 0.0;
String line = "Deposits made: \n"+"ID \t Amount \n";
for(int index = 0; index<ca.getSize(); index++)
{
if(ca.getTrans(index).getTransId()==2)
{
nums = ca.getTrans(index).getTransNumber();
amount = ca.getTrans(index).getTransAmount();
}
line+=String.format("%-10d %10.2f", nums,amount)+"\n";
}
JTextArea text = new JTextArea(line);
text.setBorder(null);
text.setOpaque(false);
text.setFont(new Font("Monospaced", Font.PLAIN, 14) );
JOptionPane.showMessageDialog(null, text);
}
}
}
public static int getTransCode()
{
int code;
String userInput;
userInput=JOptionPane.showInputDialog("Enter the transaction code:\n"+
"1) Check \n2) Deposit \n0) Exit the program");
code=Integer.parseInt(userInput);
return code;
}
public static double initialBalance()
{
String userInput;
userInput = JOptionPane.showInputDialog("Enter initial account balance :");
initialBalance = Double.parseDouble(userInput);
return initialBalance;
}
public static double getTransAmt()
{
double amount;
String userInput;
userInput=JOptionPane.showInputDialog("Enter the transaction amount: ");
amount=Double.parseDouble(userInput);
return amount;
}
public static double getServiceCharge()
{
double charge = 0.0;
if(transactionCode==1)
{
charge = 0.15;
}
else if(transactionCode==2)
{
charge = 0.10;
}
return charge;
}
public static double processCheck(double transAmt)
{
ca.setBalance(transAmt, 1);
ca.setServiceCharge(0.15);
if(ca.getBalance()<500)
{
if(firstTime==0)
{
ca.setServiceCharge(5.00);
JOptionPane.showMessageDialog(null,"Transaction Amount: Check in Amount of $"
+ transactionAmount + "\n" +"Current Balance: $" + dollar.format(ca.getBalance())
+ "\n"+"Service Charge: Check --- charge $0.15 \n"
+ "Service Charge: Below $500 --- charge $5.00\n"
+ "Total Service Charge: $" + dollar.format(ca.getServiceCharge()) );
firstTime++;
}
JOptionPane.showMessageDialog(null,"Transaction Amount: Check in Amount of $"
+ transactionAmount + "\n" +"Current Balance: $" + dollar.format(ca.getBalance())
+ "\n"+"Service Charge: Check --- charge $0.15 \n"
+ "Total Service Charge: $" + dollar.format(ca.getServiceCharge()) );
}
else
{
JOptionPane.showMessageDialog(null,"Transaction Amount: Check in Amount of $"
+ transactionAmount + "\n" + "Current Balance: $"
+ dollar.format(ca.getBalance())
+ "\n"+"Service Charge: Check --- charge $0.15 \n"
+ "Service Charge: None\n" + "Total Service Charge: $"
+ dollar.format(ca.getServiceCharge()) );
}
if(ca.getBalance()<0)
{
ca.setServiceCharge(10.00);
JOptionPane.showMessageDialog(null, "Your balance is under $0.");
}
if(ca.getBalance()<50)
{
JOptionPane.showMessageDialog(null, "Your balance is under $50.");
}
return transAmt;
}
public static double processDeposit(double transAmt)
{
ca.setBalance(transAmt, 2);
ca.setServiceCharge(0.10);
if(ca.getBalance()<500)
{
if(firstTime==0)
{
ca.setServiceCharge(5.00);
JOptionPane.showMessageDialog(null,"Transaction Amount: Deposit in Amount of "
+ transactionAmount + "\n" +"Current Balance: "
+ dollar.format(ca.getBalance())
+ "\n"+"Service Charge: Deposit --- charge $0.10 \n"
+ "Service Charge: Below $500 --- charge $5.00\n"
+ "Total Service Charge: " + dollar.format(ca.getServiceCharge()));
}
JOptionPane.showMessageDialog(null,"Transaction Amount: Deposit in Amount of "
+ transactionAmount + "\n" +"Current Balance: "
+ dollar.format(ca.getBalance())
+ "\n"+"Service Charge: Deposit --- charge $0.10 \n"
+ "Total Service Charge: $" + dollar.format(ca.getServiceCharge()));
}
else
{
JOptionPane.showMessageDialog(null,"Transaction Amount: Deposit in Amount of "
+ transactionAmount + "\n" +"Current Balance: "
+ dollar.format(ca.getBalance())
+ "\n"+"Service Charge: Deposit --- charge $0.10 \n"
+ "Service Charge: None\n" + "Total Service Charge: $"
+ dollar.format(ca.getServiceCharge()));
}
if(ca.getBalance()<0)
{
ca.setServiceCharge(10.00);
JOptionPane.showMessageDialog(null, "Your balance is under $0.");
}
if(ca.getBalance()<50)
{
JOptionPane.showMessageDialog(null, "Your balance is under $50.");
}
return transAmt;
}
}
and here's my CheckingAccount class:
import java.util.ArrayList;
public class CheckingAccount
{
private double balance;
private double totalServiceCharge;
private ArrayList<Transaction> transList;
private int transCount = 0;
private int transSize = 0;
public CheckingAccount(double initialBalance)
{
balance = initialBalance;
totalServiceCharge = 0;
transList = new ArrayList<Transaction>();
}
public double getBalance()
{
return balance;
}
public void setBalance(double transAmt, int tCode)
{
if(tCode==1)
balance-=transAmt;
else if(tCode==2)
balance+=transAmt;
}
public void setServiceCharge(double currentServiceCharge)
{
totalServiceCharge += currentServiceCharge;
}
public double getServiceCharge()
{
return totalServiceCharge;
}
public void addTrans(Transaction newTrans)
{
transList.add(newTrans);
transCount++;
}
public int getTransCount()
{
return transCount;
}
public Transaction getTrans(int i)
{
return transList.get(i);
}
public int getSize()
{
transSize = transList.size();
return transSize;
}
}