大三每日总结
软件设计 石家庄铁道大学信息学院
实验 22:状态模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解状态模式的动机,掌握该模式的结构;
2、能够利用状态模式解决实际问题。
[实验任务一]:银行账户
用Java代码模拟实现课堂上的“银行账户”的实例,要求编写客户端测试代码模拟用户存款和取款,注意账户对象状态和行为的变化。
实验要求:
1.提交源代码;
package state;
public class Account {
private AccountState state;
private String owner;
public AccountState getState() {
return state;
}
public void setState(AccountState state) {
this.state = state;
}
public String getOwner() {
return owner;
}
public void deposit(double amount) {
state.deposit(amount);
}
public void withdraw(double amount) {
state.withdraw(amount);
}
public Account(String owner, double initBalance) {
this.owner = owner;
this.state = new GreenState(initBalance,this);
System.out.println(this.owner + "开户成功,银行卡初始金额" + initBalance);
}
}
package state;
public abstract class AccountState {
protected Account account;
protected double balance;
protected String stateName;
public void deposit(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 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 state;
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);
}
}
package state;
public class GreenState extends AccountState{
public GreenState(double balance,Account acc) {
this.balance = balance;
this.account = acc;
this.stateName = "正常状态";
}
}
package state;
import state.AccountState;
public class RedState extends AccountState {
public RedState(AccountState state) {
this.balance = state.balance;
this.account = state.account;
this.stateName="透支状态";
}
public void withdraw(double amount) {
System.out.println(account.getOwner()+"取款"+amount);
System.out.println("抱歉,您的账户已处于透支状态,不能取款");
}
}
package state;
public class YellowState extends AccountState {
public YellowState(AccountState state) {
this.balance = state.balance;
this.account = state.account;
this.stateName = "欠费状态";
}
}
2. 注意编程规范。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南