状态模式(一)

Java代码模拟实现课堂上的银行账户的实例,要求编写客户端测试代码模拟用户存款和取款,注意账户对象状态和行为的变化。

 

//Account.java
package test22;
public class Account {
    private AccountState state;
    private String name;
    public Account(String name){
        this.name=name;
        state=new Greenstate(this);
        System.out.println(name+"开户成功!初始金额:0元");
        System.out.println("******************************");
    }
    public void setState(AccountState state){
        this.state=state;
    }
    public AccountState getState() {
        return this.state;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void deposit(double money){
        state.deposit(money);
    }
    public void withdraw(double money){
        state.withdraw(money);
    }
}
//AccountState.java
package test22;
public abstract class AccountState {
    protected Account acc;
    protected double balance;
    protected String stateName;
    public abstract void stateCheck(double balance);
    public double getBalance() {
        return balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
    public String getStateName() {
        return stateName;
    }
    public void setStateName(String stateName) {
        this.stateName = stateName;
    }
    public void deposit(double amount) {
        System.out.println(acc.getName()+"存款"+amount+"元");
        balance+=amount;
        stateCheck(balance);
        System.out.println("当前余额:"+balance+"元,当前状态:"+acc.getState().stateName);
    }
    public void withdraw(double amount) {
        System.out.println(acc.getName()+"取款"+amount+"元");
        balance-=amount;
        stateCheck(balance);
        System.out.println("当前余额:"+balance+"元,当前状态:"+acc.getState().stateName);
    }
}
//Greenstate.java
package test22;
public class Greenstate extends AccountState{
    public Greenstate(AccountState state){
        this.acc=state.acc;
        this.balance=state.balance;
        this.stateName="正常状态";
    }
    public Greenstate(Account acc){
        this.balance=0;
        this.acc=acc;
        this.stateName="正常状态";
    }
    public void stateCheck(double balance){
        if(balance<-1000){
            acc.setState(new Redstate(this));
        }else if(balance>=-1000&&balance<0){
            acc.setState(new Yellowstate(this));
        }
    }
}
//Redstate.java
package test22;
public class Redstate extends AccountState {

    public Redstate(AccountState state) {
        this.acc=state.acc;
        this.balance=state.getBalance();
        this.stateName="透支状态";
    }

    public void stateCheck(double balance) {
        if(balance >= -1000 && balance < 0){
            acc.setState(new Yellowstate(this));
        }else if(balance >= 0){
            acc.setState(new Greenstate(this));
        }
    }
    public void withdraw(double amount) {
            System.out.println("对不起,"+acc.getName()+",您不能取款");
            System.out.println("当前余额:"+balance+"元,当前状态:"+acc.getState().stateName);
    }
}
//Yellowstate.java
package test22;
public class Yellowstate extends AccountState {

    public Yellowstate(AccountState state) {
        this.acc=state.acc;
        this.balance=state.getBalance();
        this.stateName="欠费状态";
    }

    public void stateCheck(double balance) {
        if(balance >=0 ){
            acc.setState(new Greenstate(this));
        }else if(balance < -1000){
            acc.setState(new Redstate(this));
        }
    }
}
//Client.java
package test22;
public class Client {
    public static void main(String args[]){
        Account acc=new Account("王五");
        acc.deposit(2000);
        System.out.println("---------------------------------------");
        acc.withdraw(500);
        System.out.println("---------------------------------------");
        acc.withdraw(2000);
        System.out.println("---------------------------------------");
        acc.withdraw(600);
        System.out.println("---------------------------------------");
        acc.withdraw(200);
    }
}

 

posted @ 2022-12-23 20:36  今天又双叒叕在敲代码  阅读(17)  评论(0编辑  收藏  举报