是否可以这样使用扫描仪?

时间:2014-12-13 21:05:56

标签: java hashtable java.util.scanner

好的,这是最新的。我正在制作一个简单的银行计划

这就是我想要做的,请注意我的帐户类(a1,a2,a3)的变量

这完全没问题,但不适合我想做的事情。 在切换案例中,我希望能够让用户在帐户下输入名称并能够对其进行编辑。

现在,我知道我是否基本上这样做了:

Account AccountObject = new Account ();
balance.put (sc.nextLine(), AO.addFunds)

然后我会有单独的用户,但资金基本上都是一样的。我如何将它们分开*

我知道一旦我弄清楚如何做到这一点,我将继续进行更复杂的项目。

import java.util.Hashtable;
import java.util.Scanner;

class Data {

    public static void main(String args[]) {
        Hashtable<String, Double> balance = new Hashtable<String, Double>();
        Scanner sc = new Scanner(System.in);
        Scanner sa = new Scanner(System.in);

        boolean quit = false;
        boolean quit2 = false;

        // Create account variables
        Account a1 = new Account();
        Account a2 = new Account();
        Account a3 = new Account();
        Account a4 = new Account();
        Account a5 = new Account();

        // Add funds to variables in Hashtable
        balance.put(sc.nextLine(), a1.addFunds());
        balance.put(sc.nextLine(), a2.addFunds());
        balance.put(sc.nextLine(), a3.addFunds());
        balance.put(sc.nextLine(), a4.addFunds());
        balance.put(sc.nextLine(), a5.addFunds());

        do {
            System.out.println("Menu: \n 1: Check balance\n 2: Add funds\n 3: Withdraw funds\n 4: Quit");
            int input = sa.nextInt();
            switch (input) {
                case 1:
                    System.out.println(balance.get(sc.nextLine()));
                    break;
                case 2:
                    System.out.println(balance.put(sc.nextLine(), a1.addFunds()));
                    break;
                case 3:
                    System.out.println(balance.put(sc.nextLine(), a1.withdrawFunds(sa.nextDouble())));
                    break;
                case 4:
                    quit = true;
                    break;
            }
        } while(!quit);
        System.out.println("Exiting menu");
    }
}

帐户类

import java.util.Scanner;

public class Account {

    int balance;
    String name;

    public double addFunds() {
        Scanner sa = new Scanner(System.in);
        double amount = sa.nextDouble();
        balance += amount;
        return balance;
    }

    public String Acct(String names) {
        Scanner sc = new Scanner(System.in);
        name = names;
        return name;
    }

    public double withdrawFunds(double amount) {
        balance -= amount;
        return balance;
    }

    public String toString() {
        return String.format("Balance: %n", balance);
    }
}

1 个答案:

答案 0 :(得分:0)

您应该创建一个帐户类,它是帐户的模型。我建议你不要在Account类中处理用户输入。

帐户类

public class Account {

    private String name;
    private int balance;

    public Account(String name, int startBalance) {
        this.name = name;
        this.balance = startBalance;
    }

    public void addFunds(int amount) {
        if (amount < 0) {
            throw new IllegalArgumentException("Amount must be absolute");
        }
        this.balance += amount;
    }

    public void withdrawFunds(int amount) {
        if (amount < 0) {
            throw new IllegalArgumentException("Amount must be absolute");
        }
        else if (amount > this.balance) {
            throw new IllegalArgumentException("You don't have that, so you cannot grab that.");
        }
        this.balance -= amount;
    }

    public String getName() {
        this.name;
    }

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

现在,如果需要,您可以创建一些帐户并将其添加到ArrayList<Account>。我不知道为什么你会使用HashMap:如果你只有一个包含所有Account个对象的列表,你就拥有了所需的所有信息。

ArrayList<Account> accounts = new ArrayList<Account>();
Scanner sc = new Scanner(System.in);

您可以像以下一样实现用户输入:

private static ArrayList<Account> accounts = new ArrayList<Account>();
private static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {
    initializeSomeBankAccounts();
    displayUI();
}

private static void initializeSomeBankAccounts() {
    for (int i = 0; i < 2; i++) {
        System.out.print("Insert " + (i > 0 ? "another " : "") + "account name: ");
        String name = sc.nextLine();
        System.out.print("Insert start balance: ");
        int startBalance = sc.nextInt();
        sc.nextLine();

        // Create a new account using the user input
        Account account = new Account(name, startBalance);
        // Add the account to our list with accounts.
        accounts.add(account);
    }
}

public static void displayUI() {
    boolean quit = false;
    while (!quit) {
        // Show a menu with the available actions
        System.out.println("Menu: \n 1: Check balance\n 2: Add funds\n 3: Withdraw funds\n 4: Quit");

        int action = sc.nextInt();
        sc.nextLine();


        Account account;
        // Since we ask the user to insert a right account name, we can
        // guarantee that the variable 'account' contains an Account
        // object.
        switch (action) {
            case 1:
                account = askAccount();
                System.out.println(account.getBalance());
                break;
            case 2:
                account = askAccount();
                System.out.print("Amount: ");
                int amount = sc.nextInt();
                account.addFunds(amount);
                break;
            case 3:
                account = askAccount();
                System.out.print("Amount: ");
                amount = sc.nextInt();
                account.withdrawFunds(amount);
                break;
            case 4:
                quit = true;
                break;
        }
    }
}

private static Account askAccount() {
    System.out.println("Which account? ");

    Account account = null;
    boolean accountFound = false;

    // Now the user has to input a valid account name, we're going to
    // search for that account name in the list. If it is found, we
    // have the whole Account object stored into the variable 'account'.
    // Otherwise, if it is not found, then we repeat to ask to insert
    // an account name, until a account name is given which is present
    // in our list.
    while (!accountFound) {
        String accountName = sc.nextLine();
        account = searchAccount(accountName);
        if (account == null) {
            System.out.println("Account not found. Insert another account:");
        }
        else {
            accountFound = true;
        }
    }
    return account;
}

/**
 * Searches an account from our list of all accounts.
 *
 * @param name The name to search for.
 * @return The account if found, or null otherwise.
 */
private static Account searchAccount(String name) {
    for (Account account : accounts) {
        if (account.getName().equals(name)) {
            return account;
        }
    }
    return null;
}

我也有一些建议:

  • 您有一些未使用的变量,即quit2。您可能想要删除它们。
  • 据我所知,你不需要两个Scanner;一个就足够了,因为您可以在同一台扫描仪上同时拨打nextLine()nextInt()
  • 您有以大写字符开头的变量,即AccountObject。在Java中,它是允许的,但Java命名约定规定应该使用小写字母启动变量。
  • 您使用的是课程Hashtable,但建议您使用HashMap<Key, Value>