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

状态模式

时间:2022-11-03 16:00:30浏览次数:53  
标签:状态 account void amount 模式 state balance public

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

 

类图

 

 

源代码

Java

   
package rjsj.no22;

public class GreenState extends AccountState{

    public GreenState(double balance,Account acc) {
        this.balance = balance;
        this.account = acc;
        this.stateName = "正常状态";
    }
}
     
package rjsj.no22;

public class RedState extends AccountState{

    public RedState(AccountState state) {
        this.balance = state.balance;
        this.account = state.account;
        this.stateName="透支状态";
    }

    public void withdraw(double amount) {
        System.out.println(account.getOwner()+"取款"+amount);
        System.out.println("抱歉,您的账户已处于透支状态,不能取款!");
    }
}
     
package rjsj.no22;

public class YellowState extends AccountState{

    public YellowState(AccountState state) {
        this.balance = state.balance;
        this.account = state.account;
        this.stateName="欠费状态";
    }
}
     
package rjsj.no22;

public class Account {
    private AccountState state;
    private String owner;

    public AccountState getState() {
        return state;
    }

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

    public String getOwner() {
        return owner;
    }

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

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

    public Account(String owner, double initBalance) {
        this.owner = owner;
        this.state = new GreenState(initBalance,this);
        System.out.println(this.owner + "开户成功!银行卡初始金额:" + initBalance);
        System.out.println("----------------------------------------------------");
    }
}
     
package rjsj.no22;

public abstract class AccountState {
    protected Account account;//账户名
    protected double balance;//账户金额
    protected String stateName;//当前状态

    public void deposit(double amount) {
        System.out.println(account.getOwner() + "存款" + amount);
        this.balance = this.balance + amount;
        stateCheck();
        System.out.println("账户余额:" + this.balance);
        System.out.println("账户状态:" + account.getState().stateName);
    }

    public void withdraw(double amount) {
        System.out.println(account.getOwner() + "取款" + amount);
        this.balance = this.balance - amount;
        stateCheck();
        System.out.println("账户余额:" + this.balance);
        System.out.println("账户状态:" + account.getState().stateName);
    }

    public void stateCheck(){//状态审查
        if( balance >= -1000 && balance < 0 ) {
            account.setState(new YellowState(this));
        }else if( balance < -1000 ) {
            account.setState(new RedState(this));
        }else if ( balance > 0 ){
            account.setState(new GreenState(this.balance,this.account));
        }
    }
}
     
package rjsj.no22;

public class Client {

    public static void main(String[] args) {
        Account account = new Account("张三",100);
        account.deposit(8888);
        System.out.println("------------------------------");
        account.withdraw(666);
        System.out.println("------------------------------");
        account.deposit(50);
        System.out.println("------------------------------");
        account.withdraw(9000);
        System.out.println("------------------------------");
        account.withdraw(50000);
        System.out.println("------------------------------");
        account.deposit(3000);
        System.out.println("------------------------------");
        account.withdraw(100);
    }
}
 

 

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("张三",100);
        account->deposit(100);
        cout<<"--------------------------------------------------"<<endl;
        account->withdraw(1000);
        cout<<"--------------------------------------------------"<<endl;
        account->deposit(1000);
        cout<<"--------------------------------------------------"<<endl;
        account->withdraw(2000);
        cout<<"--------------------------------------------------"<<endl;
        account->withdraw(10000);
        cout<<"--------------------------------------------------"<<endl;
        account->withdraw(1);
        cout<<"--------------------------------------------------"<<endl;
        account->deposit(100000);
        cout<<"--------------------------------------------------"<<endl;
        return 0;
}
 

 

运行结果:

标签:状态,account,void,amount,模式,state,balance,public
From: https://www.cnblogs.com/libin159/p/16854754.html

相关文章

  • 策略模式
    旅行方式的选择旅游的出行方式有乘坐飞机旅行、乘火车旅行和自行车游,不同的旅游方式有不同的实现过程,客户可以根据自己的需要选择一种合适的旅行方式。 类图  Jav......
  • springboot中使用职责链模式(转)
    转:https://www.51cto.com/article/720107.html一、什么是责任链模式?责任链模式(ChainofResponsibilityPattern),顾名思义,为请求者和接受者之间创建一条对象处理链路,避免......
  • 警惕工具和模式的陷阱
    目前出现了很多优秀的工具和思维模式,可以帮助我们解决很多问题.但是根据"没有银弹"原则,不存在一种工具或者模式能解决所有的问题.任何问题表现的现状都和具体的情境下发......
  • 静态代理模式
    前言为某个对象提供一个代理,以控制对这个对象的访问。代理类和委托类有共同的父类或父接口,这样在任何使用委托类对象的地方都可以用代理对象替代。代理类负责请求的预处......
  • 设计模式之结构型模式
    目录一、适配器模式二、装饰器模式三、代理模式四、外观模式五、享元模式六、桥接模式七、组合模式创建型模式(5个)工厂方法模式、抽象工厂模式、单例模式、建造者模式、原......
  • 设计模式之代理,手动实现动态代理,揭秘原理实现
    开心一刻周末,带着老婆儿子一起逛公园。儿子一个人跑在前面,吧唧一下不小心摔了一跤,脑袋瓜子摔了个包,稀里哗啦的哭道:“爸爸,我会不会摔成傻子!”我指了指我头上的伤痕安......
  • 设计模式十三(数据访问对象模式,前端控制器模式)
    数据访问对象模式(DataAccessObjectPattern)或DAO模式用于把低级的数据访问API或操作从高级的业务服务中分离出来。以下是数据访问对象模式的参与者。数据访问对象......
  • 设计模式之代理模式
    概述代理模式是一种应用很广泛的结构型设计模式,而且变化很多。在代理模式中引入了一个新的代理对象,代理对象可以在客户端对象和目标对象之间起到中介的作用,去掉客户不能看......
  • Pthread 并发编程(一)——深入剖析线程基本元素和状态
    Pthread并发编程(一)——深入剖析线程基本元素和状态前言在本篇文章当中讲主要给大家介绍pthread并发编程当中关于线程的基础概念,并且深入剖析进程的相关属性和设置,以及......
  • 1-设计模式介绍
    1,设计模式概述1.1软件设计模式的产生背景"设计模式"最初被用于建筑领域的设计中。1977年美国著名建筑大师`克里斯托夫·亚历山大在他的著作《建筑模式语言:城镇、建筑、......