public class AutomatedTellerMachine extends Teller { public void withdraw(float amount) { Account a = getAccount(); if (a.deduct(amount)) dispense(amount); printReceipt(); } }
public class Account { private float total; public boolean deduct(float t) { if (t <= total) { total -= t; return true; } return false; } }
/** * Warehouse * By: Jiabo * Date: Mar 21, 2004 * Time: 2:48:10 PM */ public class Warehouse {
// max capability of the warehouse private int MAX; private int contents;
// init with max capacity public Warehouse(int max) { this.MAX = max; this.contents = 0; }
public synchronized void get(int amount) throws ProducerConsumerException {
// the amount you want to get is bigger than the contends that the warehouse stores if (amount > this.contents) { throw new NotEnoughGoodsException(); }
amount -= contents; }
public synchronized void put(int amount) throws ProducerConsumerException {
// the amount you want to put is out of the capability of the warehouse if (amount > (this.MAX - this.contents)) { throw new WarehouseFullException(); } else if (this.contents == 0) { // warehouse is empty throw new WarehouseEmptyException(); }