今日收获
实验 22:状态模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解状态模式的动机,掌握该模式的结构;
2、能够利用状态模式解决实际问题。
[实验任务一]:银行账户
用Java代码模拟实现课堂上的“银行账户”的实例,要求编写客户端测试代码模拟用户存款和取款,注意账户对象状态和行为的变化。
package 实验22状态模式; public class ATM { /*所有状态*/ private ATMState readyState; private ATMState noCashState; private ATMState noServiceState; private ATMState currState;//当前状态 private int totalAmount;//账户现钞总数 /*测试用的临时变量*/ private String pwd;//密码 private int balance;//余额 private int amount;//取款金额 public ATM(int totalAmount, int balance, int amount, String pwd) throws Exception{ //初始化所有状态 readyState = new ReadyState(this); noCashState = new NoCashState(this); noServiceState = new NoServiceState(this); if(totalAmount > 0){ currState = readyState; } else if(totalAmount == 0){ currState = noCashState; } else{ throw new Exception(); } //初始化测试数据 this.totalAmount = totalAmount; this.balance = balance; this.amount = amount; this.pwd = pwd; } /*把具体行为委托给状态对象*/ /** * 插卡 */ public void insertCard(){ currState.insertCard(); } /** * 提交密码 */ public void submitPwd(){ currState.submitPwd(); } /** * 取款 */ public void getCash(){ currState.getCash(); } /** * 查询余额 */ public void queryBalance(){ currState.queryBalance(); } /** * 取卡 */ public void ejectCard(){ currState.ejectCard(); } public String toString(){ return "账户总额¥" + totalAmount; } public String SKT() { if(totalAmount >= 0) { return "账户状态为绿色正常状态,既可以存,也可以取款"; } if(totalAmount >= -1000 && totalAmount < 0) { return "账户状态为蓝色欠费状态,既可以存,也可以取款"; } if(totalAmount < -1000) { return "账户状态为红色透支状态,只能存款"; } return ""; } /*此处略去大量getter and setter*/ public String getPwd(){ return pwd; } public int getBalance() { return balance; } public int getTotalAmount() { return totalAmount; } public int getAmount() { return amount; } public void setTotalAmount(int totalAmount) { this.totalAmount = totalAmount; } public void setBalance(int balance) { this.balance = balance; } public ATMState getNoCashState() { return noCashState; } public void setCurrState(ATMState currState) { this.currState = currState; } }
package 实验22状态模式; public interface ATMState { public abstract void insertCard(); public abstract void submitPwd(); public abstract void getCash(); public abstract void queryBalance(); public abstract void ejectCard(); }
package 实验22状态模式; public class NoCashState implements ATMState{ private ATM atm;//保留状态持有者的引用,以便对其进行操作 public NoCashState(ATM atm){ this.atm = atm; } @Override public void insertCard() { System.out.println("插卡完成"); } @Override public void submitPwd() { System.out.println("密码提交完成"); //验证密码并做相应处理 if("123".equals(atm.getPwd())){ System.out.println("密码验证通过"); } else{ System.out.println("密码验证失败"); //弹出卡片 ejectCard(); } } @Override public void getCash() { System.out.println("取款失败,机内无钞"); } @Override public void queryBalance() { System.out.println("账户余额¥" + atm.getBalance()); System.out.println("账户余额查询完成"); } @Override public void ejectCard() { System.out.println("取卡完成"); } }
package 实验22状态模式; public class NoServiceState implements ATMState{ private ATM atm;//保留状态持有者的引用,以便对其进行操作 public NoServiceState(ATM atm){ this.atm = atm; } @Override public void insertCard() { System.out.println("插卡失败,机器发生了故障"); } @Override public void submitPwd() { System.out.println("密码提交失败,机器发生了故障"); } @Override public void getCash() { System.out.println("取款失败,机器发生了故障"); } @Override public void queryBalance() { System.out.println("余额查询失败,机器发生了故障"); } @Override public void ejectCard() { System.out.println("取卡失败,机器发生了故障"); } }
package 实验22状态模式; public class ReadyState implements ATMState{ private ATM atm;//保留状态持有者的引用,以便对其进行操作 public ReadyState(ATM atm){ this.atm = atm; } @Override public void insertCard() { System.out.println("插卡完成"); } @Override public void submitPwd() { System.out.println("密码提交完成"); //验证密码并做相应处理 if("6300".equals(atm.getPwd())){ System.out.println("密码验证通过"); } else{ System.out.println("密码验证失败"); //弹出卡片 ejectCard(); } } @Override public void getCash() { //atm.getTotalAmount() >= atm.getAmount() && atm.getBalance() >= atm.getAmount() if(atm.getTotalAmount() != 9999){ //更新账户余额 atm.setBalance(atm.getBalance() - atm.getAmount()); //更新机内现钞总数 atm.setTotalAmount(atm.getTotalAmount() - atm.getAmount()); System.out.println("吐出¥" + atm.getAmount()); System.out.println("取款完成"); //弹出卡片 ejectCard(); //检查机内余钞 if(atm.getTotalAmount() == 0){//若无钞,切换到NoService状态 atm.setCurrState(atm.getNoCashState()); System.out.println("无钞信息已经发送至银行"); } } else{ System.out.println("取款失败,余额不足"); //弹出卡片 ejectCard(); } } @Override public void queryBalance() { System.out.println("余额¥" + atm.getBalance()); System.out.println("余额查询完成"); } @Override public void ejectCard() { System.out.println("取卡完成"); } }
package 实验22状态模式; public class shijian5_yinhangzhanghu { public static void main(String[] args) { /*测试数据*/ /* 机内总数 账户余额 取款金额 密码 * 1000 500 200 123 * 1000 300 500 123 * 0 500 200 123 * */ try { test(1000, 500, 200, "6300"); System.out.println("------------------------------------------------------------"); test(1000, 300, 1500, "6300"); System.out.println("------------------------------------------------------------"); test(-1500, 500, 200, "6300"); } catch (Exception e) { System.out.println("账户余额小于-1000,账户状态为红色透支状态,请存钱后再取"); //System.out.println("机器故障,维修请求已经发送至维修方"); } } private static void test(int totalAmount, int balance, int amount, String pwd)throws Exception{ //创建ATM ATM atm; atm = new ATM(totalAmount, balance, amount, pwd); //输出初始状态 System.out.println(atm.toString()); System.out.println(atm.SKT()); atm.insertCard(); atm.submitPwd(); atm.getCash(); //输出结束状态 System.out.println(atm.toString()); } }