软件设计:实验 22:状态模式

实验 22:状态模式

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

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

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

 

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

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

实验要求:

1. 画出对应的类图

2. 提交源代码

3. 注意编程规范。

1.

 

2.// AccountState.java

public interface AccountState {

    void deposit(double amount);

    void withdraw(double amount);

}

 

// NormalState.java

public class NormalState implements AccountState {

    private BankAccount account;

 

    public NormalState(BankAccount account) {

        this.account = account;

    }

 

    @Override

    public void deposit(double amount) {

        account.setBalance(account.getBalance() + amount);

        System.out.println("Deposited: " + amount + ", New Balance: " + account.getBalance());

    }

 

    @Override

    public void withdraw(double amount) {

        if (amount <= account.getBalance()) {

            account.setBalance(account.getBalance() - amount);

            System.out.println("Withdrew: " + amount + ", New Balance: " + account.getBalance());

        } else {

            System.out.println("Insufficient funds! Current Balance: " + account.getBalance());

            account.setState(new OverdrawnState(account));

        }

    }

}

 

// OverdrawnState.java

public class OverdrawnState implements AccountState {

    private BankAccount account;

 

    public OverdrawnState(BankAccount account) {

        this.account = account;

    }

 

    @Override

    public void deposit(double amount) {

        account.setBalance(account.getBalance() + amount);

        System.out.println("Deposited: " + amount + ", New Balance: " + account.getBalance());

        if (account.getBalance() >= 0) {

            account.setState(new NormalState(account));

        }

    }

 

    @Override

    public void withdraw(double amount) {

        System.out.println("Cannot withdraw, account is overdrawn! Current Balance: " + account.getBalance());

    }

}

 

// BankAccount.java

public class BankAccount {

    private double balance;

    private AccountState state;

 

    public BankAccount() {

        this.balance = 0.0;

        this.state = new NormalState(this);

    }

 

    public double getBalance() {

        return balance;

    }

 

    public void setBalance(double balance) {

        this.balance = balance;

    }

 

    public void setState(AccountState state) {

        this.state = state;

    }

 

    public void deposit(double amount) {

        state.deposit(amount);

    }

 

    public void withdraw(double amount) {

        state.withdraw(amount);

    }

}

 

// Client.java

public class Client {

    public static void main(String[] args) {

        BankAccount account = new BankAccount();

        

        account.deposit(100);

        account.withdraw(50);

        account.withdraw(70); // This will trigger overdrawn state

        account.deposit(30); // This will bring the account back to normal

        account.withdraw(20);

    }

}

 

 

posted @ 2024-12-02 22:03  痛苦代码源  阅读(7)  评论(0编辑  收藏  举报