22.状态模式

[实验任务一]:银行账户

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

代码

Java代码

Account.java

package test22;

public class Account {

    private AccountState state;
    private String owner;

    public Account(String owner,double init){
        this.owner = owner;
        System.out.println("注册用户姓名为:"+owner);
        this.state = new GreenState(this);
    }

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

    public AccountState getState() {
        return state;
    }

    public void deposit(double amount){
        state.deposit(amount);
    }

    public void withdraw(double amount){
        state.withdraw(amount);
    }
}

AccountState.java

package test22;

public abstract class AccountState {

    protected Account acc;
    protected double balance;
    protected String name;

    public String getName(){
        return this.name;
    }

    public abstract void stateCheck();

    public void deposit(double balance){
        this.balance += balance;
        stateCheck();
        System.out.println("当前余额为:"+balance+"当前状态为:"+acc.getState().getName());
    }

    public void withdraw(double amount){
        if (balance>-1000) {
            this.balance -= amount;
        } else {
            System.out.println("不能再取钱了");
        }
        this.stateCheck();
        System.out.println("当前余额为:"+balance+"当前状态为:"+acc.getState().getName());
    }

}

Client.java

package test22;

public class Client {

    public static void main(String[] args) {
        Account hzy = new Account("hzy", 100);

        hzy.deposit(100);

        hzy.deposit(100);

        hzy.withdraw(200);
        hzy.withdraw(1200);
        hzy.withdraw(1200);

    }

}

GreenState.java

package test22;

public class GreenState extends AccountState{

    public GreenState(AccountState state){
        this.acc = state.acc;
        this.balance = state.balance;
        this.name = "green";
        AccountState accountState = new AccountState() {
            @Override
            public void stateCheck() {

            }
        };
        System.out.println(accountState.acc);
    }

    public GreenState(Account account){
        this.acc = account;
        this.balance = 0;
        this.name = "green";
    }

    @Override
    public void stateCheck() {
        if (balance>=0){
            acc.setState(new GreenState(this));
        }else if(balance>=-1000){
            acc.setState(new YellowState(this));
        }else {
            acc.setState(new RedState(this));
        }
    }
}

RedState.java

package test22;

public class RedState extends AccountState{

    public RedState(AccountState state){
        this.acc = state.acc;
        this.balance = state.balance;
        this.name = "red";
    }

    @Override
    public void stateCheck() {
        if (balance>=0){
            acc.setState(new GreenState(this));
        }else if(balance>=-1000){
            acc.setState(new YellowState(this));
        }else {
            acc.setState(new RedState(this));
        }
    }
}

YellowState.java

package test22;

public class YellowState extends AccountState{

    public YellowState(AccountState state){
        this.acc = state.acc;
        this.balance = state.balance;
        this.name = "yellow";
    }

    @Override
    public void stateCheck() {
        if (balance>=0){
            acc.setState(new GreenState(this));
        }else if(balance>=-1000){
            acc.setState(new YellowState(this));
        }else {
            acc.setState(new RedState(this));
        }
    }
}

C++代码

#include <iostream>

using namespace std;

class Account;
class RedState;

class AccountState{
public:
    double balance;
    Account *acc;
    virtual void stateCheck(){};
    void deposit(double amount){
        cout<<"往账户中保存了";
        cout<<amount;
        cout<<"元"<<endl;
        balance += amount;
        this->stateCheck();
    }
    virtual void withdraw(double amount){
        cout<<"从账户中取出了";
        cout<<amount;
        cout<<"元"<<endl;
        balance -= amount;
        this->stateCheck();
    }
};
class Account{
private:
    AccountState *state;
    string owner;
public:
    Account(string owner,double init);
    void setState(AccountState *state){
        this->state = state;
    }
    void deposit(double amount){
        state->deposit(amount);
    }
    void withdraw(double amount){
        state->withdraw(amount);
    }
};
class GreenState: public AccountState{
public:
    GreenState(double balance,Account *acc){
        this->balance = balance;
        this->acc = acc;
    }
    GreenState(AccountState *state){
        this->acc = state->acc;
        this->balance = state->balance;
    }
    void stateCheck();
};
class YellowSate: public AccountState{
public:
    YellowSate(AccountState *state){
        this->acc = state->acc;
        this->balance = state->balance;
    }
    void stateCheck();
};
class RedState: public AccountState{
public:
    RedState(AccountState *state){
        this->acc = state->acc;
        this->balance = state->balance;
    }
    void stateCheck();
    void withdraw(double amount) override{
        cout<<"已经不能取钱了"<<endl;
//        balance += amount;
        cout<<"还剩";
        cout<<balance;
        cout<<"元"<<endl;
    }
};
void GreenState::stateCheck() {
    if(balance>=0){
        acc->setState(this);
        cout<<"当前状态:绿色"<<endl;
    }else if(balance>=-100){
        acc->setState(new YellowSate(this));
        cout<<"当前状态:黄色"<<endl;
    } else{
        acc->setState(new RedState(this));
        cout<<"当前状态:红色"<<endl;
    }
}
void YellowSate::stateCheck() {
    if(balance>=0){
        acc->setState(new GreenState(this));
        cout<<"当前状态:绿色"<<endl;
    }else if(balance>=-100){
        acc->setState(this);
        cout<<"当前状态:黄色"<<endl;
    } else{
        acc->setState(new RedState(this));
        cout<<"当前状态:红色"<<endl;
    }
}
void RedState::stateCheck() {
    if(balance>=0){
        acc->setState(new GreenState(this));
        cout<<"当前状态:绿色"<<endl;
    }else if(balance>=-100){
        acc->setState(new YellowSate(this));
        cout<<"当前状态:黄色"<<endl;
    } else{
        acc->setState(this);
        cout<<"当前状态:红色"<<endl;
    }
}
Account::Account(string owner, double init) {
    this->setState(new GreenState(init, this));
    this->owner = owner;
    state->deposit(init);
    cout<<owner+"注册账户并保存了";
    cout<<init;
    cout<<"元";
}
int main(){
    Account *account = new Account("hzy",0);
    account->deposit(100);
    account->withdraw(200);
    account->withdraw(1000);
    account->withdraw(100);
}
posted @ 2022-12-05 13:08  又一岁荣枯  阅读(14)  评论(0编辑  收藏  举报