实验 22:状态模式

发布时间 2024-01-09 15:10:38作者: wardream

 

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

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

实验要求:

1.      提交源代码;

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

    }

}

 

 

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

    }

}

 

 

package test22;

 

public class Client {

    public static void main(String args[]){

        Account acc=new Account("zhangrong");

        acc.deposit(1000);

        System.out.println("---------------------------------------");

        acc.withdraw(500);

        System.out.println("---------------------------------------");

        acc.withdraw(2000);

        System.out.println("---------------------------------------");

        acc.withdraw(600);

        System.out.println("---------------------------------------");

        acc.withdraw(200);

    }

}

 

 

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

                    }

             }

  }

 

 

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

    }

}

 

 

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

        }

    }

}

 

 

 

2. 注意编程规范。