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

提 示:
创建 Account 类的两个子类:SavingAccount 和 CheckingAccount 子类
a. 修改 Account 类;将 balance 属性的访问方式改为 protected
b. 创建 SavingAccount 类,该类继承 Account 类
c. 该类必须包含一个类型为 double 的 interestRate 属性
d. 该类必须包括带有两个参数(balance 和 interest_rate)的公有构造器。该
构 造器必须通过调用 super(balance)将 balance 参数传递给父类构造
器。
实现 CheckingAccount 类 1. CheckingAccount 类必须扩展 Account 类 2. 该类必须包含一个类型为 double 的 overdraftProtection 属性。
3. 该类必须包含一个带有参数(balance)的共有构造器。该构造器必须通过调
用 super(balance)将 balance 参数传递给父类构造器。
4. 给类必须包括另一个带有两个参数(balance 和 protect)的公有构造器。该
构造器必须通过调用 super(balance)并设置 overdragtProtection 属性, 将 balance 参数传递给父类构造器。
5. CheckingAccount 类必须覆盖 withdraw 方法。此方法必须执行下列检
查。如 果当前余额足够弥补取款 amount,则正常进行。如果不够弥补但是
存在透支 保护,则尝试用 overdraftProtection 得值来弥补该差值
(balance-amount). 如果弥补该透支所需要的金额大于当前的保护级别。
则整个交易失败,但余 额未受影响。
6. 在主 exercise1 目录中,编译并执行 TestBanking 程序。输出应为:
Creating the customer Jane Smith.
Creating her Savings Account with a 500.00 balance and 3% interest.
尚硅谷 Java 基础实战—Bank 项目
Creating the customer Owen Bryant.
Creating his Checking Account with a 500.00 balance and no
overdraft protection.
Creating the customer Tim Soley.
Creating his Checking Account with a 500.00 balance and 500.00 in
overdraft protection.
Creating the customer Maria Soley.
Maria shares her Checking Account with her husband Tim.
Retrieving the customer Jane Smith with her savings account.
Withdraw 150.00: true
Deposit 22.50: true
Withdraw 47.62: true
Withdraw 400.00: false
Customer [Simms, Jane] has a balance of 324.88
Retrieving the customer Owen Bryant with his checking account with
no overdraft protection.
Withdraw 150.00: true
Deposit 22.50: true
Withdraw 47.62: true
Withdraw 400.00: false
Customer [Bryant, Owen] has a balance of 324.88
Retrieving the customer Tim Soley with his checking account that
has overdraft protection.
Withdraw 150.00: true
Deposit 22.50: true
Withdraw 47.62: true
Withdraw 400.00: true
Customer [Soley, Tim] has a balance of 0.0
Retrieving the customer Maria Soley with her joint checking account
with husband Tim.
Deposit 150.00: true
Withdraw 750.00: false
Customer [Soley, Maria] has a balance of 150.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 boolean withdraw(double amt){if (balance >= amt){balance -= amt ;return true ;} else {System.out.println("余额不足!");return false ;}}
}

Bank.java

package banking;public class Bank {private Customer[] customers ;private int numberOfCustomer ;public Bank(){customers = new Customer[5] ;numberOfCustomer = 0 ;}public void addCustomer(String f, String l){Customer cus= new Customer(f,l) ;customers[numberOfCustomer] = cus ;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 boolean withdraw(double amt){if (balance >= amt){balance -= amt ;return true ;} else if ((amt-balance) < overdraftProtection){overdraftProtection -= (amt-balance) ;balance = 0;return true ;} else {System.out.println("超过透支额度,交易失败");return false ;}}
}

Customer.java

package banking;public class Customer {private String firstName ;private String lastName ;private Account account ;public Customer(String f, String l){firstName = f ;lastName = l ;}public String getFirstName(){return firstName ;}public String getLastName(){return lastName ;}public void setAccount(Account acc){account = acc ;}public Account getAccount(){return account ;}
}

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;public class TestBanking {public static void main(String[] args) {Bank bank = new Bank();Customer customer;Account account;//// Create bank customers and their accounts//System.out.println("Creating the customer Jane Smith.");bank.addCustomer("Jane", "Simms");//codeaccount = new SavingAccount(500.00, 0.03);customer = bank.getCustomer(0);customer.setAccount(account);System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest.");//codeSystem.out.println("Creating the customer Owen Bryant.");//codebank.addCustomer("Owen", "Bryant");customer = bank.getCustomer(1);account = new CheckingAccount(500.00);customer.setAccount(account);System.out.println("Creating his Checking Account with a 500.00 balance and no overdraft protection.");//codeSystem.out.println("Creating the customer Tim Soley.");bank.addCustomer("Tim", "Soley");customer = bank.getCustomer(2);account = new CheckingAccount(500.00, 500.00);customer.setAccount(account);System.out.println("Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection.");//codeSystem.out.println("Creating the customer Maria Soley.");//codebank.addCustomer("Maria", "Soley");customer = bank.getCustomer(3);//account = bank.getCustomer(2).getAccount();
//    customer.setAccount(account);System.out.println("Maria shares her Checking Account with her husband Tim.");customer.setAccount(bank.getCustomer(2).getAccount());System.out.println();//// Demonstrate behavior of various account types//// Test a standard Savings AccountSystem.out.println("Retrieving the customer Jane Smith with her savings account.");customer = bank.getCustomer(0);account = customer.getAccount();// Perform some account transactionsSystem.out.println("Withdraw 150.00: " + account.withdraw(150.00));System.out.println("Deposit 22.50: " + account.deposit(22.50));System.out.println("Withdraw 47.62: " + account.withdraw(47.62));System.out.println("Withdraw 400.00: " + account.withdraw(400.00));// Print out the final account balanceSystem.out.println("Customer [" + customer.getLastName()+ ", " + customer.getFirstName()+ "] has a balance of " + account.getBalance());System.out.println();// Test a Checking Account w/o overdraft protectionSystem.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection.");customer = bank.getCustomer(1);account = customer.getAccount();// Perform some account transactionsSystem.out.println("Withdraw 150.00: " + account.withdraw(150.00));System.out.println("Deposit 22.50: " + account.deposit(22.50));System.out.println("Withdraw 47.62: " + account.withdraw(47.62));System.out.println("Withdraw 400.00: " + account.withdraw(400.00));// Print out the final account balanceSystem.out.println("Customer [" + customer.getLastName()+ ", " + customer.getFirstName()+ "] has a balance of " + account.getBalance());System.out.println();// Test a Checking Account with overdraft protectionSystem.out.println("Retrieving the customer Tim Soley with his checking account that has overdraft protection.");customer = bank.getCustomer(2);account = customer.getAccount();// Perform some account transactionsSystem.out.println("Withdraw 150.00: " + account.withdraw(150.00));System.out.println("Deposit 22.50: " + account.deposit(22.50));System.out.println("Withdraw 47.62: " + account.withdraw(47.62));System.out.println("Withdraw 400.00: " + account.withdraw(400.00));// Print out the final account balanceSystem.out.println("Customer [" + customer.getLastName()+ ", " + customer.getFirstName()+ "] has a balance of " + account.getBalance());System.out.println();// Test a Checking Account with overdraft protectionSystem.out.println("Retrieving the customer Maria Soley with her joint checking account with husband Tim.");customer = bank.getCustomer(3);account = customer.getAccount();// Perform some account transactionsSystem.out.println("Deposit 150.00: " + account.deposit(150.00));System.out.println("Withdraw 750.00: " + account.withdraw(750.00));// Print out the final account balanceSystem.out.println("Customer [" + customer.getLastName()+ ", " + customer.getFirstName()+ "] has a balance of " + account.getBalance());}
}

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

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

    实验题目 7:(在6基础上修改) 将建立一个 OverdraftException 异常,它由 Account 类的withdraw()方法抛出. 实验目的: 自定义异常 实验说明: 创建 Overd ...

  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. OpenCV 笔记(07)— Mat 对象输出格式设置(Python 格式、CSV 格式、NumPy 格式、C 语言格式)
  2. Leangoo敏捷开发项目管理平台新增测试用例管理、测试结果统计功能
  3. 基于Keras Application和Densenet迁移学习(transfer learning)的乳腺癌图像分类模型(良性、恶性)
  4. 用函数式编程思维解析anagrams函数
  5. Java自学笔记(16):常用类:Math,Data和Calender,Format,Scanner
  6. 2025年的呼叫中心是什么样的?
  7. 64位和32位的区别
  8. jquery.hotkeys监听键盘按下事件keydown
  9. JS中,把一个数字转换为字串
  10. (丘维声)高等代数课程笔记:商空间
  11. 计算机软考网络工程师视频资料,计算机软考网络工程师视频教程
  12. 计算机如何引用表格,(Excel如何实现跨文件表引用数据)excel引用其他表格数据路径...
  13. 京东返利PHP采集关键字,php-爬虫练习:抓取京东商品列表与详情-2019年10月18日...
  14. MySQL关系一对多一对一多对多
  15. CMakeList.txt的简单使用
  16. Windows中怎么下载桌面便签小工具 便签小工具简单使用教程
  17. SSM开发相关安装教程(idea、tomcat、maven、DB)
  18. 多项式函数在某一点处的泰勒展开
  19. Lua——迭代器的使用、pairs 和 ipairs区别
  20. 已被多次定制!!“模拟微信答题的H5小游戏

热门文章

  1. springMVC源码分析--访问请求执行ServletInvocableHandlerMethod和InvocableHandlerMethod
  2. aix 下mysql库使用_AIX中常用的SMIT的使用
  3. 计算机机房雷电接地,机房防雷接地系统解决方案
  4. 安全生产六步法是什么_海孜煤矿安全生产管理“六步法”实施办法.doc
  5. pandas处理Excel基本方法
  6. 扫地机器人拖实木地板_云鲸拖扫一体机,自动清洗拖布这个方案解决了这类产品的一个痛点...
  7. spring boot 2.1.5 @WebFilter 自己使用的问题
  8. 使用android.view.TouchDelegate扩大View的触摸点击区域
  9. 转换说明%f %e %g 与精度控制
  10. 利用iPS细胞筛选新药研究进展