• 状态模式——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;
    }
    

      

    运行结果:

     

  • 相关阅读:
    android通过Canvas和Paint截取无锯齿圆形图片
    【转】mysql的cardinality异常,导致索引不可用
    mysql索引无效且sending data耗时巨大原因分析
    linux shell脚本通过参数名传递参数值
    git日志输出格式及两个版本之间差异列表
    jenkins结合ansible用shell实现自动化部署和回滚
    Linux下cp -rf总是提示覆盖的解决办法
    jenkins集成ansible注意事项Failed to connect to the host via ssh.
    ansible操作远程服务器报Error: ansible requires the stdlib json or simplejson module, neither was found!
    利用ssh-copy-id无需密码登录远程服务器
  • 原文地址:https://www.cnblogs.com/znjy/p/15582940.html
Copyright © 2020-2023  润新知