Getters and setters not working as it should

Tags: , , , ,



I need help im working on an assignment and getters and setters are not working. so im using an action listener when add amount is clicked it should add the amount that was entered to deposit amount and it does that but when i call getSavingBalance(); the balance still remains at zero. not that familiar with stack over flow couldnt post my entire code because they say i need to add more details so i just left whats of most importance.

JButton addBtn = new JButton("Add Amount"); 
        addBtn.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent event) {

                double deposit= Double.parseDouble(balance.getText());

                setDeposit(deposit); 
                accBal.setText("Account Balance: "+getSavingBalance());
            }



public class Bank {

    int accountNumber; 
    static String accountType; 
    static double savingBalance;  
    static double deposit; 

    public Bank() {
        this.accountNumber = 85061; 
        accountType = "Savings";

    }


    public static void setDeposit(double depos) {

        deposit = deposit+depos; 
    }


    public static void setSavingBalance(double saving) {

        savingBalance = saving; 

        savingBalance+= deposit -withdraw;

    }



    public static void savingsFrame() {

        JLabel accBal = new JLabel("Account Balance: "+ getSavingBalance()); 
        JFrame savingsFrame = new JFrame("Bank Account"); 
        savingsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel savingsPanel= new JPanel(new GridLayout(2,2,1,1));  
        //savingsPanel.setBackground(Color.gray);
        savingsPanel.setPreferredSize(new Dimension(550,100));

        TitledBorder savingsTitle = new TitledBorder("Savings Account");
        savingsTitle.setTitleJustification(TitledBorder.CENTER);
        savingsPanel.setBorder(savingsTitle);

        JLabel bal = new JLabel("Deposit Amount: "); 
        JTextField balance = new JTextField(); 

        JLabel draw = new JLabel("Withdraw Amount: "); 
        JTextField withdraw = new JTextField();

        JButton addBtn = new JButton("Add Amount"); 
        addBtn.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent event) {

                double deposit= Double.parseDouble(balance.getText());

                setDeposit(deposit); 
                accBal.setText("Account Balance: "+getSavingBalance());
            }
        });

        JButton withdrawBtn = new JButton("Withdraw Amount"); 
        withdrawBtn.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent event) {



            }
        });


        JButton updateBtn = new JButton("Update Account"); 
        updateBtn.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent event) {

                accBal.setText("Account Balanceee: "+ getSavingBalance());



            }
        });





    }

Answer

In your action listener, savingBalance is not updated when you deposit. You should update savingBalance when you call setDeposit.



Source: stackoverflow