2024.12.2

设计模式实验二十二

软件设计                  石家庄铁道大学信息学院

 

实验 22:状态模式

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

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

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

 
   

 

 

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

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

实验要求:

1.     画出对应的类图;

 

 

2.     提交源代码;

// 抽象状态类

abstract class AccountState {

    protected Account acc;

    protected double balance;

 

    public AccountState(Account acc, double balance) {

        this.acc = acc;

        this.balance = balance;

    }

 

    public abstract void stateCheck();

 

    public void deposit(double amount) {

        balance += amount;

        System.out.println("存入金额:" + amount);

        stateCheck();

    }

 

    public void withdraw(double amount) {

        if (balance >= amount) {

            balance -= amount;

            System.out.println("取出金额:" + amount);

        } else {

            System.out.println("余额不足,无法取款!");

        }

        stateCheck();

    }

}

 

// 绿色状态类

class GreenState extends AccountState {

    public GreenState(Account acc, double balance) {

        super(acc, balance);

    }

 

    @Override

    public void stateCheck() {

        if (balance < 0) {

            acc.setState(new RedState(this));

        } else if (balance < 1000) {

            acc.setState(new YellowState(this));

        }

    }

}

 

// 黄色状态类

class YellowState extends AccountState {

    public YellowState(AccountState state) {

        super(state.acc, state.balance);

    }

 

    @Override

    public void stateCheck() {

        if (balance < 0) {

            acc.setState(new RedState(this));

        } else if (balance >= 1000) {

            acc.setState(new GreenState(acc, balance));

        }

    }

}

 

// 红色状态类

class RedState extends AccountState {

    public RedState(AccountState state) {

        super(state.acc, state.balance);

    }

 

    @Override

    public void withdraw(double amount) {

        System.out.println("账户余额为负,无法取款!");

    }

 

    @Override

    public void stateCheck() {

        if (balance >= 0 && balance < 1000) {

            acc.setState(new YellowState(this));

        } else if (balance >= 1000) {

            acc.setState(new GreenState(acc, balance));

        }

    }

}

 

// 银行账户类

class Account {

    private AccountState state;

    private String owner;

 

    public Account(String owner, double initBalance) {

        this.owner = owner;

        this.state = new GreenState(this, initBalance);

        System.out.println(owner + " 开设账户,初始余额为:" + initBalance);

    }

 

    public void setState(AccountState state) {

        this.state = state;

    }

 

    public void deposit(double amount) {

        state.deposit(amount);

    }

 

    public void withdraw(double amount) {

        state.withdraw(amount);

    }

}

 

// 客户端测试代码

public class BankAccountTest {

    public static void main(String[] args) {

        Account account = new Account("张三", 500);

 

        account.deposit(300);  // 余额 800,仍为黄状态

        account.withdraw(900); // 余额 -100,切换到红状态

        account.deposit(200);  // 余额 100,切换到黄状态

        account.deposit(900);  // 余额 1000,切换到绿状态

        account.withdraw(500); // 余额 500,切换到黄状态

    }

}

3. 注意编程规范。

 

 

posted @ 2024-12-25 10:25  cvjj  阅读(27)  评论(0)    收藏  举报