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

问题描述 投票:0回答:1

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

java account bank
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.