-1
import java.util.Scanner;

public class AccountTest {
    public static void main(String[] args) {



        Account account1 = new Account("John Blue", 50.00);
        Account account2 = new Account ("Jane Green", -7.53);
        System.out.printf("To exit, enter -1 for deposit amount.");




        System.out.printf("%s balance: $%.2f%n",
            account1.getName(), account1.getBalance());
        System.out.printf("%s balance: $%.2f%n%n",
            account2.getName(), account2.getBalance());


        Scanner input = new Scanner(System.in);

        System.out.print("Enter deposit amount for account1: ");
        double depositAmount = input.nextDouble();
        System.out.printf("%nadding %.2f to account1 balance%n%n",
            depositAmount);
        account1.deposit(depositAmount);

        System.out.print("Enter withdraw amount for account1: ");
        double withdrawalAmount = input.nextDouble();
        System.out.printf("\nsubtracting %.2f from accojaunt1 balance\n",
            withdrawalAmount);
        account1.Withdraw(withdrawalAmount);


        System.out.printf("%s balance: $%.2f%n",
            account1.getName(), account1.getBalance());
        System.out.printf("%s balance: $%.2f%n%n",
            account2.getName(), account2.getBalance());

        System.out.print("Enter deposit amount for account2: ");
        depositAmount = input.nextDouble();
        System.out.printf("%nadding %.2f to account2 balance%n%n",
            depositAmount);
        account2.deposit(depositAmount);


        System.out.print("Enter withdrawal amount for account2: ");
        withdrawalAmount = input.nextDouble();
        System.out.printf("\nsubtracting %.2f from account2 balance\n",
            withdrawalAmount);
        account2.Withdraw(withdrawalAmount);

        System.out.printf("%s balance: $%.2f%n",
            account1.getName(), account1.getBalance());
        System.out.printf("%s balance: $%.2f%n%n",
            account2.getName(), account2.getBalance());
}
}

Where should I add a while loop? I have tried and tried and get so many errors I don't know how to loop it. I need it to loop the part where it displays the account amount then deposits and withdraws so that you can keep depositing and withdrawing and displaying until you want to quit, and need it to exit if you enter -1.

  • https://stackoverflow.com/questions/10490344/how-to-get-out-of-while-loop-in-java-with-scanner-method-hasnext-as-condition like this? – LOLWTFasdasd asdad Oct 02 '18 at 18:18
  • 2
    It seems like you jumped into coding this without a real plan of how you wanted it to function. I suggest stepping back, taking a pencil and some paper, and writing out the structure of the program and what you want it to perform before writing anything. – beefoak Oct 02 '18 at 18:18
  • I was given this code in my class and told to modify it to add withdraw and loop. I added withdraw but when I sketched out looping it didn't pan out in the code. – DaShiny Oct 02 '18 at 18:49

1 Answers1

1

Usually, when programmers need to sort of 'inflate' an idea on computers, if this idea is complex (programmer-wise) they really have to sketch it on paper first.

You're dealing with an algorithm, which naturally suggests that you need to design it first, so it works well when implemented.

TL;DR

If we slowly write out the pseudo-code that should work this snippet out correctly, it would look like something like this:

start

while true do the following
    ask for some input
    if some input was -1, break out of loop
    else do some fancy calculations

end
mohkamfer
  • 435
  • 3
  • 12