首页 > 解决方案 > 如何重组银行方法中的循环

问题描述

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

然后询问帐户名称(这会按名称从数组列表中获取帐户)

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

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

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

我也无法弄清楚如何让循环在输入 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;
                }
            }
        }
    }
}

标签: javafor-loopif-statementmethodswhile-loop

解决方案


你可以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;
    }
}

推荐阅读