软件设计-状态模式
银行账户
用Java 语言(一种计算机语言,尤用于创建网站)语言(一种计算机语言,尤用于创建网站)代码模拟实现课堂上的"银行账户"的实例,要求编写客户端测试代码模拟用户存款和取款,注意账户对象状态和行为的变化。
类图
源代码
Java 语言(一种计算机语言,尤用于创建网站)语言(一种计算机语言,尤用于创建网站)
包rjsj.no22
公共类绿色状态扩展了AccountState{
公共绿色状态(双余额,帐户ACC){ 0
这个. balance=余额;
此. account=acc
this.stateName='正常状态;
}
}
包rjsj.no22
公共类红色状态扩展了AccountState{
公共状态(帐户状态){ 0
这个。平衡=状态。平衡;
这个。帐户=状态。账户;
this.stateName='透支状态;
}
公开作废提款(双倍金额){ 0
系统。出去。println(帐户。getowner())取款金额);
System.out.println('抱歉,您的账户已处于透支状态,不能取款!');
}
}
包rjsj.no22
公共类黄州扩展了AccountState{
公共黄色状态(帐户状态){ 0
这个。平衡=状态。平衡;
这个。帐户=状态。账户;
this.stateName='欠费状态;
}
}
包rjsj.no22
公共类帐户{
私人账户国家;
私有字符串所有者;
公共帐户状态GetState(){ 0
返回状态;
}
公共void setState(帐户状态状态){ 0
这个.状态=状态
}
公共字符串getOwner(){ 0
归还所有者;
}
公共无效存款(双倍金额){ 0
存款(金额);
}
公开作废提款(双倍金额){ 0
状态。提取(金额);
}
公共帐户(字符串所有者,双initBalance){ 0
this.owner=所有者;
这个。state=new GreenState(initBalance,this);
System.out.println(this.owner)开户成功!银行卡初始金额:' initBalance);
系统。出去。println('-');
}
}
包rjsj.no22
公共抽象类AccountState {
受保护账户账户;//账户名
受保护的双重平衡;//账户金额
受保护的字符串状态名;//当前状态
公共无效存款(双倍金额){ 0
系统。出去。println(帐户。getowner())存款金额);
这个余额=这个余额。
状态检查();
System.out.println('账户余额:“这个。余额);
System.out.println('账户状态:' account.getS
tate().stateName);
}
public void withdraw(double amount) {
System.out.println(account.getOwner() + "取款" + amount);
this.balance = this.balance - amount;
stateCheck();
System.out.println("账户余额:" + this.balance);
System.out.println("账户状态:" + account.getState().stateName);
}
public void stateCheck(){//状态审查
if( balance = -1000 balance 0 ) {
account.setState(new YellowState(this));
}else if( balance -1000 ) {
account.setState(new RedState(this));
}else if ( balance 0 ){
account.setState(new GreenState(this.balance,this.account));
}
}
}
package rjsj.no22; public class Client { public static void main(String[] args) { Account account = new Account("张三",100); account.deposit(8888); System.out.println("------------------------------"); account.withdraw(666); System.out.println("------------------------------"); account.deposit(50); System.out.println("------------------------------"); account.withdraw(9000); System.out.println("------------------------------"); account.withdraw(50000); System.out.println("------------------------------"); account.deposit(3000); System.out.println("------------------------------"); account.withdraw(100); } }
C++
#includeiostream 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=-1000balance0) { 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=-1000balance0) { 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=-1000balance0) { 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){ coutacc-getOwner()"存款"amountendl; this-balance+=amount; stateCheck(); cout"账户余额:"this-balanceendl; cout"账户状态:"acc-getState()-stateNameendl; } void AccountState::withdraw(double amount){ coutacc-getOwner()"取款"amountendl; this-balance-=amount; stateCheck(); cout"账户余额:"this-balanceendl; cout"账户状态:"acc-getState()-stateNameendl; } Account::Account(string owner,double init){ this-owner=owner; this-state=new GreenState(init,this); cout"恭喜"this-owner"开户成功!银行卡初始金额:"initendl; cout"--------------------------------------------------"endl; } int main(){ Account *account=new Account("张三",100); account-deposit(100); cout"--------------------------------------------------"endl; account-withdraw(1000); cout"--------------------------------------------------"endl; account-deposit(1000); cout"--------------------------------------------------"endl; account-withdraw(2000); cout"--------------------------------------------------"endl; account-withdraw(10000); cout"--------------------------------------------------"endl; account-withdraw(1); cout"--------------------------------------------------"endl; account-deposit(100000); cout"--------------------------------------------------"endl; return 0; }
运行结果:
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/145049.html