Геттеры и сеттеры не работают должным образом - PullRequest
1 голос
/ 23 апреля 2020

Мне нужна помощь, я работаю над заданием, а геттеры и сеттеры не работают. так что я использую слушатель действия, когда нажата кнопка «Добавить сумму», она должна добавить сумму, которая была введена к сумме депозита, и делает это, но когда я вызываю getSavingBalance (); баланс все еще остается на нуле. не очень знакомый со стеком над потоком, не смог опубликовать весь мой код, потому что они говорят, что мне нужно добавить больше деталей, поэтому я просто оставил то, что наиболее важно.

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



            }
        });





    }

Ответы [ 2 ]

2 голосов
/ 23 апреля 2020

В вашем слушателе действий вы звоните setDeposit(deposit);, что увеличивает значение double deposit, а не double savingBalance.

Затем вы звоните getSavingBalance(), который не возвращает ожидаемую сумму , потому что переменная double savingBalance не была увеличена - предполагается, что в геттере нет логики c для вычисления чего-то там?

1 голос
/ 24 апреля 2020

В вашем слушателе действий savingBalance не обновляется при внесении вами депозита. Вам следует обновить savingBalance, когда вы звоните setDeposit.

...