import java.util.Vector;
/**
* Banks.
* A bank is a collection of BankAccounts indexed by account numbers.
*
*/
public class Bank
{
private String bankName;
private Vector
bankAccounts = new Vector ();
/**
* Constructor for objects of class Bank.
* Newly created banks contain no accounts.
*/
public Bank(String bankName)
{
this.bankName = bankName;
}
public String getBankName()
{
return bankName;
}
/**
* Open a new bank account and return its account number.
* Account numbers are assigned to new accounts automatically:
* the first account created has account number "1", the second
* has account number "2", and so on.
*/
public String openNewAccount(String ownerName, double initialBalance, double overdraftLimit)
{
Account newAccount = new Account(ownerName, initialBalance, overdraftLimit);
bankAccounts.add(newAccount);
return newAccount.getAccountNumber();
}
/**
* If the account exists and has a zero balance,
* remove it from the bank. If it does not exist, or
* does exist but has a non-zero balance, do nothing.
* Note that removing an account has NO effect on the
* account numbers of other accounts, or on the
* automatically allocated account numbers for new
* accounts.
*
* @param accountNumber the account number to remove
*/
public void closeAccount(String accountNumber)
{
for(int i = 0; i < bankAccounts.size(); i++)
{
if(bankAccounts.get(i).getAccountNumber().equals(accountNumber))
{
bankAccounts.remove(i);
break;
}
}
}
/**
* The bank account with the given account number, or null
* if no such account exists.
*
* @param accountNumber the account number
*/
public BankAccount getAccount(String accountNumber)
{
for(int i = 0; i < bankAccounts.size(); i++)
{
if(bankAccounts.get(i).getAccountNumber().equals(accountNumber))
return bankAccounts.get(i);
}
return null;
}
/**
* The value of the total debt of all overdrawn accounts.
* For example, when the bank contains four accounts, with balances
* of 99, -20, -230, 0 then this method returns 250.
*/
public double totalDebt()
{
double debt = 0;
for(int i = 0; i < bankAccounts.size(); i++)
{
if(bankAccounts.get(i).getBalance() < 0)
debt += bankAccounts.get(i).getBalance();
}
return Math.abs(debt);
}
/**
* Print a list of the account numbers and account details
* for all accounts.
*/
public void printAccounts()
{
for(int i = 0; i < bankAccounts.size(); i++)
{
System.out.println("Account Number: " + bankAccounts.get(i).getAccountNumber());
System.out.println("Owner: " + bankAccounts.get(i).getOwnerName());
System.out.println("Balance: " + bankAccounts.get(i).getBalance());
System.out.println("Overdraft Limit: " + bankAccounts.get(i).getOverdraftLimit());
System.out.println("............................................");
}
}
/**
* The total number of accounts in this bank.
*/
public int size()
{
return bankAccounts.size();
}
}]
**
* Cash machines.
* Cash machines which can dispense twenty pound notes,
* ten pound notes and five pound notes.
*
*/
public class CashMachine
{
/** The bank. */
private Bank bank;
/** Number of twenty pound notes in machine. */
private int twenties;
/** Number of ten pound notes in machine. */
private int tens;
/** Number of five pound notes in machine. */
private int fives;
/** Was the last request for cash successful? */
private boolean lastRequestOK;
/**
* Constructor for objects of class CashMachine.
* Newly created machines contain no bank notes.
*
* @param bank the bank to which this machine is connected
*/
public CashMachine(Bank bank)
{
this.bank = bank;
twenties = 0;
tens = 0;
fives = 0;
lastRequestOK = true;
}
/**
* Add some twenty pound notes to the machine's reserves.
*
* @param n the number of notes to add
*/
public void addTwenties(int n)
{
twenties += n;
}
/**
* Add some ten pound notes to the machine's reserves.
*
* @param n the number of notes to add
*/
public void addTens(int n)
{
tens += n;
}
/**
* Add some five pound notes to the machine's reserves.
*
* @param n the number of notes to add
*/
public void addFives(int n)
{
fives += n;
}
/**
* If the given account exists, return its current balance,
* otherwise return 0.
*
* @param accountNumber the account number
*/
public int getUserBalance(String accountNumber)
{
BankAccount account = bank.getAccount(accountNumber);
// YOUR CODE GOES HERE
return -1; // replace this with something sensible
}
/**
* Request some cash.
* Only dispense cash if the account exists and contains
* sufficient funds (taking into account its overdraft limit)
* and if the exact amount requested is possible
* using the machine's current reserves of bank notes.
*
* Set lastRequestOK to indicate if the request was met.
*
* Print messages to standard output saying what notes have
* been dispensed or explaining why the request cannot be met.
*
* Update the bank note reserves and account balance accordingly.
*
* @param accountNumber the account number for this transaction
* @param amount the amount of cash requested
*/
public void request(String accountNumber, int amount)
{
lastRequestOK = false;
BankAccount account = bank.getAccount(accountNumber);
// check if bank account exists; print a message and return
// without dispensing any cash if it does not exist
// YOUR CODE GOES HERE
int availableFunds = -1; // replace this by something sensible
if (availableFunds < amount) {
System.out.println("You have insufficient funds.");
} else if (reserves() == 0) {
System.out.println("This machine is empty.");
} else if (reserves() < amount) {
System.out.println("This machine only contains " + reserves() + " pounds.");
} else {
int fivesToDispense = 0;
int tensToDispense = 0;
int twentiesToDispense = 0;
int remainder = amount;
twentiesToDispense = Math.min(twenties, remainder / 20);
remainder = remainder - twentiesToDispense * 20;
tensToDispense = Math.min(tens, remainder / 10);
remainder = remainder - tensToDispense * 10;
fivesToDispense = Math.min(fives, remainder / 5);
remainder = remainder - fivesToDispense * 5;
if (remainder == 0) {
// YOUR CODE GOES HERE
// withdraw the requested amount from the bank account
twenties = twenties - twentiesToDispense;
tens = tens - tensToDispense;
fives = fives - fivesToDispense;
System.out.print("Please take your money: ");
System.out.print(fivesToDispense + " fives, ");
System.out.print(tensToDispense + " tens, ");
System.out.print(twentiesToDispense + " twenties.");
System.out.println();
lastRequestOK = true;
} else {
System.out.println("This machine cannot dispense that amount with its current stock of banknotes.");
}
}
}
/**
* Was the last request for cash successful?
*/
public boolean getLastRequestOK()
{
return lastRequestOK;
}
/**
* The total value of bank notes in the machine.
*/
private int reserves()
{
return fives * 5 + tens * 10 + twenties * 20;
}
/**
* The number of twenty pound notes the machine currently contains.
*/
public int getTwentiesCount()
{
return twenties;
}
/**
* The number of ten pound notes the machine currently contains.
*/
public int getTensCount()
{
return tens;
}
/**
* The number of five pound notes the machine currently contains.
*/
public int getFivesCount()
{
return fives;
}
}