状态模式——C++实现

问题描述:

模拟实现课堂上的“银行账户”的实例,要求编写客户端测试代码模拟用户存款和取款,注意账户对象状态和行为的变化。

C++源代码:

 

#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;
}

  

运行结果:

 

原文地址:https://www.cnblogs.com/znjy/p/15582940.html