11.25

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

 

实验 22:状态模式

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

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

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

 

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

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

实验要求:

1. 类图

 

 

2. 源代码

①、AccountState.java

package org.example.shiyanershier;

public abstract class AccountState {
    protected Account acc;
    protected double balance;
    public abstract void stateCheck();
    public abstract void deposit(double amount);
    public abstract void withdraw(double amount);
}

 

②、GreenState.java

package org.example.shiyanershier;

public class GreenState extends AccountState {

    public GreenState() {}

    public GreenState(double amount, Account account) {
        balance = amount;
        acc = account;
    }

    @Override
    public void stateCheck() {
        if (balance >= 0) {
            acc.setState(new GreenState(balance, acc));
            System.out.println("当前为正常(绿色)状态");
        } else if(balance <0 && balance >= -1000) {
            acc.setState(new YellowState(balance, acc));
            System.out.println("当前为欠费(黄色)状态");
        } else {
            acc.setState(new RedState(balance, acc));
            System.out.println("当前透支(红色)状态");
        }

    }

    @Override
    public void deposit(double amount) {
        balance += amount;
        stateCheck();
    }

    @Override
    public void withdraw(double amount) {
        balance -= amount;
        stateCheck();
    }
}

 

③、RedState.java

package org.example.shiyanershier;

public class RedState extends AccountState {

    public RedState(double amount, Account acc) {
        this.balance = amount;
        this.acc = acc;
    }
    @Override
    public void stateCheck() {
        if (balance >= 0) {
            acc.setState(new GreenState(balance, acc));
            System.out.println("当前为正常(绿色)状态");
        } else if(balance <0 && balance >= -1000) {
            acc.setState(new YellowState(balance, acc));
            System.out.println("当前为欠费(黄色)状态");
        } else {
            acc.setState(new RedState(balance, acc));
            System.out.println("当前透支(红色)状态");
        }

    }

    @Override
    public void deposit(double amount) {
        balance += amount;
        stateCheck();
    }

    @Override
    public void withdraw(double amount) {
        System.out.println("透支状态,无法取款!");
    }
}

 

④、YellowState.java

package org.example.shiyanershier;

public class YellowState extends AccountState {

    public YellowState(double amount, Account acc) {
        this.balance = amount;
        this.acc = acc;
    }

    @Override
    public void stateCheck() {
        if (balance >= 0) {
            acc.setState(new GreenState(balance, acc));
            System.out.println("当前为正常(绿色)状态");
        } else if(balance <0 && balance >= -1000) {
            acc.setState(new YellowState(balance, acc));
            System.out.println("当前为欠费(黄色)状态");
        } else {
            acc.setState(new RedState(balance, acc));
            System.out.println("当前透支(红色)状态");
        }
    }

    @Override
    public void deposit(double amount) {
        balance += amount;
        stateCheck();

    }

    @Override
    public void withdraw(double amount) {
        balance -= amount;
        stateCheck();

    }
}

 

⑤、Account.java

package org.example.shiyanershier;

public class Account {
    private AccountState state;
    private String owner;
    public Account(String owner, double init) {
        this.owner = owner;
        state = new GreenState(init, this);
        System.out.println("账户:" + this.owner + ",余额:" + init);

    }

    public void setState(AccountState state) {
        this.state = state;
    }

    /**
     * 存款
     * @param amount
     */
    public void deposit(double amount) {
        state.deposit(amount);
        System.out.println("现金余额为" + this.state.balance);
    }


    /**
     * 取款
     * @param amount
     */
    public void withdraw(double amount){
        state.withdraw(amount);
        System.out.println("现金余额为" + state.balance);
    }
}

 

⑥、Main.java

package org.example.shiyanershier;

public class Main {
    public static void main(String[] args) {
        Account acc = new Account("何不语", 2500);
        acc.deposit(1000);
        acc.deposit(2000);
        acc.withdraw(5000);
        acc.withdraw(7000);
        acc.deposit(6000);
    }
}

 

3. 运行结果

 

 

 

posted @ 2024-12-18 17:51  七安。  阅读(2)  评论(0编辑  收藏  举报