#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; }
标签:acc,状态,double,void,模式,state,new,balance From: https://www.cnblogs.com/qiuyutingyyds/p/17001589.html