状态模式
package com.state;
public class Account {
private AccountState state;
private String owner;
public Account(String owner){
this.owner=owner;
this.state=new GreenState(this);
System.out.printf(this.owner+"开户成功!");
System.out.printf("\n");
System.out.printf("--------------------------------------------------\n");
}
public AccountState getState() {
return state;
}
public void setState(AccountState
state) {
this.state = state;
}
public String getOwner() {
return owner;
}
public void setOwner(String
owner) {
this.owner = owner;
}
public void deposit(double amount){
state.deposit(amount);
}
public void withdraw(double amount){
state.withdraw(amount);
}
}
package com.state;
public abstract class AccountState {
protected Account acc;
protected double blance;
protected String stateName;
public abstract void checkState(double blance);
public void deposit(double amount){
this.blance+=amount;
checkState(this.blance);
System.out.printf(acc.getOwner()+"存款"+amount+"元,现有余额"+this.blance+"元,当前状态为:"+acc.getState().getStateName()+"\n");
}
public void withdraw(double amount){
this.blance-=amount;
checkState(this.blance);
System.out.printf(acc.getOwner()+"取款"+amount+"元,现有余额"+this.blance+"元,当前状态为:"+acc.getState().getStateName()+"\n");
}
public Account getAcc() {
return acc;
}
public void setAcc(Account acc) {
this.acc = acc;
}
public double getBlance() {
return blance;
}
public void setBlance(double blance) {
this.blance = blance;
}
public String getStateName() {
return stateName;
}
public void setStateName(String stateName) {
this.stateName = stateName;
}
}
package com.state;
public class GreenState extends AccountState{
public GreenState(AccountState state){
this.acc=state.acc;
this.blance=state.getBlance();
this.stateName="绿色状态";
}
public GreenState(Account acc){
this.blance=0;
this.acc=acc;
this.stateName="绿色状态";
}
public void checkState(double blance){
if(blance<0&&blance>=-1000){
acc.setState(new YellowState(this));
}else if(blance<-1000){
acc.setState(new RedState(this));
}
}
public void deposit(double amount){
this.blance+=amount;
checkState(this.blance);
System.out.printf(acc.getOwner()+"存款"+amount+"元,现有余额"+this.blance+"元,当前状态为:"+acc.getState().getStateName()+"\n");
}
public void withdraw(double amount){
this.blance-=amount;
checkState(this.blance);
System.out.printf(acc.getOwner()+"取款"+amount+"元,现有余额"+this.blance+"元,当前状态为:"+acc.getState().getStateName()+"\n");
}
}
package com.state;
public class YellowState extends AccountState{
public YellowState(AccountState state){
this.acc=state.acc;
this.blance=state.getBlance();
this.stateName="黄色状态";
}
public void checkState(double blance){
if(blance>=0){
acc.setState(new GreenState(this));
}else if(blance<-1000){
acc.setState(new RedState(this));
}
}
public void deposit(double amount){
this.blance+=amount;
checkState(this.blance);
System.out.printf(acc.getOwner()+"存款"+amount+"元,现有余额"+this.blance+"元,当前状态为:"+acc.getState().getStateName()+"\n");
}
public void withdraw(double amount){
this.blance-=amount;
checkState(this.blance);
System.out.printf(acc.getOwner()+"取款"+amount+"元,现有余额"+this.blance+"元,当前状态为:"+acc.getState().getStateName()+"\n");
}
}
package com.state;
public class RedState extends AccountState{
public RedState(AccountState state){
this.acc=state.acc;
this.blance=state.getBlance();
this.stateName="红色状态";
}
public void checkState(double blance){
if(blance<0&&blance>=-1000){
acc.setState(new YellowState(this));
}else if(blance>=0){
acc.setState(new GreenState(this));
}
}
public void deposit(double amount){
this.blance+=amount;
checkState(this.blance);
System.out.printf(acc.getOwner()+"存款"+amount+"元,现有余额"+this.blance+"元,当前状态为:"+acc.getState().getStateName()+"\n");
}
public void withdraw(double amount){
checkState(this.blance);
System.out.printf(acc.getOwner()+"取款"+amount+"元,取款失败,现有余额"+this.blance+"元,当前状态为:"+acc.getState().getStateName()+"\n");
}
}
package com.state;
public class Client {
public static void main(String[] args) {
Account account=new Account("张三");
account.deposit(1000);
System.out.printf("--------------------------------------------------\n");
account.deposit(10000);
System.out.printf("--------------------------------------------------\n");
account.withdraw(10000);
System.out.printf("--------------------------------------------------\n");
account.withdraw(1100);
System.out.printf("--------------------------------------------------\n");
account.withdraw(1000);
System.out.printf("--------------------------------------------------\n");
account.withdraw(100);
System.out.printf("--------------------------------------------------\n");
}
}