Browse Source

(feat) Implement up to the end of Exercise 2

ie use Person in BankAccount, implement transferFunds(), demo those
main
Rob Hallam 1 year ago
parent
commit
9dd0c86f0e
1 changed files with 55 additions and 16 deletions
  1. +55
    -16
      week23.java

+ 55
- 16
week23.java View File

@@ -58,14 +58,14 @@ class DemoPerson {
}

class BankAccount {
private String name;
private Person person;
private int accountNumber;
private double balance;

private static int nextAccountNumber = 0;

public BankAccount(String name) {
this.name = name;
public BankAccount(Person person) {
this.person = person;
this.balance = 0.0;
this.accountNumber = nextAccountNumber;
nextAccountNumber++;
@@ -73,15 +73,15 @@ class BankAccount {

@Override
public String toString() {
return "BankAccount [name=" + name + ", accountNumber=" + accountNumber + ", balance=" + balance + "]";
return "BankAccount [person=" + person + ", accountNumber=" + accountNumber + ", balance=" + balance + "]";
}

public int getAccountNumber() {
return accountNumber;
public Person getPerson() {
return person;
}

public String getName() {
return name;
public int getAccountNumber() {
return accountNumber;
}

public double getBalance() {
@@ -94,9 +94,19 @@ class BankAccount {

public void withdraw(double amount) {
// No overdraft facilities here!
if (amount <= balance) {
// wait, reading ahead, there are...
// if (amount <= balance) {
this.balance -= amount;
}
//}
}

/** Transfer funds out of the account to another {@link BankAccount}
* @param amount Amount to transfer
* @param recipient The lucky beneficiary
*/
public void transferFunds(double amount, BankAccount recipient) {
this.withdraw(amount);
recipient.deposit(amount);
}
}

@@ -104,24 +114,53 @@ class BankAccount {
* Exercise 2.4 & 2.6 - create two bank account objects and print their account
* numbers, which should be 0 and 1.
*
* I forget the name for the implementation detail but a static member is shared across class objects / instances?
* Then 2.8 & 2.9 where we change it to use Person
*/
class DemoBankAccount {
public static void main(String[] args) {
BankAccount account1 = new BankAccount("Jerry");
BankAccount account2 = new BankAccount("Margo");
Person jerry = new Person();
jerry.setName("Jerry");
Person margo = new Person();
margo.setName("Margo");

BankAccount account1 = new BankAccount(jerry);
BankAccount account2 = new BankAccount(margo);

System.out.println(String.format("%s's account number: %d", account1.getName(), account1.getAccountNumber()));
System.out.println(String.format("%s's account number: %d", account2.getName(), account2.getAccountNumber()));
System.out.println(String.format("%s's account number: %d", account1.getPerson(), account1.getAccountNumber()));
System.out.println(String.format("%s's account number: %d", account2.getPerson(), account2.getAccountNumber()));

System.out.println(String.format("Jerry will deposit £10, Margo will deposit £25"));
account1.deposit(10.00);
account2.deposit(25.00);
System.out.println(String.format("Accounts:\n\t%s\n\t%s", account1, account2));
System.out.println(String.format("Accounts:\n\t%s\n\t%s\n", account1, account2));

System.out.println(String.format("Jerry goes to the pub and needs some cash for that. Margo goes to buy new garden furniture."));
account1.withdraw(5.00);
account2.withdraw(15.00);
System.out.println(String.format("Accounts:\n\t%s\n\t%s", account1, account2));

System.out.println(".".repeat(80));
System.out.println(String.format("Now we're going to set up two accounts for the one person.\n"));
Person neighbour = new Person();
neighbour.setName("Tom");
BankAccount account3 = new BankAccount(neighbour);
BankAccount account4 = new BankAccount(neighbour);
System.out.println(String.format("Accounts:\n\t%s\n\t%s\n", account3, account4));

System.out.println(String.format("%s deposits some money in each.", neighbour));
account3.deposit(12.50);
account4.deposit(3.33);

System.out.println(String.format("Actually wait, the account wasn't set up in %s's name...", neighbour));
neighbour.setName("Barbara");
System.out.println(String.format("Accounts:\n\t%s\n\t%s\n", account3, account4));

System.out.println(".".repeat(80));
System.out.println(String.format("To help %s buy a new goat, %s lends them some money.",
neighbour, margo));
account2.transferFunds(10.00, account3);
System.out.println(String.format("Accounts:\n\t%s\n\t%s\n\t%s\n", account2, account3, account4));
}
}



Loading…
Cancel
Save