【Java基础 项目实例--Bank项目5】Account 和 customer 对象等 继承、多态、方法的重写
延续
Java基础 项目实例--Bank项目4
实验要求
实验题目 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). 如果弥补该透支所需要的金额大于当前的保护级别。 则整个交易失败,但余 额未受影响。
编程后的笔记
1.迫切需要理清楚继承、多态、方法的重写的逻辑 2.针对每个人物对象, 首先先在Bank()中进行构造--调用addCustomer()方法实现类,然后 bank.getCustomer() 返回该customer 对象; 3.接着声明一个账户account类 ,调用方法 customer.setAccount()设置其本人的账户; 4.通过步骤2-3,把bank类 -customer类-account类的关系链接到了一起 5.这样设计,只需要一个bank类 数组即可!针对每个bank类 都可以使用get 获取到对应的account( ) ,再使用类customer获取到 类account. ---以上大概就是面向对象的解决办法 --其实面向过程也好,直接开个结构体数组,该存的都存上--粗暴(但很繁琐)!
UML 结构图
具体工程组织
具体代码实现:
Account.java
package Banking_5;
public class Account {
protected double balance;//余额 ,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 Boolean withdraw(double amt) {
if(amt>this.balance)
return false;
else{
this.balance-=amt;
return true;
}
}
}
Bank.java
package Banking_5; public class Bank {
private Customer[] customers ; //用于存放客户
private int numberofCustomers; //用于记录Customer的个数 public Bank(){
numberofCustomers=0;
customers = new Customer[100]; ///这里记得要初始化!不然要发生java.lang.NullPointerException
}
public void addCustomer(String f,String l){
int i= this.numberofCustomers;
customers[i]=new Customer(f,l);//新建一个构造对象
this.numberofCustomers++;
}
public int getNumOfCustomers() {
return numberofCustomers;
}
public Customer getCustomer(int index) {
return customers[index];
}
}
CheckingAccount.java
package Banking_5; public class CheckingAccount extends Account{
private double overdraft;//超额限额保护
public CheckingAccount(double balance,double overd){
super(balance);
this.overdraft=overd;
}
public void showinfo(){
System.out.println("您的余额:"+this.getBalance()+"\t"+
"您的可透支余额:"+this.overdraft);
}
public Boolean withdraw(double amt){
if(amt<=super.getBalance())
super.setBalance(super.getBalance()-amt );
else{
double val=amt-super.getBalance();
if(val<=this.overdraft)
{
super.setBalance(0);
this.overdraft-=val;
}
else{
System.out.println("该消费超过可透支额的限额");
return false;
}
}
return true;
}
}
Customer.java
package Banking_5; public class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer(){}
public Customer(String f, String l){
this.firstName=f;
this.lastName=l;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}
SavingAccount.java
package Banking_5;
public class SavingAccount extends Account{
private double interest_Rate; //利率
public SavingAccount(double balance,double interest_Rate){
super(balance);
this.interest_Rate=interest_Rate;
}
}
TestBanking.java( 在测试包中的 测试类)
package TestBanks;
import Banking_5.*; public class TestBanking_5 { public static void main(String[] args) {
Bank bank = new Bank();
Customer customer;
Account account;
//
// Create Bank customers and their account
System.out.println("Creating the customer Jane Smith.");
System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest.");
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
account = new SavingAccount(500,0.03);
customer.setAccount(account); System.out.println("Creating the customer Owen Bryant.");
//code
bank.addCustomer("Owen","Bryant");
customer=bank.getCustomer(1);
account=new CheckingAccount(500,0);
customer.setAccount(account);
System.out.println("Creating his Checking Account with a 500.00 balance and no overdraft protection."); //code
System.out.println("Creating the customer Tim Soley.");
bank.addCustomer("Tim", "Soley");
customer = bank.getCustomer(2);
account=new CheckingAccount(500,500);
customer.setAccount(account);
System.out.println("Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection.");
//code System.out.println("Creating the customer Maria Soley.");
bank.addCustomer("Maria", "Soley");
customer = bank.getCustomer(3);
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 Account
System.out.println("Retrieving the customer Jane Smith with her savings account.");
customer = bank.getCustomer(0);
account = customer.getAccount();
// Perform some account transactions
System.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 balance
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName()
+ "] has a balance of " + account.getBalance()); System.out.println(); // Test a Checking Account w/o overdraft protection
System.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 transactions
System.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 balance
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName()
+ "] has a balance of " + account.getBalance()); System.out.println(); // Test a Checking Account with overdraft protection
System.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 transactions
System.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 balance
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName()
+ "] has a balance of " + account.getBalance()); System.out.println(); // Test a Checking Account with overdraft protection
System.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 transactions
System.out.println("Deposit 150.00: " + account.deposit(150.00));
System.out.println("Withdraw 750.00: " + account.withdraw(750.00));
// Print out the final account balance
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName()
+ "] has a balance of " + account.getBalance()); }
}
运行结果:
Creating the customer Jane Smith.
Creating her Savings Account with a 500.00 balance and 3% interest.
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
【Java基础 项目实例--Bank项目5】Account 和 customer 对象等 继承、多态、方法的重写的更多相关文章
- 【Java基础 项目实例 -- Bank项目2】Account 和 customer 对象
总结: customer.setAccount(account); //引用,日后的account 和 customer.getAccount()的结果始终一致 实验目的 扩展银行项目,添加一个 Cu ...
- 【Java 基础实例—Bank 项目1】
(上图Wie任务要求的UML结构) Account.java 文件: package Banking_1; public class Account { private double balance; ...
- 为什么阿里巴巴Java开发手册中强制要求整型包装类对象值用 equals 方法比较?
在阅读<阿里巴巴Java开发手册>时,发现有一条关于整型包装类对象之间值比较的规约,具体内容如下: 这条建议非常值得大家关注, 而且该问题在 Java 面试中十分常见. 还需要思考以下几个 ...
- 【Java 基础实验_Bank项目_06】单例模式(Static Bank) , 查询异常输出
基于上一个实验Banking_5 ,代码先全部复制过来. 笔记心得: 1.SavingAccount() 需要两种构造方法,接受单个参数和两个的 2.Account 有两个类型 SavingAccou ...
- Java基础自学小项目
实现一个基于文本界面的<家庭记账软件> 需求:能够记录家庭的收入,支出,并能够收支明细表 主要涉及一下知识点: - 局部变量和基本数据类型 - 循环语句 - 分支语句 - 方法调用和返回值 ...
- Java基础学习笔记十 Java基础语法之final、static、匿名对象、内部类
final关键字 继承的出现提高了代码的复用性,并方便开发.但随之也有问题,有些类在描述完之后,不想被继承,或者有些类中的部分方法功能是固定的,不想让子类重写.可是当子类继承了这些特殊类之后,就可以对 ...
- JAVA基础知识之JVM-——使用反射生成并操作对象
Class对象可以获取类里的方法,由Method对象表示,调用Method的invoke可以执行对应的方法:可以获取构造器,由Constructor对象表示,调用Constructor对象的newIn ...
- JAVA基础复习与总结<九> 线程的基本概念_Thread继承创建线程
多线程 一.线程的概念 1.1 程序.进程.线程 程序:Program 是一个静态的概念 进程:Process 是一个动态的概念 进程是程序的一次动态执行过程,占用特定的地址空间. 每个进程都是独立的 ...
- java基础第六篇之常用思想、封装、继承和多态
a.累加求和思想:求1~100的和,求数组/集合中元素的和,求偶数的数,求总分 int sum=0//循环外部定义sum变量,循环里面对每个元素累加 for (; ; ) { //sum+=数据 } ...
随机推荐
- TCP/IP和OSI/RM以及协议端口
TCP/IP:数据链路层:ARP,RARP网络层: IP,ICMP,IGMP传输层:TCP ,UDP,UGP应用层:Telnet,FTP,SMTP,SNMP. OSI:物理层:EIA/TIA-232, ...
- FMZ发明者量化平台回测机制说明
原文连接:https://www.fmz.com/digest-topic/4009 大部分策略在实盘之前都需要回测进行验证,FMZ支持部分品种数字货币现货.期货和永续合约,以及商品期货所有品种.但发 ...
- VS开发】C中调用C++文件中定义的function函数
[VS开发]C中调用C++文件中定义的function函数 标签(空格分隔): [VS开发] 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 精要一揽 C调用 ...
- Django之session详解
好文章来自追风的小蚂蚁:https://www.cnblogs.com/zhuifeng-mayi/p/9099811.html cookie:Cookie,有时也用其复数形式 Cookies,指某些 ...
- centos7服务搭建常用服务配置之一:SSH
目录 1 SSH服务协议 1.1 ssh服务协议说明 1.2 ssh服务工作机制 1.3 ssh加密技术说明 1.3.1 ssh实现安全链接建立,利用要是和锁头 1.3.2 ssh加密算法 1.4 s ...
- kindeditor的配置jsp版
1.将kindeditor资源下载下来,点击这里下载: 2.将资源解压,因为是jsp版本所以只需要保留jsp的文件即可,最终目录为下图 3.在所给的jsp的demo中做配置 注意:demo.jsp中引 ...
- class CAdoInterface
AdoInterface.h #pragma once //#pragma warning (push) //以防会产生警告 C4018,但这个警告是没关系的,所以使用下一句代码屏蔽它 //#prag ...
- 火狐 , IE , 谷歌浏览器的 驱动下载地址汇总
一.Firefox和驱动下载地址 所有火狐浏览器版本下载地址:http://ftp.mozilla.org/pub/firefox/releases/ 所有火狐驱动geckodriver版本下载地址: ...
- opencv实现人脸识别(五) 运用tkinter进行GUI绘制 整合人脸识别模块
因为之前学习过tkinter库,所以在学习了人脸识别模块的编写后, 打算绘制一个简单的GUI来应用人脸识别功能. 主界面如下所示: 签到打开在点开后直接进行人脸识别,如果成功则自动关闭视频窗口. 录入 ...
- C语言快速判断素数——不超时
这属于算法上的问题,好好考虑一下算法,还要考虑一下素数的定义. 素数是只有1和本身能整除的整数.所以在求素数的时候,要将素数与1到素数本身中间的所有整数都相除,看是否有整除的数,如果有,那肯定不是素数 ...