【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+=数据 } ...
随机推荐
- 《剑指offer》树专题 (牛客10.25)
考察的知识点主要在于树的数据结构(BST,AVL).遍历方式(前序,中序,后序,层次).遍历算法(DFS,BFS,回溯)以及遍历时借助的数据结构如队列和栈.由于树本身就是一个递归定义的结构,所以在递归 ...
- 1. PHP基本语法规则
1.1 PHP标记: PHP语言,是一种可以嵌入到“html”代码中的后台处理语言(程序) 有以下几种标记形式,只推荐第一种. 1,<?php php代码写在这里..... ?> ...
- Linux 工作管理 (job control)
fg , bg 有时,命令需要很长的时间才能执行完成.对于这种情况,我们使用‘bg’命令可以将任务放在后台执行,而用‘fg’可以调到前台来使用. 我们可以通过‘&’在后台启动一个程序: fin ...
- 《C专家编程》读书笔记之第8~11章
八.为什么程序员无法分清万圣节和圣诞节 1. 整形提升是指char,short int和位段类型(无论signed或unsigned)以及枚举类型将被提升为int或unsigned int(如果int ...
- python搞搞大数据之hbase——初探
使用python链接mysql读入一个表并把它再写到hbase 里去(九头蛇万岁) 先声明一下需要用的库: 俩!!: happybase (写这个的老哥真的happy) pymysql 建议使用 ...
- AtCoder M-SOLUTIONS 2019 Task E. Product of Arithmetic Progression
problem link Official editorial: code: int main() { #if defined LOCAL && !defined DUIPAI ifs ...
- TypeScript的变量声明
1.全新的变量声明方式 let和const是JavaScript ES6中新添加的变量声明方式.let在很多方面与var是相似的,但是它可以避免一些在JavaScript里常见一些问题. 而const ...
- (十六)JDBC 处理大数据
目录 前言: 基本概念 对于Mysql的Text类型 流地址的写法 blob类型数据 备注 前言: 在实际开发中,程序需要把 大文本或二进制 数据保存到数据库中: 实际上,我们并不存储大的数据到数据库 ...
- resful规范: 进行数据交换时的代码潜规则
目前主流的三种web服务交互方案: REST (Representational State Transfer) 表征性状态转移 SOAP (Simple Object Access Protocol ...
- java 内部类、匿名内部类
一:内部类 1:什么是内部类? 大部分时候,类被定义成一个独立的程序单元.在某些情况下,也会把一个类放在另一个类的内部定义,这个定义在其他类内部的类就被称为内部类(有些地方也叫做嵌套类),包含内部类的 ...