状态模式javac++
软件设计 石家庄铁道大学信息学院
实验 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;}