SoftWare Engineering -- WEEK.2

这篇博客介绍了使用Java实现银行账户管理的代码,包括基本的BankAccount类、带有透支限制的CurrentAccount子类以及Bank服务类。博主通过创建、关闭账户,存款、取款、检查余额等操作展示了系统功能。同时,博客提到了在敏捷开发中需求分析的重要性,特别是用户故事和故事卡片的应用。测试类中使用了JUnit进行单元测试。
摘要由CSDN通过智能技术生成

SoftWare Engineering – WEEK.2

2022.3.10 @Raoquan


WEEK.2 List

0. Lab 1

  • code

1. Requirements

  • definition
  • Theory and Techniques

2. Requirements in Agile Development

  • User stories
  • Story cards

0. Lab 1

虽然我没有上课,但看老师的意思大概是根据银行的背景来开发一个程序,选择waterflow或者agile的方式。(但这个程序真的很小,所以我并没那么正经
但是我们需要有必要的javaDoc和测试

a. exe 1

直接把BankAccount类的代码copy下来就好。然后我们要根据需求,写几个方法,比如存钱,取钱,查看余额。
然后比较有意思的一点是,第二问说最大透支限额是500,那意思是第一题的透支额度可以无限大???反正我先当他是无限大的写了
然后我加了一个flag来判断有没有欠钱,从而让服务更加完整
记得加javaDoc,我这个doc也不是经常用,将就着看

/**
 * Pojo class {@code BankAccount}
 *
 * <p> This class provides the entity about bank accounts' basic information
 * Also,it has the method to deposit, withdraw and check balance.
 *
 * @author Chenyang He
 * @version 1.0
 *
 */
public class BankAccount {
    private int accNo;
    private String accName;
    private double balance;

    /** The account has not been overdrawn by default {@value} */
    boolean flagOverdraft = false;

    /**
     * Initialize BankAccount
     *
     * @param accNo the ID that will be used
     * @param accName the name that will be used
     */
    public BankAccount(int accNo,String accName){
        this.accNo=accNo;
        this.accName=accName;
        this.balance=0.0;
    }

    /**
     * Initialize BankAccount
     *
     * @param accName the name that will be used
     * @param accNo the ID that will be used
     */
    public BankAccount(String accName,int accNo){
        this.accNo=accNo;
        this.accName=accName;
        this.balance=0.0;
    }

    /**
     * Get the ID of account
     *
     * @return accNo
     */
    public int getAccNo() {
        return accNo;
    }

    /**
     * Set the ID of account
     *
     * @param accNo the ID that will be used
     */
    public void setAccNo(int accNo) {
        this.accNo = accNo;
    }

    /**
     * Get the name of account
     *
     * @return accName
     */
    public String getAccName() {
        return accName;
    }

    /**
     * Get the ID of account
     *
     * @param accName the name that will be used
     */
    public void setAccName(String accName) {
        this.accName = accName;
    }

    /**
     * Get the balance of account
     *
     * @return balance
     */
    public double getBalance() {
        return balance;
    }

    /**
     * Set the balance of account
     *
     * @param balance the balance that will be set
     */
    public void setBalance(double balance) {
        this.balance = balance;
    }


    public String toString() {
        return "BankAccount{" +
                "accNo=" + accNo +
                ", accName='" + accName + '\'' +
                ", balance=" + balance +
                '}';
    }

    /**
     * Add the balance of account
     *
     * @param addBalance the balance will be added
     */
    public void deposit(double addBalance){
        if(flagOverdraft){
            System.out.println("The account "+this.getAccName()+" will add balance: "+addBalance);
            if(addBalance>=Math.abs(this.getBalance())){
                this.setBalance(this.getBalance()+addBalance);
                this.flagOverdraft=false;
                System.out.println("Your bill has been paid off, Remaining balance: "+this.getBalance());
                return;
            }
            else{
                this.setBalance(this.getBalance()+addBalance);
                System.out.println("You still owe money: "+Math.abs(this.getBalance()));
                return;
            }
        }
        else{
            System.out.println("The account "+this.getAccName()+" will add balance: "+addBalance);
            this.setBalance(this.getBalance()+addBalance);
            return;
        }
    }

    /**
     * Reduce the balance of account
     *
     * @param withdrawBalance the balance will be reduced
     */
    public void withdraw(double withdrawBalance){
        if(flagOverdraft){
            System.out.println("The account "+this.getAccName()+" will reduce balance: "+withdrawBalance);
            this.setBalance(this.getBalance()-withdrawBalance);
            System.out.println("You have owen money: "+Math.abs(this.getBalance()));
            return;
        }
        else{
            if(withdrawBalance>=this.getBalance()){
                this.setBalance(this.getBalance()-withdrawBalance);
                this.flagOverdraft=true;
                System.out.println("You have owen money: "+Math.abs(this.getBalance()));
                return;
            }
            else{
                this.setBalance(this.getBalance()-withdrawBalance);
                System.out.println("Remaining balance: "+this.getBalance());
                return;
            }
        }
    }

    /**
     * Check the balance of account
     *
     * @param accNo the number that will be used to find account
     * @return the balance of the account
     */
    public double checkBalance(int accNo){
        if(flagOverdraft){
            System.out.println("The account "+this.getAccName()+" has owen money: "+Math.abs(this.getBalance()));
            return this.getBalance();
        }
        else{
            System.out.println("The account "+this.getAccName()+" has balance: "+this.getBalance());
            return this.getBalance();
        }
    }
}

b. exe 2

根据题意,我们将创建一个子类,并且要设置默认每日限额500,就是每天最多能用500.所以我们继承之后要重写取钱的方法。
我们只需要加一句判断就可以

/**
 * Pojo class {@code CurrentAccount}
 *
 * <p> This class provides the entity about bank accounts' basic information.
 * Class {@code BankAccount} is the parent of this class.
 *
 * @author Chenyang He
 * @version 1.0
 *
 */
public class CurrentAccount extends BankAccount{

    /**
     * Initialize CurrentAccount, and set the balance to 500
     *
     * @param accNo the ID that will be used
     * @param accName the name that will be used
     */
    public CurrentAccount(int accNo, String accName) {
        super(accNo, accName);
    }

    /**
     * Initialize CurrentAccount, and set the balance to 500
     *
     * @param accNo the ID that will be used
     * @param accName the name that will be used
     */
    public CurrentAccount(String accName,int accNo){
        super(accName,accNo);
    }

    /** This account has default overdraft  {@value} */
    private double overdraft=500.0;
    
    /**
     * Reduce the balance of account
     *
     * @param withdrawBalance the balance will be reduced
     */
    public void withdraw(double withdrawBalance){
        if(flagOverdraft){
            System.out.println("The account "+this.getAccName()+" will reduce balance: "+withdrawBalance);
            this.setBalance(this.getBalance()-withdrawBalance);
            System.out.println("You have owen money: "+Math.abs(this.getBalance()));
            return;
        }
        else{
            if(withdrawBalance>=this.getBalance()){
                if(withdrawBalance-this.getBalance()>this.overdraft){
                    System.out.println("You cannot exceed your quota");
                    return;
                }
                this.setBalance(this.getBalance()-withdrawBalance);
                this.flagOverdraft=true;
                System.out.println("You have owen money: "+Math.abs(this.getBalance()));
                return;
            }
            else{
                this.setBalance(this.getBalance()-withdrawBalance);
                System.out.println("Remaining balance: "+this.getBalance());
                return;
            }
        }
    }
}

c. exe 3

我们先分析一下题意,bank里面肯定会有所有的账户,而账户的类型有两种,还可以对这些账户进行操作。
那么首先我们需要有一个东西来存所有的账户,然后要有很多方法来对这些账户进行操作
由于继承关系,我们需要利用多态来调用不同账户对应自己的方法(题中只有取钱有所区别,参照上一题)。所以我们的Arraylist要采用泛型

/**
 * Service class {@code Bank}
 *
 * <p> This class provides the entity about bank's service
 *
 * @author Chenyang He
 * @version 1.0
 *
 */
public class Bank {

    /** This list save all the account in the bank  {@value} */
    ArrayList<BankAccount> accountList = new ArrayList<>();

    /**
     * Create a new account in the bank
     *
     * @param accNo the accNo will be created
     * @param type the type of account
     */
    public void openNew(int accNo,int type){
        for(int i=0;i<accountList.size();i++){
            if(accountList.get(i).getAccNo()==accNo){
                System.out.println("Repeat account");
                return;
            }
            else continue;
        }

        if(type==1){
            accountList.add(new BankAccount(accNo,"account"+accNo));
        }
        else if(type==2){
            accountList.add(new CurrentAccount(accNo,"account"+accNo));
        }
    }

    /**
     * Close the account in the bank
     *
     * @param accNo the accNo will be closed
     */
    public void closeAccount(int accNo){
        for(int i=0;i<accountList.size();i++){
            if(accountList.get(i).getAccNo()==accNo){
                accountList.remove(i);
                return;
            }
            else continue;
        }
        System.out.println("Invalid account");
    }

    /**
     * Deposit the account's balance in the bank
     *
     * @param accNo the accNo will be used
     * @param addBalance the balance will be added
     */
    public void deposit(int accNo,double addBalance){
        for(int i=0;i<accountList.size();i++){
            if(accountList.get(i).getAccNo()==accNo){
                accountList.get(i).deposit(addBalance);
                return;
            }
            else continue;
        }
        System.out.println("Invalid account");
    }

    /**
     * Withdraw the account's balance in the bank
     *
     * @param accNo the accNo will be used
     * @param withdrawBalance the balance will be reduced
     */
    public void withdraw(int accNo,double withdrawBalance){
        for(int i=0;i<accountList.size();i++){
            if(accountList.get(i).getAccNo()==accNo){
                accountList.get(i).withdraw(withdrawBalance);
                return;
            }
            else continue;
        }
        System.out.println("Invalid account");
    }

    /**
     * Check the account's balance in the bank
     *
     * @param accNo the accNo will be used
     */
    public double checkBalance(int accNo){
        for(int i=0;i<accountList.size();i++){
            if(accountList.get(i).getAccNo()==accNo){
                return accountList.get(i).getBalance();
            }
            else continue;
        }
        System.out.println("Invalid account");
        return 0;
    }

    /**
     * toString method for bank
     *
     * @return the information about bank's accounts
     */
    public String toString() {
        String infor="";
        for(int i=0;i<accountList.size();i++){
            infor=infor+accountList.get(i).toString()+"\n";
        }
        return infor;
    }
}

最后我们要写测试类来对上面三个题进行测试,我实在不想写doc了,整个lab的代码就这样。我考虑的条件多了些所以比较长

public class Test {

    @org.junit.Test
    public void BankTest(){
        BankAccount t=new BankAccount(1,"account1");
        t.deposit(100);
        t.checkBalance();
        System.out.println("\n");
        t.withdraw(200);
        t.checkBalance();
        System.out.println("\n");
        t.deposit(200);
        t.checkBalance();
        System.out.println("\n");
        System.out.println(t.toString());
    }


    @org.junit.Test
    public void currentTest(){
        CurrentAccount t=new CurrentAccount(2,"account2");
        t.deposit(100);
        t.checkBalance();
        System.out.println("\n");
        t.withdraw(800);
        t.checkBalance();
        System.out.println("\n");
        t.deposit(200);
        t.checkBalance();
        System.out.println("\n");
        System.out.println(t.toString());
    }

    @org.junit.Test
    public void bank(){
        Bank b=new Bank();
        b.openNew(1,1);
        b.openNew(2,1);
        b.openNew(3,2);
        b.openNew(4,2);
        System.out.println(b.toString());
        System.out.println("\n");

        b.closeAccount(1);
        b.closeAccount(5);
        System.out.println(b.toString());
        System.out.println("\n");

        b.deposit(1,200);
        b.deposit(2,400);
        b.deposit(3,500);
        b.deposit(4,100);
        System.out.println(b.toString());
        System.out.println("\n");


        b.withdraw(3,1100);
        b.withdraw(3,200);
        System.out.println(b.toString());
        System.out.println("\n");

        System.out.println(b.checkBalance(1));
        System.out.println(b.checkBalance(2));
        System.out.println(b.checkBalance(3));
        System.out.println(b.checkBalance(3));
        System.out.println("\n");

    }
}

顺便提一下测试类中的 @org.junit.Test 这个东西的作用,当你在测试类的方法上面写上这句代码,我的idea就变成了图中这样。在方法的旁边出现一个绿色的东西,点击就可以run方法体中的代码。
可以理解为加上注解@Test后,不需要写main方法直接运行该方法,这样会省去很多繁琐
但是@Test是在junit这个jar包中,需要先导入才可以使用,如果本地环境没有的话还需要下载。
但idea很方便,你直接写上@Test,然后会报红,根据提示下载导入就可以用了
请添加图片描述

1. Requirements

对于这个ppt,咋说呢,感觉怪乱的,而且都是大白话,现在好像也没啥太值得记忆。准备放到期末再背吧。我就大致列个图先:请添加图片描述

2. Requirements in Agile Development

这里就开始详细讲解敏捷开发中的步骤,这章是关于刚开始的需求分析。也就是之前提过的user stories及它衍生而来的各种东西。然后对于这些story来说,我们要学会写story card

其实我觉得ppt也比较清楚,例子也很多。所以我又画了图,放到期末再背,感觉这些大白话理解一下还是能扯出来。
主要是需要看看哪些例子,明白story怎么写;story是怎么拆分的;story的acceptance criteria是怎么写的;整个项目的词汇表应该收录哪些词汇,以及词汇表的格式请添加图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值