|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- 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));
- }
- }
|