软件设计22|状态模式(Java)
一、效果如下:
二、类图如下:
三、工程目录如下:
四、代码如下:
ATM.java
1 package status; 2 3 public class ATM { 4 // 所有状态 5 private ATMState readyState; 6 private ATMState noCashState; 7 private ATMState noServiceState; 8 9 private ATMState currState;// 当前状态 10 private int totalAmount;// 账户现钞总数 11 12 // 测试用的临时变量 13 private String pwd;// 密码 14 private int balance;// 余额 15 private int amount;// 取款金额 16 private int cash;//存款金额 17 18 public ATM(int totalAmount, int balance, int amount, String pwd,int cash) throws Exception { 19 20 // 初始化所有状态 21 readyState = new ReadyState(this); 22 noCashState = new NoCashState(this); 23 setNoServiceState(new NoServiceState(this)); 24 25 if (totalAmount > 0) { 26 currState = readyState; 27 } else if (totalAmount == 0) { 28 currState = noCashState; 29 } else { 30 throw new Exception(); 31 } 32 33 // 初始化测试数据 34 this.totalAmount = totalAmount; 35 this.balance = balance; 36 this.amount = amount; 37 this.pwd = pwd; 38 this.cash=cash; 39 } 40 41 // 插卡 42 public void insertCard() { 43 currState.insertCard(); 44 } 45 46 // 提交密码 47 public void submitPwd() { 48 currState.submitPwd(); 49 } 50 51 // 取款 52 public void getCash() { 53 currState.getCash(); 54 } 55 56 // 存款 57 public void setCash(int cash) { 58 currState.setCash(cash); 59 } 60 61 // 查询余额 62 public void queryBalance() { 63 currState.queryBalance(); 64 } 65 66 // 取卡 67 public void ejectCard() { 68 currState.ejectCard(); 69 } 70 71 public String toString() { 72 return "账户总额¥" + totalAmount; 73 } 74 75 public String SKT() { 76 if (totalAmount >= 0) { 77 return "账户状态为绿色正常状态,既可以存,也可以取款"; 78 } 79 if (totalAmount >= -1000 && totalAmount < 0) { 80 return "账户状态为蓝色欠费状态,既可以存,也可以取款"; 81 } 82 if (totalAmount < -1000) { 83 return "账户状态为红色透支状态,只能存款"; 84 } 85 return ""; 86 } 87 88 public String getPwd() { 89 return pwd; 90 } 91 92 public int getBalance() { 93 return balance; 94 } 95 96 public int getTotalAmount() { 97 return totalAmount; 98 } 99 100 public int getAmount() { 101 return amount; 102 } 103 104 public void setTotalAmount(int totalAmount) { 105 this.totalAmount = totalAmount; 106 } 107 108 public void setBalance(int balance) { 109 this.balance = balance; 110 } 111 112 public ATMState getNoCashState() { 113 return noCashState; 114 } 115 116 public void setCurrState(ATMState currState) { 117 this.currState = currState; 118 } 119 120 public ATMState getNoServiceState() { 121 return noServiceState; 122 } 123 124 public void setNoServiceState(ATMState noServiceState) { 125 this.noServiceState = noServiceState; 126 } 127 }
ATMState.java
1 package status; 2 3 public interface ATMState { 4 5 public abstract void insertCard();//插卡 6 7 public abstract void submitPwd();//提交密码 8 9 public abstract void getCash();//取款 10 11 public abstract void queryBalance();//查询余额 12 13 public abstract void ejectCard();//取卡 14 15 public abstract void setCash(int cash);//存款 16 17 }
Client.java
1 package status; 2 3 public class Client { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 try { 8 test(1000, 500, 200, "ym123", 0); 9 System.out.println("------------------------------------------------------------"); 10 test(1000, 300, 1500, "ym123", 0); 11 System.out.println("------------------------------------------------------------"); 12 test(1000, 500, 0, "ym123", 300); 13 System.out.println("------------------------------------------------------------"); 14 test(-1500, 500, 200, "ym123", 0); 15 16 } catch (Exception e) { 17 System.out.println("账户余额小于-1000,账户状态为红色透支状态,请存钱后再取"); 18 // System.out.println("机器故障,维修请求已经发送至维修方"); 19 } 20 } 21 22 // 取款 23 private static void test(int totalAmount, int balance, int amount, String pwd, int cash) throws Exception { 24 // 创建ATM 25 ATM atm; 26 atm = new ATM(totalAmount, balance, amount, pwd, cash); 27 // 输出初始状态 28 System.out.println(atm.toString()); 29 System.out.println(atm.SKT()); 30 atm.insertCard(); 31 atm.submitPwd(); 32 if (cash == 0) { 33 atm.getCash(); 34 } else { 35 atm.setCash(cash); 36 } 37 // 输出结束状态 38 System.out.println(atm.toString()); 39 } 40 41 }
NoCashState.java
1 package status; 2 3 public class NoCashState implements ATMState { 4 5 private ATM atm;// 保留状态持有者的引用,以便对其进行操作 6 7 public NoCashState(ATM atm) { 8 this.atm = atm; 9 } 10 11 @Override 12 public void insertCard() { 13 // TODO Auto-generated method stub 14 System.out.println("插卡完成"); 15 } 16 17 @Override 18 public void submitPwd() { 19 // TODO Auto-generated method stub 20 System.out.println("密码提交完成"); 21 // 验证密码并做相应处理 22 if ("ym123".equals(atm.getPwd())) { 23 System.out.println("密码验证通过"); 24 } else { 25 System.out.println("密码验证失败"); 26 ejectCard(); 27 } 28 } 29 30 @Override 31 public void getCash() { 32 // TODO Auto-generated method stub 33 System.out.println("取款失败,机内无钞"); 34 } 35 36 @Override 37 public void queryBalance() { 38 // TODO Auto-generated method stub 39 System.out.println("账户余额¥" + atm.getBalance()); 40 System.out.println("账户余额查询完成"); 41 } 42 43 @Override 44 public void ejectCard() { 45 // TODO Auto-generated method stub 46 System.out.println("取卡完成"); 47 } 48 49 @Override 50 public void setCash(int cash) { 51 // TODO Auto-generated method stub 52 53 } 54 55 }
NoServiceState.java
1 package status; 2 3 /** 4 * 机器故障 5 * 6 * @author Administrator 7 * 8 */ 9 public class NoServiceState implements ATMState { 10 11 private ATM atm;// 保留状态持有者的引用,以便对其进行操作 12 13 public NoServiceState(ATM atm) { 14 this.setAtm(atm); 15 } 16 17 @Override 18 public void insertCard() { 19 // TODO Auto-generated method stub 20 System.out.println("插卡失败,机器发生了故障"); 21 } 22 23 @Override 24 public void submitPwd() { 25 // TODO Auto-generated method stub 26 System.out.println("密码提交失败,机器发生了故障"); 27 } 28 29 @Override 30 public void getCash() { 31 // TODO Auto-generated method stub 32 System.out.println("取款失败,机器发生了故障"); 33 } 34 35 @Override 36 public void queryBalance() { 37 // TODO Auto-generated method stub 38 System.out.println("余额查询失败,机器发生了故障"); 39 } 40 41 @Override 42 public void ejectCard() { 43 // TODO Auto-generated method stub 44 System.out.println("取卡失败,机器发生了故障"); 45 } 46 47 @Override 48 public void setCash(int cash) { 49 // TODO Auto-generated method stub 50 System.out.println("存款失败,机器发生了故障"); 51 } 52 53 public ATM getAtm() { 54 return atm; 55 } 56 57 public void setAtm(ATM atm) { 58 this.atm = atm; 59 } 60 61 }
ReadyState.java
1 package status; 2 3 public class ReadyState implements ATMState { 4 5 private ATM atm;// 保留状态持有者的引用,以便对其进行操作 6 7 public ReadyState(ATM atm) { 8 this.atm = atm; 9 } 10 11 @Override 12 public void insertCard() { 13 // TODO Auto-generated method stub 14 System.out.println("插卡完成"); 15 } 16 17 @Override 18 public void submitPwd() { 19 // TODO Auto-generated method stub 20 System.out.println("密码提交完成"); 21 // 验证密码并做相应处理 22 if ("ym123".equals(atm.getPwd())) { 23 System.out.println("密码验证通过"); 24 } else { 25 System.out.println("密码验证失败"); 26 ejectCard(); 27 } 28 } 29 30 @Override 31 public void getCash() { 32 // TODO Auto-generated method stub 33 if (atm.getTotalAmount() != 9999) { 34 // 更新账户余额 35 atm.setBalance(atm.getBalance() - atm.getAmount()); 36 // 更新机内现钞总数 37 atm.setTotalAmount(atm.getTotalAmount() - atm.getAmount()); 38 System.out.println("吐出¥" + atm.getAmount()); 39 System.out.println("取款完成"); 40 // 弹出卡片 41 ejectCard(); 42 // 检查机内余钞 43 if (atm.getTotalAmount() == 0) {// 若无钞,切换到NoService状态 44 atm.setCurrState(atm.getNoCashState()); 45 System.out.println("无钞信息已经发送至银行"); 46 } 47 } else { 48 System.out.println("取款失败,余额不足"); 49 // 弹出卡片 50 ejectCard(); 51 } 52 } 53 54 @Override 55 public void queryBalance() { 56 // TODO Auto-generated method stub 57 System.out.println("余额¥" + atm.getBalance()); 58 System.out.println("余额查询完成"); 59 } 60 61 @Override 62 public void ejectCard() { 63 // TODO Auto-generated method stub 64 System.out.println("取卡完成"); 65 } 66 67 @Override 68 public void setCash(int cash) { 69 // TODO Auto-generated method stub 70 // 更新账户余额 71 atm.setBalance(atm.getBalance() + cash); 72 // 更新机内现钞总数 73 atm.setTotalAmount(atm.getTotalAmount() + cash); 74 System.out.println("存款完成"); 75 // 弹出卡片 76 ejectCard(); 77 } 78 79 }
参考链接:银行账户(设计模式--状态模式)