King Bogle
King Bogle

Reputation: 23

Getters and setters not working as it should

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());



            }
        });





    }

Upvotes: 1

Views: 220

Answers (2)

Ausaf Ahmed
Ausaf Ahmed

Reputation: 26

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

Upvotes: 1

Petar Bivolarski
Petar Bivolarski

Reputation: 1767

In you action listener you are calling setDeposit(deposit);, which increments the value of double deposit and not of double savingBalance.

You are then calling getSavingBalance(), which does not return the expected amount, because the variable double savingBalance was not incremented - asuming you do not have any logic in the getter to calculcate something there?

Upvotes: 2

Related Questions