延续

  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 对象等 继承、多态、方法的重写的更多相关文章

  1. 【Java基础 项目实例 -- Bank项目2】Account 和 customer 对象

    总结: customer.setAccount(account); //引用,日后的account 和 customer.getAccount()的结果始终一致 实验目的 扩展银行项目,添加一个 Cu ...

  2. 【Java 基础实例—Bank 项目1】

    (上图Wie任务要求的UML结构) Account.java 文件: package Banking_1; public class Account { private double balance; ...

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

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

  4. 【Java 基础实验_Bank项目_06】单例模式(Static Bank) , 查询异常输出

    基于上一个实验Banking_5 ,代码先全部复制过来. 笔记心得: 1.SavingAccount() 需要两种构造方法,接受单个参数和两个的 2.Account 有两个类型 SavingAccou ...

  5. Java基础自学小项目

    实现一个基于文本界面的<家庭记账软件> 需求:能够记录家庭的收入,支出,并能够收支明细表 主要涉及一下知识点: - 局部变量和基本数据类型 - 循环语句 - 分支语句 - 方法调用和返回值 ...

  6. Java基础学习笔记十 Java基础语法之final、static、匿名对象、内部类

    final关键字 继承的出现提高了代码的复用性,并方便开发.但随之也有问题,有些类在描述完之后,不想被继承,或者有些类中的部分方法功能是固定的,不想让子类重写.可是当子类继承了这些特殊类之后,就可以对 ...

  7. JAVA基础知识之JVM-——使用反射生成并操作对象

    Class对象可以获取类里的方法,由Method对象表示,调用Method的invoke可以执行对应的方法:可以获取构造器,由Constructor对象表示,调用Constructor对象的newIn ...

  8. JAVA基础复习与总结<九> 线程的基本概念_Thread继承创建线程

    多线程 一.线程的概念 1.1 程序.进程.线程 程序:Program 是一个静态的概念 进程:Process 是一个动态的概念 进程是程序的一次动态执行过程,占用特定的地址空间. 每个进程都是独立的 ...

  9. java基础第六篇之常用思想、封装、继承和多态

    a.累加求和思想:求1~100的和,求数组/集合中元素的和,求偶数的数,求总分 int sum=0//循环外部定义sum变量,循环里面对每个元素累加 for (; ; ) { //sum+=数据 } ...

随机推荐

  1. Mysql错误--Table 'mysql.servers' doesn't exist.

    问题: 今天在初始化数据库的时候,在配置文件里加了"--skip grant tables",登陆进去之后,发现无法修改root密码,报这个错误.   Table 'mysql.s ...

  2. Linux安装jemalloc笔记

    前言 最近研究一个工具库需要用 jemalloc 做内存分配器,但在 ubuntu 下安装过程中遇到很多问题,故记下安装过程的笔记,避免以后遇到在这上面浪费时间. 安装过程 环境:VMware Ubu ...

  3. 小程序部分机型上一个诡异的偶现bug

    如上图所示:开始的时候进到下单页面,价格是0,当选中了商品产生价格的时候,生成的价格如 ¥150,这个时候会只露出¥1以及一小半的5,后面的都被遮挡住了. wxml里是这样的写的 <view w ...

  4. Thinkphp自定义生成缩略图尺寸的方法

    Thinkphp自定义生成缩略图尺寸的方法,本实例中生成两张不同尺寸的图片:第一张是大图350*350,第二张 50*50的缩略图 Image类是Thinkphp系统自带的,可以研究下,这个缩略图类很 ...

  5. day04_XPATH提取数据

    1.XML简介 1.1.定义 ​ 可扩展标记语言(EXtensible Markup Language) 1.2.特点 一种标记语言,很类似 HTML XML 的标签需要我们自行定义 被设计为具有自我 ...

  6. 笔记-4:python组合数据类型

    1.字符串(str) 字符串是字符的序列表示, 根据字符串的内容多少分为单行字符串和多行字符串. 单行字符串可以由一对单引号(') 或双引号(")作为边界来表示, 单引号和双引号作用相同. ...

  7. fiddler笔记:Composer选项卡

    1.Composer选项卡介绍 Composer选项卡功能是可以手动构建和发送HTTP.HTTPS和FTP请求. 支持将Web Session列表中选中的Session拖入Composer选项卡,然后 ...

  8. 以太坊再爆高危漏洞!黑客增发ATN 1100万枚token事件始末

    事情发生在5月中旬,ATN技术人员发现Token合约由于存在漏洞受到攻击.不过ATN基金会随后透露,将销毁1100万个ATN,并恢复ATN总量,同时将在主链上线映射时对黑客地址内的资产予以剔除,确保原 ...

  9. Linux 创建用户 用户组 用户权限

    首先 你要有个root账号 然后才能做下面几条操作: useradd username 创建用户usernamepasswd user_pwd     给已创建的用户username设置密码 关于us ...

  10. BaseHandler的封装, 处理handler中的内存泄漏

    package de.bvb.study.common; /** * 用于规范 Message.what此属性,避免出现魔法数字 */ public final class What { public ...