首页 > 编程语言 >状态模式javac++

状态模式javac++

时间:2022-12-23 17:23:24浏览次数:54  
标签:acc java double void 模式 state c++ balance public

软件设计                  石家庄铁道大学信息学院

 

实验 22:状态模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容: 

1、理解状态模式的动机,掌握该模式的结构;

2、能够利用状态模式解决实际问题。

 

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

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

实验要求:

1. 提交源代码;

 

 

 

 

2. 注意编程规范。

 

 

 

package test22;


public class Account {
    private AccountState state;
    private String name;
    public Account(String name){
        this.name=name;
        state=new Greenstate(this);
        System.out.println(name+"开户成功!初始金额:0元");
        System.out.println("******************************");
    }
    public void setState(AccountState state){
        this.state=state;
    }
    public AccountState getState() {
        return this.state;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void deposit(double money){
        state.deposit(money);
    }
    public void withdraw(double money){
        state.withdraw(money);
    }
}
package test22;

public abstract class AccountState {
    protected Account acc;
    protected double balance;
    protected String stateName;
    public abstract void stateCheck(double balance);
    public double getBalance() {
        return balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
    public String getStateName() {
        return stateName;
    }
    public void setStateName(String stateName) {
        this.stateName = stateName;
    }
    public void deposit(double amount) {
        System.out.println(acc.getName()+"存款"+amount+"元");
        balance+=amount;
        stateCheck(balance);
        System.out.println("当前余额:"+balance+"元,当前状态:"+acc.getState().stateName);
    }
    public void withdraw(double amount) {
        System.out.println(acc.getName()+"取款"+amount+"元");
        balance-=amount;
        stateCheck(balance);
        System.out.println("当前余额:"+balance+"元,当前状态:"+acc.getState().stateName);
    }
}
package test22;

public class Client {
    public static void main(String args[]){
        Account acc=new Account("zhangrong");
        acc.deposit(1000);
        System.out.println("---------------------------------------");
        acc.withdraw(500);
        System.out.println("---------------------------------------");
        acc.withdraw(2000);
        System.out.println("---------------------------------------");
        acc.withdraw(600);
        System.out.println("---------------------------------------");
        acc.withdraw(200);
    }
}
package test22;

public class Greenstate extends AccountState{
     public Greenstate(AccountState state){
                this.acc=state.acc;
                 this.balance=state.balance;
              this.stateName="正常状态";
            }
      public Greenstate(Account acc){
                this.balance=0;
             this.acc=acc;
                this.stateName="正常状态";
            }
      public void stateCheck(double balance){
                 if(balance<-1000){
                       acc.setState(new Redstate(this));
                    }else if(balance>=-1000&&balance<0){
                       acc.setState(new Yellowstate(this));
                    }
             }
  }
package test22;

public class Redstate extends AccountState {

    public Redstate(AccountState state) {
        this.acc=state.acc;
        this.balance=state.getBalance();
        this.stateName="透支状态";
    }

    public void stateCheck(double balance) {
        if(balance >= -1000 && balance < 0){
            acc.setState(new Yellowstate(this));
        }else if(balance >= 0){
            acc.setState(new Greenstate(this));
        }
    }
    public void withdraw(double amount) {
        System.out.println("对不起,"+acc.getName()+",您不能取款");
        System.out.println("当前余额:"+balance+"元,当前状态:"+acc.getState().stateName);
    }
}
package test22;

public class Yellowstate extends AccountState {

    public Yellowstate(AccountState state) {
        this.acc=state.acc;
        this.balance=state.getBalance();
        this.stateName="欠费状态";
    }

    public void stateCheck(double balance) {
        if(balance >=0 ){
            acc.setState(new Greenstate(this));
        }else if(balance < -1000){
            acc.setState(new Redstate(this));
        }
    }
}
#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("zhangrong");
        account->deposit(1000);
        cout<<"-------------------------------------"<<endl;
        account->withdraw(500);
        cout<<"-------------------------------------"<<endl;
        account->withdraw(2000);
        cout<<"-------------------------------------"<<endl;
        account->withdraw(600);
        cout<<"-------------------------------------"<<endl;
        account->withdraw(200);
        return 0;}

 

 

标签:acc,java,double,void,模式,state,c++,balance,public
From: https://www.cnblogs.com/rongzhang/p/17001143.html

相关文章

  • 策略模式javac++
    软件设计                 石家庄铁道大学信息学院 实验23:策略模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解策略模式的动......
  • 模板方法模式javac++
    软件设计                 石家庄铁道大学信息学院 实验24:模板方法模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解模板方法......
  • 访问者模式javac++
    软件设计                 石家庄铁道大学信息学院 实验25:访问者模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解访问者模式......
  • 职责链模式javac++
    软件设计                 石家庄铁道大学信息学院 实验15:职责链模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容: 1、理解职责链模式......
  • 命令模式javac++
    软件设计                 石家庄铁道大学信息学院 实验16:命令模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容: 1、理解命令模式的动......
  • 迭代器模式
    软件设计                 石家庄铁道大学信息学院 实验18:迭代器模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容: 1、理解迭代器模式......
  • 中介者模式javac++
    软件设计                 石家庄铁道大学信息学院 实验19:中介者模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容: 1、理解中介者模式......
  • 备忘录模式javac++
    软件设计                 石家庄铁道大学信息学院 实验 20:备忘录模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容: 1、理解备忘录模......
  • Java中线程的6种状态详解
    java.lang.Thread.State枚举类中定义了六种线程的状态,可以调用线程Thread中的getState()方法获取当前线程的状态。publicenumState{NEW,RUNNABLE,......
  • 代理模式javac++
    软件设计                 石家庄铁道大学信息学院 实验14:代理模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容: 1、理解代理模式的动......