具体代码
//Account.java
package org.example.test020;
public class Account {
public AccountState getAccountState() {
return accountState;
}
public void setAccountState(AccountState accountState) {
this.accountState = accountState;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private AccountState accountState;
private String name;
public Account(String name) {
this.name = name;
this.accountState = new GreenState(this);
System.out.println(name + "开户成功!初始金额:0元");
System.out.println("---------------------------------");
}
public void inMoney(double money) {
accountState.inMoney(money);
}
public void outMoney(double money) {
accountState.outMoney(money);
}
}
//AccountState.java
package org.example.test020;
public abstract class AccountState {
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public double getMmm() {
return mmm;
}
public void setMmm(double mmm) {
this.mmm = mmm;
}
public String getStateName() {
return stateName;
}
public void setStateName(String stateName) {
this.stateName = stateName;
}
protected Account account;
protected double mmm;//余额的计算
protected String stateName;
public abstract void checkMoney(double money);
public void inMoney(double money) {
System.out.println(account.getName() + "存款" + money + "元");
this.mmm += money;
checkMoney(mmm);
System.out.println("当前余额为:" + this.mmm);
System.out.println("当前状态为:" + account.getAccountState().stateName);
}
public void outMoney(double money) {
System.out.println(account.getName() + "取款" + money + "元");
this.mmm -= money;
checkMoney(mmm);
System.out.println("当前余额为:" + this.mmm);
System.out.println("当前状态为:" + account.getAccountState().stateName);
}
}
//GreenState.java
package org.example.test020;
public class GreenState extends AccountState {
public GreenState(AccountState accountState) {
this.account = accountState.account;
this.mmm = accountState.mmm;
this.stateName = "正常状态";
}
public GreenState(Account account) {
this.mmm = 0;
this.account = account;
this.stateName = "正常状态";
}
@Override
public void checkMoney(double money) {
if (money <= -1000) {
account.setAccountState(new RedState(this));
}
else if (money >= -1000 && money < 0) {
account.setAccountState(new YellowState(this));
}
}
}
//RedState.java
package org.example.test020;
public class RedState extends AccountState {
public RedState(AccountState accountState) {
this.account = accountState.account;
this.mmm = accountState.getMmm();
this.stateName = "透支状态";
}
@Override
public void checkMoney(double money) {
if (money >= 0) {
account.setAccountState(new GreenState(this));
}
else if (money >= -1000 && money < 0) {
account.setAccountState(new YellowState(this));
}
}
public void outMoney(double m1) {
System.out.println("对不起!" + account.getName() + "不能取款!");
System.out.println("当前余额:" + mmm + ",当前状态为:" + account.getAccountState().stateName);
}
}
//YellowState.java
package org.example.test020;
public class YellowState extends AccountState {
public YellowState(AccountState accountState) {
this.account = accountState.account;
this.mmm = accountState.getMmm();
this.stateName = "欠费状态";
}
@Override
public void checkMoney(double money) {
if (money >= 0) {
account.setAccountState(new GreenState(this));
}
else if (money <= -1000) {
account.setAccountState(new RedState(this));
}
}
}
//Main.java
package org.example.test020;
public class Main {
public static void main(String[] args) {
Account account = new Account("张三");
account.inMoney(2000);
System.out.println("----------------");
account.outMoney(1000);
System.out.println("----------------");
account.outMoney(1500);
System.out.println("----------------");
account.outMoney(1000);
System.out.println("----------------");
account.outMoney(500);
}
}
运行结果