Results 1 to 13 of 13
  1. #1
    OmegaZero69 is offline Member
    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0

    Default Help with some methods, classes and arraylist in a Bank Account program

    My problem is that the program must ask the user for a balance before the GUI frame shows up with what the user wants to do. So I do not know where to put it if in the Main or in the eventListener. Also I am having trouble calling or getting the info from my arraylist. I have the arraylist inside a BankAccount class in a Transaction method but I do not know how to call it in the Main. I would appreciate the help also some suggested reading about using GUI and how to set it up better. I hit a wall and can't seem to figure it out. Thanks.

    line 166 is the listall() in Main

    Sorry if its a little long I did not know what to leave out so I put in Main and the listener that calls main's methods

    I have 4 classes Main, Transaction, BankAccount, AccountPanel

    MAIN class

    Java Code:
    import javax.swing.*;
    import java.awt.Font;
    import java.text.DecimalFormat;
    
    public class Main {
        public static JFrame frame;
        
        public static void main(String [] args)
        {
          String message,inputString;
          double balance;
          double sCharge = 0.0;
               
          inputString = JOptionPane.showInputDialog("Enter your Initial Balance: ");
          balance = Double.parseDouble(inputString);
          
          BankAccount account = new BankAccount (balance);
          account.setServiceCharge(sCharge);
    
          frame = new JFrame ("Checking Account actions");
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
          AccountPanel panel = new AccountPanel();
          frame.getContentPane().add (panel);
    
          frame.pack();
          frame.setVisible(true);
        }//main
        
        public static void inputTrans()
        {
            int TransCode,id;
            double TransAmt,pCheck, pDeposit, balance,finalB;
            double sCharge = 0.0;
            double checkC,depositC;
            int count = 0, transCount = 0;
            String inputString,message;
            
            frame.setVisible(false);
            //or if it belongs here
            BankAccount account = new BankAccount (balance);
            balance = account.getBalance();
            account.setServiceCharge(sCharge);   
            TransCode = Main.getTransCode();      
            DecimalFormat formatter = new DecimalFormat("#0.00");
    
            if(TransCode == 1)    
            {
                        TransAmt = Main.getTransAmt();
                        Transaction transaction = new Transaction(transCount, TransCode, TransAmt);
                        id= transaction.getTransId();
                        account.addTrans(transaction);
                        transCount++;
                        pCheck = Main.processCheck(balance, TransAmt);
                        checkC = .15;
                        transCount++;
                        id = 3;
                        transaction = new Transaction(transCount,id,checkC);
                        account.addTrans(transaction);
                        balance = pCheck;
                        message = "Transaction:Check in ammount of $" 
                                    + formatter.format(TransAmt)
                                    + "\nCurrent Balance : $"
                                    +formatter.format(balance) 
                                    +"\n" +"Service Charge: Check---charge $0.15\n";
    
                        if(balance < 500.0)
                        {
                            if(count == 0)
                            {
                                sCharge += 5.00;
                                account.setServiceCharge(sCharge);
                                message += "Service Charge : Below $500 --- $5.00\n";
                                transCount++;
                                id = 3;
                                transaction = new Transaction(transCount,id,5.00);
                            }// 
                            count++;
                        }//
                        if (balance <0)
                        {
                            sCharge += 10.00;
                            account.setServiceCharge(sCharge);
                            message += "Service Charge : Below $0 --- $10.00\n";
                            transCount++;
                            id = 3;
                            transaction = new Transaction(transCount,id,10.00);
                        }//
                        if (balance <= 50.0)
                        {
                            message += "Warning: Balance below $50\n";
                        }//
                    sCharge += checkC;
                    account.setServiceCharge(sCharge);
                    message += "Tota Service Charge:$"+formatter.format(sCharge);
                    JOptionPane.showMessageDialog(null, message);
                    
                    }//first if
           
            
                    else if(TransCode == 2)
                    {
                
                        TransAmt = getTransAmt();
                        Transaction transaction = new Transaction(transCount, TransCode, TransAmt);
                        id= transaction.getTransId();
                        transCount++;
                        pDeposit = processDeposit(balance, TransAmt);
                        depositC =.10;
                        id = 3;
                        transaction = new Transaction(transCount,id,depositC);
                        account.addTrans(transaction);
                        balance = pDeposit;
                        message = "Transaction:Deposit in ammount of $" 
                                + formatter.format(TransAmt)
                                + "\nCurrent Balance : $"+formatter.format(balance)
                                +"\nService Charge:Deposit---charge $0.10\n";
                        transCount++;
                
                        if(balance < 500.0)
                        {
                            if(count == 0)
                            {
                            sCharge += 5.00;
                            account.setServiceCharge(sCharge);
                            message += "Service Charge : Below $500 --- $5.00\n";
                            transCount++;
                            id = 3;
                            transaction = new Transaction(transCount,id,5.00);
                            account.addTrans(transaction);
                            }
                            count++;
                        }//
                        
                        if (balance <0)
                        {
                            sCharge += 10.00;
                            account.setServiceCharge(sCharge);
                            message += "Service Charge : Below $0 --- $10.00\n";
                            transCount++;
                            id = 3;
                            transaction = new Transaction(transCount,id,10.00);
                            account.addTrans(transaction);
                        }//
                        
                        if (balance <= 50.0)
                        {
                        message += "Warning: Balance below $50\n";
                        }//
                
                        sCharge += depositC;
                        account.setServiceCharge(sCharge);
                        message += "Total Service Charge:$"+formatter.format(sCharge);
                
                        JOptionPane.showMessageDialog(null,message);
    
                    }//else if
          
                    else
                    {
                        JOptionPane.showMessageDialog(null,"You have entered a wrong "+
                                                        "number. Please Try Again.");
                    }//else last
             }
        
        public static void listAll()
        {
            
            JTextArea text = new JTextArea();
            String message = "";
            int num;
            text.setOpaque(false);
            text.setFont(new Font("Monospaced", Font.PLAIN, 14) );
            text.setBorder(null);
            message+="List of Transactions\n\n";
            message+="ID        Type        Amount";
            for(num=0; num < BankAccount.getTransCount();num++)
            {
                message += String.format("%3d        %-9s            %6d\n", num,
                      transList[num].getTransNumber(),transList[num].getTransId(),
                        transList[num].getTransAmount());
            }
        }
        
        public static void listChecks()
        {
            
        }
        public static void listDeposits()
        {
            
        }
        
        public static int getTransCode()
        {
            String inputString;
            int TransCode;
            inputString = JOptionPane.showInputDialog ("Enter trans code: ");
            TransCode = Integer.parseInt(inputString);
            return TransCode;
        }
    
        public static double getTransAmt()
        {
            String inputString;
            inputString = JOptionPane.showInputDialog ("Enter trans amt: ");
            return Double.parseDouble(inputString);
        }
    
        public static double processCheck(double balance,double TransAmt)
        {
            return balance - TransAmt;
        }
    
        public static double processDeposit(double balance,double TransAmt)
        {
            return balance + TransAmt;
        }
    }

    RadioButtonListener
    method inside AccountPanel class

    Java Code:
    private class RadioButtonListener implements ActionListener
       {
           public void actionPerformed (ActionEvent event)
           {
               Object source = event.getSource();
               double balance;
               //balance = BankAccount.getBalance();
               //don't know if it belongs here to get it working
               if (source == listTrans)
               {
                   Main.listAll();
               }
               else if (source == trans)
               {
                   Main.inputTrans();
               }
               else if (source == checks)
               {
                   Main.listChecks();
               }
               else
               {
                   Main.listDeposits();
               }
               
           }
       }
    If there is something missing that I need to post let me know.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Help with some methods, classes and arraylist in a Bank Account program

    Also I am having trouble calling or getting the info from my arraylist.
    The ArrayList class has methods to get objects from the list. What problems are you having using the ArrayList class's methods?
    arraylist inside a BankAccount class in a Transaction method but I do not know how to call it
    How do you want to access the contents of the list? Do you need a method that returns a reference to an object in the list?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    OmegaZero69 is offline Member
    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0

    Default Re: Help with some methods, classes and arraylist in a Bank Account program

    Quote Originally Posted by Norm View Post
    The ArrayList class has methods to get objects from the list. What problems are you having using the ArrayList class's methods?
    well in line 177 when I extract the contents to list them I am getting an erro that says "non-static method getTransCount cannot be refrenced from a static context"

    Quote Originally Posted by Norm View Post
    How do you want to access the contents of the list? Do you need a method that returns a reference to an object in the list?
    The listener method decides what button was pressed and moves from there I wanted to know where do I need to put a messageDialog that asks user to input beginning balance before the panel opens and if I put the Dialog inside the the method of inputTrans then how would I know what the balance was. So I wanted to know if lines 14- 18 should go and how could i access the balance from main inside the method inputTrans so the rest of the method could work. I would get errors of balance not being initialized. Thank you for your response

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Help with some methods, classes and arraylist in a Bank Account program

    "non-static method getTransCount cannot be refrenced from a static context"
    A non-static method only exists when an instance of the class has been created.
    A static method needs to create an instance of the class and use the reference to the class to call the method.

    Too many of the methods in your class are static. Only main() needs to be static. The main() method should be short and its only task should be to call the constructor for the class. The constructor will exist in an instance of the class and will be able to call all its methods.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    OmegaZero69 is offline Member
    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0

    Default Re: Help with some methods, classes and arraylist in a Bank Account program

    Okay so main needs to be static so which methods and pieces of code so I remove or alter. Should I change them to public void the ones in main

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Help with some methods, classes and arraylist in a Bank Account program

    Only the ones that would be called from outside this class need to be public. Some could be private if they are not supposed to be called from another class.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    OmegaZero69 is offline Member
    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0

    Default Re: Help with some methods, classes and arraylist in a Bank Account program

    can you give me an example of the private method and how it would help here since if it becomes private some classes wont be able to call it and my code is a mess so I can't remember if the other ones call it too.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Help with some methods, classes and arraylist in a Bank Account program

    how it would help
    You misunderstood. There is no need to make any methods private.
    A method is made private if you do NOT want other classes to call them.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    OmegaZero69 is offline Member
    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0

    Default Re: Help with some methods, classes and arraylist in a Bank Account program

    okay then what if I get rid of the methods inside the buttonListener and move the contents of the main methods back inside the button listener

  10. #10
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    291
    Rep Power
    4

    Default Re: Help with some methods, classes and arraylist in a Bank Account program

    I would go step by step as Norm has said to you. Create constructor for your class, move all the code from main() method into the constructor, instantiate your class from main() (call the constructor) and then you will be able to adapt the rest of the code (pay attention to make visible all necessary variables by declaring them in proper position).

  11. #11
    OmegaZero69 is offline Member
    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0

    Default Re: Help with some methods, classes and arraylist in a Bank Account program

    Okay when you say move my code from main in a constructor for my class you mean BankAccount and Transaction.I have 4 classes and 2 of them have constructors.


    my Main main creates and call the object account from class BankAccount and object panel from class AccountPanel. Inside AccountPanel there is the GUI creation and a private method of RadioButtonListener that extends ActionListener.

    Inside it call the functions of the buttons which are ListAll(), listDeposits(), listTrans(), and addTrans() which happen to be inside Main as methods and the functions call the other two classes BankAccount and Transaction. So maybe my problem is more logic based since the program goes back and forth between classes.

    Just in case here is the rest of the other code that goes along with it. With main being the first post. points of interest is AccountPanel line 63 and below ( I just need to figure out how to do one of the calls and I could figure out the rest)

    AccountPanel

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    
    public class AccountPanel extends JPanel
    {
       private JLabel messageLabel;
       private JRadioButton listTrans,trans,checks,deposits;
       private JTextField actionTextField;
       
       public AccountPanel()
       {
           messageLabel = new JLabel("Choose Action:");
           messageLabel.setFont(new Font ("Monospace", Font.PLAIN, 20));
           
           actionTextField = new JTextField(10);
           listTrans = new JRadioButton("List of all Transaction");
           trans = new JRadioButton("Enter a Transaction");
           checks = new JRadioButton("List of all Checks");
           deposits = new JRadioButton("List of all Deposits");
           
           ButtonGroup radioButtonGroup = new ButtonGroup();
           radioButtonGroup.add(listTrans);
           radioButtonGroup.add(trans);
           radioButtonGroup.add(checks);
           radioButtonGroup.add(deposits);
           
           RadioButtonListener listener = new RadioButtonListener();
           listTrans.addActionListener(listener);
           trans.addActionListener(listener);
           checks.addActionListener(listener);
           deposits.addActionListener(listener);
           
           add(actionTextField);
           add(listTrans);
           add(trans);
           add(checks);
           add(deposits);     
       }
       private class RadioButtonListener implements ActionListener
       {
           public void actionPerformed (ActionEvent event)
           {
               Object source = event.getSource();
               double balance;
               int TransCode,id;
               double TransAmt,pCheck, pDeposit, balance,finalB;
               double sCharge = 0.0;
               double checkC,depositC;
               int count = 0, transCount = 0;
               String inputString,message;
            
            //frame.setVisible(false);
            //or if it belongs here
            BankAccount account = new BankAccount (balance);
            balance = account.getBalance();
            account.setServiceCharge(sCharge);   
            TransCode = Main.getTransCode();      
            DecimalFormat formatter = new DecimalFormat("#0.00");
               //balance = BankAccount.getBalance();
               //don't know if it belongs here to get it working
               if (source == listTrans)
               {
                   //moved it here from main to see if I can do it from here
                   // instead of in main as methods.
                   JTextArea text = new JTextArea();
                   int num;
                   text.setOpaque(false);
                   text.setFont(new Font("Monospaced", Font.PLAIN, 14) );
                   text.setBorder(null);
                   message+="List of Transactions\n\n";
                   message+="ID        Type        Amount";
                   for(num=0; num < BankAccount.getTransCount();num++)
                    {
                        message += String.format("%3d        %-9s            %6d\n", num,
                                transList[num].getTransNumber(),transList[num].getTransId(),
                                transList[num].getTransAmount());
                    }
               }
               
               else if (source == trans)
               {
                   if(TransCode == 1)    
                    {
                        TransAmt = Main.getTransAmt();
                        Transaction transaction = new Transaction(transCount, TransCode, TransAmt);
                        id= transaction.getTransId();
                        account.addTrans(transaction);
                        transCount++;
                        pCheck = Main.processCheck(balance, TransAmt);
                        checkC = .15;
                        transCount++;
                        id = 3;
                        transaction = new Transaction(transCount,id,checkC);
                        account.addTrans(transaction);
                        balance = pCheck;
                        message = "Transaction:Check in ammount of $" 
                                    + formatter.format(TransAmt)
                                    + "\nCurrent Balance : $"
                                    +formatter.format(balance) 
                                    +"\n" +"Service Charge: Check---charge $0.15\n";
    
                        if(balance < 500.0)
                        {
                            if(count == 0)
                            {
                                sCharge += 5.00;
                                account.setServiceCharge(sCharge);
                                message += "Service Charge : Below $500 --- $5.00\n";
                                transCount++;
                                id = 3;
                                transaction = new Transaction(transCount,id,5.00);
                            }// 
                            count++;
                        }//
                        if (balance <0)
                        {
                            sCharge += 10.00;
                            account.setServiceCharge(sCharge);
                            message += "Service Charge : Below $0 --- $10.00\n";
                            transCount++;
                            id = 3;
                            transaction = new Transaction(transCount,id,10.00);
                        }//
                        if (balance <= 50.0)
                        {
                            message += "Warning: Balance below $50\n";
                        }//
                    sCharge += checkC;
                    account.setServiceCharge(sCharge);
                    message += "Tota Service Charge:$"+formatter.format(sCharge);
                    JOptionPane.showMessageDialog(null, message);
                    
                    }//first if
           
            
                    else if(TransCode == 2)
                    {
                
                        TransAmt = Main.getTransAmt();
                        Transaction transaction = new Transaction(transCount, TransCode, TransAmt);
                        id= transaction.getTransId();
                        transCount++;
                        pDeposit = Main.processDeposit(balance, TransAmt);
                        depositC =.10;
                        id = 3;
                        transaction = new Transaction(transCount,id,depositC);
                        account.addTrans(transaction);
                        balance = pDeposit;
                        message = "Transaction:Deposit in ammount of $" 
                                + formatter.format(TransAmt)
                                + "\nCurrent Balance : $"+formatter.format(balance)
                                +"\nService Charge:Deposit---charge $0.10\n";
                        transCount++;
                
                        if(balance < 500.0)
                        {
                            if(count == 0)
                            {
                            sCharge += 5.00;
                            account.setServiceCharge(sCharge);
                            message += "Service Charge : Below $500 --- $5.00\n";
                            transCount++;
                            id = 3;
                            transaction = new Transaction(transCount,id,5.00);
                            account.addTrans(transaction);
                            }
                            count++;
                        }//
                        
                        if (balance <0)
                        {
                            sCharge += 10.00;
                            account.setServiceCharge(sCharge);
                            message += "Service Charge : Below $0 --- $10.00\n";
                            transCount++;
                            id = 3;
                            transaction = new Transaction(transCount,id,10.00);
                            account.addTrans(transaction);
                        }//
                        
                        if (balance <= 50.0)
                        {
                        message += "Warning: Balance below $50\n";
                        }//
                
                        sCharge += depositC;
                        account.setServiceCharge(sCharge);
                        message += "Total Service Charge:$"+formatter.format(sCharge);
                
                        JOptionPane.showMessageDialog(null,message);
    
                    }//else if
          
                    else
                    {
                        JOptionPane.showMessageDialog(null,"You have entered a wrong "+
                                                        "number. Please Try Again.");
                    }//else last
               }
               else if (source == checks)
               {
                   Main.listChecks();
               }
               else
               {
                   Main.listDeposits();
               }
               
           }
       }
    
    }
    BankAccount

    Java Code:
    import java.awt.Font;
    import javax.swing.JOptionPane;
    import java.util.ArrayList;
    import javax.swing.JTextArea;
    
    public class BankAccount 
    {
        private double balance;
        private double sCharge;
        private ArrayList<Transaction> transList;  
        private int transCount;   
        
        public BankAccount(double initialBalance)
        {
            balance = initialBalance;
            sCharge = 0.0;
        }
        
        public double getBalance()
        {
            return balance;
        }
    
        public void setBalance(double TransAmt, int TransCode)
        {
            if(TransCode == 1)
                balance = balance - TransAmt;
            else //if(tCode == 2)
                balance = balance + TransAmt;
        }
    
        public double getServiceCharge()
        {
            return sCharge;
        }
    
        public void setServiceCharge(double currentServiceCharge)
        {
            sCharge =+ currentServiceCharge;
        }
        public void addTrans(Transaction newTrans)
        {
            transList.add(newTrans);
        }
            
        public int getTransCount()  //returns the current value of transCount;
        {
            return transCount;
        }
           
        public Transaction getTrans(int i) // returns the i-th Transaction object in the list
        {
            return transList.get(i);
        }
    }
    Transaction

    Java Code:
    import java.awt.Font;
    import javax.swing.JTextArea;
    
    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;
        }
    }

  12. #12
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    291
    Rep Power
    4

    Default Re: Help with some methods, classes and arraylist in a Bank Account program

    Okay when you say move my code from main in a constructor for my class you mean BankAccount and Transaction.
    I was referring to your Main class, right approach is to make an constructor with all the code, which is now in main() method, and you will get rid of all
    "non-static method getTransCount cannot be refrenced from a static context"
    One more question, I didn't dig into the logic of your program but I wonder what is your idea behind creating an instance
    BankAccount account = new BankAccount (balance);
    but not using it later in your code
    for(num=0; num < BankAccount.getTransCount();num++)
    Edit: I just observed that getTransCount() is not even static, so it might as well be the reason for "non-static method..." error..
    Last edited by milovan; 11-03-2013 at 09:43 AM.

  13. #13
    OmegaZero69 is offline Member
    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0

    Default Re: Help with some methods, classes and arraylist in a Bank Account program

    Quote Originally Posted by milovan View Post
    One more question, I didn't dig into the logic of your program but I wonder what is your idea behind creating an instance

    but not using it later in your code
    Quote Originally Posted by milovan View Post
    I just observed that getTransCount() is not even static, so it might as well be the reason for "non-static method..." error..
    that stays private I tried changing the method to static but it says "nonstatic variable cannot be referenced by static context"

    This program is a tweaked version of a previous program that I created and they told us to add some new data to make it into a GUI program. So maybe my mistake was my approach and what I added to what part.

    This is Main the first version with JOPtionPane's

    Java Code:
    import javax.swing.*;
    import java.text.DecimalFormat;
    import java.awt.event.*;
    public class Main {
        
        public static void main(String[] args) {
            // TODO code application logic here
        int TransCode;
        double TransAmt,pCheck, pDeposit, balance,finalB;
        double sCharge = 0.0;
        double checkC,depositC;
        int count = 0;
        String inputString,message;
        DecimalFormat formatter = new DecimalFormat("#0.00");
     
        
        inputString = JOptionPane.showInputDialog ("Enter your Initial Balance: ");
        balance = Double.parseDouble(inputString);
        BankAccount account = new BankAccount (balance);
        account.setServiceCharge(sCharge);
       do{
            TransCode = getTransCode();
           
            if(TransCode == 1)
            {
                TransAmt = getTransAmt();
                pCheck = processCheck(balance, TransAmt);
                checkC = .15;
                balance = pCheck;
                message = "Transaction:Check in ammount of $"+ formatter.format(TransAmt) + "\n"
                                    + "Current Balance : $"+formatter.format(balance) 
                                    +"\n" +"Service Charge: Check---charge $0.15\n";
    
                if(balance < 500.0){
                    if(count == 0)
                    {
                    sCharge += 5.00;
                    account.setServiceCharge(sCharge);
                    message += "Service Charge : Below $500 --- $5.00\n";
                    }
                    count++;
                }
                if (balance <0){
                    sCharge += 10.00;
                    account.setServiceCharge(sCharge);
                    message += "Service Charge : Below $0 --- $10.00\n";
                }
                if (balance <= 50.0){
                    message += "Warning: Balance below $50\n";
                }
                sCharge += checkC;
                account.setServiceCharge(sCharge);
                message += "Tota Service Charge:$"+formatter.format(sCharge);
                JOptionPane.showMessageDialog(null, message);
            }//first if
            
            else if(TransCode == 2)
            {
                
                TransAmt = getTransAmt();
                pDeposit = processDeposit(balance, TransAmt);
                depositC =.10;
                balance = pDeposit;
                message = "Transaction:Deposit in ammount of $" + formatter.format(TransAmt) + "\n"
                            + "Current Balance : $"+formatter.format(balance)+"\n"
                            +"Service Charge:Deposit---charge $0.10\n";
                
                if(balance < 500.0){
                    if(count == 0)
                    {
                    sCharge += 5.00;
                    account.setServiceCharge(sCharge);
                    message += "Service Charge : Below $500 --- $5.00\n";
                    }
                    count++;
                }
                if (balance <0){
                    sCharge += 10.00;
                    account.setServiceCharge(sCharge);
                    message += "Service Charge : Below $0 --- $10.00\n";
                }
                if (balance <= 50.0){
                    message += "Warning: Balance below $50\n";
                }
                sCharge += depositC;
                account.setServiceCharge(sCharge);
                message += "Total Service Charge:$"+formatter.format(sCharge);
                
                JOptionPane.showMessageDialog(null,message);
    
            }
            else if (TransCode == 0){
            }
    
            else
            {
                JOptionPane.showMessageDialog(null,"You have entered a wrong "+
                        "number. Please Try Again.");
            }
        }while (TransCode != 0);//do
        finalB = balance - sCharge;
        JOptionPane.showMessageDialog(null,"Transaction:End"
                        + "\nCurrent Balance : $" + formatter.format(balance)
                        + "\nTotal Service Charge: $" + formatter.format(sCharge) 
                        + "\nFinal Balance: $" + formatter.format(finalB));
       
        
        }//main
    
        
        public static int getTransCode()
       {
            String inputString;
            int TransCode;
            inputString = JOptionPane.showInputDialog ("Enter trans code: ");
            TransCode = Integer.parseInt(inputString);
            return TransCode;
       }
    
       public static double getTransAmt()
       {
            String inputString;
            inputString = JOptionPane.showInputDialog ("Enter trans amt: ");
            return Double.parseDouble(inputString);
       }
    
       public static double processCheck(double balance,double TransAmt)
       {
            return balance - TransAmt;
       }
    
       public static double processDeposit(double balance,double TransAmt)
    
       {
            return balance + TransAmt;
       }
    }

    and Bank Account

    Java Code:
    import javax.swing.JOptionPane;
    public class BankAccount {
        private double balance;
        private double sCharge;
    
        public BankAccount(double initialBalance)
          {
                balance = initialBalance;
                sCharge = 0.0;
          }
    
          public double getBalance()
          {
                
                return balance;
          }
    
          public void setBalance(double TransAmt, int TransCode)
          {
                if(TransCode == 1)
                    balance = balance - TransAmt;
                else //if(tCode == 2)
                    balance = balance + TransAmt;
          }
    
          public double getServiceCharge()
          {
                return sCharge;
          }
    
            public void setServiceCharge(double currentServiceCharge)
          {
                sCharge =+ currentServiceCharge;
          }
    }

    And what we needed to add was this

    Java Code:
    New data and methods for the CheckingAccount Class
    
    ---------------------------------------------------------------------------
    
    private ArrayList<Transaction> transList;  // keeps a list of Transaction objects for the account
    
     
    
    private int transCount;   // the count of Transaction objects and used as the ID for each transaction
    
                              
    
    public void addTrans( Transaction newTrans)   // adds a transaction object to the transList
    
     
    
    public int gettransCount()  //returns the current value of transCount;
    
     
    
    public Transaction getTrans(int i) // returns the i-th Transaction object in the list
    
     
    
    --------------------------------------------------------------------------
    
    [B]New Transaction class[/B]
    
    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;
        }
    
    }
    With this being the Assignment instructions

    As a follow up to Assignment1, 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
    So If I can get some guidance on where to begin or how because I have attempted it a couple of times I am getting stuck in the same place with the calling of balance from BankAccount in the eventListener. I understand some of the basics of it but I have never tried a gui with multiple classes and calling of the class methods. I have also learned from examples and the ones I have read are of 2 classes and calling it but it isn't static like this one where balance is initialized in the beginning and changes throughout the program. Thank you for the help I greatly appreciate it.

Similar Threads

  1. Simple Bank Account Program - Completely Confused
    By Interista in forum New To Java
    Replies: 4
    Last Post: 11-02-2011, 02:12 AM
  2. basic bank account
    By captain_turkiye in forum New To Java
    Replies: 5
    Last Post: 10-16-2011, 05:15 PM
  3. Bank Account Project
    By Haal in forum New To Java
    Replies: 0
    Last Post: 03-20-2011, 07:30 PM
  4. how to make to classes in the bank account program
    By buzzing in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 04:02 AM
  5. Bank Account
    By HPcompaq256 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 09:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •