Account:

package banking5;
//账户 public class Account {
protected double balance; public Account(double int_balance) {
balance = int_balance;
} public double getBlance() {
return balance;
} public boolean deposit(double amt) {
balance += amt;
return true;
} public boolean withdraw(double amt) {
if (balance >= amt) {
balance -= amt;
return true;
} else {
System.out.println("余额不足");
return false;
}
}
}

Customer:

package banking5;

public class Customer {
private String firstName;
private String lastName;
private Account account; public Customer(String f, String l) {
firstName = f;
lastName = l;
} public String getFirstName() {
return firstName;
} public String getLastName() {
return lastName;
} public Account getAccount() {
return account;
} public void setAccount(Account acct) {
account = acct;
}
}

Bank:

package banking5;

public class Bank {

    private Customer[] customers;
private int numberOfCustomers; public Bank() {
customers = new Customer[5];
} public void addCustomer(String f, String l) {
Customer cust = new Customer(f, l);
customers[numberOfCustomers] = cust;
numberOfCustomers++;
} public int getNumberofCustomer() {
return numberOfCustomers;
} public Customer getCustomer(int index) {
return customers[index];
}
}

CheckingAccount:

package banking5;

//信用卡账户
public class CheckingAccount extends Account {
private double overdraftProtection;// 透支额度 public CheckingAccount(double balance) {
super(balance);
} public CheckingAccount(double balance, double protect) {
super(balance);
this.overdraftProtection = protect;
} public double getOverdraftProtection() {
return overdraftProtection;
} public void setOverdraftProtection(double overdraftProtection) {
this.overdraftProtection = overdraftProtection;
} public boolean withdraw(double amt) {
if (balance >= amt) {
balance -= amt;
return true;
} else if (overdraftProtection >= amt - balance) { overdraftProtection -= (amt - balance);
balance = 0;
return true;
} else {
System.out.println("额度不够");
return false;
}
}
}

SavingAccount:

package banking5;

public class SavingAccount extends Account {
private double interestRate;// 利率 public SavingAccount(double balance, double init_rate) {
super(balance);
this.interestRate = init_rate;
} public double getInterestRate() {
return interestRate;
} public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
} }

TestBanking5:

package TestBanking;
/*
* This class creates the program to test the banking classes.
* It creates a new Bank, sets the Customer (with an initial balance),
* and performs a series of transactions with the Account object.
*/ import banking5.Account;
import banking5.Bank;
import banking5.CheckingAccount;
import banking5.Customer;
import banking5.SavingAccount; public class TestBanking5 { public static void main(String[] args) {
Bank bank = new Bank();
Customer customer;
Account account; // Create bank customers and their accounts
// System.out.println("Creating the customer Jane Smith.");
bank.addCustomer("Jane", "Simms");
// code
account = new SavingAccount(500.00, 0.03);
customer = bank.getCustomer(0);
customer.setAccount(account); System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest.");
// code
System.out.println("Creating the customer Owen Bryant.");
// code
bank.addCustomer("Owner", "Bryant");
customer = bank.getCustomer(1);
account = new CheckingAccount(500.00);
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.00, 500.00);
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.");
// code
bank.addCustomer("Maria", "Soley");
customer = bank.getCustomer(3);
// account =bank.getCustomer(2).getAccount(); 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.getBlance()); 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.getBlance()); 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.getBlance()); 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.getBlance());
}
} 输出结果:

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, Owner] 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

 

最新文章

  1. 华清远见成为ARM大学计划正式合作伙伴
  2. NE Upgrade python script. Need to write a Tkinter GUI for it
  3. 使用 JavaScript File API 实现文件上传
  4. Visual Studio 2013开启JavaScript的智能提示功能
  5. python练习程序(c100经典例15)
  6. 实体框架 (EF) 入门 => 二、在全新的数据库中使用 Code First
  7. NAND闪存颗粒结构及工作原理
  8. HDFS深度历险 之 从客户端逻辑看HDFS写入机制
  9. 移动端目标识别(1)——使用TensorFlow Lite将tensorflow模型部署到移动端(ssd)之TensorFlow Lite简介
  10. Telnet Protocol Specification
  11. django --- DetailView源码分析
  12. 虚拟货币ICO是什么意思 看完秒懂
  13. react-native启动页面设置,react-native-splash-screen
  14. PAT B1040 有几个PAT (25 分)
  15. Hadoop与Spark之间的比较
  16. CorelDRAW中如何复制对象属性详解
  17. WPF ListView ListBox 常用的样式记录
  18. JS框架设计之对象扩展一种子模块
  19. linux和Android的Makefile和android.mk
  20. quic协议实践

热门文章

  1. Centos7.x 装机优化
  2. mysql的group by
  3. Linux下swap到底有没有必要使用
  4. POJ2686(状压)
  5. LateX公式表
  6. 线段树 区间加 gcd 差分 小阳的贝壳
  7. 201771030125-王英雪 实验一 软件工程准备一<构建之法与博客首秀>
  8. 【matlab 基础篇 03】一文带你全面了解 plot 绘图函数的使用(超详细+图文并茂)
  9. 爬虫系列 一次采集.NET WebForm网站的坎坷历程
  10. 设计模式之GOF23原型模式01