@Override publicvoidhandle(){ int count = DB.take(1000); if (count == 0) { atm.setState(new DefficientState(atm)); } System.out.println("$1000 HAVE BEEN TAKEN!"); } }
@Override publicvoidhandle(){ if (DB.query() > 0) { DB.take(1000); atm.setState(new SufficientState(atm)); System.out.println("$1000 HAVE BEEN TAKEN!"); } else { System.out.println("NOT SUFFICIENT IS AVAILABLE!"); } } }
测试
1 2 3 4 5 6 7 8 9 10 11 12 13
/** * @date: 09/04/2017 4:57 PM * @author: [email protected] */ publicclassClient{ publicstaticvoidmain(String[] args){ ATM atm = new ATM(); // 模拟11次取款行为,每次1000 for (int i = 0; i < 11; i++) { atm.withdraw(); } } }
结果
1 2 3 4 5 6 7 8 9 10 11
$1000 HAVE BEEN TAKEN! $1000 HAVE BEEN TAKEN! $1000 HAVE BEEN TAKEN! $1000 HAVE BEEN TAKEN! $1000 HAVE BEEN TAKEN! $1000 HAVE BEEN TAKEN! $1000 HAVE BEEN TAKEN! $1000 HAVE BEEN TAKEN! $1000 HAVE BEEN TAKEN! $1000 HAVE BEEN TAKEN! NOT SUFFICIENT IS AVAILABLE!