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;
    }   
  }

balance comes out differently every time;

That's hard to understand. Computers are very good at doing the same thing everytime.
If you enter the same amount when the program starts it should do the same thing each time.

Try debugging the code by adding println statements to print out the values of variables as their values change to see where the different results come from.

One problem I see is that the code creates a new arraylist on line 43 every time a transaction is added. That will destroy the old arraylist.

That's what I thought, too. I enter the same amount (initial balance = 500, 1 for check, and transaction amount 50) and the balance should come out 450. but it doesn't. i keep getting different numbers; sometimes, it gives me a negative number.

so should i get rid of the line 43? is this what you are referring to?

group.add(transaction);

This is the line I was refering to:
transList = new ArrayList<Transaction>();

i keep getting different numbers;

What was printed out when you added printlns to show the values of the variables as they are changed?
The print out should show you all the values the variable gets and what is causing it to be different each time the code is executed.

I figured out; i think it had to do with the transList=new ArrayList<Transaction>(); being in other method, instead of being in the constructor. the Calculation comes out okay now. but i have another problem.

so when the program runs, it's supposed to show the transaction code after every transaction ends. for instance, when the user is done with the deposit, thr program is supposed to show the transaction code in order to continue the program. unless the user enters 0, the program doesn't end. instead, the dialogue box for transaction disappears and the program still runs, but the dialogue box for listing is still there. is there any way i could fix this problem?

ahhhh please ignore the previous question. i guess i have to click something from the radio button first.
when i click any listing options, it doesn't display the amount. instead, it displays something like 0.02, 0.04 something.

sorry for asking too many questions at the same time. i'm still a newbie at programming :'(

At what lines of code are the dialog boxes shown?

the dialogue box for listing is still there

Are you saying that more than one dialog box is being shown at the same time?

I figured out the dialog box problem. The GUI dialog box won't shut down unless the user does so. And I guess I have to click one of any choices after each transaction is done.
but i still can't understand why the amount wouldn't show in dialog box. other parts (like ID and parts) DO show but the amount, it keeps displaying things like 0.0, 0.2, 0.4.... did i write something wrong?

keeps displaying things like 0.0, 0.2, 0.4.

Can you show the lines of code that are creating that display? Your code is way too big to go through.

from line 118 t 126

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";
}

from 119 to 126

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";
}

from 119 to 126

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";
}

from 119 to 126

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";
}

from 119 to 126

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";
}

from 119 to 126

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";
}

from 119 to 126

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";
}

from line 118 t 126

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";
}

from 119 to 126

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";
}

from 119 to 126

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";
}

from line 118 t 126

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";
}

I actually figured out everything.....it took a few hours to get everything together.
anyway, thanks for your help :)

I have the exact protect. How many assignments were you assigned?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.