如何重组银行业务方法中的循环

时间:2018-09-10 11:44:05

标签: java for-loop if-statement methods while-loop

我目前正在尝试编写一种方法,该方法首先要求存款,提款或退出。

然后询问帐户名称(这将从名称中从阵列列表中获取帐户)

然后提示您输入所选存款或提取的金额。

然后返回(存款,提款,退出)提示

我编写的代码顺序错误。 (它要求输入名称,然后是操作,然后是金额),我不知道如何将其更改为上面所需的顺序。

我也不知道如何在输入3时退出循环。

我想我很近...似乎无法从这里前进。 (ps while true循环只是我试图弄清楚其余部分时使用的一个临时循环)

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;
                }
            }
        }
    }
}

3 个答案:

答案 0 :(得分:0)

从您的代码中,我了解到accountsarrayList的{​​{1}}。

如果是这样,则可以删除此Account条件

while

并使用while(accountName.equalsIgnoreCase(y.getName())) {

if

这可能是重组的重点之一。

答案 1 :(得分:0)

我想您只是想将顺序更改为以下内容:

  1. 操作(存款,提款,退出)
  2. 帐户名
  3. 金额(仅存入/提取)

这可以通过在方法开始时扫描动作轻松地实现。 因此,您只需要将这部分移到循环循环的开头即可:

Scanner scan3 = new Scanner(System.in);
System.out.println("1:Deposit   2:Withdraw  3:Quit");
int request = scan3.nextInt();

此外,您还必须将第二个while循环更改为if语句。否则,一旦输入循环,循环将不会停止,并且无法选择其他操作。

所以改变这个:

while(accountName.equalsIgnoreCase(y.getName()))

对此:

if(accountName.equalsIgnoreCase(y.getName()))

所以更正后的代码如下:

public void banking()
{
    while(true)
    {
    Scanner scan3 = new Scanner(System.in);
    System.out.println("1:Deposit   2:Withdraw  3:Quit");
    int request = scan3.nextInt();

    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...
    {
        if(accountName.equalsIgnoreCase(y.getName()))
        {
            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;
                }
        }
    }
    }
}

还可以进一步提高效率。例如,如果用户选择“退出”,则避免for循环。 (但这是可选的,不是必需的)

即使这可以解决您的问题,但我必须告诉您,永远不要使用true作为while循环的条件。

答案 2 :(得分:0)

您可以像这样使用switch()

interface Account {
    String getName();

    BigDecimal getBalance();

    void deposit(BigDecimal value);

    void withdraw(BigDecimal value);
}

enum Action {
    DEPOSIT(1),
    WITHDRAW(2),
    QUIT(3),
    UNRECOGNIZED(0);

    private final int value;

    Action(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public static Action getAction(int value) {
        for (Action action : Action.values()) {
            if (action.getValue() == value) {
                return action;
            }
        }
        return UNRECOGNIZED;
    }
}

private static Account getAcc(List<Account> accounts, String acc) {
    return accounts.stream().filter(account -> account.getName().equalsIgnoreCase(acc)).findFirst()
            .orElseThrow(RuntimeException::new);
}

public static void banking(List<Account> accounts) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("please enter number for given action:");
    System.out.println("1 - : deposit");
    System.out.println("2 - : withdraw");
    System.out.println("3 - : quit");

    Action action = Action.getAction(scanner.nextInt());

    if (Action.QUIT == action){
        System.out.println("quiting ...");
        return;
    }


    System.out.println("please enter the name for the account");
    String accountName = scanner.nextLine();
    Account account = getAcc(accounts, accountName);

    switch (action) {
    case WITHDRAW:
        System.out.println("please make a withdrawl");
        BigDecimal withdrawl = new BigDecimal(scanner.nextDouble());
        if (withdrawl.compareTo(account.getBalance()) > 0) {
            System.out.println("Insufficient funds");
        } else {
            account.withdraw(withdrawl);
        }
        break;
    case DEPOSIT:
        System.out.println("please make a deposit");
        BigDecimal newBalance = new BigDecimal(scanner.nextDouble());
        account.deposit(newBalance);
        break;
    default:
        break;
    }
}