// Create an account that can has a 500.00 balance. account = new Account(500.00); System.out.println("Creating an account with a 500.00 balance."); // code account.withdraw(150.00); System.out.println("Withdraw 150.00");
Creating the customer Jane Smith. Creating her account with a 500.00 balance. Withdraw 150.00 Deposit 22.50 Withdraw 47.62 Customer [Smith, Jane] has a balance of 324.88
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 Banking.Account; import Banking.Customer;
// 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 account with a 500.00 balance. Withdraw 150.00: true Deposit 22.50: true Withdraw 47.62: true Withdraw 400.00: false Customer [Smith, Jane] has a balance of 324.88
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 Banking.Account; import Banking.Customer;
// Create an account that can has a 500.00 balance. account = new Account(500.00); System.out.println("Creating the customer Jane Smith."); customer = new Customer("Jane","Smith");
System.out.println("Creating her account with a 500.00 balance."); customer.setAccount(account); account = customer.getAccount();
// Print out the final account balance System.out.println("Customer [" + customer.getLastName() + ", " + customer.getFirstName() + "] has a balance of " + account.getBalance()); } }
实验四
题目
将用数组实现银行与客户间的多重关系。
目的
在类中使用数组作为模拟集合操作。
提示
对银行来说,可添加 Bank 类。 Bank 对象跟踪自身与其客户间的关系。用 Customer 对象的数组实现这个集合化的关系。还要保持一个整数属性来跟踪银行当前有多少客户。
创建 Bank 类
为 Bank 类增加两个属性 : customers(Customer对象的数组)和 numberOfCustomers(整数,跟踪下一个 customers 数组索引)
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 Banking.*;
publicclassTestBanking04{
publicstaticvoidmain(String[] args){ Bank bank = new Bank();
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
/* * 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. */ publicclassTestBanking05{ publicstaticvoidmain(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");
System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest."); account = new SavingAccount(500.00, 0.03); customer = bank.getCustomer(0); customer.setAccount(account);
System.out.println("Creating the customer Owen Bryant."); bank.addCustomer("Owen", "Bryant"); customer = bank.getCustomer(1); System.out.println("Creating his Checking Account with a 500.00 balance and no overdraft protection."); account = new CheckingAccount(500.00, 0); customer.setAccount(account);
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."); account = new CheckingAccount(500.00, 500.00); customer.setAccount(account);
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());
publicclassTestBanking05_1{ publicstaticvoidmain(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("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);
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())); } } } }
/* * 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_2.*;
publicclassTestBanking05_2{
publicstaticvoidmain(String[] args){ Bank bank = new Bank(); Customer customer; Account account;
// Create two customers and their accounts bank.addCustomer("Jane", "Simms"); customer = bank.getCustomer(0); // account = customer.getChecking(); customer.setSaving(new SavingAccount(500.00, 0.05)); customer.setChecking(new CheckingAccount(200.00, customer.getSaving()));
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())); } } } }
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 Banking6.*;
publicclassTestBanking06{
publicstaticvoidmain(String[] args){ Bank bank = Bank.getBanking(); Customer customer; CustomerReport report = new CustomerReport();
// 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("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));
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 Banking7.*;
publicclassTestBanking07{
publicstaticvoidmain(String[] args){ Bank bank = Bank.getBanking(); Customer customer; Account account;
// Create two 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, 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();
/* * 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 Banking8.*;
publicclassTestBanking08{
publicstaticvoidmain(String[] args){ Bank bank = Bank.getBanking(); Customer customer; CustomerReport report = new CustomerReport();
// 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("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 report.generateReport(); } }
可选:修改 CustomerReport 类
修改 CustomerReport 类,使用 Iterator 实现对客户的迭代
在 Bank 类中,添加一个名为 getCustomers 的方法,该方法返回一个客户列表上的 iterator