I have two interfaces Bank and Account which have some functions:
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;
}
I have a class Driver which has two inner classes Bank and Account which implements the interfaces above, the Bank has a HashMap as accounts of customers and a customer can have multiple accounts with different account numbers. The problem that i couldn't solve it, is the generating of these account numbers even multiple for each customer!!! :
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;
}
}
}
How to generate account numbers? There is a HashMap with all existing Accounts into the Bank. A customer can have multiple accounts with different account numbers! How does it work like this!?
Your Bank
class could provide this feature by internally having a counter. It will work as all accounts are only maintained by this one bank object, and not shared with others.
If a user requests an account the bank will give it the number the counter currently displays and then increment the counter. The next request will thus get a different number.
Could look like
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++;
}
}
Obviously this has some drawbacks. It is limited to the range of int
(about 4 Mrd accounts, can of course be extended by using long
). Users can also see how many accounts the bank already has. And it is easy to guess a valid ID of another users account.
For a more random approach you could use
String uniqueID = UUID.randomUUID().toString();
see here for more information.