方法未使用正确的解析输入

时间:2018-09-23 09:13:33

标签: java parsing input parameters arguments

我收到了几乎完成的作业,但是遇到了一个我不知道如何解决的重大问题。我的问题是,当用户输入余额并接受其名称时,但是当存款和取款方法运行时,他们不使用该数据,而是从默认值0开始。

package assignment1;

import java.util.Scanner;
import java.util.ArrayList;

public class TestBank {    
    public static void main(String[] args) {
        Bank bank = new Bank();

        bank.enterCustomers();

        System.out.println();
        System.out.println("==========================");
        System.out.println("Opening account balance");

        bank.printBalances();

        System.out.println();

        bank.banking();

        System.out.println();
        System.out.println("==========================");
        System.out.println("Closing account balance");
        bank.printBalances();
    }
}

class Bank {
    String accounts;
    Scanner scanner = new Scanner(System.in);
    Account account = new Account();

    public void enterCustomers() {
        ArrayList CustomerDetails = new ArrayList();
        while (true) {
            System.out.print("Enter Name: ");
            account.name = scanner.next();

            if (account.name.equals("q")) {
                break;
            } else {
                Input.promptText("Enter balance: ");
                account.balance = scanner.nextDouble();

                CustomerDetails.add(account.name);
                CustomerDetails.add(account.balance);

                System.out.println("Enter customer names or q to quit entering names");
            }
        }
    }

    public void printBalances() {
        System.out.println(account.name + " " + Math.round(account.balance * 100 / 100));
    }

    public void banking() {
        Account account = new Account();
        account.deposit();
        account.withdraw();

        int Newbalance = (int) (Math.round(account.balance));

        switch (Newbalance) {
            case 1:
                account.deposit();
                break;
            case 2:
                account.withdraw();
                break;
            case 0:
                System.exit(0);
            default:
                break;
        }
    }

    class Account {
        private String name;
        private double balance;

        public Account() {
            this.name = name;
            this.balance = balance;
        }

        public String getName() {
            return this.name;
        }

        public double getBalance() {
            return this.balance;
        }

        public double deposit() {
            Input.promptText("Enter a valid deposit: ");
            double amount = scanner.nextDouble();

            balance = balance + amount;

            Input.promptText("your balance after your deposit is: " + balance);
            return balance;
        }

        public double withdraw() {
            System.out.println();
            Input.promptText("Enter a valid withdraw: ");
            double amount = scanner.nextDouble();

            if (amount > balance); else {
                balance = balance - amount;
            }

            System.out.println("your balance after your withdrawal is: " + balance);
            return balance;
        }
    }
}

class Input {
    private static Scanner scanner = new Scanner(System.in);

    public static String getText() {
        return scanner.nextLine();
    }

    public static String getText(String prompt) {
        System.out.print(prompt);
        return scanner.nextLine();
    }

    public static String getString() {
        return scanner.nextLine();
    }

    public static String getString(String prompt) {
        System.out.print(prompt);
        return scanner.nextLine();
    }

    public static double getDouble() {
        return scanner.nextDouble();
    }

    public static double getDouble(String prompt) {
        System.out.print(prompt);
        return scanner.nextDouble();
    }

    public static void promptText(String text) {
        System.out.printf("%s", text);
    }

    public static void outputText(String text) {
        System.out.printf("%s\n", text);
    }

    public static void outputInteger(String text, int value) {
        System.out.printf("%s%d\n", text, value);
    }

    public static void outputDouble(String text, double value) {
        outputDouble(text, value, 2);
    }

    public static void outputDouble(String text, double value, int decimalPlaces) {
        System.out.printf("%s%." + decimalPlaces + "f\n", text, value);
    }

    public static void outputBoolean(String text, boolean value) {
        System.out.printf("%s%b\n", text, value);
    }
}

我又如何做到这一点,例如,输入的名称是John,输入的是300。然后添加了另一个名称,例如输入400的亚当,以便打印约翰斯余额= 300,输入亚当斯= 400

1 个答案:

答案 0 :(得分:1)

您的错误在于banking()方法。您正在创建新帐户并对其进行操作,而不是先前创建的帐户。

public void banking() {

    // remove the below line
    // Account account = new Account();

    account.deposit();
    account.withdraw();

    int Newbalance = (int) (Math.round(account.balance));

    switch (Newbalance) {
        case 1:
            account.deposit();
            break;
        case 2:
            account.withdraw();
            break;
        case 0:
            System.exit(0);
        default:
            break;
    }
}

以下是输出:

Enter Name: test
Enter balance: 500
Enter customer names or q to quit entering names
Enter Name: q

==========================
Opening account balance
q 500

Enter a valid deposit: 100
your balance after your deposit is: 600.0
Enter a valid withdraw: 50
your balance after your withdrawal is: 550.0

==========================
Closing account balance
q 550

另一个错误是您正在使用帐户name != q时从输入中获取帐户,但是您使用的是最后一个帐户。您仅存储一个帐户,而不是全部。在上面,它应该在帐户test上运行,而应在帐户q上运行。

因此,要对其进行修复,您应该修复enterCustomers()方法:

public void enterCustomers() {

        ArrayList CustomerDetails = new ArrayList();

        while (true) {

            System.out.print("Enter Name: ");
            // account.name = scanner.next();
            // don't set input directly to account
            String inputName = scanner.next();
            // check given input
            if (inputName.equals("q")) {

                break;

            } else {
                Input.promptText("Enter balance: ");
                account.name = inputName;
                account.balance = scanner.nextDouble();

                CustomerDetails.add(account.name);
                CustomerDetails.add(account.balance);

                System.out.println("Enter customer names or q to quit entering names");

            }

        }

    }

现在的输出是:

Enter Name: test
Enter balance: 500
Enter customer names or q to quit entering names
Enter Name: q

==========================
Opening account balance
test 500

Enter a valid deposit: 100
your balance after your deposit is: 600.0
Enter a valid withdraw: 50
your balance after your withdrawal is: 550.0

==========================
Closing account balance
test 550