实验基本要求:

  1. 实验题目 7:(在6基础上修改)
  2. 将建立一个 OverdraftException 异常,它由 Account 类的withdraw()方法 抛出。
  3.  
  4. 实验目的: 自定义异常
  5.  
  6. 实验说明:
  7. 创建 OverdraftException
  8. 1 banking.domain 包中建立一个共有类 OverdraftException. 这个类 扩展 Exception 类。 2 添加一个 double 类型的私有属性 deficit.增加一个共有访问方法 getDeficit
  9. 3 添加一个有两个参数的共有构造器。deficit 参数初始化 deficit 属性
  10.  
  11. 修改 Account
  12.  
  13. 4 重写 withdraw 方法使它不返回值(即 void).声明方法抛出 overdraftException异常
  14. 5 修改代码抛出新异常,指明“资金不足”以及不足数额(当前余额扣除请求的数额)
  15.  
  16. 修改 CheckingAccount
  17.  
  18. 6 重写 withdraw 方法使它不返回值(即 void).声明方法抛出 overdraftException 异常
  19.  
  20. 7 修改代码使其在需要时抛出异常。两种情况要处理:第一是存在没有透支保 护的赤字,对这个异常使用“no overdraft protection”信息。第二是 overdraftProtection 使 Insufficient funds for overdraft protection 信息

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

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

  1. package Banking_7;
  2.  
  3. public class CheckingAccount extends Account {
  4. private double overdraft;//超额限额保护
  5. public CheckingAccount(double balance,double overd){
  6. super(balance);
  7. this.overdraft=overd;
  8. }
  9. public CheckingAccount(double balance){
  10. super(balance);
  11. this.overdraft=0;
  12. }
  13. public void showinfo(){
  14. System.out.println("您的余额:"+this.getBalance()+"\t"+
  15. "您的可透支余额:"+this.overdraft);
  16. }
  17. public void withdraw(double amt) throws OverdraftException{
  18. if(amt<=super.getBalance())
  19. super.setBalance(super.getBalance()-amt );
  20. else{
  21. if(this.overdraft==0){
  22. throw new OverdraftException("no overdraft protection(没有透支保护):",(amt-this.balance));
  23. }
  24.  
  25. double val=amt-super.getBalance();
  26. if(val<=this.overdraft)
  27. {
  28. super.setBalance(0);
  29. this.overdraft-=val;
  30. }
  31. else{
  32. throw new OverdraftException("Insufficient funds for overdraft protection: 数额不足以弥补赤字," +
  33. "缺少资金或额度:",val-this.overdraft);
  34. }
  35. }
  36. }
  37. }

Account.java

  1. package Banking_7;
  2. public class Account {
  3.  
  4. protected double balance=0;//余额 ,uml前该变量是 '-'
  5. public Account(double init_balance){
  6. balance=init_balance;
  7. }
  8. public double getBalance() {
  9. return balance;
  10. }
  11. public void setBalance(double b){this.balance=b;}
  12. //存钱
  13.  
  14. public boolean deposit(double amt){
  15. this.balance+=amt;return true;
  16. }
  17. //取钱
  18. public void withdraw(double amt) throws OverdraftException{
  19. if(amt>this.balance){
  20. throw new OverdraftException("资金不足,缺少金额:",(amt-this.balance));
  21. }
  22. else{
  23. this.balance-=amt;
  24. }
  25. }
  26. }

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

  1. package Banking_7;
  2.  
  3. public class OverdraftException extends Exception{
  4. private double deficit;
  5.  
  6. public double getDeficit() {
  7. return deficit;
  8. }
  9.  
  10. public void setDeficit(double deficit) {
  11. this.deficit = deficit;
  12. }
  13.  
  14. public OverdraftException(double deficit) {
  15. this.deficit = deficit;
  16. }
  17.  
  18. public OverdraftException(String message,double deficit) {
  19. super(message);
  20. this.deficit=deficit;
  21. System.out.println(message+deficit);
  22. }
  23.  
  24. }

TestBanking_7.java  (测试类)

  1. package TestBanks;/*
  2. * This class creates the program to test the banking classes.
  3. * It creates a set of customers, with a few accounts each,
  4. * and generates a report of current account balances.
  5. */
  6.  
  7. import Banking_7.*;
  8.  
  9. public class TestBanking_7 {
  10.  
  11. public static void main(String[] args) {
  12. Bank bank = Bank.getBank();
  13. Customer customer;
  14. Account account;
  15.  
  16. // Create two customers and their accounts
  17. bank.addCustomer("Jane", "Simms");
  18. customer = bank.getCustomer(0);
  19. customer.addAccount(new SavingAccount(500.00, 0.05),1);
  20. customer.addAccount(new CheckingAccount(200.00, 500.00),2);
  21. bank.addCustomer("Owen", "Bryant");
  22. customer = bank.getCustomer(1);
  23. customer.addAccount(new CheckingAccount(200.00),2);
  24.  
  25. // Test the checking account of Jane Simms (with overdraft protection)
  26. customer = bank.getCustomer(0);
  27. account = customer.getCheckingAccount();
  28. System.out.println("Customer [" + customer.getLastName()
  29. + ", " + customer.getFirstName() + "]"
  30. + " has a checking balance of "
  31. + account.getBalance()
  32. + " with a 500.00 overdraft protection.");
  33. try {
  34. System.out.println("Checking Acct [Jane Simms] : withdraw 150.00");
  35. account.withdraw(150.00);
  36. System.out.println("Checking Acct [Jane Simms] : deposit 22.50");
  37. account.deposit(22.50);
  38. System.out.println("Checking Acct [Jane Simms] : withdraw 147.62");
  39. account.withdraw(147.62);
  40. System.out.println("Checking Acct [Jane Simms] : withdraw 470.00");
  41. account.withdraw(470.00);
  42. } catch (OverdraftException e1) {
  43. System.out.println("Exception: " + e1.getMessage()
  44. + " Deficit: " + e1.getDeficit());
  45. } finally {
  46. System.out.println("Customer [" + customer.getLastName()
  47. + ", " + customer.getFirstName() + "]"
  48. + " has a checking balance of "
  49. + account.getBalance());
  50. }
  51. System.out.println();
  52.  
  53. // Test the checking account of Owen Bryant (without overdraft protection)
  54. customer = bank.getCustomer(1);
  55. account = customer.getCheckingAccount();
  56. System.out.println("Customer [" + customer.getLastName()
  57. + ", " + customer.getFirstName() + "]"
  58. + " has a checking balance of "
  59. + account.getBalance());
  60. try {
  61. System.out.println("Checking Acct [Owen Bryant] : withdraw 100.00");
  62. account.withdraw(100.00);
  63. System.out.println("Checking Acct [Owen Bryant] : deposit 25.00");
  64. account.deposit(25.00);
  65. System.out.println("Checking Acct [Owen Bryant] : withdraw 175.00");
  66. account.withdraw(175.00);
  67. } catch (OverdraftException e1) {
  68. System.out.println("Exception: " + e1.getMessage()
  69. + " Deficit: " + e1.getDeficit());
  70. } finally {
  71. System.out.println("Customer [" + customer.getLastName()
  72. + ", " + customer.getFirstName() + "]"
  73. + " has a checking balance of "
  74. + account.getBalance());
  75. }
  76. }
  77. }

运行结果:

  1. Customer [Simms, Jane] has a checking balance of 200.0 with a 500.00 overdraft protection.
  2. Checking Acct [Jane Simms] : withdraw 150.00
  3. Checking Acct [Jane Simms] : deposit 22.50
  4. Checking Acct [Jane Simms] : withdraw 147.62
  5. Checking Acct [Jane Simms] : withdraw 470.00
  6. Insufficient funds for overdraft protection: 数额不足以弥补赤字,缺少资金或额度:45.120000000000005
  7. Exception: Insufficient funds for overdraft protection: 数额不足以弥补赤字,缺少资金或额度: Deficit: 45.120000000000005
  8. Customer [Simms, Jane] has a checking balance of 0.0
  9.  
  10. Customer [Bryant, Owen] has a checking balance of 200.0
  11. Checking Acct [Owen Bryant] : withdraw 100.00
  12. Checking Acct [Owen Bryant] : deposit 25.00
  13. Checking Acct [Owen Bryant] : withdraw 175.00
  14. no overdraft protection(没有透支保护):50.0
  15. Exception: no overdraft protection(没有透支保护): Deficit: 50.0
  16. 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. 删除Excel表格中一堆英文中的汉字

    昨天需要处理一个Excel文件,删除一堆英文里的汉字,开始搜了下方法,没找到,然后手动一个多小时,弄了一半吧也就,结果电脑卡了,忘了保存,就白做了...不知道为啥这次没有自动保存,所以,重要的事说三遍 ...

  2. 【VS开发】fatal error C1001:编译器中发生内部错误

    自己编译boost的库文件时遇到这个错误的,大概报错情况如下:  mp_defer.hpp<50>:fatal error C1001:编译器中发生内部错误.  1> 要解决此问题, ...

  3. 最新 中至数据java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.中至数据等10家互联网公司的校招Offer,因为某些自身原因最终选择了中至数据.6.7月主要是做系统复习.项目复盘.Leet ...

  4. java spring事务管理相关

    一般项目结构为: 数据持久层dao     业务层service     控制层controller 事务控制是在业务层service起作用的,所以需要同时对多张表做添加,修改或删除操作时应该在ser ...

  5. [转帖]超详细的Oracle数据库在不同损坏级别的恢复总结

    超详细的Oracle数据库在不同损坏级别的恢复总结 原创 波波说运维 2019-07-20 00:02:00 概述 在 DBA 的日常工作中不可避免存在着数据库的损坏,今天主要介绍 Oracle 数据 ...

  6. Linux (x86) Exploit 开发系列教程之七 绕过 ASLR -- 第二部分

    (1)原理: 使用爆破技巧,来绕过共享库地址随机化.爆破:攻击者选择特定的 Libc 基址,并持续攻击程序直到成功.这个技巧是用于绕过 ASLR 的最简单的技巧. (2)漏洞代码 //vuln.c # ...

  7. 写CSDN博客

    文章目录 前言 写博客的规范 写博客的小技巧 版权声明模板 博客表格模板 更改博客字体和颜色 LaTeX 数学公式 前言 这是一篇关于写CSDN博客的文章.记录我的博客规范,技巧,模板,心得. 写博客 ...

  8. Spring @Transactional注解在什么情况下会失效,为什么?

    出处:  https://www.cnblogs.com/hunrry/p/9183209.html   https://www.cnblogs.com/protected/p/6652188.htm ...

  9. 导入别的项目到我的eclipse上出现红色感叹号问题

        项目红色感叹号问题问题 一般我们在导入别的项目到我的eclipse上面会发现,项目上面有红色的错误     原因 因为我电脑上的 jdk版本和别人电脑jdk版本不一样,那么对于的jre版本也不 ...

  10. uboot 添加自定义命令

    ref : https://www.cnblogs.com/FREMONT/p/9824226.html 1.添加命令 1.1在common目录下,新建一个cmd_xx.c, 需要添加的命令格式为: ...