java bank項目_第1頁
java bank項目_第2頁
java bank項目_第3頁
java bank項目_第4頁
java bank項目_第5頁
已閱讀5頁,還剩25頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、Java 基礎實戰Bank 項目  實驗題目 1: 創建一個簡單的銀行程序包 實驗目的: Java 語言中面向對象的封裝性及構造器的創建和使用。 實驗說明: 在這個練習里,創建一個簡單版本的 Account 類。將這個源文件放入 banking 程序包中。在創建單個帳戶的默認程序包中,已編寫了一個測試程序 TestBanking。這個測試程序初始化帳戶余額,并可執行幾種簡單的事物處理。最后,該測試程 序顯示該帳戶的最終余額。 提示: 1創建 banking 包 2 在 banking 包下創建

2、Account 類。該類必須實現上述 UML 框圖中的模型。 a. 聲明一個私有對象屬性:balance,這個屬性保留了銀行帳戶的當前(或 即時)余額。 b. 聲明一個帶有一個參數(init_balance)的公有構造器,這個參數為 balance 屬性賦值。 c. 聲明一個公有方法 geBalance,該方法用于獲取經常余額。 d. 聲明一個公有方法 deposit,該方法向當前余額增加金額。 e. 聲明一個公有方法 withdraw 從當前余額中減去金額。 3打開TestBanking.java文件,按提示完成編寫,并編譯 Tes

3、tBanking.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.

4、0;* 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;    /

5、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"

6、);   /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: 擴展銀

7、行項目,添加一個 Customer 類。Customer 類將包含一個 Account對象。 實驗目的: 使用引用類型的成員變量。 提 示: 1. 在banking包下的創建Customer類。該類必須實現上面的UML圖表中的模型。 a. 聲明三個私有對象屬性:firstName、lastName 和 account。 b. 聲明一個公有構造器,這個構造器帶有兩個代表對象屬性的參數(f 和 l) c. 聲明兩個公有存取器來訪問該對象屬性,方法 getFirstName 和 getLastName 返 回相應的屬性。

8、 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 Custom

9、er 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

10、.*;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.");    /c

11、ode    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.printl

12、n("Withdraw 47.62");    /code    / Print out the final account balance    System.out.println("Customer " + customer.getLastName()      + ", " + customer.getFirstName()      + " has a balance of " + ac

13、count.getBalance();  Java 基礎實戰Bank 項目  實驗題目 3: 修改 withdraw 方法以返回一個布爾值,指示交易是否成功。 實驗目的: 使用有返回值的方法。 提 示: 1 修改 Account 類 a. 修改 deposit 方法返回 true(意味所有存款是成功的)。 b. 修改 withdraw 方法來檢查提款數目是否大于余額。如果amt小于 balance, 則從余額中扣除提款數目并返回 true,否則余額不變返回 false。 2 在 exe

14、rcise3 主目錄編譯并運行 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 文件/*

15、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. */import banking.*;public class TestBanking   public static void main(String args

16、)     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 b

17、alance.");    /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(&qu

18、ot;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()      + "

19、;, " + customer.getFirstName()      + " has a balance of " + account.getBalance();  Java 基礎實戰Bank 項目  實驗題目 4: 將用數組實現銀行與客戶間的多重關系。 實驗目的: 在類中使用數組作為模擬集合操作。 提示: 對銀行來說,可添加 Bank 類。 Bank 對象跟蹤自身與其客戶間的關系。用 Customer 對象的數組實現這個集合化的關系。還要保持一個整數屬

20、性來跟蹤銀 行當前有多少客戶。 a. 創建 Bank 類 b. 為 Bank 類 增 加 兩 個 屬 性 : customers(Customer對象的數組 ) 和 numberOfCustomers(整數,跟蹤下一個 customers 數組索引) c. 添加公有構造器,以合適的最大尺寸(至少大于 5)初始化 customers 數組。 d. 添加 addCustomer 方法。該方法必須依照參數(姓,名)構造一個新的 Customer 對象然后把它放到 customer 數組中。還必須把 numberofCustomers 屬性的值加 1。

21、 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 文件/*

22、60;* 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 arg

23、s)     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+

24、 )       Customer customer = bank.getCustomer(i);      System.out.println("Customer " + (i+1) + " is "+ customer.getLastName()+ ", " + customer.getFirstName();      Java 基礎實戰Bank 項目  實驗題目 5: 在銀行項目中創建 Account 的兩

25、個子類:SavingAccount 和 CheckingAccount 實驗目的: 繼承、多態、方法的重寫。 提 示: 創建 Account 類的兩個子類:SavingAccount 和 CheckingAccount 子類 a. 修改 Account 類;將 balance 屬性的訪問方式改為 protected b. 創建 SavingAccount 類,該類繼承 Account 類 c. 該類必須包含一個類型為 double 的 interestRate 屬性 d. 該類必須包括帶有兩個參數(balance

26、和 interest_rate)的公有構造器。該構 造器必須通過調用 super(balance)將 balance 參數傳遞給父類構造器。 實現 CheckingAccount 類 1 CheckingAccount 類必須擴展 Account 類 2 該類必須包含一個類型為 double 的 overdraftProtection 屬性。 3 該類必須包含一個帶有參數(balance)的共有構造器。該構造器必須通過調 用 super(balance)將 balance 參數傳遞給父類構造器。 4 給類必須包括另一個帶有兩個參數(balanc

27、e 和 protect)的公有構造器。該 構造器必須通過調用 super(balance)并設置 overdragtProtection 屬性,將 balance 參數傳遞給父類構造器。 5 CheckingAccount 類必須覆蓋 withdraw 方法。此方法必須執行下列檢查。如 果當前余額足夠彌補取款 amount,則正常進行。如果不夠彌補但是存在透支 保護,則嘗試用 overdraftProtection 得值來彌補該差值(balance-amount). 如果彌補該透支所需要的金額大于當前的保護級別。則整個交易失敗,但余 額未受影響。 6 在主 exercise

28、1 目錄中,編譯并執行 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 Che

29、cking 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: trueDeposit 22.50: trueWithdraw 47.62: trueWithdraw 400.00: fal

30、seCustomer Simms, Jane has a balance of 324.88Retrieving the customer Owen Bryant with his checking account with no overdraft protection.Withdraw 150.00: trueDeposit 22.50: trueWithdraw 47.62: trueWithdraw 400.00: falseCustomer Bryant, Owen has a balance of 324.88Retrieving the customer Tim Soley wi

31、th his checking account that has overdraft protection.Withdraw 150.00: trueDeposit 22.50: trueWithdraw 47.62: trueWithdraw 400.00: trueCustomer Soley, Tim has a balance of 0.0Retrieving the customer Maria Soley with her joint checking account with husband Tim.Deposit 150.00: trueWithdraw 750.00: fal

32、seCustomer 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.*;

33、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

34、("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

35、("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("Cr

36、eating 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.");    /c

37、ode    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

38、(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.");

39、60;   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.de

40、posit(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 " + c

41、ustomer.getLastName()      + ", " + customer.getFirstName()      + " has a balance of " + account.getBalance();    System.out.println();    / Test a Checking Account w/o overdraft protection    System.out.println("R

42、etrieving 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: "

43、 + 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);  &

44、#160; / Print out the final account balance    System.out.println("Customer " + customer.getLastName()      + ", " + customer.getFirstName()      + " has a balance of " + account.getBalance();    System.out.println();

45、60;   / 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();  

46、60; / 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.6

47、2);    System.out.println("Withdraw 400.00: " + account.withdraw(400.00);    / Print out the final account balance    System.out.println("Customer " + customer.getLastName()      + ", " + customer.getFirstName()   

48、  + " 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."); 

49、;   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.with

50、draw(750.00);    / Print out the final account balance    System.out.println("Customer " + customer.getLastName()      + ", " + customer.getFirstName()      + " has a balance of " + account.getBalance();   實驗題目

51、:5_續1創建客戶賬戶實驗目的:instanceof 運算符的應用提示:修改Customer類1 修改Customer類來處理具有多種類型的聯合賬戶。(例如用數組表示多重性一節所作的,該類必須包括以下的公有方法:addAccount(Account),getAccount(int)和getNumOfAccounts()。每個Customer可以有多個Account。(聲明至少有5個)2 完成TestBanking程序該程序創建一個客戶和賬戶的集合,并生成這些客戶及其賬戶余額的報告。在TestBanking.Java文件中,你會發現注釋塊以/*/來開頭和結尾。這些注釋只是必須提供的代碼的位置。3

52、 使用instanceof操作符測試擁有的賬戶類型,并且將account_type設置為適當的值,例如:“SavingsAccount”或“CheckingAccount”。4 編譯并運行該程序,將看到下列結果CUSTOMERS REPORT=Customer: Simms, JaneSavings Account: current balance is ¥500.00Checking Account: current balance is ¥200.00Customer: Bryant, OwenChecking Account: current balance is ¥200.00Cust

53、omer: Soley, TimSavings Account: current balance is ¥1,500.00Checking Account: current balance is ¥200.00Customer: Soley, MariaChecking Account: current balance is ¥200.00Savings Account: current balance is ¥150.00/TestBanking程序/* * This class creates the program to test the banking classes.

54、60;* 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 = NumberForma

55、t.getCurrencyInstance();    Bank     bank = new Bank();    Customer customer;    / Create several customers and their accounts    bank.addCustomer("Jane", "Simms");    customer = bank.getCustomer(0);    custome

56、r.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);  &#

57、160; 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")

58、;    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.pr

59、intln("tttCUSTOMERS REPORT");    System.out.println("ttt=");    for ( int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx+ )       customer = bank.getCustomer(cust_idx);      System.out.println();     

60、; 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 = ""/

61、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 acco

62、unt and the balance.* Feel free to use the currency_format formatter* to generate a "currency string" for the balance.*/             實驗題目:5_續2  實現更為復雜的透支保護機制注釋-這是練習1的選擇練習。它包括了更為復雜的透支保護機制模型。它使用客戶儲蓄。它使用客戶儲蓄賬戶完成透支保護。本練習必須在完成上述兩個練習后進行。實驗目的:繼承、多態、方法的

63、重寫。說明:修改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類必須擴展A

64、ccount類3.該類必須包括一個關聯屬性,稱作protectedBy,表示透支保護。該屬性的類型為SavingAccount,缺省值必須是null。除此之外該類沒有其他的數據屬性。4.該類必須包括一個帶有參數(balance)的公有構造器,該構造器必須通過調用super(balance)將balance參數傳遞到父類構造器。5.修改構造器為CheckingAccount(double balance,SavingAccount protect)構造器。該構造器必須通過調用super(balance)將balance參數傳遞給父類構造器。6. CheckingAccount類必須覆蓋withd

65、raw方法。該類必須執行下面的檢查:如果當前余額足夠彌補取款amount,則正常進行。如果不夠彌補但是存在透支保護則嘗試用儲蓄賬戶來彌補這個差值(balance-amount)。如果后一個交易失敗,則整個交易一定失敗,但余額未受影響。修改Customer類,使其擁有兩個賬戶:一個儲蓄賬戶和一個支票賬戶:兩個都是可選的。1.在練習5_續1修改,原來的Customer類包含一個稱作account的關聯屬性,可用該屬性控制一個Account對象。重寫這個類,使其包含兩個關聯屬性:savingAccount和checkingAccount,這兩個屬性的缺省值是null2.包含兩個訪問方法:getSav

66、ing和getChecking,這兩個方法分別返回儲蓄和支票總數。3. 包含兩個相反的方法:SetSaving和setChecking,這兩個方法分別為savingAccount和checkingAccount這兩個關聯屬性賦值。完成TestBanking程序Customer Simms, Jane has a checking balance of 200.0 and a savings balance of500.0Checking Acct Jane Simms : withdraw 150.00 succeeds? trueChecking Acct Jane Simms : deposit 22.

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論