首页 > 其他分享 >22.状态模式

22.状态模式

时间:2022-12-05 13:25:01浏览次数:32  
标签:acc 状态 22 void AccountState 模式 state balance public

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

用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);
}

标签:acc,状态,22,void,AccountState,模式,state,balance,public
From: https://www.cnblogs.com/java-six/p/16952010.html

相关文章

  • nchu-software-oop-2022-6
    7-1电信计费系列1-座机计费分数80  作者蔡轲  单位南昌航空大学实现一个简单的电信计费程序:假设南昌市电信分公司针对市内座机用户采用的计费方式:月租20......
  • nchu-software-oop-2022-8
    7-1电信计费系列3-短信计费分数50  作者蔡轲  单位南昌航空大学实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:1、接收短信免费,发送短信0.1......
  • nchu-software-oop-2022-7
    7-1电信计费系列2-手机+座机计费分数80  作者蔡轲  单位南昌航空大学实现南昌市电信分公司的计费程序,假设该公司针对手机和座机用户分别采取了两种计费......
  • C++ IMPL模式解析(下)
    二进制兼容在上一章结尾处提到了二进制兼容的概念,这里先说说二进制兼容的问题。为什么是二进制兼容简单说,就是我的可执行程序调用你的动态库(so/dll),若动态库发生改动,我......
  • 设计模式之代理模式学习分享[Darren]
    很高兴又在每晚的这个时候和博友有分享今天的学习成果了,感谢大家一直对Darren的支持,也希望各位博友们能将自己的好文章分享出来,在此Darren只是抛砖,希望大家不要吝啬自己的学......
  • 江苏省土木建筑学会智能建筑与智慧城市专业委员会2022年会
     10月21日,由江苏省土木建筑学会智能建筑与智慧城市专业委员会、常州市土木工程建筑学会智能建筑专业委员会、常州市土木工程建筑学会建筑电气专业委员会主办的“江苏省土木......
  • 2022年湖南省建筑电气设计与技术年会
       2022年10月28日,安科瑞电气股份有限公司参加在长沙金源阳光大酒店举办的2022年湖南省建筑电气设计与技术年会。年会由湖南省土木建筑学会电气专业委员会,湖南省建筑电......
  • VK1625是一种64*8点 LCD液晶段码屏显示驱动控制电路(IC/芯片),可兼容替代市面1625,具省电
    产品品牌:永嘉微电/VINKA产品型号:VK1625封装形式:LQFP100/QFP100/DICE概述:VK1625是一个点阵式存储映射的LCD驱动器,可支持最大512点(64EGx8COM)的LCD屏。单片机可通过3/4线串......
  • 单例模式常用模板类
    单例模式可以确保软件生命周期内仅有一个实例化对象,方便各处便利调用。单例模板类申明定义如下:template<typenameT>classSingleTon{public:staticT&getIns......
  • 批处理及有状态等应用类型在 K8S 上应该如何配置?
    众所周知,Kubernetes(K8S)更适合运行无状态应用,但是除了无状态应用.我们还会有很多其他应用类型,如:有状态应用,批处理,监控代理(每台主机上都得跑),更复杂的应用......