Java基础-实验7Banking_7 -添加银行透支扣款系统的 thorw异常机制

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java基础-实验7Banking_7 -添加银行透支扣款系统的 thorw异常机制相关的知识,希望对你有一定的参考价值。

实验基本要求:

技术分享图片
实验题目 7:(在6基础上修改) 
 将建立一个 OverdraftException 异常,它由 Account 类的withdraw()方法 抛出。 

实验目的: 自定义异常 
 
实验说明: 
 创建 OverdraftException 类 
 1. 在 banking.domain 包中建立一个共有类 OverdraftException. 这个类 扩展 Exception 类。  2. 添加一个 double 类型的私有属性 deficit.增加一个共有访问方法 getDeficit 
 3. 添加一个有两个参数的共有构造器。deficit 参数初始化 deficit 属性 
 
修改 Account 类 
 
4. 重写 withdraw 方法使它不返回值(即 void).声明方法抛出 overdraftException异常  
5. 修改代码抛出新异常,指明“资金不足”以及不足数额(当前余额扣除请求的数额) 
 
修改 CheckingAccount 类 

 6. 重写 withdraw 方法使它不返回值(即 void).声明方法抛出 overdraftException 异常  

7. 修改代码使其在需要时抛出异常。两种情况要处理:第一是存在没有透支保 护的赤字,对这个异常使用“no overdraft protection”信息。第二是 overdraftProtection 数 额 不 足 以 弥 补 赤 字 : 对 这 个 异 常 可 使 用 ”Insufficient funds for overdraft protection” 信息 
View Code

 

工程组织:(未展示的文件和实验6的基本完全一直)

CheckingAccount.java  (钱款+额度不足, 不再返回int)

技术分享图片
package Banking_7;


public class CheckingAccount extends Account {
    private double overdraft;//超额限额保护
    public CheckingAccount(double balance,double overd){
        super(balance);
        this.overdraft=overd;
    }
    public CheckingAccount(double balance){
        super(balance);
        this.overdraft=0;
    }
    public void showinfo(){
        System.out.println("您的余额:"+this.getBalance()+"\t"+
                "您的可透支余额:"+this.overdraft);
    }
    public void withdraw(double amt) throws OverdraftException{
        if(amt<=super.getBalance())
            super.setBalance(super.getBalance()-amt );
        else{
            if(this.overdraft==0){
                throw new OverdraftException("no overdraft protection(没有透支保护):",(amt-this.balance));
            }

            double val=amt-super.getBalance();
            if(val<=this.overdraft)
            {
                super.setBalance(0);
                this.overdraft-=val;
            }
            else{
                throw new OverdraftException("Insufficient funds for overdraft protection: 数额不足以弥补赤字," +
                        "缺少资金或额度:",val-this.overdraft);
            }
        }
    }
}
View Code

Account.java

技术分享图片
package Banking_7;
public class Account {

    protected double balance=0;//余额  ,uml前该变量是 ‘-‘
    public Account(double init_balance){
        balance=init_balance;
    }
    public double getBalance() {
        return balance;
    }
    public void setBalance(double b){this.balance=b;}
    //存钱

    public boolean deposit(double amt){
        this.balance+=amt;return true;
    }
    //取钱
    public void withdraw(double amt) throws OverdraftException{
        if(amt>this.balance){
            throw new OverdraftException("资金不足,缺少金额:",(amt-this.balance));
        }
        else{
            this.balance-=amt;
        }
    }
}
View Code

OverdraftException.java (透支额度的 异常保护机制 )

技术分享图片
package Banking_7;

public class OverdraftException extends Exception{
    private double deficit;

    public double getDeficit() {
        return deficit;
    }

    public void setDeficit(double deficit) {
        this.deficit = deficit;
    }

    public OverdraftException(double deficit) {
        this.deficit = deficit;
    }

    public OverdraftException(String message,double deficit) {
        super(message);
        this.deficit=deficit;
        System.out.println(message+deficit);
    }

}
View Code

TestBanking_7.java  (测试类)

技术分享图片
package TestBanks;/*
 * This class creates the program to test the banking classes.
 * It creates a set of customers, with a few accounts each,
 * and generates a report of current account balances.
 */

import Banking_7.*;

public class TestBanking_7 {

  public static void main(String[] args) {
    Bank     bank = Bank.getBank();
    Customer customer;
    Account  account;

    // Create two customers and their accounts
    bank.addCustomer("Jane", "Simms");
    customer = bank.getCustomer(0);
    customer.addAccount(new SavingAccount(500.00, 0.05),1);
    customer.addAccount(new CheckingAccount(200.00, 500.00),2);
    bank.addCustomer("Owen", "Bryant");
    customer = bank.getCustomer(1);
    customer.addAccount(new CheckingAccount(200.00),2);

    // Test the checking account of Jane Simms (with overdraft protection)
    customer = bank.getCustomer(0);
    account = customer.getCheckingAccount();
    System.out.println("Customer [" + customer.getLastName()
               + ", " + customer.getFirstName() + "]"
               + " has a checking balance of "
               + account.getBalance()
             + " with a 500.00 overdraft protection.");
    try {
      System.out.println("Checking Acct [Jane Simms] : withdraw 150.00");
      account.withdraw(150.00);
      System.out.println("Checking Acct [Jane Simms] : deposit 22.50");
      account.deposit(22.50);
      System.out.println("Checking Acct [Jane Simms] : withdraw 147.62");
      account.withdraw(147.62);
      System.out.println("Checking Acct [Jane Simms] : withdraw 470.00");
      account.withdraw(470.00);
    } catch (OverdraftException e1) {
      System.out.println("Exception: " + e1.getMessage()
             + "   Deficit: " + e1.getDeficit());
    } finally {
      System.out.println("Customer [" + customer.getLastName()
             + ", " + customer.getFirstName() + "]"
             + " has a checking balance of "
             + account.getBalance());
    }
    System.out.println();

    // Test the checking account of Owen Bryant (without overdraft protection)
    customer = bank.getCustomer(1);
    account = customer.getCheckingAccount();
    System.out.println("Customer [" + customer.getLastName()
               + ", " + customer.getFirstName() + "]"
               + " has a checking balance of "
               + account.getBalance());
    try {
      System.out.println("Checking Acct [Owen Bryant] : withdraw 100.00");
      account.withdraw(100.00);
      System.out.println("Checking Acct [Owen Bryant] : deposit 25.00");
      account.deposit(25.00);
      System.out.println("Checking Acct [Owen Bryant] : withdraw 175.00");
      account.withdraw(175.00);
    } catch (OverdraftException e1) {
      System.out.println("Exception: " + e1.getMessage()
             + "   Deficit: " + e1.getDeficit());
    } finally {
      System.out.println("Customer [" + customer.getLastName()
             + ", " + customer.getFirstName() + "]"
             + " has a checking balance of "
             + account.getBalance());
    }
  }
}
View Code

 

运行结果:

技术分享图片
Customer [Simms, Jane] has a checking balance of 200.0 with a 500.00 overdraft protection.
Checking Acct [Jane Simms] : withdraw 150.00
Checking Acct [Jane Simms] : deposit 22.50
Checking Acct [Jane Simms] : withdraw 147.62
Checking Acct [Jane Simms] : withdraw 470.00
Insufficient funds for overdraft protection: 数额不足以弥补赤字,缺少资金或额度:45.120000000000005
Exception: Insufficient funds for overdraft protection: 数额不足以弥补赤字,缺少资金或额度:   Deficit: 45.120000000000005
Customer [Simms, Jane] has a checking balance of 0.0

Customer [Bryant, Owen] has a checking balance of 200.0
Checking Acct [Owen Bryant] : withdraw 100.00
Checking Acct [Owen Bryant] : deposit 25.00
Checking Acct [Owen Bryant] : withdraw 175.00
no overdraft protection(没有透支保护):50.0
Exception: no overdraft protection(没有透支保护):   Deficit: 50.0
Customer [Bryant, Owen] has a checking balance of 125.0
View Code

 

以上是关于Java基础-实验7Banking_7 -添加银行透支扣款系统的 thorw异常机制的主要内容,如果未能解决你的问题,请参考以下文章

2019_5_13_周事务

Java基础_基础语法知识7

Java基础进阶_7

Tutorial 01_JAVA基本语法[实验任务四]

07_Java基础语法_第7天(练习)_讲义

中国MOOC_零基础学Java语言_第7周 函数_1分解质因数