软件设计实验22

实验 22:状态模式

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#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;
}

  

posted @   Lindseyyip  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
历史上的今天:
2021-11-03 pintia7-2 双向循环链表应用
点击右上角即可分享
微信分享提示