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!?