实验 22:状态模式

发布时间 2023-12-09 20:45:18作者: 林浅

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容: 

1、理解状态模式的动机,掌握该模式的结构;

2、能够利用状态模式解决实际问题。

 

[实验任务一]:银行账户

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

实验要求:

1. 提交源代码;

//Account.java

package shiyan22;

/**

 * @Description:环境类

 * @author 张紫诺

 *

 */

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 shiyan22;

/**

 * @Description:抽象状态类

 * @author 张紫诺

 *

 */

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 shiyan22;

/**

 * @Description:正常状态类

 * @author 张紫诺

 *

 */

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 shiyan22;

/**

 * @Description:透支状态类

 * @author 张紫诺

 *

 */

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 shiyan22;

/**

 * @Description:欠费状态类

 * @author 张紫诺

 *

 */

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 shiyan22;

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);

    }

}

2. 注意编程规范。