状态模式

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

 

在某银行系统定义的账户有三种状态: (1) 如果账户(Account)中余额(balance)大于等于0,此时账户的状态为绿色(GreenState),即正常状态,表示既可以向该账户存款(deposit)也可以从该账户取款(withdraw); (2) 如果账户中余额小于0,并且大于等于-1000,则账户的状态为黄色(YellowState),即欠费状态,此时既可以向该账户存款也可以从该账户取款; (3) 如果账户中余额小于-1000,那么账户的状态为红色(RedState),即透支状态,此时用户只能向该账户存款,不能再从中取款。 现用状态模式来实现状态的转化问题,用户只需要执行简单的存款和取款操作,系统根据余额数量自动转换到相应的状态

c++实现

#include<iostream>
using namespace std;
class Account;
class AccountState{
public:
    Account *acc;
    double balance;
    string stateName;
public:
    virtual void stateCheck()=0;
    void deposit(double amount);
    virtual void withdraw(double amount);
};
class Account{
private:
    AccountState *state;
    string owner;
public:
    Account(string owner,double init);

    void setState(AccountState *state) {
        this->state=state;
    }
    AccountState* getState() {
        return this->state;
    }
    string getOwner() {
        return this->owner;
    }
    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<<"您的账户处于透支状态,不能取款!"<<endl;}
    void stateCheck();
};
class YellowState :public AccountState{
public:
    YellowState(AccountState *state) {
        this->balance = state->balance;
        this->acc = state->acc;
        this->stateName="欠费状态";
    }
    void stateCheck();
};
class GreenState:public AccountState{
public:
    GreenState(double balance,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() {
        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(){
    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() {
    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 AccountState::deposit(double amount){
    cout<<acc->getOwner()<<"存款"<<amount<<endl;
    this->balance+=amount;
    stateCheck();
    cout<<"账户余额:"<<this->balance<<endl;
    cout<<"现在账户状态:"<<acc->getState()->stateName<<endl;
}
void AccountState::withdraw(double amount){
    cout<<acc->getOwner()<<"取款"<<amount<<endl;
    this->balance-=amount;
    stateCheck();
    cout<<"账户余额:"<<this->balance<<endl;
    cout<<"现在账户状态:"<<acc->getState()->stateName<<endl;
}
Account::Account(string owner,double init){
    this->owner=owner;
    this->state=new GreenState(init,this);
    cout<<"恭喜"<<this->owner<<"开户成功!银行卡初始金额:"<<init<<endl;
    cout<<"------------------------------"<<endl;
}
int main(){
    Account *account=new Account("张三",1000);
    account->deposit(100);
    account->withdraw(500);
    account->withdraw(700);
    account->withdraw(1000);
    return 0;
}

java实现

package state;
//账户类
public class Account {
    private AccountState state;
    private String name;

    public Account(String name,double init_money) {
        this.name = name;
        state=new Green(init_money,this);
        System.out.println("余额:"+init_money);
    }

    public void setState(AccountState state) {
        this.state = state;
    }

    //存款
    public void deposit(double money){
        state.deposit(money);
        System.out.println("余额:"+this.state.economy);
    }

//    取款
    public void withDraw(double money){
        state.withDraw(money);
        System.out.println("余额:"+this.state.economy);
    }
}
package state;

public abstract class AccountState {
    protected Account account;
    protected double economy;
    public abstract void check();
    public abstract void deposit(double m);
    public abstract void withDraw(double m);
}
package state;
//正常状态 money>0
public class Green extends AccountState{
    public Green(double money,Account acc) {
        economy=money;
        account=acc;
    }

    @Override
    public void check() {
        if(economy>0){
            System.out.println("---正常状态---");
            account.setState(new Green(economy,account));
        }else if(economy<=0&&economy>=-1000){
            System.out.println("---欠费状态---");
            account.setState(new Yellow(economy,account));
        }else if(economy<-1000){
            System.out.println("---透支状态---");
            account.setState(new Red(economy,account));
        }
    }

    @Override
    public void deposit(double m) {
        economy += m;
        check();
    }

    @Override
    public void withDraw(double m) {
        economy -= m;
        check();
    }
}
package state;
//警告状态 money<0&&money>-1000
public class Yellow extends AccountState{

    public Yellow(double m,Account acc){
        economy=m;
        account=acc;
    }

    @Override
    public void check() {
        if(economy>0){
            System.out.println("---正常状态---");
            account.setState(new Green(economy,account));
        }else if(economy<=0&&economy>=-1000){
            System.out.println("---欠费状态---");
            account.setState(new Yellow(economy,account));
        }else if(economy<-1000){
            System.out.println("---透支状态---");
            account.setState(new Red(economy,account));
        }
    }

    @Override
    public void deposit(double m) {
        economy += m;
        check();
    }

    @Override
    public void withDraw(double m) {
        economy -= m;
        check();
    }
}

 

posted on 2022-11-24 15:53  跨越&尘世  阅读(102)  评论(0编辑  收藏  举报