实验基本要求:

实验题目 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” 信息

工程组织:(未展示的文件和实验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);
}
}
}
}

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;
}
}
}

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);
} }

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());
}
}
}

运行结果:

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

【Java基础-实验7】Banking_7 -添加银行透支扣款系统的 thorw异常机制的更多相关文章

  1. 【Java 基础实验_Bank项目_06】单例模式(Static Bank) , 查询异常输出

    基于上一个实验Banking_5 ,代码先全部复制过来. 笔记心得: 1.SavingAccount() 需要两种构造方法,接受单个参数和两个的 2.Account 有两个类型 SavingAccou ...

  2. Java基础之扩展GUI——添加状态栏(Sketcher 1 with a status bar)

    控制台程序. 为了显示各个应用程序参数的状态,并且将各个参数显示在各自的面板中,在应用程序窗口的底部添加状态栏是常见且非常方便的方式. 定义状态栏时没有Swing类可用,所以必须自己建立StatusB ...

  3. 【Java 基础 实验-抽象类应用的练习】(抽象类Employee被SalariedEmployee和HourEmployee继承 , 遍历,Scanner 输出)

    笔记总结: 1.Employee为抽象类,两个子类进行继承, public abstract double earning();两个子类分别实现 2.Employee[] emps[i].toStri ...

  4. Java基础知识强化之IO流笔记07:自定义的异常概述和自定义异常实现

    1. 开发的时候往往会出现很多问题(java内部系统框架中没有提供这些异常) 比如说:考试成绩必须在0~100之间. 很明显java没有对应的异常,需要我们自己来做一个异常. (1)继承自Except ...

  5. build path libraries java基础--Jar包添加到build path方式说明--01

    摘自: http://blog.csdn.net/haolongabc/article/details/7007701 java基础--Jar包添加到build path方式说明--01 前言:这段短 ...

  6. 【Java基础 项目实例--Bank项目5】Account 和 customer 对象等 继承、多态、方法的重写

    延续 Java基础 项目实例--Bank项目4 实验要求 实验题目 5: 在银行项目中创建 Account 的两个子类:SavingAccount 和 CheckingAccount 实验目的: 继承 ...

  7. Java基础复习笔记系列 八 多线程编程

    Java基础复习笔记系列之 多线程编程 参考地址: http://blog.csdn.net/xuweilinjijis/article/details/8878649 今天的故事,让我们从上面这个图 ...

  8. JavaSE学习总结(一)——Java基础

    一.Java是什么 Java 是由 Sun Microsystems 在 1995 年首先发布的编程语言和计算平台.Java 是一项用于开发应用程序的技术,可以让 Web 变得更有意思和更实用.有许多 ...

  9. java基础解析系列(四)---LinkedHashMap的原理及LRU算法的实现

    java基础解析系列(四)---LinkedHashMap的原理及LRU算法的实现 java基础解析系列(一)---String.StringBuffer.StringBuilder java基础解析 ...

随机推荐

  1. 19年最新 Python0基础学习书籍推荐(内涵PDF地址以及书籍源码)

    去年看过一篇文章,是关于18年的最适合0基础学习的书籍,今年,最新的书籍也已经统计出来.书籍的PDF太过于难找,所以很多PDF都找不到. 仅仅只能找到英文版PDF 本文章统计了18.19并做过对比,在 ...

  2. Feign【token传递】

    使用feign调用服务的时候,存在一个问题,比如当前服务调用A服务,在请求头中包含了某些特殊的字段信息,比如当前操作人的token信息,调用A的时候可以正常拿到token,然而在去调用B服务的时候,可 ...

  3. 服务提供者框架讲解 之 myJDBC

    利用一个简单的myJDBC,讲一下'服务提供者框架'的思想.主要是思想 目录 什么是 服务提供者框架 代码讲解 服务接口 服务提供者接口 服务注册API.服务访问API 静态工厂方法 服务实现类 – ...

  4. WINDOWS记事本的换行\r\n

    今天发现,\r\n才能换行,好像记事本不能改.

  5. JS 03事件

    <script type="text/javascript"> function getUserInput() { //获取用户输入的内容 var val = docu ...

  6. 怎样在网页中嵌入JS代码

    有四种方法: 方法1: 在<script>标签内直接写代码 <body> <button id="btn">click</button&g ...

  7. C#特性 详解

    一:Conditional:条件特性,预定义了一个条件方法. 使用方法: [Conditional("DEBUG")] public void test() { MessageBo ...

  8. multer实现图片上传

    multer实现图片上传: ejs代码: <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  9. [Vuex系列] - Actions的理解之我见

    Actions如何定义的 恕小端不才,对Action的总结如下: Action 可以提交mutation方法,通过mutation来改变state Action 函数可以接收一个context对象,通 ...

  10. FlowPortal BPM多汇报线的设置及使用

    1.在组织结构中设置多汇报线 2.流程中使用汇报线 3.流程节点上使用汇报线 流程节点默认启用流程中指定的汇报线,若流程中的某个节点需要启用特殊的汇报线,可通过设置节点业务属性实现.