软件设计22|状态模式(C++)
用C++模拟实现课堂上的“银行账户”的实例,要求编写客户端测试代码模拟用户存款和取款,注意账户对象状态和行为的变化。
“银行账户”实例:
一、效果如下:
二、类图如下:
三、代码如下:
1 #include<iostream> 2 using namespace std; 3 4 class Account; 5 6 class AccountState { 7 public: 8 virtual void stateCheck(Account* account) = 0; 9 }; 10 11 class Account 12 { 13 private: 14 AccountState* state;//当前状态 15 int balance;// 余额 16 int amount;// 取款金额 17 int cash;//存款金额 18 public: 19 Account() {} 20 //ATM状态 21 void setState(AccountState* state) { 22 this->state = state; 23 } 24 //取款操作 25 void withdraw(int amount) { 26 if (balance < -1000) { 27 cout << "您已处于透支状态,请先存款!!!" << endl; 28 cout << "账户余额:" << balance << endl; 29 } 30 else { 31 cout << "向账户提出申请:取款:" << amount << "元。" << endl; 32 if (amount != 0) { 33 balance = balance - amount; 34 cout << "取款成功,余额为:" << balance << endl; 35 } 36 } 37 } 38 //存款操作 39 void deposit(int cash) { 40 cout << "向账户提出申请:存款:" << cash << "元。" << endl; 41 if (cash != 0) { 42 balance = balance + cash; 43 cout << "存款成功,余额为:" << balance << endl; 44 } 45 } 46 //状态 47 void stateCheck() { 48 state->stateCheck(this); 49 } 50 //余额 51 int getBalance() { 52 return balance; 53 } 54 void setBalance(int balance) { 55 this->balance = balance; 56 cout << "账户余额为:" << balance << endl; 57 } 58 //取款金额 59 int getAmount() { 60 return amount; 61 } 62 //存款金额 63 int getCash() { 64 return cash; 65 } 66 }; 67 68 //账户的状态为黄色(YellowState),即欠费状态,此时既可以向该账户存款也可以从该账户取款 69 class YellowState : public AccountState { 70 private: 71 Account* account; 72 public: 73 void stateCheck(Account* account) { 74 cout << "欠费状态,此时既可以向该账户存款也可以从该账户取款" << endl; 75 } 76 }; 77 78 //账户的状态为绿色(GreenState),即正常状态,表示既可以向该账户存款(deposit)也可以从该账户取款(withdraw) 79 class GreenState :public AccountState { 80 private: 81 Account* account; 82 public: 83 void stateCheck(Account* account) { 84 cout << "正常状态,表示既可以向该账户存款(deposit)也可以从该账户取款(withdraw)" << endl; 85 } 86 }; 87 88 //账户的状态为红色(RedState),即透支状态,此时用户只能向该账户存款,不能再从中取款 89 class RedState :public AccountState { 90 private: 91 Account* account; 92 public: 93 void stateCheck(Account* account) { 94 cout << "透支状态,此时用户只能向该账户存款,不能再从中取款" << endl; 95 } 96 }; 97 98 int main() { 99 Account* account = new Account(); 100 Account* account1 = new Account(); 101 Account* account2 = new Account(); 102 103 cout << "******************************" << endl; 104 cout << "***********Account1***********" << endl; 105 account->setBalance(1000); 106 account->setState(new GreenState()); 107 account->stateCheck(); 108 account->deposit(200); 109 account->withdraw(300); 110 account->getBalance(); 111 cout << endl; 112 113 cout << "******************************" << endl; 114 cout << "***********Account2***********" << endl; 115 account1->setBalance(-2000); 116 account1->setState(new RedState()); 117 account1->stateCheck(); 118 account1->deposit(200); 119 account1->withdraw(300); 120 account1->getBalance(); 121 cout << endl; 122 123 cout << "******************************" << endl; 124 cout << "***********Account3***********" << endl; 125 account2->setBalance(-200); 126 account2->setState(new YellowState()); 127 account2->stateCheck(); 128 account2->deposit(200); 129 account2->withdraw(300); 130 account2->getBalance(); 131 }