实验题目 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” 信息编译并运行 TestBanking 程序
Customer [simms,Jane]has a checking balance of 200.0 with a 500.0
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
Exception: Insufficient funds for overdraft protection Deifcit:470.0
Customer [Simms,Jane]has a checking balance of 0.0
Customer [Bryant,Owen]has a checking balance of 200.0
Checking Acct[Bryant,Owen]: withdraw 100.00
Checking Acct[Bryant,Owen]: deposit25.00
Checking Acct[Bryant,Owen]: withdraw 175.00
Exception: no overdraft protection Deficit:50.0
Customer [Bryant,Owen]has a checking balance of 125.0

Account.java

package banking;public class Account {protected double balance ;public Account(){}public Account(double init_balance){balance = init_balance ;}public void setBalance(double balance) {this.balance = balance;}public double getBalance() {return balance;}public boolean deposit(double amt){if (amt > 0){balance += amt ;return true ;}else {System.out.println("请输入正确的存款数");return false ;}}public void withdraw(double amt) throws OverdraftException{if (balance >= amt){balance -= amt ;} else {throw new OverdraftException("资金不足", amt - balance) ;}}
}

Bank.java

package banking;public class Bank {private Customer[] customers ;private int numberOfCustomer ;private Bank(){customers = new Customer[5] ;numberOfCustomer = 0 ;}private static Bank bank = new Bank() ;public static Bank getBank(){return bank ;}public void addCustomer(String f, String l){Customer cust= new Customer(f,l) ;customers[numberOfCustomer] = cust ;numberOfCustomer++ ;}public int getNumOfCustomers(){return numberOfCustomer ;}public Customer getCustomer(int index){return customers[index] ;}
}

CheckingAccount.java

package banking;public class CheckingAccount extends Account{private Double overdraftProtection ;public CheckingAccount(double balance){super(balance);}public CheckingAccount(double balance, double protect){super(balance);overdraftProtection = protect ;}public double getOverdraftProtection() {return overdraftProtection;}public void setOverdraftProtection(double overdraftProtection) {this.overdraftProtection = overdraftProtection;}public void withdraw(double amt) throws OverdraftException{if (balance >= amt){balance -= amt ;} else{if(overdraftProtection == null){throw new OverdraftException("no overdraft exception", amt - balance) ;}else if (overdraftProtection <= amt - balance){throw new OverdraftException("Insuffient funds for overdraft exception", amt - balance - overdraftProtection) ;}else {overdraftProtection -= (amt - balance) ;balance = 0 ;}}}
}

Customer.java

package banking;public class Customer {private String firstName ;private String lastName ;private Account[] accounts ;private int numberOfAccounts ;public Customer(String f, String l){firstName = f ;lastName = l ;accounts = new Account[5] ;}public String getFirstName(){return firstName ;}public String getLastName(){return lastName ;}/*    public void setAccount(Account acct){accounts = acct ;}public Account getAccount(){return account ;}*/public void addAccount(Account acct){accounts[numberOfAccounts] = acct ;numberOfAccounts++ ;}public Account getAccount(int index){return accounts[index] ;}public int getNumOfAccounts(){return numberOfAccounts ;}
}

CustomerReport.java

package banking;import java.text.NumberFormat;public class CustomerReport {Bank bank = Bank.getBank() ;Customer customer ;public void generateReport(){NumberFormat currency_format = NumberFormat.getCurrencyInstance() ;System.out.println("\t\t\tCUSTOMERS REPORT");System.out.println("\t\t\t=================");for (int cust_idx = 0 ; cust_idx < bank.getNumOfCustomers() ; cust_idx++){customer = bank.getCustomer(cust_idx) ;System.out.println();System.out.println("Customer: " + customer.getLastName() + ", " + customer.getFirstName());for (int acct_idx = 0; acct_idx < customer.getNumOfAccounts(); acct_idx++){Account account = customer.getAccount(acct_idx) ;String account_type = "" ;if (account instanceof SavingAccount){account_type = "SavingAccount" ;}if (account instanceof CheckingAccount){account_type = "CheckingAccount" ;}System.out.println(account_type + ":current balance is" + currency_format.format(account.getBalance()));}}}}

OverdraftException.java

package banking;public class OverdraftException extends Exception{static final long serialVersionUID = -3387516993124229948L ;private double deficit ;public double getDeficit(){return deficit ;}public OverdraftException(String msg, double deficit){super(msg);this.deficit = deficit ;}}

SavingAccount.java

package banking;public class SavingAccount extends Account{private double interestRate ;public SavingAccount(double balance, double interest_rate){super(balance);interestRate = interest_rate ;}public double getInterestRate() {return interestRate;}public void setInterestRate(double interestRate) {this.interestRate = interestRate;}
}

TestBanking.java

package banking;import java.text.NumberFormat;public class TestBanking {public static void main(String[] args) {Bank     bank = Bank.getBank();Customer customer;Account  account;// Create two customers and their accountsbank.addCustomer("Jane", "Simms");customer = bank.getCustomer(0);customer.addAccount(new CheckingAccount(200.00, 500.00));customer.addAccount(new SavingAccount(500.00, 0.05));System.out.println(customer.getAccount(0).getBalance());bank.addCustomer("Owen", "Bryant");customer = bank.getCustomer(1);customer.addAccount(new CheckingAccount(200.00));// Test the checking account of Jane Simms (with overdraft protection)customer = bank.getCustomer(0);account = customer.getAccount(1);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.getAccount(0);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());}}
}

银行业务管理软件(7)相关推荐

  1. 银行业务管理软件(5)

    实验题目 5: 在银行项目中创建 Account 的两个子类:SavingAccount 和 CheckingAccount 实验目的: 继承.多态.方法的重写. 提 示: 创建 Account 类的 ...

  2. 银行业务管理软件(6)

    实验题目 6:(在5_续1的基础上修改) 修改 Bank 类来实现单子设计模式: 实验目的: 单子模式. 提示: 1. 修改 Bank 类,创建名为 getBanking 的公有静态方法,它返回一个 ...

  3. 银行业务管理软件(8)

    实验题目 8: 将替换这样的数组代码:这些数组代码用于实现银行和客户间,以及客户与他们 的帐户间的关系的多样性. 实验目的: 使用集合 实验说明: 修改 Bank 类 修改 Bank 类,利用 Arr ...

  4. 银行业务管理软件 (1)

    实验说明: 在这个练习里,创建一个简单版本的 Account 类.将这个源文件放入 banking 程 序包中.在创建单个帐户的默认程序包中,已编写了一个测试程序 TestBanking. 这个测试程 ...

  5. 银行业务管理软件 (5).1

    实验题目: 5_续 1 创建客户账户 实验目的: instanceof 运算符的应用 提 示: 修改 Customer 类 1.修改 Customer 类来处理具有多种类型的联合账户.(例如用数组表示 ...

  6. 【软件工程】 文档 - 银行业务管理 - 结构化设计

    软件工程 银行业务管理和现金结算系统 --- 结构化设计文档 ***原创所有,本文禁止一切形式的转载. 一.   体系结构设计 1)    软件结构化设计概述 该阶段主要在于定义银行业务管理系统的主要 ...

  7. Money Pro for Mac 1.9.2 中文破解版下载 账单计划预算管理软件

    Money Pro for Mac 提供一站式账单计划.预算管理和账户跟踪. 本应用程序拥有轻松同步功能. Money Pro 是家庭预算乃至商务应用的理想之选. 用户手册中文版能为您提供很大的帮助. ...

  8. vue右键自定义菜单_一款小巧的开源右键菜单管理软件

    要说右键管理软件,果核上面目前收集了几款,例如年久失修的右键管家. 虽然很多年没有更新了,但是软件的功能却正常,日常删除多余的右键菜单没问题. 另外,就是火绒家的右键管家,基本功能也够用 不过嘛,今天 ...

  9. 施工日志管理软件app_康智颐app下载-康智颐客户端下载v1.4.9 安卓官方版

    康智颐app是一款健康管理监测软件,软件专注为用户带来专业的健康服务,帮助用户更加方便的监测身体的各种指标,让你随时都能了解自己的健康状态,为你的身体健康保驾护航,感兴趣的朋友快来下载吧! 康智颐客户 ...

最新文章

  1. 社交媒体分析-恶意内容自动检测相关论文
  2. 科技和法律的碰撞——人脸识别为何在旧金山被叫停
  3. 机器学习中用到的概率知识_机器学习中有关概率论知识的小结
  4. linux 消息对lie_Linux系统编程—消息队列
  5. ddd架构 无法重构_DDD有什么用?
  6. Python库安装注意事项
  7. AS函数的一些特殊应用
  8. MXY-API管理系统安装教程
  9. aria2 配置教程
  10. 百度磁盘搜索和git、ssh的试用
  11. [django]梳理drf知识点
  12. 编码格式问题 错误:JSON parse error: Invalid UTF-8 middle byte 0x3f
  13. Excel如何利用时间差操作,求得员工的工龄
  14. Mysql期初数和期末数_账户中记录四种核算指标,即期初余额、 本期增加发生额、本期减少发生额和期末余额。其关系式包括( )。_学小易找答案...
  15. LaTeX长表格自动换行(longtable)
  16. **旗舰店服务器迁移方案
  17. 周三直播 | PaddleGAN又开金手指,零门槛人像转卡通
  18. sco unix 管理员速成
  19. 抖音APP逆向资料记录
  20. [python]判断麻将和牌算法

热门文章

  1. ftrace 的使用---更新中
  2. 逻辑分析仪Kingst第一天
  3. 读《春夜十话,数学与情绪》
  4. 烁博科技|浅谈视频安全监控行业发展
  5. 网线/水晶头/RJ45 网线线序
  6. 通过Python jira将问题分配给经办人
  7. C#开源网络通信库PESocket的使用
  8. Amazing!你的超级大礼包已送出,请注意查收!
  9. 最新版 SQL 8.0.21 安装与初次运行全攻略
  10. CSS3利用text-shadow属性实现多种效果文字特效