11.23实验 22:状态模式
[实验任务一]:银行账户
用Java代码模拟实现课堂上的“银行账户”的实例,要求编写客户端测试代码模拟用户存款和取款,注意账户对象状态和行为的变化。
实验要求:
1. 画出对应的类图;
2. 提交源代码;
#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("安安");
account->deposit(2000);
cout<<"-------------------------------------"<<endl;
account->withdraw(500);
cout<<"-------------------------------------"<<endl;
account->withdraw(2000);
cout<<"-------------------------------------"<<endl;
account->withdraw(600);
cout<<"-------------------------------------"<<endl;
account->withdraw(200);
return 0;
}
3. 注意编程规范。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统