|
- 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 Person person;
- private int accountNumber;
- private double balance;
-
- private static int nextAccountNumber = 0;
-
- public BankAccount(Person person) {
- this.person = person;
- this.balance = 0.0;
- this.accountNumber = nextAccountNumber;
- nextAccountNumber++;
- }
-
- @Override
- public String toString() {
- return "BankAccount [person=" + person + ", accountNumber=" + accountNumber + ", balance=" + balance + "]";
- }
-
- public Person getPerson() {
- return person;
- }
-
- public int getAccountNumber() {
- return accountNumber;
- }
-
- public double getBalance() {
- return balance;
- }
-
- public void setBalance(double balance) {
- this.balance = balance;
- }
-
- public void deposit (double amount) {
- this.balance += amount;
- }
-
- public void withdraw(double amount) {
- // No overdraft facilities here!
- // 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);
- }
- }
-
- /**
- * Exercise 2.4 & 2.6 - create two bank account objects and print their account
- * numbers, which should be 0 and 1.
- *
- * Then 2.8 & 2.9 where we change it to use Person
- */
- class DemoBankAccount {
- public static void main(String[] args) {
- 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.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\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));
- }
- }
-
- class Student extends Person {
- private double gpa;
-
- public double getGpa() {
- return gpa;
- }
-
- public void setGpa(double gpa) {
- this.gpa = gpa;
- }
-
- @Override
- public String toString() {
- return "Student [gpa=" + gpa + ", name=" + this.getName() + "]";
- // Ah, we can't access private members of parent classes from the child class
- }
- }
-
- class Lecturer extends Person {
- private double salary;
-
- public double getSalary() {
- return salary;
- }
-
- public void setSalary(double salary) {
- this.salary = salary;
- }
-
- @Override
- public String toString() {
- return "Lecturer [salary=" + salary + ", name=" + this.getName() + "]";
- }
- }
-
- /**
- * Exercise 3.1, 3.2, 3.3 - implement Student and Lcturer classes
- *
- */
- class DemoStudentLecturer {
- public static void main(String[] args) {
- Student adrian = new Student();
- adrian.setName("Adrian");
- adrian.setGpa(3.5);
-
- Lecturer trefusis = new Lecturer();
- trefusis.setName("Donald");
- trefusis.setSalary(24199);
-
- System.out.println(String.format("At a particular Oxbridge college, we have two people"));
- System.out.println(String.format("%s\t\t&\t\t%s", adrian, trefusis));
- }
- }
-
- class SavingsAccount extends BankAccount {
- @Override
- public String toString() {
- return "SavingsAccount [interestRate=" + interestRate + "]";
- }
-
- private double interestRate;
-
- /** Set up savings account for the thrifty
- * @param person A {@link Person} object
- * @param interestRate Expressed as %
- */
- public SavingsAccount(Person person, double interestRate) {
- super(person);
- this.interestRate = interestRate;
- }
-
- @Override
- public void withdraw(double amount) {
- if (amount <= this.getBalance()) {
- this.setBalance(this.getBalance() - amount);
- }
- }
-
- public void addInterest() {
- this.setBalance(this.getBalance() + (this.getBalance() * (this.interestRate / 100)));
- }
- }
-
- class DemoSavingsAccount {
- public static void main(String[] args) {
- Person hotblack = new Person();
- hotblack.setName("Hotblack Desiato");
-
- SavingsAccount savings1 = new SavingsAccount(hotblack, 6);
- savings1.deposit(100.00);
-
- System.out.println(String.format("%s plays a gig with DA and gets paid", hotblack));
- System.out.println(String.format("Account:\n\t%s", savings1));
- System.out.println(".".repeat(80));
-
- System.out.println(
- String.format("%s has a PGGB and tries to take out £200 to cover the cleanup expenses!", hotblack));
- savings1.withdraw(200.00);
- System.out.println(String.format("Account:\n\t%s", savings1));
- System.out.println(String.format("Fortunately (?), his bank blocks the transaction."));
- System.out.println(".".repeat(80));
-
- System.out.println(String.format("After 1 year, %s's account accrues interest!", hotblack));
- savings1.addInterest();
- System.out.println(String.format("Account:\n\t%s", savings1));
- System.out.println(String.format("%s wonders what it would be like after 1000 years...", hotblack));
- System.out.println(".".repeat(80));
-
- for (int i=0; i < 999; i++) {
- savings1.addInterest();
- if (i % 20 == 0) {
- System.out.printf("%.2f\t", savings1.getBalance());
- }
- }
- System.out.println(String.format("...finally, after 1000 years spent dead for tax purposes, %s checks his account", hotblack));
- System.out.println(String.format("Account:\n\t%s", savings1));
-
- }
- }
-
- class PolymorphismTest {
- public static void main(String[] args) {
- Person telemachus = new Person();
- telemachus.setName("Τηλέμαχος");
- BankAccount b = new SavingsAccount(telemachus, 10);
-
- System.out.println(String.format("%s", b));
- }
- }
|