lundi 10 septembre 2018

Java How to restructure loops in banking method

Im currently trying to write a method that will first ask for a deposit, withdraw, or quit.

Then ask for the account name (this fetches the account from the array list by the name)

Then prompt for an amount for the selected deposit or withdraw..

Then return back to the (deposit, withdraw, quit) prompt

The code I have written is in the wrong order. (It asks for the name, then the action, then the amount) and i can't work out how to change it to the desired order above. I also can't work out how to get the loop to 'quit' on an input of 3.

I think I'm close... just can't seem to move forward from here. (ps the while true loop is just a temporary loop I was using while I tried to figure out the rest) Thanks in advance!

public void banking()
{
    while(true)
    {        

    Scanner scan4 = new Scanner(System.in);
    System.out.println("please enter the name for the account"); //takes the name of the account to select the correct object in arraylist
    String accountName = scan4.nextLine();

    for(Account y: accounts)                                    //for all the objects in the arraylist...
    {
        while(accountName.equalsIgnoreCase(y.getName()))
        {
            Scanner scan3 = new Scanner(System.in);
            System.out.println("1:Deposit   2:Withdraw  3:Quit");
            int request = scan3.nextInt();

            if(request == 1)
                {
                    Scanner scan = new Scanner(System.in);
                    System.out.println("please make a deposit");
                    double newBalance = scan.nextDouble();
                    y.deposit(newBalance);
                }

            else if (request == 2)
                {
                    Scanner scan2 = new Scanner(System.in);
                    System.out.println("please make a withdrawl");
                    double withdrawl = scan2.nextDouble();

                    if(withdrawl > y.getBalance())
                        {
                            System.out.println("Insufficient funds");
                        }
                    else
                        {
                            y.withdraw(withdrawl);
                        }


                }

            else if (request == 3)
                {
                   break;
                }
        }
    }
    }
}

Aucun commentaire:

Enregistrer un commentaire