执行顺序由上至下
package banking5;
public class Account {
protected double balance;
public Account(double balance) {
this.balance = balance;
}
public double getBalance() {
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;
}
}
package banking5;
public class Bank {
private Customer[] customers; // 用于存放客户
private int numberOfCustomers;// 记录客户的个数
public Bank() {
customers = new Customer[5];
}
// 添加一个Customer到数组中
public void addCustomer(String f, String l) {
customers[numberOfCustomers] = new Customer(f, l);
numberOfCustomers++;
}
// 获取Customer的个数
public int getNumOfCustomers() {
return numberOfCustomers;
}
// 返回指定索引位置的Customer
public Customer getCustomer(int index) {
return customers[index];
}
}
package banking5;
public class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
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 {
return false;
}
return true;
}
}
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;
}
}
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, 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
bank.addCustomer("Owen", "Bryant");
System.out.println("Creating the customer Owen Bryant.");
// code
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);
// customer.setAccount(account);
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());
}
}
instanceof 练习
package banking5_1;
public class Customer {
private String firstName;
private String lastName;
// private Account account;
private Account accounts[];
private int numberOfAccounts;// 记录Account 的个数
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
accounts = new Account[5];
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
// 添加一个账户到Account[]中
public void addAccount(Account acct) {
accounts[numberOfAccounts] = acct;
numberOfAccounts++;
}
// 返回账户的个数
public int getNumOfAccounts() {
return numberOfAccounts;
}
// 返回指定索引处的账户
public Account getAccount(int index) {
return accounts[index];
}
}
package testbanking;
/*
* 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 java.text.NumberFormat;
import banking5_1.Account;
import banking5_1.Bank;
import banking5_1.CheckingAccount;
import banking5_1.Customer;
import banking5_1.SavingAccount;
public class TestBanking5_1 {
public static void main(String[] args) {
NumberFormat currency_format = NumberFormat.getCurrencyInstance();
Bank bank = new Bank();
Customer customer;
// Create several customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
customer.addAccount(new SavingAccount(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 SavingAccount(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 SavingAccount(150.00, 0.05));
// Generate a report
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 = 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 SavingAccount) {
account_type = "SavingAccount";
}
if (account instanceof CheckingAccount) {
account_type = "CheckingAccount";
}
// 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(account_type + "current balance is "
+ currency_format.format(account.getBalance()));
}
}
}
}
复习: