使用 Java 中的帐户创建一个简单的银行

Create a simple bank with accounts in Java

我有两个接口 Bank 和 Account,它们有一些功能:

public interface Bank {
    String createAccount(String owner) throws IOException;
    boolean closeAccount(String number) throws IOException;
    Set<String> getAccountNumbers() throws IOException;
    Account getAccount(String number) throws IOException;
    void transfer(Account a, Account b, double amount) throws IOException, IllegalArgumentException, OverdrawException, InactiveException;
}
public interface Account {
    String getNumber() throws IOException;
    String getOwner() throws IOException;
    boolean isActive() throws IOException;
    void deposit(double amount) throws IOException, IllegalArgumentException, InactiveException;
    void withdraw(double amount) throws IOException, IllegalArgumentException, OverdrawException, InactiveException;
    double getBalance() throws IOException;
}

我有一个 class 驱动程序,它有两个内部 classes Bank 和 Account 实现了上面的接口,Bank 有一个 HashMap 作为客户的账户,一个客户可以有多个账户不同的帐号。我无法解决的问题是,为每个客户生成甚至多个帐号!!! :

    public class Driver {
        private Bank bank = null;
        static class Bank implements Bank {
            private final Map<String, Account> accounts = new HashMap<>();
            @Override
            public Set<String> getAccountNumbers() {
                return accounts.keySet();
            }
            @Override
            public String createAccount(String owner) {
                //TODO create new Account and return the account number
            }

            @Override
            public boolean closeAccount(String number) {
                //TODO if the account isActive and balance is zero then set it as inactive and return true.
                return false;
            }

            @Override
            public bank.Account getAccount(String number) {
                return accounts.get(number);
            }

            @Override
            public void transfer(bank.Account from, bank.Account to, double amount)
                    throws IOException, InactiveException, OverdrawException {
                if (amount <= from.getBalance()) {
                    from.withdraw(amount);
                    to.deposit(amount);
                }
        }

        static class Account implements Account {
            private String number;
            private String owner;
            private double balance;
            private boolean active = true;

            Account(String owner) {
                this.owner = owner;
                //TODO set the account number ??? 
            }

            @Override
            public double getBalance() {
                return balance;
            }

            @Override
            public String getOwner() {
                return owner;
            }

            @Override
            public String getNumber() {
                return number;
            }

            @Override
            public boolean isActive() {
                return active;
            }

            @Override
            public void deposit(double amount) throws InactiveException {
                if (!isActive())
                    throw new InactiveException();
                if (amount < 0)
                    throw new IllegalArgumentException();
                balance += amount;
            }

            @Override
            public void withdraw(double amount) throws InactiveException, OverdrawException {
                if (!isActive())
                    throw new InactiveException();
                if (amount > balance)
                    throw new OverdrawException();
                if (amount < 0)
                    throw new IllegalArgumentException();
                balance -= amount;
            }
        }
}

如何生成帐号? 有一个 HashMap,其中包含银行中所有现有帐户。一个客户可以有多个不同账号的账户!它是如何工作的!?

您的 Bank class 可以通过内部有一个 计数器 来提供此功能。它将起作用,因为所有帐户仅由这个银行对象维护,不与其他人共享。

如果用户请求一个帐户,银行将给它计数器当前显示的数字,然后递增计数器。下一个请求将因此得到一个不同的数字。

可能看起来像

public class Bank {
    private int nextAccountId = 1;

    public int createAccount(String owner) {
        // Get account ID
        int accountId = getUniqueAccountId();

        // Create account
        ...

        return accountID;
    }

    private int getUniqueAccountId() {
        int accountId = nextAccountId;
        // Increment ID for next request
        nextAccountId++;

        return accountId;

        // Method can be made compact by just using
        // return nextAccountId++;
    }
}

显然这有一些缺点。限制在int的范围内(大约4个Mrd账号,当然可以使用long来扩展)。用户还可以查看银行已有的账户数量。而且很容易猜到另一个用户帐户的有效 ID。

对于更随机的方法,您可以使用

String uniqueID = UUID.randomUUID().toString();

有关详细信息,请参阅 here