【问题标题】:Is it possible to use a scanner this way?可以这样使用扫描仪吗?
【发布时间】:2023-04-10 21:09:01
【问题描述】:

好的,这就是发生的事情。我正在制作一个简单的银行程序

这就是我想做的,注意我的 Account 类的变量 (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);
    }
}

【问题讨论】:

  • 你到底在问什么?标题不够,您还必须在帖子中添加主要问题。
  • 我也不明白这个问题。为什么你有两个Scanners?难道只有一个扫描仪就足够了吗?还有balance这个变量有什么用?
  • @MCEmperor 我会重申。我有有帐户的用户。我如何在哈希表中添加/提取/检查这些帐户的余额。我有两台扫描仪,因为一台只用于数字,一台用于字符串
  • @UriD.Charles 不要用 cmets 添加信息,编辑你的帖子... cmets 是供用户询问更多信息
  • 在包含balance.put(sc.nextLine(), a1.addFunds())的行中,用户必须在sc.nextLine()输入什么?

标签: java hashtable java.util.scanner


【解决方案1】:

您应该创建一个 Account 类,它是一个帐户的模型。我建议您不要在 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&lt;Account&gt;。我不知道您为什么要使用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。您可能想要删除它们。
  • 据我所知,你不需要有两个Scanners;一个就足够了,因为您可以在同一个 Scanner 上同时调用 nextLine()nextInt()
  • 您有以大写字符开头的变量,即AccountObject。在 Java 中是允许的,但 Java 命名约定规定变量应以小写字母开头。
  • 您使用的是Hashtable类,但建议使用HashMap&lt;Key, Value&gt;

【讨论】:

    猜你喜欢
    • 2019-10-22
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    相关资源
    最近更新 更多