So I'm writing an event-driven program. The program is supposed to calculate checks and deposits, but the calculation comes out completely wrong. For instance, if i type 500 for initial balance, click 'entering a transaction', type 1 for checks, and type the amount 50 for transaction amount, the remaining balance should be 450. instead, the balance comes out differently every time; sometimes it would be like -600, or -400, or -1000. i have no idea.
i also have another question. i'm not really familiar with GUI programming but I tried my best to write. for 'listing all transactions', the list won't appear at all. same for other choices (listing all checks, listing all deposits).
so here's the problem:
modify the CheckingAccount class to add an array(self expanding type) or ArrayList of Transaction objects and a int variable transCount(incremented each time a new Transaction object is created). Create a Transaction class that stores an int transNumber(will be the current value of transCount), an int transId (1 = check, 2 = deposit 3 = service charge) and a double transAmt. Prompt the user for the choice of a) entering a transaction, b) listing all transations, c) listing all checks, d) listing all deposits.(implement these options with JFrame,JLabel, JRadioButton, ButtonGroup, and a listener) The listings must go to a Dialog box. Add methods to the CheckingAccount class like addTrans(put a trans into the account), getTrans(get a transaction from the array) getTransCount(get the current value of transCount class variable) and any other methods you wish to implement. This program is event-driven. You cannot put all the processing steps/logic in main. You can put them in helper methods in the Main class, in the GUI objects class, or in the CheckingAccount class, so they can be called by the listener method.
and here are my codes:
Main:
import javax.swing.JFrame;
public class Main
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Checking Account Actions");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CheckingAccountActions panel = new CheckingAccountActions();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
CheckingAccountActions class:
import java.awt.*;
import java.awt.event.*;
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;
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 transactionCode;
double transAmt = 0;
int i = 0;
if(source==transaction)
{
transactionCode = getTransCode();
t = new Transaction(ca.getTransCount(),transactionCode,transAmt);
ca.addTrans(t);
switch(transactionCode)
{
case 1:
transAmt = getTransAmt();
ca.setBalance(transAmt, transactionCode);
processCheck(transAmt);
JOptionPane.showMessageDialog(null, transList.get(i));
break;
case 2:
transAmt = getTransAmt();
ca.setBalance(transAmt, transactionCode);
processDeposit(transAmt);
JOptionPane.showMessageDialog(null, transList.get(i));
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.");
}
}
else if(source==listTrans)
{
int types = 0;
int nums = 0;
double amount = 0;
String line;
line = String.format("List All Transactions" + "\n"
+ "ID Type Amount" + "\n");
for(int index = 0; index < ca.getSize(); index++)
{
types = ca.getTrans(index).getTransId();
nums = ca.getTrans(index).getTransNumber();
amount = ca.getTrans(index).getTransAmount();
line += String.format("%-10s %3d %10s", nums,types,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==checks)
{
String line = String.format("Checks made: \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)
{
String line = String.format("Deposits made: \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()
{
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 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;
}
}
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;
}
public double getBalance()
{
return balance;
}
public void setBalance(double transAmt, int tCode)
{
if(tCode==1)
balance-=transAmt;
else if(tCode==2)
balance+=transAmt;
}
public double getServiceCharge()
{
return totalServiceCharge;
}
public void setServiceCharge(double currentServiceCharge)
{
totalServiceCharge += currentServiceCharge;
}
public void addTrans(Transaction newTrans)
{
transList = new ArrayList<Transaction>();
transList.add(newTrans);
}
public int getTransCount()
{
transCount++;
return transCount;
}
public Transaction getTrans(int i)
{
return transList.get(i);
}
public int getSize()
{
transSize = transList.size();
return transSize;
}
}
Transaction class:
public class Transaction
{
private int transNumber;
private int transId;
private double transAmt;
public Transaction(int number, int id, double amount)
{
transNumber = number;
transId = id;
transAmt = amount;
}
public int getTransNumber()
{
return transNumber;
}
public int getTransId()
{
return transId;
}
public double getTransAmount()
{
return transAmt;
}
}