3 完整解决方案
图4 银行账户结构图
温馨提示:代码有点长,需要有耐心!
-
//银行账户:环境类
-
class
Account { -
private AccountState //维持一个对抽象状态对象的引用state; -
private String //开户名owner; -
private double balance 0;= //账户余额 -
-
public Account(String doubleowner, init) { -
this.owner = owner; -
this.balance = balance; -
this.state = newNormalState( this);//设置初始状态 -
System.out.println(this.owner + "开户,初始金额为"+ init); -
System.out.println("---------------------------------------------"); -
} -
-
public double getBalance() { -
return this.balance; -
} -
-
public void setBalance( doublebalance) { -
this.balance = balance; -
} -
-
public void setState(AccountState state) { -
this.state = state; -
} -
-
public void deposit( doubleamount) { -
System.out.println(this.owner + "存款"+ amount); -
state.deposit(amount); //调用状态对象的deposit()方法 -
System.out.println("现在余额为"+ this.balance); -
System.out.println("现在帐户状态为"+ this.state.getClass().getName()); -
System.out.println("---------------------------------------------"); -
} -
-
public void withdraw( doubleamount) { -
System.out.println(this.owner + "取款"+ amount); -
state.withdraw(amount); //调用状态对象的withdraw()方法 -
System.out.println("现在余额为"+ this.balance); -
System.out.println("现在帐户状态为"+ this. state.getClass().getName()); -
System.out.println("---------------------------------------------"); -
} -
-
public void computeInterest() -
{ -
state.computeInterest(); //调用状态对象的computeInterest()方法 -
} - }
-
-
//抽象状态类
-
abstract
class AccountState { -
protected Account acc; -
public abstract void deposit( doubleamount); -
public abstract void withdraw( doubleamount); -
public abstract void computeInterest(); -
public abstract void stateCheck(); - }
-
-
//正常状态:具体状态类
-
class
NormalState extendsAccountState { -
public NormalState(Account acc) { -
this.acc = acc; -
} -
-
public
NormalState(AccountState state) { -
this.acc = state.acc; -
} -
-
public void deposit( doubleamount) { -
acc.setBalance(acc.getBalance() + amount); -
stateCheck(); -
} -
-
public void withdraw( doubleamount) { -
acc.setBalance(acc.getBalance() - amount); -
stateCheck(); -
} -
-
public void computeInterest() -
{ -
System.out.println("正常状态,无须支付利息!"); -
} -
-
//状态转换 -
public void stateCheck() { -
if (acc.getBalance() 2000> - && 0)acc.getBalance() <= { -
acc.setState(new OverdraftState( this)); -
} -
else if (acc.getBalance() 2000)== - { -
acc.setState(new RestrictedState( this)); -
} -
else if (acc.getBalance() 2000)< - { -
System.out.println("操作受限!"); -
} -
} -
}
-
-
//透支状态:具体状态类
-
class
OverdraftState extendsAccountState - {
-
public OverdraftState(AccountState state) { -
this.acc = state.acc; -
} -
-
public void deposit( doubleamount) { -
acc.setBalance(acc.getBalance() + amount); -
stateCheck(); -
} -
-
public void withdraw( doubleamount) { -
acc.setBalance(acc.getBalance() - amount); -
stateCheck(); -
} -
-
public void computeInterest() { -
System.out.println("计算利息!"); -
} -
-
//状态转换 -
public void stateCheck() { -
if (acc.getBalance() 0)> { -
acc.setState(new NormalState( this)); -
} -
else if (acc.getBalance() 2000)== - { -
acc.setState(new RestrictedState( this)); -
} -
else if (acc.getBalance() 2000)< - { -
System.out.println("操作受限!"); -
} -
} - }
-
-
//受限状态:具体状态类
-
class
RestrictedState extendsAccountState { -
public RestrictedState(AccountState state) { -
this.acc = state.acc; -
} -
-
public void deposit( doubleamount) { -
acc.setBalance(acc.getBalance() + amount); -
stateCheck(); -
} -
-
public void withdraw( doubleamount) { -
System.out.println("帐号受限,取款失败"); -
} -
-
public void computeInterest() { -
System.out.println("计算利息!"); -
} -
-
//状态转换 -
public void stateCheck() { -
if(acc.getBalance() > 0){ -
acc.setState(new NormalState( this)); -
} -
else if(acc.getBalance() > 2000)- { -
acc.setState(new OverdraftState( this)); -
} -
} - }
-
class
Client { -
public static void main(String args[]) { -
Account acc = new Account( "段誉",0.0); -
acc.deposit(1000); -
acc.withdraw(2000); -
acc.deposit(3000); -
acc.withdraw(4000); -
acc.withdraw(1000); -
acc.computeInterest(); -
} - }
段誉开户,初始金额为0.0 --------------------------------------------- 段誉存款1000.0 现在余额为1000.0 现在帐户状态为NormalState --------------------------------------------- 段誉取款2000.0 现在余额为-1000.0 现在帐户状态为OverdraftState --------------------------------------------- 段誉存款3000.0 现在余额为2000.0 现在帐户状态为NormalState --------------------------------------------- 段誉取款4000.0 现在余额为-2000.0 现在帐户状态为RestrictedState --------------------------------------------- 段誉取款1000.0 帐号受限,取款失败 现在余额为-2000.0 现在帐户状态为RestrictedState --------------------------------------------- 计算利息! |
【作者:刘伟 http://blog.csdn.net/lovelion】