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

时间:2018-03-02 22:37:37

标签: java account bank

我有两个接口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;
}

我有一个类驱动程序,它有两个内部类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,所有现有帐户都存入银行。客户可以拥有多个帐号不同的帐户!它是如何工作的??

1 个答案:

答案 0 :(得分:0)

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

如果用户请求帐户,银行将向其提供计数器当前显示的编号,然后递增计数器。因此,下一个请求将获得不同的数字。

可能看起来像

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