package banking;
public class Account {
private double balance; //银行帐户的当前(或即时)余额
//公有构造器 ,这个参数为 balance 属性赋值
public Account(double init_balance) {
this.balance = init_balance;
}
//用于获取经常余额
public double getBalance() {
return balance;
}
//向当前余额增加金额
public void deposit(double amt){
balance+=amt;
}
//从当前余额中减去金额
public void withdraw(double amt){
balance-=amt;
}
}
package banking;
import banking.*;
public class TestBanking {
public static void main(String[] args) {
Account account;
// Create an account that can has a 500.00 balance.
System.out.println("Creating an account with a 500.00 balance.");
//code
account = new Account(500.00);
System.out.println("Withdraw 150.00");
//code
account.withdraw(150.00);
System.out.println("Deposit 22.50");
//code
account.deposit(22.50);
System.out.println("Withdraw 47.62");
//code
account.withdraw(47.62);
// Print out the final account balance
System.out.println("The account has a balance of " + account.getBalance());
}
}
package banking;
public class Account {
private double balance; //银行帐户的当前(或即时)余额
//公有构造器 ,这个参数为 balance 属性赋值
public Account(double init_balance) {
this.balance = init_balance;
}
//用于获取经常余额
public double getBalance() {
return balance;
}
//向当前余额增加金额
public void deposit(double amt){
balance+=amt;
}
//从当前余额中减去金额
public void withdraw(double amt){
balance-=amt;
}
}
package banking;
public class Customer {
private String firstName;
private String lastName;
private Account account;
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 acct) {
this.account = acct;
}
}
package banking;/*
* 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 banking.*;
public class TestBanking {
public static void main(String[] args) {
Customer customer;
Account account;
// Create an account that can has a 500.00 balance.
System.out.println("Creating the customer Jane Smith.");
//code
customer = new Customer("Jane","Smith");
System.out.println("Creating her account with a 500.00 balance.");
//code
account = new Account(500.00);
customer.setAccount(account);
System.out.println("Withdraw 150.00");
//code
customer.getAccount().withdraw(150.00);
System.out.println("Deposit 22.50");
//code
customer.getAccount().deposit(22.50);
System.out.println("Withdraw 47.62");
//code
customer.getAccount().withdraw(47.62);
// Print out the final account balance
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName()
+ "] has a balance of " + account.getBalance());
}
}
package banking;
public class Account {
private double balance; //银行帐户的当前(或即时)余额
//公有构造器 ,这个参数为 balance 属性赋值
public Account(double init_balance) {
this.balance = init_balance;
}
//用于获取经常余额
public double getBalance() {
return balance;
}
/**
* 向当前余额增加金额
* @param amt 增加金额
* @return 返回 true(意味所有存款是成功的)
*/
public boolean deposit(double amt){
balance+=amt;
return true;
}
/**
* 从当前余额中减去金额
* @param amt 提款数目
* @return 如果 amt小于 balance, 则从余额中扣除提款数目并返回 true,否则余额不变返回 false。
*/
public boolean withdraw(double amt){
if (amt < balance){
balance-=amt;
return true;
}else{
return false;
}
}
}
package banking;
public class Customer {
private String firstName;
private String lastName;
private Account account;
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 acct) {
this.account = acct;
}
}
package banking;/*
* 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 banking.*;
public class TestBanking {
public static void main(String[] args) {
Customer customer;
Account account;
// Create an account that can has a 500.00 balance.
System.out.println("Creating the customer Jane Smith.");
//code
customer = new Customer("Jane","Smith");
System.out.println("Creating her account with a 500.00 balance.");
//code
account = new Account(500.00);
customer.setAccount(account);
// 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());
}
}
package banking;
public class Account {
private double balance; //银行帐户的当前(或即时)余额
//公有构造器 ,这个参数为 balance 属性赋值
public Account(double init_balance) {
this.balance = init_balance;
}
//用于获取经常余额
public double getBalance() {
return balance;
}
/**
* 向当前余额增加金额
* @param amt 增加金额
* @return 返回 true(意味所有存款是成功的)
*/
public boolean deposit(double amt){
balance+=amt;
return true;
}
/**
* 从当前余额中减去金额
* @param amt 提款数目
* @return 如果 amt小于 balance, 则从余额中扣除提款数目并返回 true,否则余额不变返回 false。
*/
public boolean withdraw(double amt){
if (amt < balance){
balance-=amt;
return true;
}else{
return false;
}
}
}
package banking;
public class Customer {
private String firstName;
private String lastName;
private Account account;
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 acct) {
this.account = acct;
}
}
package banking;
/**
* 银行类
*/
public class Bank {
private Customer[] customers; //Customer对象的数组
private int numberOfCustomer; //整数,跟踪下一个 customers 数组索引
/**
* 公有构造器,以合适的最大尺寸(至少大于 5)初始化 customers 数组。
*/
public Bank() {
customers = new Customer[10];
}
/**
* 该方法必须依照参数(姓,名)构造一个新的 Customer 对象然后把它放到 customer 数组中。还必须把 numberOfCustomers 属性的值加 1。
* @param f 姓
* @param l 名
*/
public void addCustomer(String f,String l){
customers[numberOfCustomer++]=new Customer(f,l);
}
/**
* 通过下标索引获取 customer
* @param index 下标索引
* @return customer
*/
public Customer getCustomer(int index) {
return customers[index];
}
public int getNumOfCustomers() {
return numberOfCustomer;
}
}
package banking;/*
* 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 banking.*;
public class TestBanking {
public static void main(String[] args) {
Bank bank = new Bank();
// Add Customer Jane, Simms
//code
bank.addCustomer("Jane","Simms");
//Add Customer Owen, Bryant
//code
bank.addCustomer("Owen","Bryant");
// Add Customer Tim, Soley
//code
bank.addCustomer("Tim","Soley");
// Add Customer Maria, Soley
//code
bank.addCustomer("Maria","Soley");
for (int i = 0; i < bank.getNumOfCustomers(); i++) {
Customer customer = bank.getCustomer(i);
System.out.println("Customer [" + (i + 1) + "] is "
+ customer.getLastName()
+ ", " + customer.getFirstName());
}
}
}
package banking;
public class Account {
protected double balance; //银行帐户的当前(或即时)余额
//公有构造器 ,这个参数为 balance 属性赋值
public Account(double init_balance) {
this.balance = init_balance;
}
//用于获取经常余额
public double getBalance() {
return balance;
}
/**
* 向当前余额增加金额
* @param amt 增加金额
* @return 返回 true(意味所有存款是成功的)
*/
public boolean deposit(double amt){
balance+=amt;
return true;
}
/**
* 从当前余额中减去金额
* @param amt 提款数目
* @return 如果 amt小于 balance, 则从余额中扣除提款数目并返回 true,否则余额不变返回 false。
*/
public boolean withdraw(double amt){
if (amt < balance){
balance-=amt;
return true;
}else{
return false;
}
}
}
package banking;
public class Customer {
private String firstName;
private String lastName;
private Account account;
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 acct) {
this.account = acct;
}
}
package banking;
/**
* 银行类
*/
public class Bank {
private Customer[] customers; //Customer对象的数组
private int numberOfCustomer; //整数,跟踪下一个 customers 数组索引
/**
* 公有构造器,以合适的最大尺寸(至少大于 5)初始化 customers 数组。
*/
public Bank() {
customers = new Customer[10];
}
/**
* 该方法必须依照参数(姓,名)构造一个新的 Customer 对象然后把它放到 customer 数组中。还必须把 numberOfCustomers 属性的值加 1。
* @param f 姓
* @param l 名
*/
public void addCustomer(String f,String l){
customers[numberOfCustomer++]=new Customer(f,l);
}
/**
* 通过下标索引获取 customer
* @param index 下标索引
* @return customer
*/
public Customer getCustomer(int index) {
return customers[index];
}
public int getNumOfCustomers() {
return numberOfCustomer;
}
}
package banking;
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;
}
/**
* 此方法必须执行下列检查。如 果当前余额足够弥补取款 amount,则正常进行。
* 如果不够弥补但是存在透支保护,则尝试用 overdraftProtection 得值来弥补该差值(balance-amount).
* 如果弥补该透支所需要的金额大于当前的保护级别。则整个交易失败,但余额未受影响。
* @param amt 提款数目
* @return 返回true代表交易成功,否则交易失败
*/
@Override
public boolean withdraw(double amt){
if (balance < amt){
if (amt-balance>overdraftProtection) {
return false;
}else {
overdraftProtection-=amt-balance;
balance=0;
return true;
}
}else {
balance-=amt;
return true;
}
}
}
package banking;
public class SavingsAccount extends Account{
private double interestRate;
public SavingsAccount(double balance, double interest_Rate) {
super(balance);
this.interestRate = interest_Rate;
}
}
package banking;/*
* 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 banking.*;
public class TestBanking {
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
System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest.");
//code
bank.getCustomer(0).setAccount(new SavingsAccount(500.00,0.03));
System.out.println("Creating the customer Owen Bryant.");
//code
bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
System.out.println("Creating his Checking Account with a 500.00 balance and no overdraft protection.");
//code
customer.setAccount(new CheckingAccount(500.00,0));
System.out.println("Creating the customer Tim Soley.");
bank.addCustomer("Tim", "Soley");
customer = bank.getCustomer(2);
System.out.println("Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection.");
//code
customer.setAccount(new CheckingAccount(500.00,500.00));
System.out.println("Creating the customer Maria Soley.");
//code
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());
}
}
package banking;
public class Account {
protected double balance; //银行帐户的当前(或即时)余额
//公有构造器 ,这个参数为 balance 属性赋值
public Account(double init_balance) {
this.balance = init_balance;
}
//用于获取经常余额
public double getBalance() {
return balance;
}
/**
* 向当前余额增加金额
* @param amt 增加金额
* @return 返回 true(意味所有存款是成功的)
*/
public boolean deposit(double amt){
balance+=amt;
return true;
}
/**
* 从当前余额中减去金额
* @param amt 提款数目
* @return 如果 amt小于 balance, 则从余额中扣除提款数目并返回 true,否则余额不变返回 false。
*/
public boolean withdraw(double amt){
if (amt < balance){
balance-=amt;
return true;
}else{
return false;
}
}
}
package banking;
public class Customer {
private String firstName;
private String lastName;
private Account[] accounts = new Account[10];
private int numOfAccounts;
public Customer(String f, String l) {
this.firstName = f;
this.lastName = l;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
/**
* 通过下标索引获取 account
* @param index 下标索引
* @return account
*/
public Account getAccount(int index) {
return accounts[index];
}
/**
* 获取 numOfAccounts
* @return numOfAccounts
*/
public int getNumOfAccounts() {
return numOfAccounts;
}
/**
* 添加 account,并将 numOfAccounts+1
* @param acct account
*/
public void addAccount(Account acct) {
accounts[numOfAccounts++]=acct;
}
}
package banking;
/**
* 银行类
*/
public class Bank {
private Customer[] customers; //Customer对象的数组
private int numberOfCustomer; //整数,跟踪下一个 customers 数组索引
private static Bank bankInstance = null;
/**
* getBanking 的公有静态方法,它返回一个 Bank 类的实例。
* @return 返回一个 Bank 类的实例。
*/
public static Bank getBank(){
if(bankInstance==null){
bankInstance = new Bank();
}
return bankInstance;
}
/**
* 单例模式中构造器也应该是私有的,以合适的最大尺寸(至少大于 5)初始化 customers 数组。
*/
private Bank() {
customers = new Customer[10];
}
/**
* 该方法必须依照参数(姓,名)构造一个新的 Customer 对象然后把它放到 customer 数组中。还必须把 numberOfCustomers 属性的值加 1。
* @param f 姓
* @param l 名
*/
public void addCustomer(String f,String l){
customers[numberOfCustomer++]=new Customer(f,l);
}
/**
* 通过下标索引获取 customer
* @param index 下标索引
* @return customer
*/
public Customer getCustomer(int index) {
return customers[index];
}
public int getNumOfCustomers() {
return numberOfCustomer;
}
}
package banking;
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;
}
/**
* 此方法必须执行下列检查。如 果当前余额足够弥补取款 amount,则正常进行。
* 如果不够弥补但是存在透支保护,则尝试用 overdraftProtection 得值来弥补该差值(balance-amount).
* 如果弥补该透支所需要的金额大于当前的保护级别。则整个交易失败,但余额未受影响。
* @param amt 提款数目
* @return 返回true代表交易成功,否则交易失败
*/
@Override
public boolean withdraw(double amt){
if (balance < amt){
if (amt-balance>overdraftProtection) {
return false;
}else {
overdraftProtection-=amt-balance;
balance=0;
return true;
}
}else {
balance-=amt;
return true;
}
}
}
package banking;
public class SavingsAccount extends Account{
private double interestRate;
public SavingsAccount(double balance, double interest_Rate) {
super(balance);
this.interestRate = interest_Rate;
}
}
package banking.reports;
import banking.Account;
import banking.Bank;
import banking.Customer;
import banking.SavingsAccount;
public class CustomerReport {
/**
* 生成客户报告,Generate a report
*/
public static void generalReport() {
Bank bank = Bank.getBank();
System.out.println("\t\t\tCUSTOMERS REPORT");
System.out.println("\t\t\t================");
for (int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++) {
Customer customer = bank.getCustomer(cust_idx);
System.out.println();
System.out.println("Customer: "
+ customer.getLastName() + ", "
+ customer.getFirstName());
for (int acct_idx = 0; acct_idx < customer.getNumOfAccounts(); acct_idx++) {
Account account = customer.getAccount(acct_idx);
String account_type = "";
// Determine the account type
/*** Step 1:
**** Use the instanceof operator to test what type of account
**** we have and set account_type to an appropriate value, such
**** as "Savings Account" or "Checking Account".
***/
if (account instanceof SavingsAccount) {
account_type = "Savings Account";
} else {
account_type = "Checking Account";
}
// Print the current balance of the account
/*** Step 2:
**** Print out the type of account and the balance.
**** Feel free to use the currency_format formatter
**** to generate a "currency string" for the balance.
***/
System.out.println("\t" + account_type + ": current balance is ¥" + account.getBalance());
}
}
}
}
package banking;/*
* This class creates the program to test the banking classes.
* It creates a set of customers, with a few accounts each,
* and generates a report of current account balances.
*/
import banking.reports.CustomerReport;
import java.text.NumberFormat;
public class TestBanking {
public static void main(String[] args) {
NumberFormat currency_format = NumberFormat.getCurrencyInstance();
Bank bank = Bank.getBank();
Customer customer;
// Create several customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
customer.addAccount(new SavingsAccount(500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00, 400.00));
bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
customer.addAccount(new CheckingAccount(200.00));
bank.addCustomer("Tim", "Soley");
customer = bank.getCustomer(2);
customer.addAccount(new SavingsAccount(1500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00));
bank.addCustomer("Maria", "Soley");
customer = bank.getCustomer(3);
// Maria and Tim have a shared checking account
customer.addAccount(bank.getCustomer(2).getAccount(1));
customer.addAccount(new SavingsAccount(150.00, 0.05));
CustomerReport.generalReport();
}
}
package banking;
import banking.domain.OverdraftException;
public class Account {
protected double balance; //银行帐户的当前(或即时)余额
//公有构造器 ,这个参数为 balance 属性赋值
public Account(double init_balance) {
this.balance = init_balance;
}
//用于获取经常余额
public double getBalance() {
return balance;
}
/**
* 向当前余额增加金额
* @param amt 增加金额
* @return 返回 true(意味所有存款是成功的)
*/
public boolean deposit(double amt){
balance+=amt;
return true;
}
/**
* 从当前余额中减去金额
* @param amt 提款数目
* @return 如果 amt小于 balance, 则从余额中扣除提款数目并返回 true,否则余额不变返回 false。
*/
public void withdraw(double amt) throws OverdraftException {
if (amt < balance){
balance-=amt;
}else{
throw new OverdraftException("资金不足",balance-amt);
}
}
}
package banking;
public class Customer {
private String firstName;
private String lastName;
private Account[] accounts = new Account[10];
private int numOfAccounts;
public Customer(String f, String l) {
this.firstName = f;
this.lastName = l;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
/**
* 通过下标索引获取 account
* @param index 下标索引
* @return account
*/
public Account getAccount(int index) {
return accounts[index];
}
/**
* 获取 numOfAccounts
* @return numOfAccounts
*/
public int getNumOfAccounts() {
return numOfAccounts;
}
/**
* 添加 account,并将 numOfAccounts+1
* @param acct account
*/
public void addAccount(Account acct) {
accounts[numOfAccounts++]=acct;
}
}
package banking;
/**
* 银行类
*/
public class Bank {
private Customer[] customers; //Customer对象的数组
private int numberOfCustomer; //整数,跟踪下一个 customers 数组索引
private static Bank bankInstance = null;
/**
* getBanking 的公有静态方法,它返回一个 Bank 类的实例。
* @return 返回一个 Bank 类的实例。
*/
public static Bank getBank(){
if(bankInstance==null){
bankInstance = new Bank();
}
return bankInstance;
}
/**
* 单例模式中构造器也应该是私有的,以合适的最大尺寸(至少大于 5)初始化 customers 数组。
*/
private Bank() {
customers = new Customer[10];
}
/**
* 该方法必须依照参数(姓,名)构造一个新的 Customer 对象然后把它放到 customer 数组中。还必须把 numberOfCustomers 属性的值加 1。
* @param f 姓
* @param l 名
*/
public void addCustomer(String f,String l){
customers[numberOfCustomer++]=new Customer(f,l);
}
/**
* 通过下标索引获取 customer
* @param index 下标索引
* @return customer
*/
public Customer getCustomer(int index) {
return customers[index];
}
public int getNumOfCustomers() {
return numberOfCustomer;
}
}
package banking;
import banking.domain.OverdraftException;
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;
}
/**
* 此方法必须执行下列检查。如 果当前余额足够弥补取款 amount,则正常进行。
* 如果不够弥补但是存在透支保护,则尝试用 overdraftProtection 得值来弥补该差值(balance-amount).
* 如果弥补该透支所需要的金额大于当前的保护级别。则整个交易失败,但余额未受影响。
* @param amt 提款数目
* @return 返回true代表交易成功,否则交易失败
*/
@Override
public void withdraw(double amt) throws OverdraftException {
if (balance < amt){
if(overdraftProtection==0){
throw new OverdraftException("no overdraft protection",amt-balance);
}
if (amt-balance>overdraftProtection) {
throw new OverdraftException("Insufficient funds for overdraft protection",amt);
}else {
overdraftProtection-=amt-balance;
balance=0;
}
}else {
balance-=amt;
}
}
}
package banking;
public class SavingsAccount extends Account{
private double interestRate;
public SavingsAccount(double balance, double interest_Rate) {
super(balance);
this.interestRate = interest_Rate;
}
}
package banking.reports;
import banking.Account;
import banking.Bank;
import banking.Customer;
import banking.SavingsAccount;
public class CustomerReport {
/**
* 客户报告,Generate a report
*/
public static void generalReport(){
Bank bank = Bank.getBank();
System.out.println("\t\t\tCUSTOMERS REPORT");
System.out.println("\t\t\t================");
for (int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++) {
Customer customer = bank.getCustomer(cust_idx);
System.out.println();
System.out.println("Customer: "
+ customer.getLastName() + ", "
+ customer.getFirstName());
for (int acct_idx = 0; acct_idx < customer.getNumOfAccounts(); acct_idx++) {
Account account = customer.getAccount(acct_idx);
String account_type = "";
// Determine the account type
/*** Step 1:
**** Use the instanceof operator to test what type of account
**** we have and set account_type to an appropriate value, such
**** as "Savings Account" or "Checking Account".
***/
if (account instanceof SavingsAccount) {
account_type = "Savings Account";
} else {
account_type = "Checking Account";
}
// Print the current balance of the account
/*** Step 2:
**** Print out the type of account and the balance.
**** Feel free to use the currency_format formatter
**** to generate a "currency string" for the balance.
***/
System.out.println("\t"+account_type + ": current balance is ¥" + account.getBalance());
}
}
}
}
package banking.domain;
/**
* 自定义异常类
*/
public class OverdraftException extends Exception{
private double deficit;
public OverdraftException(String message, double deficit) {
super(message);
this.deficit = deficit;
}
public double getDeficit() {
return deficit;
}
}
package banking;/*
* This class creates the program to test the banking classes.
* It creates a set of customers, with a few accounts each,
* and generates a report of current account balances.
*/
import banking.domain.*;
public class TestBanking {
public static void main(String[] args) {
Bank bank = Bank.getBank();
Customer customer;
Account account;
// Create two customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
customer.addAccount(new SavingsAccount(500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00, 500.00));
bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
customer.addAccount(new CheckingAccount(200.00));
// Test the checking account of Jane Simms (with overdraft protection)
customer = bank.getCustomer(0);
account = customer.getAccount(1);
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a checking balance of "
+ account.getBalance()
+ " with a 500.00 overdraft protection.");
try {
System.out.println("Checking Acct [Jane Simms] : withdraw 150.00");
account.withdraw(150.00);
System.out.println("Checking Acct [Jane Simms] : deposit 22.50");
account.deposit(22.50);
System.out.println("Checking Acct [Jane Simms] : withdraw 147.62");
account.withdraw(147.62);
System.out.println("Checking Acct [Jane Simms] : withdraw 470.00");
account.withdraw(470.00);
} catch (OverdraftException e1) {
System.out.println("Exception: " + e1.getMessage()
+ " Deficit: " + e1.getDeficit());
} finally {
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a checking balance of "
+ account.getBalance());
}
System.out.println();
// Test the checking account of Owen Bryant (without overdraft protection)
customer = bank.getCustomer(1);
account = customer.getAccount(0);
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a checking balance of "
+ account.getBalance());
try {
System.out.println("Checking Acct [Owen Bryant] : withdraw 100.00");
account.withdraw(100.00);
System.out.println("Checking Acct [Owen Bryant] : deposit 25.00");
account.deposit(25.00);
System.out.println("Checking Acct [Owen Bryant] : withdraw 175.00");
account.withdraw(175.00);
} catch (OverdraftException e1) {
System.out.println("Exception: " + e1.getMessage()
+ " Deficit: " + e1.getDeficit());
} finally {
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a checking balance of "
+ account.getBalance());
}
}
}
package banking;
import banking.domain.OverdraftException;
public class Account {
//银行帐户的当前(或即时)余额
protected double balance;
//公有构造器 ,这个参数为 balance 属性赋值
public Account(double init_balance) {
this.balance = init_balance;
}
//用于获取经常余额
public double getBalance() {
return balance;
}
/**
* 向当前余额增加金额
* @param amt 增加金额
* @return 返回 true(意味所有存款是成功的)
*/
public boolean deposit(double amt){
balance+=amt;
return true;
}
/**
* 从当前余额中减去金额,抛出"资金不足"异常
* @param amt 提款数目
*/
public void withdraw(double amt) throws OverdraftException {
if (amt < balance){
balance-=amt;
}else{
throw new OverdraftException("资金不足",balance-amt);
}
}
}
package banking;
import java.util.ArrayList;
import java.util.List;
public class Customer {
private String firstName;
private String lastName;
private List<Account> accounts = new ArrayList<>();
public Customer(String f, String l) {
this.firstName = f;
this.lastName = l;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
/**
* 通过下标索引获取 account
* @param index 下标索引
* @return account
*/
public Account getAccount(int index) {
return accounts.get(index);
}
/**
* 返回账户大小
* @return 返回账户大小
*/
public int getNumOfAccounts() {
return accounts.size();
}
/**
* 添加 account
* @param acct account
*/
public void addAccount(Account acct) {
accounts.add(acct);
}
}
package banking;
import java.util.ArrayList;
import java.util.List;
/**
* 银行类
*/
public class Bank {
private List<Customer> customers; //Customer对象集合
private static Bank bankInstance = null;
/**
* getBanking 的公有静态方法,它返回一个 Bank 类的实例。
* @return 返回一个 Bank 类的实例。
*/
public static Bank getBank(){
if(bankInstance==null){
bankInstance = new Bank();
}
return bankInstance;
}
/**
* 单例模式中构造器也应该是私有的,初始化 customers 集合。
*/
private Bank() {
customers = new ArrayList<>();
}
/**
* 该方法必须依照参数(姓,名)构造一个新的 Customer 对象然后把它放到 customer 集合中。
* @param f 姓
* @param l 名
*/
public void addCustomer(String f,String l){
customers.add(new Customer(f,l));
}
/**
* 通过下标索引获取 customer
* @param index 下标索引
* @return customer
*/
public Customer getCustomer(int index) {
return customers.get(index);
}
/**
* 返回用户列表的大小
* @return 用户列表的大小
*/
public int getNumOfCustomers() {
return customers.size();
}
}
package banking;
import banking.domain.OverdraftException;
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;
}
/**
* 此方法必须执行下列检查。如 果当前余额足够弥补取款 amount,则正常进行。
* 如果不够弥补但是存在透支保护,则尝试用 overdraftProtection 得值来弥补该差值(balance-amount).
* 如果弥补该透支所需要的金额大于当前的保护级别。则整个交易失败,但余额未受影响。
* @param amt 提款数目
* @return 返回true代表交易成功,否则交易失败
*/
@Override
public void withdraw(double amt) throws OverdraftException {
if (balance < amt){
if(overdraftProtection==0){
throw new OverdraftException("no overdraft protection",amt-balance);
}
if (amt-balance>overdraftProtection) {
throw new OverdraftException("Insufficient funds for overdraft protection",amt);
}else {
overdraftProtection-=amt-balance;
balance=0;
}
}else {
balance-=amt;
}
}
}
package banking;
public class SavingsAccount extends Account{
private double interestRate;
public SavingsAccount(double balance, double interest_Rate) {
super(balance);
this.interestRate = interest_Rate;
}
}
package banking.reports;
import banking.Account;
import banking.Bank;
import banking.Customer;
import banking.SavingsAccount;
public class CustomerReport {
/**
* 客户报告,Generate a report
*/
public void generateReport(){
Bank bank = Bank.getBank();
System.out.println("\t\t\tCUSTOMERS REPORT");
System.out.println("\t\t\t================");
for (int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++) {
Customer customer = bank.getCustomer(cust_idx);
System.out.println();
System.out.println("Customer: "
+ customer.getLastName() + ", "
+ customer.getFirstName());
for (int acct_idx = 0; acct_idx < customer.getNumOfAccounts(); acct_idx++) {
Account account = customer.getAccount(acct_idx);
String account_type = "";
// Determine the account type
/*** Step 1:
**** Use the instanceof operator to test what type of account
**** we have and set account_type to an appropriate value, such
**** as "Savings Account" or "Checking Account".
***/
if (account instanceof SavingsAccount) {
account_type = "Savings Account";
} else {
account_type = "Checking Account";
}
// Print the current balance of the account
/*** Step 2:
**** Print out the type of account and the balance.
**** Feel free to use the currency_format formatter
**** to generate a "currency string" for the balance.
***/
System.out.println("\t"+account_type + ": current balance is ¥" + account.getBalance());
}
}
}
}
package banking.domain;
/**
* 自定义异常类
*/
public class OverdraftException extends Exception{
private double deficit;
public OverdraftException(String message, double deficit) {
super(message);
this.deficit = deficit;
}
public double getDeficit() {
return deficit;
}
}
package banking;/*
* This class creates the program to test the banking classes.
* It creates a set of customers, with a few accounts each,
* and generates a report of current account balances.
*/
import banking.domain.*;
import banking.reports.CustomerReport;
public class TestBanking {
public static void main(String[] args) {
Bank bank = Bank.getBank();
Customer customer;
CustomerReport report = new CustomerReport();
// Create several customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
customer.addAccount(new SavingsAccount(500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00, 400.00));
bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
customer.addAccount(new CheckingAccount(200.00));
bank.addCustomer("Tim", "Soley");
customer = bank.getCustomer(2);
customer.addAccount(new SavingsAccount(1500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00));
bank.addCustomer("Maria", "Soley");
customer = bank.getCustomer(3);
// Maria and Tim have a shared checking account
customer.addAccount(bank.getCustomer(2).getAccount(1));
customer.addAccount(new SavingsAccount(150.00, 0.05));
// Generate a report
report.generateReport();
}
}