首页 > 其他分享 >软件设计:实验 22:状态模式

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

时间:2024-12-02 22:11:19浏览次数:4  
标签:account 22 软件设计 double void amount 实验 getBalance public

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

    }

}

 

 

标签:account,22,软件设计,double,void,amount,实验,getBalance,public
From: https://www.cnblogs.com/xuan-2004/p/18582853

相关文章

  • 实验5 继承和多态
    实验任务3实验代码:1#pragmaonce2#include<iostream>3#include<string>45classMachinePets{6public:7MachinePets(conststd::string&s);8std::stringget_nickname()const;9virtualstd::stringtalk()=0;10......
  • 实验5 C++
    任务三:pets.hpp1#pragmaonce2#include<iostream>3#include<string>45usingnamespacestd;67classMachinePets{8private:9stringnickname;10public:11MachinePets(conststrings):nickname{s}{}12stringge......
  • 实验5
    task1publisher.hpp#pragmaonce#include<iostream>#include<string>usingstd::cout;usingstd::endl;usingstd::string;//发行/出版物类:Publisher(抽象类)classPublisher{public:Publisher(conststring&s="");......
  • 实验五 继承和多态
    1.实验任务三pets.hpp1#pragmaonce2#include<iostream>3#include<string>4usingnamespacestd;56classMachinePets{7public:8MachinePets(conststring&s="");9public:10virtualstringtal......
  • 实验五 C语言指针应用编程
    实验五C语言指针应用编程实验任务1——数组求最大最小值#include<stdio.h>#defineN5voidinput(intx[],intn);voidoutput(intx[],intn);voidfind_min_max(intx[],intn,int*pmin,int*pmax);intmain(){ inta[N]; intmin,max; printf("录入%d个数......
  • 实验5 继承和多态
    任务3源码:1#pragmaonce23#include<iostream>4#include<string>56usingstd::string;789classMachinePets{10public:11MachinePets(conststring&s);1213public:14virtualstringget_nickname......
  • 实验5 继承和多态
    实验任务三源码1#pragmaonce2#include<iostream>3#include<string>45usingstd::string;6usingstd::cout;7usingstd::endl;89//电子宠物类10classMachinePets{11private:12stringnickname;13public:14MachinePets(cons......
  • 实验5
    实验任务3pets.hpp代码点击查看代码#include<iostream>#include<string>usingnamespacestd;classMachinePets{public: MachinePets(conststrings); virtualstringtalk()const=0; stringget_nickname()const; private: stringnickname; };Mac......
  • 实验五 继承和多态
    实验一:代码:publisher.hpp:1#pragmaonce23#include<iostream>4#include<string>56usingstd::cout;7usingstd::endl;8usingstd::string;910classPublisher{11public:12Publisher(conststring&s="");......
  • OPP实验五
    任务一、 任务二、 2【通过sort库函数来实现,其中,写了一个比较函数来作为sort的参数】 3【通过ChatGPT】 3、任务三、 1#pragmaonce2#include<string>3usingnamespacestd;45classMachinePets{6private:7stringnickname;8pub......