Java基础实战_Bank项目_项目需求.txt

来源:互联网 发布:淘宝app在哪里 编辑:程序博客网 时间:2024/05/08 14:48
Java 基础实战—Bank 项目 


 实验题目 1: 
创建一个简单的银行程序包 
实验目的: 
Java 语言中面向对象的封装性及构造器的创建和使用。 
实验说明: 
在这个练习里,创建一个简单版本的 Account 类。将这个源文件放入 banking 程序包中。在创建单个帐户的默认程序包中,已编写了一个测试程序 TestBanking。这个测试程序初始化帐户余额,并可执行几种简单的事物处理。最后,该测试程 序显示该帐户的最终余额。 
提示: 
1.创建 banking 包 
2. 在 banking 包下创建 Account 类。该类必须实现上述 UML 框图中的模型。 
a. 声明一个私有对象属性:balance,这个属性保留了银行帐户的当前(或 即时)余额。 
b. 声明一个带有一个参数(init_balance)的公有构造器,这个参数为 balance 属性赋值。 
c. 声明一个公有方法 geBalance,该方法用于获取经常余额。 


d. 声明一个公有方法 deposit,该方法向当前余额增加金额。 


e. 声明一个公有方法 withdraw 从当前余额中减去金额。 


3.打开TestBanking.java文件,按提示完成编写,并编译 TestBanking.java 文件。 
4. 运行 TestBanking 类。可以看到下列输出结果: 
Creating an account with a 500.00 balance 
Withdraw 150.00 
Deposit 22.50 
Withdraw 47.62 
The account has a balance of 324.88 






//TestBanking.java 文件
/*
 * 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.
 */
package test;
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
    System.out.println("Withdraw 150.00");
    //code


    System.out.println("Deposit 22.50");
   
//code
    System.out.println("Withdraw 47.62");
    //code
    // Print out the final account balance
    System.out.println("The account has a balance of " + account.getBalance());
  }
}








Java 基础实战—Bank 项目 


 实验题目 2: 
扩展银行项目,添加一个 Customer 类。Customer 类将包含一个 Account对象。 
实验目的: 
使用引用类型的成员变量。 
提 示: 
1. 在banking包下的创建Customer类。该类必须实现上面的UML图表中的模型。 
a. 声明三个私有对象属性:firstName、lastName 和 account。 


b. 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(f 和 l) 


c. 声明两个公有存取器来访问该对象属性,方法 getFirstName 和 getLastName 返 


回相应的属性。 
d. 声明 setAccount 方法来对 account 属性赋值。 


e. 声明 getAccount 方法以获取 account 属性。 


2. 在 exercise2 主目录里,编译运行这个 TestBanking 程序。应该看到如下输出结果: 


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 




//TestBanking.java 文件
/*
 * 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
    System.out.println("Creating her account with a 500.00 balance.");
    //code
    System.out.println("Withdraw 150.00");
   
//code
    System.out.println("Deposit 22.50");
  //code
    System.out.println("Withdraw 47.62");
    //code
    // Print out the final account balance
    System.out.println("Customer [" + customer.getLastName()
      + ", " + customer.getFirstName()
      + "] has a balance of " + account.getBalance());
  }
}








Java 基础实战—Bank 项目 


 实验题目 3: 
修改 withdraw 方法以返回一个布尔值,指示交易是否成功。 
实验目的: 
使用有返回值的方法。 
提 示: 
1. 修改 Account 类 
a. 修改 deposit 方法返回 true(意味所有存款是成功的)。 


b. 修改 withdraw 方法来检查提款数目是否大于余额。如果amt小于 balance, 则从余额中扣除提款数目并返回 true,否则余额不变返回 false。 


2. 在 exercise3 主目录编译并运行 TestBanking 程序,将看到下列输出; 
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 




//TestBanking.java 文件
/*
 * 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 
    System.out.println("Creating her account with a 500.00 balance.");
    
//code
    // 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());
  }
}






Java 基础实战—Bank 项目 


 实验题目 4: 
将用数组实现银行与客户间的多重关系。 
实验目的: 
在类中使用数组作为模拟集合操作。 
提示: 
对银行来说,可添加 Bank 类。 Bank 对象跟踪自身与其客户间的关系。用 Customer 对象的数组实现这个集合化的关系。还要保持一个整数属性来跟踪银 行当前有多少客户。 
a. 创建 Bank 类 


b. 为 Bank 类 增 加 两 个 属 性 : customers(Customer对象的数组 ) 和 numberOfCustomers(整数,跟踪下一个 customers 数组索引) 


c. 添加公有构造器,以合适的最大尺寸(至少大于 5)初始化 customers 数组。 


d. 添加 addCustomer 方法。该方法必须依照参数(姓,名)构造一个新的 Customer 


对象然后把它放到 customer 数组中。还必须把 numberofCustomers 属性的值加 1。 
e. 添加 getNumOfCustomers 访问方法,它返回 numberofCustomers 属性值。 


f. 添加 getCustomer方法。它返回与给出的index参数相关的客户。 


g. 编译并运行 TestBanking 程序。可以看到下列输出结果: 


Customer [1] is Simms,Jane 
Customer [2] is Bryant,Owen 
Customer [3] is Soley,Tim 
Customer [4] is Soley,Maria 




//TestBanking.java 文件
/*
 * 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
    //Add Customer Owen, Bryant
//code
    // Add Customer Tim, Soley
//code
    // Add Customer Maria, Soley
//code
    for ( int i = 0; i < bank.getNumOfCustomers(); i++ ) {
      Customer customer = bank.getCustomer(i);


      System.out.println("Customer [" + (i+1) + "] is "
+ customer.getLastName()
+ ", " + customer.getFirstName());
    }
  }
}








Java 基础实战—Bank 项目 


 实验题目 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). 如果弥补该透支所需要的金额大于当前的保护级别。则整个交易失败,但余 额未受影响。 
6. 在主 exercise1 目录中,编译并执行 TestBanking 程序。输出应为: 
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




//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.*;


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


    System.out.println("Creating the customer Owen Bryant.");
    //code
    customer = bank.getCustomer(1);
    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);
    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
    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());


  }
}








 实验题目:5_续1
创建客户账户
实验目的:
instanceof 运算符的应用
提示:
修改Customer类
1. 修改Customer类来处理具有多种类型的联合账户。


(例如用数组表示多重性一节所作的,该类必须包括以下的公有方法:addAccount(Account),getAccount(int)和getNumOfAccounts()。每个Customer可以有多个Account。(声明至少有5个)
2. 完成TestBanking程序


该程序创建一个客户和账户的集合,并生成这些客户及其账户余额的报告。在TestBanking.Java文件中,你会发现注释块以/***…***/来开头和结尾。这些注释只是必须提供的代码的位置。
3. 使用instanceof操作符测试拥有的账户类型,并且将account_type设置为适当的值,例如:“SavingsAccount”或“CheckingAccount”。


4. 编译并运行该程序,将看到下列结果


CUSTOMERS REPORT
================
Customer: Simms, Jane
Savings Account: current balance is ¥500.00
Checking Account: current balance is ¥200.00
Customer: Bryant, Owen
Checking Account: current balance is ¥200.00
Customer: Soley, Tim
Savings Account: current balance is ¥1,500.00
Checking Account: current balance is ¥200.00
Customer: Soley, Maria
Checking Account: current balance is ¥200.00
Savings Account: current balance is ¥150.00




//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 banking.*;
import java.text.NumberFormat;


public class TestBanking {


  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".
***/


// 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.
***/
      }
    }
  }
}








 实验题目:5_续2 


 实现更为复杂的透支保护机制
注释-这是练习1的选择练习。它包括了更为复杂的透支保护机制模型。它使用客户储蓄。它使用客户储蓄账户完成透支保护。本练习必须在完成上述两个练习后进行。
实验目的:
继承、多态、方法的重写。
说明:
修改SavingAccount类
1.仿照练习1“Account类的两个子类”一节实现SavingsAccount类。
2.SavingAccount类必须扩展Account类。
3.该类必须包含一个类型为double的interestRate属性
4.该类必须包括一个带有两个参数(balance和interest_rate)的公有构造器。该构造器必须通过调用super(balance)来将balance参数传递给父类构造器
修改CheckingAccount类
1.仿照练习1“Account类的两个子类”一节实现CheckingAccount类。
2.CheckingAccount类必须扩展Account类
3.该类必须包括一个关联属性,称作protectedBy,表示透支保护。该属性的类型为SavingAccount,缺省值必须是null。除此之外该类没有其他的数据属性。
4.该类必须包括一个带有参数(balance)的公有构造器,该构造器必须通过调用super(balance)将balance参数传递到父类构造器。
5.修改构造器为CheckingAccount(double balance,SavingAccount protect)构造器。该构造器必须通过调用super(balance)将balance参数传递给父类构造器。
6. CheckingAccount类必须覆盖withdraw方法。该类必须执行下面的检查:如果当前余额足够弥补取款amount,则正常进行。如果不够弥补但是存在透支保护则尝试用储蓄账户来弥补这个差值(balance-amount)。如果后一个交易失败,则整个交易一定失败,但余额未受影响。
修改Customer类,使其拥有两个账户:一个储蓄账户和一个支票账户:两个都是可选的。
1.在练习5_续1修改,原来的Customer类包含一个称作account的关联属性,可用该属性控制一个Account对象。重写这个类,使其包含两个关联属性:
savingAccount和checkingAccount,这两个属性的缺省值是null
2.包含两个访问方法:getSaving和getChecking,这两个方法分别返回储蓄和支票总数。
3. 包含两个相反的方法:SetSaving和setChecking,这两个方法分别为savingAccount和checkingAccount这两个关联属性赋值。
完成TestBanking程序
Customer [Simms, Jane] has a checking balance of 200.0 and a savings balance of
500.0
Checking Acct [Jane Simms] : withdraw 150.00 succeeds? true
Checking Acct [Jane Simms] : deposit 22.50 succeeds? true
Checking Acct [Jane Simms] : withdraw 147.62 succeeds? true
Customer [Simms, Jane] has a checking balance of 0.0 and a savings balance of 424.88
Customer [Bryant, Owen] has a checking balance of 200.0
Checking Acct [Owen Bryant] : withdraw 100.00 succeeds? true
Checking Acct [Owen Bryant] : deposit 25.00 succeeds? true
Checking Acct [Owen Bryant] : withdraw 175.00 succeeds? false
Customer [Bryant, Owen] has a checking balance of 125.0




//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.*;


public class TestBanking {


  public static void main(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.setSavings(new SavingsAccount(500.00, 0.05));
    customer.setChecking(new CheckingAccount(200.00, customer.getSavings()));
    
bank.addCustomer("Owen", "Bryant");
    customer = bank.getCustomer(1);
    customer.setChecking(new CheckingAccount(200.00));


    // Test the checking account of Jane Simms (with overdraft protection)
    customer = bank.getCustomer(0);
    System.out.println("Customer [" + customer.getLastName()
      + ", " + customer.getFirstName() + "]"
      + " has a checking balance of "
      + customer.getChecking().getBalance()
      + " and a savings balance of "
      + customer.getSavings().getBalance());
    System.out.println("Checking Acct [Jane Simms] : withdraw 150.00 succeeds? "
      + account.withdraw(150.00));
    System.out.println("Checking Acct [Jane Simms] : deposit 22.50 succeeds? "
      + account.deposit(22.50));
    System.out.println("Checking Acct [Jane Simms] : withdraw 147.62 succeeds? "
      + account.withdraw(147.62));
    System.out.println("Customer [" + customer.getLastName()
      + ", " + customer.getFirstName() + "]"
      + " has a checking balance of "
      + account.getBalance()
      + " and a savings balance of "
      + customer.getSavings().getBalance());
    System.out.println();


    // Test the checking account of Owen Bryant (without overdraft protection)
    customer = bank.getCustomer(1);
    account = customer.getChecking();
    System.out.println("Customer [" + customer.getLastName()
      + ", " + customer.getFirstName() + "]"
      + " has a checking balance of "
      + account.getBalance());
    System.out.println("Checking Acct [Owen Bryant] : withdraw 100.00 succeeds? "
      + account.withdraw(100.00));
    System.out.println("Checking Acct [Owen Bryant] : deposit 25.00 succeeds? "
      + account.deposit(25.00));
    System.out.println("Checking Acct [Owen Bryant] : withdraw 175.00 succeeds? "
      + account.withdraw(175.00));
    System.out.println("Customer [" + customer.getLastName()
      + ", " + customer.getFirstName() + "]"
      + " has a checking balance of "
      + account.getBalance());
    System.out.println();
  }
}








Java 基础实战—Bank 项目 


 实验题目 6:(在5_续1的基础上修改) 
修改 Bank 类来实现单子设计模式: 
实验目的: 
单子模式。 
提示: 
1. 修改 Bank 类,创建名为 getBanking 的公有静态方法,它返回一个 Bank 类的实例。 
2. 单个的实例应是静态属性,且为私有。同样,Bank 构造器也应该是私有的 
创建 CustomerReport 类 
1.在前面的银行项目练习中,“客户报告”嵌入在 TestBanking 应用程序的 main 方法中。在这个练习中,改代码被放在,banking.reports 包的 CustomerReport 类中。您的任务是修改这个类,使其使用单一银行对象。 
2. 查找标注为注释块/*** ***/的代码行.修改该行以检索单子银行对象。 
编译并运行 TestBanking 应用程序 
看到下列输入结果: 
CUSTOMER REPORT 
======================= 
Customer:simms,jane 
Savings Account:current balance is $500.00 Checking Account:current balance is $200.00 
Customer:Bryant,owen 


Checking Account:current balance is $200.00 
Customer: Soley,Tim 
Savings Account:current balance is $1,500.00 
Checking Account:current balance is $200.00 
Customer:Soley ,Maria 
Checking Account:current balance is $200.00 
Savings Account:current balance is $150.00 




//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 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();
  }
}










Java 基础实战—Bank 项目 


 实验题目 7:(在6基础上修改) 
将建立一个 OverdraftException 异常,它由 Account 类的withdraw()方法抛出。 
实验目的: 
自定义异常 
实验说明: 
创建 OverdraftException 类 
1. 在 banking.domain 包中建立一个共有类 OverdraftException. 这个类扩展 Exception 类。 
2. 添加一个 double 类型的私有属性 deficit.增加一个共有访问方法 getDeficit 
3. 添加一个有两个参数的共有构造器。deficit 参数初始化 deficit 属性 
修改 Account 类 
4. 重写 withdraw 方法使它不返回值(即 void).声明方法抛出 overdraftException 
异常 
5. 修改代码抛出新异常,指明“资金不足”以及不足数额(当前余额扣除请求 
的数额) 
修改 CheckingAccount 类 
6. 重写 withdraw 方法使它不返回值(即 void).声明方法抛出 overdraftException 
异常 
7. 修改代码使其在需要时抛出异常。两种情况要处理:第一是存在没有透支保
护的赤字,对这个异常使用“no overdraft protection”信息。第二是 overdraftProtection 数额不足以弥补赤字:对这个异常可使用 ”Insufficient funds for overdraft protection” 信息 
编译并运行 TestBanking 程序 
Customer [simms,Jane]has a checking balance of 200.0 with a 500.0 overdraft protection 
Checking Acct[Jane Simms]: withdraw 150.00 
Checking Acct[Jane Simms]: deposit 22.50 
Checking Acct[Jane Simms]: withdraw 147.62 
Checking Acct[Jane Simms]: withdraw 470.00 
Exception: Insufficient funds for overdraft protection Deifcit:470.0 
Customer [Simms,Jane]has a checking balance of 0.0 
Customer [Bryant,Owen]has a checking balance of 200.0 
Checking Acct[Bryant,Owen]: withdraw 100.00 
Checking Acct[Bryant,Owen]: deposit25.00 
Checking Acct[Bryant,Owen]: withdraw 175.00 
Exception: no overdraft protection Deficit:50.0 
Customer [Bryant,Owen]has a checking balance of 125.0 




//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 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());
    }
  }
}










Java 基础实战—Bank 项目 


 实验题目 8: 
将替换这样的数组代码:这些数组代码用于实现银行和客户间,以及客户与他们 的帐户间的关系的多样性。 
实验目的: 
使用集合 
实验说明: 
修改 Bank 类 
修改 Bank 类,利用 ArrayList 实现多重的客户关系,不要忘记倒入必须的java.uti类 
1. 将 Customer 属性的声明修改为List 类型,不再使用 numberOfCustomers 属性。 
2. 修改 Bank 构造器,将 customers 属性的声明修改为List 类型,不再使用 numberOfcustomers 属性 
3. 修改 addCustomer 方法,使用 add 方法 
4. 修改 getCustomer 方法,使用 get 方法 
5. 修改 getNumofCustomer 方法,使用 size 方法 
修改 Customer 类 
6. 修改 Customer 类,使用 ArrayList 实现多重的账户关系。修改方法同上。 


编译运行 TestBanking 程序 
这里,不必修改 CustomerReport 代码,因为并没有改变 Bank 和 Customer 类的接口。
编译运行TestBanking 应看到下列输出结果: 
CUSTOMERS REPORT 
================== 
Customer:Simms,Jane 
Savings Account:current balance is
$500.00 Checking Account:current balance is $200.00 
Customer:Bryant,Owen 
Checking Accout:current balance is $200 
Customer:Soley,Tim
U7 
Savings Account:current balance is $1,500.00 
Checking Account:current balance is $200.00 
Customer:Soley,Tim 
Checking Account:current balance is $200.00 
Savings Account :current balance is $150.00 




可选:修改 CustomerReport 类 
修改 CustomerReport 类,使用 Iterator 实现对客户的迭代 
1. 在 Bank 类中,添加一个名为 getCustomers 的方法,该方法返回一个客户列 表上的 iterator 
2. 在 Customer 类中,添加一个名为个体 Accounts 的方法,该方法返回一个帐 户列表上的 iterator 
3. 修改 CustomerReport 类,使用一对嵌套的 while 循环(而不是使用嵌套的 for 循环),在客户的 iterator 与帐户的 iterator 上进行迭代 
4. 重新编译运行 TestBanking 程序,应看到与上面一样的输出结果 




//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 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();
  }
}

0 0
原创粉丝点击