11.23實驗 22:狀態模式

發表於2024-12-02

[實驗任務一]:銀行賬戶

用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. 注意程式設計規範。

相關文章