next up previous
Next: A class to handle Up: Unit 05 Previous: Exercise: modification of the

Solution of the exercise on the class BankAccount

public class BankAccount {
  private String name, surname;
  private double balance;

  ...

  public void withdraw(double val) {
    if (val <= balance)
      balance -= val;
    else
      System.out.println("Not sufficient money for withdrawal");
  }

  public boolean sameOwner(BankAccount ba) {
    return this.name.equals(ba.name) &&
           this.surname.equals(ba.surname);
  }

  public void transferTo (BankAccount ba, double val) {
    if (val <= this.balance) {
      this.withdraw(val);
      ba.deposit(val);
    } else
      System.out.println("It is not possible to withdraw " + val +
                         " Euro from account " + this);
  }

  public void transferFrom (BankAccount ba, double val) {
    if (val <= ba.balance) {
      ba.withdraw(val);
      this.deposit(val);
    } else
      System.out.println("It is not possible to withdraw " + val +
                         " Euro from account " + ba);
  }
}


next up previous
Next: A class to handle Up: Unit 05 Previous: Exercise: modification of the