diff --git a/week23.java b/week23.java new file mode 100644 index 0000000..c1f4374 --- /dev/null +++ b/week23.java @@ -0,0 +1,127 @@ +import java.util.Scanner; + +class Person { + private int age; + private String name; + + public int getAge() { + return age; + } + public void setAge(int age) { + this.age = age; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + public String toString() { + return this.getName(); + } + +} + +class DemoPerson { + public static void main(String[] args) { + + System.out.println(String.format("Would you like to meet a person?")); + Person simon = new Person(); + simon.setAge(38); + simon.setName("Simon"); + System.out.println(String.format("Their name is %s and their age is %d", + simon.getName(), simon.getAge())); + + String newName = getUserInput("What shall we change their name to?"); + simon.setName(newName); + + + System.out.println(String.format("Their name is now %s, but their age remains %d", + simon, simon.getAge())); // uses toString() + } + + /** A simple wrapper to get some user input easily + * @param prompt What to prompt user for + * @return User's input + */ + public static String getUserInput(String prompt) { + Scanner userInput = new Scanner(System.in); + System.out.println(String.format("%s", prompt)); + + if (userInput.hasNextLine()) { + return userInput.nextLine(); + } else { + return ""; + } + } +} + +class BankAccount { + private String name; + private int accountNumber; + private double balance; + + private static int nextAccountNumber = 0; + + public BankAccount(String name) { + this.name = name; + this.balance = 0.0; + this.accountNumber = nextAccountNumber; + nextAccountNumber++; + } + + @Override + public String toString() { + return "BankAccount [name=" + name + ", accountNumber=" + accountNumber + ", balance=" + balance + "]"; + } + + public int getAccountNumber() { + return accountNumber; + } + + public String getName() { + return name; + } + + public double getBalance() { + return balance; + } + + public void deposit (double amount) { + this.balance += amount; + } + + public void withdraw(double amount) { + // No overdraft facilities here! + if (amount <= balance) { + this.balance -= amount; + } + } +} + +/** + * 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? + */ +class DemoBankAccount { + public static void main(String[] args) { + 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("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("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)); + } +}