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

由于是c++,不像java那么灵活,所以类的调用方面出了些许多问题,包括调用,出现了很多错误,不过好在都解决了。

代码:

#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(500);
cout<<"------------------------------"<<endl;
account->deposit(1000);
cout<<"------------------------------"<<endl;
account->withdraw(2000);
cout<<"------------------------------"<<endl;
cout<<account->getState()->stateName;
account->withdraw(100);
cout<<"------------------------------"<<endl;
account->deposit(2000);
cout<<"------------------------------"<<endl;
return 0;
}

1

#include<iostream>
using namespace std;
class Account;
class AccountState{
public:
Account *acc;
double balance;
string stateName;
public:
virtual void stateCheck()=0;
virtual void deposit(double amount)=0;
virtual void withdraw(double amount)=0;
};
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 checkState(){
state->stateCheck();
}
void deposit(double amount) {
cout<<this->owner<<"存款"<<amount<<endl;
state->deposit(amount);
cout<<"账户余额:"<<state->balance<<endl;
cout<<"现在账户状态:"<<state->stateName<<endl;
}
void withdraw(double amount) {
cout<<this->owner<<"取款"<<amount<<endl;
state->withdraw(amount);
cout<<"账户余额"<<state->balance<<endl;
cout<<"现在账户状态:"<<state->stateName<<endl;
}
};
class RedState :public AccountState{
public:
RedState(AccountState *state) {
this->balance = state->balance;
this->acc = state->acc;
this->stateName="透支状态";
}
void stateCheck();
void deposit(double amount) {
this->balance+=amount;
stateCheck();
}
void withdraw(double amount) {
cout<<"您的账户处于透支状态,不能取款!"<<endl;
}
};
class YellowState :public AccountState{
public:
YellowState(AccountState *state) {
this->balance = state->balance;
this->acc = state->acc;
this->stateName="欠费状态";
}
void stateCheck();
void deposit(double amount) {
this->balance+=amount;
stateCheck();
}
void withdraw(double amount) {
this->balance-=amount;
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 deposit(double amount) {
this->balance+=amount;
stateCheck();
}
void withdraw(double amount) {
this->balance-=amount;
stateCheck();
}
};
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));
}
}
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(500);
cout<<"------------------------------"<<endl;
account->deposit(1000);
cout<<"------------------------------"<<endl;
account->withdraw(2000);
cout<<"------------------------------"<<endl;
account->withdraw(100);
cout<<"------------------------------"<<endl;
account->deposit(2000);
cout<<"------------------------------"<<endl;
return 0;
}

2

两种方法运行结果:

个人觉得第一种比较好。

c++实现状态模式的更多相关文章

  1. StatePattern(状态模式)

    /** * 状态模式 * @author TMAC-J * 状态模式和策略模式很像,其实仔细研究发现完全不一样 * 策略模式各策略之间没有任何关系,独立的 * 状态模式各状态之间接口方法都是一样的 * ...

  2. 设计模式(十二):通过ATM取款机来认识“状态模式”(State Pattern)

    说到状态模式,如果你看过之前发布的重构系列的文章中的<代码重构(六):代码重构完整案例>这篇博客的话,那么你应该对“状态模式”并不陌生,因为我们之前使用到了状态模式进行重构.上一篇博客我们 ...

  3. php实现设计模式之 状态模式

    <?php /*状态模式:允许一个对象在其内部状态改变时改变它的行为.对象看起来似乎修改了它的类.(行为模式) * * 在很多情况下,一个对象的行为取决于一个或多个动态变化的属性,这样的属性叫做 ...

  4. Java 策略模式和状态模式

    本文是转载的,转载地址:大白话解释Strategy模式和State模式的区别 先上图: 本质上讲,策略模式和状态模式做得是同一件事:去耦合.怎么去耦合?就是把干什么(语境类)和怎么干(策略接口)分开, ...

  5. javascript - 状态模式 - 简化分支判断流程

    状态模式笔记   当一个对象的内部状态发生改变时,会导致行为的改变,这像是改变了对象   状态模式既是解决程序中臃肿的分支判断语句问题,将每个分支转化为一种状态独立出来,方便每种状态的管理又不至于每次 ...

  6. C#设计模式系列:状态模式(State)

    1.状态模式简介 1.1>.定义 状态模式的核心思想是允许一个对象在它的内部状态改变时改变它的行为,即不同的状态对应不同的行为. 状态模式的针对性很强,当有状态变化的时候可以选择状态模式. 1. ...

  7. 十一个行为模式之状态模式(State Pattern)

    定义: 当一个对象有多个状态,并且在每个状态下有不同的行为,可以使用状态模式来在其内部改变状态时改变其行为,而客户端不会察觉状态的改变,仍使用同样的方法或接口与对象进行交互. 结构图: Context ...

  8. java设计模式之状态模式

    状态模式 允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类. 状态模式UML图 上下文环境(Context):它定义了客户程序需要的接口并维护一个具体状态角色的实例,将与状态相关 ...

  9. iOS - 在工程中试玩状态模式

    做了一个项目,项目中一个藏品详情界面针对不同用户,和用户所处于的状态的不同,展示的效果和操作的权限都会不同.想到了状态模式,从来没有用过,赶紧学一下然后用一用.期待兴奋 看了这么多的博客,终于找到一个 ...

  10. [Head First设计模式]生活中学设计模式——状态模式

    系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...

随机推荐

  1. Kubernetes上安装Metrics-Server

    操作场景 metrics-server 可实现 Kubernetes 的 Resource Metrics API(metrics.k8s.io),通过此 API 可以查询 Pod 与 Node 的部 ...

  2. react 16.8版本新特性以及对react开发的影响

    Facebook团队对社区上的MVC框架都不太满意的情况下,开发了一套开源的前端框架react,于2013年发布第一个版本. react最开始倡导函数式编程,使用function以及内部方法React ...

  3. 微信小程序下滑时能实现加载更多数据

    wxml代码: <view class="scroll"> <!-- 绑订页面上拉触底事件的处理函数onReachBottom事件 --> <scro ...

  4. 微信小程序结合原生JS实现电商模板(二)

    接 <微信小程序结合原生JS实现电商模板(一)>,在首页列表加入购物车到购物和模块增删数量,动态计算商品价格实现后,本次提交主要实现了商品详情(还不完善)简单页面,从商品详情页跳转到购物车 ...

  5. Applied-Social-Network-Analysis-in-Python 相关笔记4

    模型数据越多,Average系数就越小. perferential attachment model 有比较小的平均路径长度,但有着小的cc. rewire:重新连接 如果仅看这个共同的邻居数的话,数 ...

  6. LGP7890题解

    前置芝士的光速幂技巧. 本题解不是正解,和正解唯一的差别在于对幂的处理. 我们能够发现有: \[F(n,m,k)=\frac 1 n \binom {n+m-1} m \] 证明见这里. 然后我们开始 ...

  7. centos7安装mysql(完整)

    安装包下载并上传到Linux系统中 官网5.7版本:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.29-1.el7.x86_64.rpm-b ...

  8. kubernetes允许master调度

    1,让 Master 也当作 Node 使用 (1)如果想让 Pod 也能调度到在 Master(本样例即 localhost.localdomain)上,可以执行如下命令使其作为一个工作节点: 注意 ...

  9. springcloud学习01-用intellij idea搭建Eureka服务

    0.配置intellij idea工具:https://www.cnblogs.com/wang-liang-blogs/p/12060702.html 1.使用maven构建工具构建主工程项目. 1 ...

  10. [XMAN筛选赛](web)ctf用户登录

    0x00 题目概述 就只写了几道web题,有些考察点比较明显,所以个人感觉来说web总体不难. 一航的writeup写得差不多,我这写个他没写的wirteup. 看题: 链接点进去是一个登录页面,习惯 ...