状态模式(二)
#include<iostream> #include<string> using namespace std; class Account; class AccountState{ public: Account *acc; double balance; string stateName; public: virtual void stateCheck(double balance)=0; double getBalance(){ return balance; } void setBalance(double balance){ this->balance=balance; } string getStateName(){ return stateName; } void setStateName(string statename){ this->stateName=statename; } void deposit(double amount); virtual void withdraw(double amount); }; class Account{ private: AccountState *state; string name; public: Account(string name); void setState(AccountState *state) { this->state=state; } AccountState* getState() { return this->state; } string getName() { return this->name; } void deposit(double amount) { state->deposit(amount); } void withdraw(double amount) { state->withdraw(amount); } }; class RedState :public AccountState{ public: RedState(AccountState *state) { this->balance = state->balance; this->acc = state->acc; this->stateName="透支状态"; } void withdraw(double amount){ cout<<"对不起,"<<acc->getName()<<",您不能取款!"<<endl; cout<<"当前余额:"<<balance<<"元,当前状态:"<<acc->getState()->stateName<<endl; } void stateCheck(double balance); }; class YellowState :public AccountState{ public: YellowState(AccountState *state) { this->balance = state->balance; this->acc = state->acc; this->stateName="欠费状态"; } void stateCheck(double balance); }; class GreenState:public AccountState{ public: GreenState(Account *acc) { this->balance = balance; this->acc = acc; this->stateName="正常状态"; } GreenState(AccountState *state) { this->acc=state->acc; this->balance=state->balance; this->stateName="正常状态"; } void stateCheck(double balance) { if(balance>=-1000&&balance<0) { acc->setState(new YellowState(this)); }else if(balance<-1000) { acc->setState(new RedState(this)); } else{ acc->setState(new GreenState(this)); } } }; void RedState::stateCheck(double balance){ if(balance>=-1000&&balance<0) { acc->setState(new YellowState(this)); }else if(balance<-1000) { acc->setState(new RedState(this)); } else { acc->setState(new GreenState(this)); } } void YellowState::stateCheck(double balance) { if(balance>=-1000&&balance<0) { acc->setState(new YellowState(this)); }else if(balance<-1000) { acc->setState(new RedState(this)); } else{ acc->setState(new GreenState(this)); } } Account::Account(string owner){ this->name=owner; this->state=new GreenState(this); cout<<"恭喜"<<owner<<"开户成功!初始金额:0元"<<endl; cout<<"**************************************"<<endl; } void AccountState::deposit(double amount){ cout<<this->acc->getName()<<"存款"<<amount<<"元"<<endl; balance+=amount; stateCheck(balance); cout<<"当前余额:"<<balance<<"元,当前状态:"<<acc->getState()->stateName<<endl; } void AccountState::withdraw(double amount){ cout<<acc->getName()<<"取款"<<amount<<"元"<<endl; balance-=amount; stateCheck(balance); cout<<"当前余额:"<<balance<<"元,当前状态:"<<acc->getState()->stateName<<endl; } int main(){ Account *account=new Account("王五"); account->deposit(2000); cout<<"-------------------------------------"<<endl; account->withdraw(500); cout<<"-------------------------------------"<<endl; account->withdraw(2000); cout<<"-------------------------------------"<<endl; account->withdraw(600); cout<<"-------------------------------------"<<endl; account->withdraw(200); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端