The Java Program: BankAccount.java

  1 public final class BankAccount {
  2 
  3    private int balance;     // amount in cents; always >= 0
  4 
  5    public BankAccount (int init) {
  6       assert init>=0;
  7       balance = init;
  8       assert balance>=0;
  9    }
 10 
 11    public BankAccount ()         { this(0); }
 12 
 13    public void deposit   (final int amount) {
 14       assert amount > 0;
 15       balance += amount;
 16       assert balance>=0;
 17    }
 18 
 19    public void withdrawl (final int amount) {
 20       assert amount > 0;
 21       if (balance >= amount) balance -= amount;
 22       assert balance>=0;
 23    }
 24 
 25    public int getBalance () {
 26       return balance;
 27    }
 28 
 29    public String toString () {
 30       return String.format ("$%d.%02d", balance/100, balance%100);
 31    }
 32 }