延续

  Java基础 项目实例--Bank项目4

实验要求

 1 实验题目 5:
 2  在银行项目中创建 Account 的两个子类:SavingAccount 和 CheckingAccount
 3
 4
 5 实验目的:
 6  继承、多态、方法的重写。
 7  提 示:
 8  创建 Account 类的两个子类:SavingAccount 和 CheckingAccount 子类
 9
10 a. 修改 Account 类;将 balance 属性的访问方式改为 protected
11
12 b. 创建 SavingAccount 类,该类继承 Account 类
13
14 c. 该类必须包含一个类型为 double 的 interestRate 属性
15
16 d. 该类必须包括带有两个参数(balance 和 interest_rate)的公有构造器。该 构 造器必须通过调用 super(balance)将 balance 参数传递给父类构造 器。
17
18 实现 CheckingAccount 类
19
20 1. CheckingAccount 类必须扩展 Account 类
21
22 2. 该类必须包含一个类型为 double 的 overdraftProtection 属性。
23
24 3. 该类必须包含一个带有参数(balance)的共有构造器。该构造器必须通过调 用 super(balance)将 balance 参数传递给父类构造器。
25
26 4. 给类必须包括另一个带有两个参数(balance 和 protect)的公有构造器。该 构造器必须通过调用 super(balance)并设置 overdragtProtection 属性, 将 balance 参数传递给父类构造器。
27
28 5. CheckingAccount 类必须覆盖 withdraw 方法。此方法必须执行下列检 查。如 果当前余额足够弥补取款 amount,则正常进行。如果不够弥补但是 存在透支 保护,则尝试用 overdraftProtection 得值来弥补该差值 (balance-amount). 如果弥补该透支所需要的金额大于当前的保护级别。 则整个交易失败,但余 额未受影响。 

View Code

编程后的笔记

  

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;}}
}

View Code

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];}
}

View Code

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;}
}

View Code

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;}
}

View Code

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;}
}

View Code

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 accountSystem.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.");//codebank.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.");//codeSystem.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 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());}
}

View Code


运行结果:

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.88Retrieving 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.88Retrieving 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.0Retrieving 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

View Code

转载于:https://www.cnblogs.com/zhazhaacmer/p/9774033.html

【Java基础 项目实例--Bank项目5】Account 和 customer 对象等 继承、多态、方法的重写...相关推荐

  1. Java基础实战Bank项目01-04

    Bank01: 创建一个简单的银行程序包 实验目的: Java 语言中面向对象的封装性及构造器的创建和使用. 实验说明: 在这个练习里,创建一个简单版本的 Account 类.将这个源文件放入 ban ...

  2. 尚硅谷 java基础第二个项目之客户关系管理系统

    尚硅谷 java基础第二个项目之客户关系管理系统. 做了一些完善,增加性别,电话,邮箱有效性验证.其中电话和邮箱验证直接"饮用"了网友的果汁. 在此感谢各位原著大佬们的分享. 具体 ...

  3. java写一个外网访问的接口_【JAVA基础】一个案例搞懂类、对象、重载、封装、继承、多态、覆盖、抽象和接口概念及区别(中篇)...

    0 前言 初学JAVA时,总会对一些概念一知半解,相互混淆,不明其设计的用意,如类.对象.重载.封装.继承.多态.覆盖.抽象类.接口概念.为便于理解和巩固,本文将基于一个案例及其变形,展现各个概念的定 ...

  4. char怎么比较_为什么阿里巴巴Java开发手册中强制要求整型包装类对象值用 equals 方法比较?...

    在阅读<阿里巴巴Java开发手册>时,发现有一条关于整型包装类对象之间值比较的规约,具体内容如下: 这条建议非常值得大家关注, 而且该问题在 Java 面试中十分常见. 还需要思考以下几个 ...

  5. 父子继承与方法的重写 java 114818255

    父子继承与方法的重写 java 114818255 定义了一个父类 子类继承父类 测试类 方法的覆盖 父类的私有属性 super 局部变量,实例变量,父类的实例变量

  6. Solidity基础教程:合约的继承与方法的重写

    Solidity基础教程:合约的继承与方法的重写 合约继承 合约继承使用is关键字 contract ERC721 is Context, ERC165, IERC721, IERC721Metada ...

  7. 适合有一些Java基础的实战项目

    有些同学学了很久的Java,记了许多知识,但一用的时候发现脑子里一片空白,这是为什么呢,归根到底就是项目练得少,没有实战经验.为了避免这种现象,学姐今天给大家分享一个Java初级游戏项目--大鱼吃小鱼 ...

  8. 小白自学笔记——JAVA基础 2.12 项目一 家庭记账软件

    需求说明 模拟实现基于文本界面的<家庭记账软件>. 该软件能够记录家庭的收入.支出,并能够打印收支明细表. 假设家庭起始的生活基本金为10000元. 每次登记收入(菜单2)后,收入的金额应 ...

  9. Java基础------第一个项目

    黑马信息教育管理系统 一.项目分布 Controller:客服层:1.用于接收用户传递过来的数据信息2.用于将接收到的数据传递给业务员进行数据逻辑操作3.将业务员处理的逻辑结果展示给用户 Servic ...

  10. Java基础语法之变量、运算符、流程控制、数组和方法等基础语法

    变量.运算符.流程控制.数组和方法等基础语法.Java程序的执行流程,符合Java语法规则的程序. 1.1.1 Java初识 对Java进行简单介绍,Java程序如何执行,以及Java程序的结构. J ...

最新文章

  1. javascript 二级动态下拉菜单选项
  2. 经典C语言程序100例之九五
  3. 使用css制作三角,兼容IE6,用到的标签divsspan
  4. linux路由信息预览为空,route - 显示并设置Linux中静态路由表
  5. 用ajax替换html代码,替换Ajax响应一个div的内部HTML(Replace inner HTML of a div w
  6. IDA python 脚本编程使用参考资料链接
  7. 如何在Linux上安装设备驱动程序
  8. OAuth 授权timestamp refused问题
  9. 大数据处理方面的 7 个开源搜索引擎
  10. 华为NP课程笔记24-BFD
  11. Java学习资料--网盘分享
  12. 计算机硬件关系密切,与计算机硬件关系最密切的软件是.
  13. SPSS一元线性回归
  14. 考试用计算机反思800字,期中考试反思800字(5篇)
  15. android 高德地图线路规划,路线规划-Android平台-开发指南-高德地图车机版 | 高德地图API...
  16. python——加解密hashlib/hmac/random/secrets/base64/pycrypto
  17. java抠图人物背景图片_人物抠图换背景两种实用方法!
  18. XSS注入进阶练习篇(三) XSS原型链污染
  19. 超级计算机控制人的电影,想知道这部电影的名字,讲述通过超级计算机进入人脑.回到过去.其中有个老教授死亡留下一封信给给主角。...
  20. a5 1c语言实现,A5算法的C语言实现

热门文章

  1. skype 无法连接
  2. css子元素和后代元素选择器
  3. leetcode-412 -Fizz Buzz-(fizz bzz)-java
  4. 关于小米笔记本Pro内部风扇异响问题解决方法
  5. [数字媒体] PR视频剪辑之竖屏实现横屏旋转切换和大视频文件缩小
  6. 计算机管理能看到移动硬盘,无法识别移动硬盘并且不显示磁盘图标.
  7. 关于Linux运行steam的解决方案
  8. AWD简单介绍和搭建AWD平台
  9. PPT转换成图片及合成长图
  10. JAVA基础算法(6)----- 国际象棋 α 皇后问题