简体   繁体   中英

Resetting an object to zero?

Hi guys I know this is a weird question but can an object return to being zero ? I'm asking because I have a huge design flaw with a basic GUI that i'm running where if I answer what I wanted on a menu it would continue to use the same old answer.

To get around that I would ask again on the lower part but then I ran into a worse problem..it kept on asking. I feel trapped tbh and I can't find anyway inside my programming book to reset or get around this.

Object[] menuPossibilities = {"Create a New Account", "Deposit", "Withdraw", "Display Balance", "Exit"};

Object menuValues = JOptionPane.showInputDialog(null, "Please pick an option from this menu: ",
            "input", JOptionPane.INFORMATION_MESSAGE, null, menuPossibilities, menuPossibilities [0]);
while (!menuValues.equals("Exit")){

    Bank newBank = new Bank();
    // Bank newBank1 = new Bank();
    // Bank newBank2 = new Bank(); Do the same thing as below but switch out
    // bank1 and bank2 as a substitute.
    ArrayList<BankAccount> bankList = newBank.getBankAccounts();

        if (menuValues.equals("Create a New Account")){

            newBank.openAccount();
        }

        else if (menuValues.equals("Deposit")){
            newBank.deposit();
        }
        else if (menuValues.equals("Withdraw")){
            newBank.withdraw();
        }
        else if (menuValues.equals("Display Balace")){
            newBank.deposit();
        }
        else if (menuValues.equals("Exit")){
            System.out.println("Thank you for using our service.");
        }

        Object menuValuesTWO = JOptionPane.showInputDialog(null, "Since you did not pick 5 please pick another option: ",
                    "input", JOptionPane.INFORMATION_MESSAGE, null, menuPossibilities, menuPossibilities [0]);

}

The above is the code I have in case I wasn't very clear. But basically If I take out the menuvalueTWO and hit the create bank account then it'll loop back around and say "So wanna make another bank account?" .

... can an object return to being zero?

Not unless some method explicitly sets it to zero.

In cases like this the problem is typically something else; for example, that you are actually looking at different objects .

In your example, it looks like you have made some mistakes in the way that you have modeled and/or implemented operations like openAccount , deposit and withdraw . I would expect:

  • openAccount() should return the BankAcount it created, and also add it to the Bank's bank account list.

  • deposit() should be an operation on a BankAccount and should take a parameter saying how much to deposit.

  • withdraw() should be an operation on a BankAccount and should take a parameter saying how much to withdraw.

And so on.

(Alternatively, if deposit and withdraw are operations on the Bank object, then they need another parameter - an account number? - to say which account to operate on.)

Object menuValuesTWO = JOptionPane.showInputDialog(null, "Since you did not pick 5 please pick another option: ",
                    "input", JOptionPane.INFORMATION_MESSAGE, null, menuPossibilities, menuPossibilities [0]);

should be

menuValues = JOptionPane.showInputDialog(null, "Since you did not pick 5 please pick another option: ",
                    "input", JOptionPane.INFORMATION_MESSAGE, null, menuPossibilities, menuPossibilities [0]);

I can't validate the change as I cannot run your code. But as @Stephen C suggested you must use the same object that you are looping on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM