【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+=数据 } ...
随机推荐
- VS.vs15
1.20190615 安装的 vs2015(cn_visual_studio_enterprise_2015_with_update_3_x86_x64_dvd_8923298.iso) 的目录为: ...
- css常见双栏和三栏布局
左侧固定右侧自适应 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- oracle中表记录被另一个用户锁住
应用场景 在查询oracle表时,提示表记录被另一个用户锁住了 有可能是在使用了pl/sql工具后修改某个表记录时,加锁,或者发生故障异常退出,下次登录进去修改不了 查询锁 --查看数据库的锁的来源. ...
- 字典的setdefault()
setdefault(key, default) 函数 ---有key获取值.没key设置 key:default dict.setdefault(key, default=None) 如果 key ...
- Java 私有构造函数的使用
被private修饰的私有构造函数无法在其他类中调用,也就是该类无法在其他类中实例化. 这种情况常用的使用场景:1.单例模式: 2.防止实例化. 一.单例模式 单例模式是一种常用的设计模式,思想是单例 ...
- vue采坑之——vue里面渲染html 并添加样式
在工作中,有次遇到要把返回的字符串分割成两部分,一部分用另外的样式显示. 这时候,我想通过对得到字符串进行处理,在需要特别样式的字符串片段用html标签(用的span)包裹起来再通过变量绑定就好了.不 ...
- Nodejs:单线程为什么能支持高并发?
1.Nodejs是一个平台,构建在chrome的V8上(js语言解释器),采用事件驱动.非阻塞模型( c++库:libuv). 参考官方: Node.js is a platform built ...
- Inno Setup CreateProcess 失败:代码 740(Inno Setup打包的程序提升为管理员权限)
原文参考 https://www.cnblogs.com/SnailProgramer/p/4243666.html http://blog.csdn.net/x356982611/article/d ...
- sqlserver查询数据的所有表名和行数及空间占用量
//查询所有表名 select name from sysobjects where xtype='u' --modify_date指表结构最后更新日期,并非数据最后更新日期SELECT na ...
- git diff 的简单使用(比较版本区别)
假如我们修改viewMail.vue 文件(部分代码) 从 //根据ID获取详情 getById () { let that = this; this.viewMailModal = true; th ...