实验:用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. tensorflow源码解析之framework-function

    目录 什么是function FunctionDef 函数相关类 关系图 涉及的文件 迭代记录 1. 什么是function 在讲解function的概念之前,我们要先回顾下op.op是规定了输入和输 ...

  2. Python 让我舅舅的书法作品和 PIL 库发生点美的误会

    Python 让我舅舅的书法作品和 PIL 库发生点美的误会 1. 前言 不久之前写过一篇文章,详细介绍了 PIL 库中的 Image 模块的使用.曾经学习过.使用过一段时间的 PS,认识 PIL 后 ...

  3. ASP.NET CORE 项目搭建(2022 年 3 月版)

    ASP.NET CORE 项目搭建(2022 年 3 月版) 自读 沉淀了多年的技术积累,在 .NET FRAMEWORK 的框架下尝试造过自己的轮子. 摸索着闭门造过 基于 OWIN 服务后端. 摸 ...

  4. Mock平台3-初识Antd React 开箱即用中台前端框架

    微信搜索[大奇测试开],关注这个坚持分享测试开发干货的家伙. 内容提要 首先说下为啥这次测试开发系列教程前端选择Antd React,其实也是纠结对比过最终决定挑战一把,想法大概有几下几点: 笔者自己 ...

  5. SAS 数值存储方式和精度问题

    本文链接:https://www.cnblogs.com/snoopy1866/p/16021137.html 1 数值存储方式 SAS使用8个字节存储数值,使用浮点计数法表示数值. 浮点计数法由4个 ...

  6. WOE(weight of evidence, 证据权重)

    1. WOE(weight of evidence, 证据权重) WOE是一种衡量正常样本( Good)和违约样本( Bad)分布的差异方法 WOE=ln(Distr Good/Distr Bad)例 ...

  7. spring事务详解(基于注解和声明的两种实现方式)

    Spring事务( Transaction ) 事务的概念 事务是一些sql语句的集合,作为一个整体执行,一起成功或者一起失败. 使用事务的时机 一个操作需要多天sql语句一起完成才能成功 程序中事务 ...

  8. 5月28日 python学习总结 CSS学习(二)

    CSS属性相关 宽和高 width属性可以为元素设置宽度. height属性可以为元素设置高度. 块级标签才能设置宽度,内联标签的宽度由内容来决定. 字体属性 文字字体 font-family可以把多 ...

  9. MySQL知识补充(表字段操作、视图、触发器、事物、存储过程、内置函数、流程控制、索引、慢查询)

    今日内容概要 表字段操作补充(掌握) 视图(了解) 触发器(了解) 事务(掌握) 存储过程(了解) 内置函数(了解) 流程控制(了解) 索引(熟悉) 内容详细 1.表字段操作补充 # 1.添加表字段 ...

  10. 说一下linux启动过程boot流程

    linux启动过程 https://www.ibm.com/developerworks/cn/linux/l-linuxboot/index.html http://www.ruanyifeng.c ...