




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
1、Java 基礎實戰(zhàn)Bank 項目實驗題目 1:創(chuàng)建一個簡單的銀行程序包實驗目的:Java 語言中面向?qū)ο蟮姆庋b性及構(gòu)造器的創(chuàng)建和使用。實驗說明:在這個練習里,創(chuàng)建一個簡單版本的 Account 類。將這個源文件放入 banking 程序包中。在創(chuàng)建單個帳戶的默認程序包中,已編寫了一個測試程序 TestBanking。這個測試程序初始化帳戶余額,并可執(zhí)行幾種簡單的事物處理。最后,該測試程 序顯示該帳戶的最終余額。提示:1創(chuàng)建 banking 包2 在 banking 包下創(chuàng)建 Account 類。該類必須實現(xiàn)上述 UML 框圖中的模型。a. 聲明一個私有對象屬性:balance,這個屬性保留了銀
2、行帳戶的當前(或 即時)余額。b. 聲明一個帶有一個參數(shù)(init_balance)的公有構(gòu)造器,這個參數(shù)為 balance 屬性賦值。c. 聲明一個公有方法 geBalance,該方法用于獲取經(jīng)常余額。d. 聲明一個公有方法 deposit,該方法向當前余額增加金額。e. 聲明一個公有方法 withdraw 從當前余額中減去金額。3打開TestBanking.java文件,按提示完成編寫,并編譯 TestBanking.java 文件。4 運行 TestBanking 類??梢钥吹较铝休敵鼋Y(jié)果:Creating an account with a 500.00 balanceWithdraw
3、 150.00Deposit 22.50Withdraw 47.62The 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;i
4、mport 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
5、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 基礎實戰(zhàn)Bank 項目實驗題目 2:擴展銀行項目,添加一個 Customer 類。Customer 類將包含一個 Account對象。實驗目的:使用引用類型的成員變量。提 示:1. 在banking包下的創(chuàng)建Customer類。該類必須實現(xiàn)上面的UML圖表中的模型。a. 聲明三個私有對
6、象屬性:firstName、lastName 和 account。b. 聲明一個公有構(gòu)造器,這個構(gòu)造器帶有兩個代表對象屬性的參數(shù)(f 和 l)c. 聲明兩個公有存取器來訪問該對象屬性,方法 getFirstName 和 getLastName 返回相應的屬性。d. 聲明 setAccount 方法來對 account 屬性賦值。e. 聲明 getAccount 方法以獲取 account 屬性。2. 在 exercise2 主目錄里,編譯運行這個 TestBanking 程序。應該看到如下輸出結(jié)果:Creating the customer Jane Smith.Creating her ac
7、count with a 500.00 balance.Withdraw 150.00Deposit 22.50Withdraw 47.62Customer 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 transact
8、ions 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 wi
9、th 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 + acco
10、unt.getBalance(); Java 基礎實戰(zhàn)Bank 項目實驗題目 3:修改 withdraw 方法以返回一個布爾值,指示交易是否成功。實驗目的:使用有返回值的方法。提 示:1 修改 Account 類a. 修改 deposit 方法返回 true(意味所有存款是成功的)。b. 修改 withdraw 方法來檢查提款數(shù)目是否大于余額。如果amt小于 balance, 則從余額中扣除提款數(shù)目并返回 true,否則余額不變返回 false。2 在 exercise3 主目錄編譯并運行 TestBanking 程序,將看到下列輸出;Creating the customer Jane Sm
11、ith.Creating her account with a 500.00 balance. Withdraw 150.00: trueDeposit 22.50: true Withdraw 47.62: true Withdraw 400.00: falseCustomer 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
12、 (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
13、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.withd
14、raw(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 基礎實戰(zhàn)Bank 項目實驗題目 4:將用數(shù)組實現(xiàn)銀行與客戶間的多重關系。實驗目的:在類中使用數(shù)組作為模擬集合操
15、作。提示:對銀行來說,可添加 Bank 類。 Bank 對象跟蹤自身與其客戶間的關系。用 Customer 對象的數(shù)組實現(xiàn)這個集合化的關系。還要保持一個整數(shù)屬性來跟蹤銀 行當前有多少客戶。a. 創(chuàng)建 Bank 類b. 為 Bank 類 增 加 兩 個 屬 性 : customers(Customer對象的數(shù)組 ) 和 numberOfCustomers(整數(shù),跟蹤下一個 customers 數(shù)組索引)c. 添加公有構(gòu)造器,以合適的最大尺寸(至少大于 5)初始化 customers 數(shù)組。d. 添加 addCustomer 方法。該方法必須依照參數(shù)(姓,名)構(gòu)造一個新的 Customer對象然后
16、把它放到 customer 數(shù)組中。還必須把 numberofCustomers 屬性的值加 1。e. 添加 getNumOfCustomers 訪問方法,它返回 numberofCustomers 屬性值。f. 添加 getCustomer方法。它返回與給出的index參數(shù)相關的客戶。g. 編譯并運行 TestBanking 程序??梢钥吹较铝休敵鼋Y(jié)果:Customer 1 is Simms,JaneCustomer 2 is Bryant,OwenCustomer 3 is Soley,TimCustomer 4 is Soley,Maria/TestBanking.java 文件/* T
17、his 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(); / Ad
18、d 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.getFi
19、rstName(); Java 基礎實戰(zhàn)Bank 項目實驗題目 5:在銀行項目中創(chuàng)建 Account 的兩個子類:SavingAccount 和 CheckingAccount實驗目的:繼承、多態(tài)、方法的重寫。提 示:創(chuàng)建 Account 類的兩個子類:SavingAccount 和 CheckingAccount 子類a. 修改 Account 類;將 balance 屬性的訪問方式改為 protectedb. 創(chuàng)建 SavingAccount 類,該類繼承 Account 類c. 該類必須包含一個類型為 double 的 interestRate 屬性d. 該類必須包括帶有兩個參數(shù)(bal
20、ance 和 interest_rate)的公有構(gòu)造器。該構(gòu) 造器必須通過調(diào)用 super(balance)將 balance 參數(shù)傳遞給父類構(gòu)造器。實現(xiàn) CheckingAccount 類1 CheckingAccount 類必須擴展 Account 類2 該類必須包含一個類型為 double 的 overdraftProtection 屬性。3 該類必須包含一個帶有參數(shù)(balance)的共有構(gòu)造器。該構(gòu)造器必須通過調(diào) 用 super(balance)將 balance 參數(shù)傳遞給父類構(gòu)造器。4 給類必須包括另一個帶有兩個參數(shù)(balance 和 protect)的公有構(gòu)造器。該 構(gòu)造器必
21、須通過調(diào)用 super(balance)并設置 overdragtProtection 屬性,將 balance 參數(shù)傳遞給父類構(gòu)造器。5 CheckingAccount 類必須覆蓋 withdraw 方法。此方法必須執(zhí)行下列檢查。如 果當前余額足夠彌補取款 amount,則正常進行。如果不夠彌補但是存在透支 保護,則嘗試用 overdraftProtection 得值來彌補該差值(balance-amount). 如果彌補該透支所需要的金額大于當前的保護級別。則整個交易失敗,但余 額未受影響。6 在主 exercise1 目錄中,編譯并執(zhí)行 TestBanking 程序。輸出應為:Creat
22、ing 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
23、.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: falseCustomer Simms, Jane has a balance of 324
24、.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 with his checking account that has overdraft
25、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: falseCustomer Soley, Maria has a balance of 15
26、0.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 b
27、ank = 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.p
28、rintln(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.getCustome
29、r(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
30、.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();
31、 / 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 t
32、he 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 acc
33、ount 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: + a
34、ccount.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
35、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
36、); 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.getFi
37、rstName() + 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(); /
38、 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
39、 + account.getBalance(); 實驗題目:5_續(xù)1創(chuàng)建客戶賬戶實驗目的:instanceof 運算符的應用提示:修改Customer類1 修改Customer類來處理具有多種類型的聯(lián)合賬戶。(例如用數(shù)組表示多重性一節(jié)所作的,該類必須包括以下的公有方法:addAccount(Account),getAccount(int)和getNumOfAccounts()。每個Customer可以有多個Account。(聲明至少有5個)2 完成TestBanking程序該程序創(chuàng)建一個客戶和賬戶的集合,并生成這些客戶及其賬戶余額的報告。在TestBanking.Java文件中,你會發(fā)現(xiàn)注釋塊
40、以/*/來開頭和結(jié)尾。這些注釋只是必須提供的代碼的位置。3 使用instanceof操作符測試擁有的賬戶類型,并且將account_type設置為適當?shù)闹?,例如:“SavingsAccount”或“CheckingAccount”。4 編譯并運行該程序,將看到下列結(jié)果CUSTOMERS REPORT=Customer: Simms, JaneSavings Account: current balance is ¥500.00Checking Account: current balance is ¥200.00Customer: Bryant, OwenChecking Account: c
41、urrent balance is ¥200.00Customer: 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 t
42、he 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.getCurrencyInst
43、ance(); 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)
44、; 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.getC
45、ustomer(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(tttCUSTOMERS REPORT); System.out.println(ttt=); for ( int cust_idx = 0; cust_idx bank.getNumOf
46、Customers(); 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
47、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 an
48、d the balance.* Feel free to use the currency_format formatter* to generate a currency string for the balance.*/ 實驗題目:5_續(xù)2實現(xiàn)更為復雜的透支保護機制注釋-這是練習1的選擇練習。它包括了更為復雜的透支保護機制模型。它使用客戶儲蓄。它使用客戶儲蓄賬戶完成透支保護。本練習必須在完成上述兩個練習后進行。實驗目的:繼承、多態(tài)、方法的重寫。說明:修改SavingAccount類1.仿照練習1“Account類的兩個子類”一節(jié)實現(xiàn)SavingsAccount類。2.SavingAccou
49、nt類必須擴展Account類。3.該類必須包含一個類型為double的interestRate屬性4.該類必須包括一個帶有兩個參數(shù)(balance和interest_rate)的公有構(gòu)造器。該構(gòu)造器必須通過調(diào)用super(balance)來將balance參數(shù)傳遞給父類構(gòu)造器修改CheckingAccount類1.仿照練習1“Account類的兩個子類”一節(jié)實現(xiàn)CheckingAccount類。2.CheckingAccount類必須擴展Account類3.該類必須包括一個關聯(lián)屬性,稱作protectedBy,表示透支保護。該屬性的類型為SavingAccount,缺省值必須是null。除此
50、之外該類沒有其他的數(shù)據(jù)屬性。4.該類必須包括一個帶有參數(shù)(balance)的公有構(gòu)造器,該構(gòu)造器必須通過調(diào)用super(balance)將balance參數(shù)傳遞到父類構(gòu)造器。5.修改構(gòu)造器為CheckingAccount(double balance,SavingAccount protect)構(gòu)造器。該構(gòu)造器必須通過調(diào)用super(balance)將balance參數(shù)傳遞給父類構(gòu)造器。6. CheckingAccount類必須覆蓋withdraw方法。該類必須執(zhí)行下面的檢查:如果當前余額足夠彌補取款amount,則正常進行。如果不夠彌補但是存在透支保護則嘗試用儲蓄賬戶來彌補這個差值(bala
51、nce-amount)。如果后一個交易失敗,則整個交易一定失敗,但余額未受影響。修改Customer類,使其擁有兩個賬戶:一個儲蓄賬戶和一個支票賬戶:兩個都是可選的。1.在練習5_續(xù)1修改,原來的Customer類包含一個稱作account的關聯(lián)屬性,可用該屬性控制一個Account對象。重寫這個類,使其包含兩個關聯(lián)屬性:savingAccount和checkingAccount,這兩個屬性的缺省值是null2.包含兩個訪問方法:getSaving和getChecking,這兩個方法分別返回儲蓄和支票總數(shù)。3. 包含兩個相反的方法:SetSaving和setChecking,這兩個方法分別為savingAccount和checkingAccount這兩個關聯(lián)屬性賦值。完成TestBanking程序Customer Simms, Jane has a checking balance of 200.0 and a savings balance of500.0Checking Acct Jane Simms : withdraw 150.00 succe
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- DB32/T 4600-2023千米級公鐵兩用斜拉橋設計規(guī)范
- 2025年特種車市場調(diào)查報告
- DB32/T 4527-2023城市地下資源協(xié)同開發(fā)調(diào)查評價規(guī)范
- 2025年孕婦產(chǎn)前托腹褲行業(yè)深度研究分析報告
- 2025年手機機殼裝飾品項目市場調(diào)查研究報告
- 2025年異構(gòu)丙基甜菜堿項目市場調(diào)查研究報告
- 2025-2030中國瓦楞包裝盒行業(yè)市場現(xiàn)狀分析及競爭格局與投資發(fā)展研究報告
- 2025-2030中國玻璃隔墻行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報告
- DB32/T 4369-2022企業(yè)公共服務網(wǎng)絡平臺建設和服務規(guī)范
- DB32/T 4323-2022雙元制職業(yè)教育人才培養(yǎng)指南
- 老產(chǎn)品芯片1-gc2145d模組設計指南
- 廣東省中山市20222022學年下學期期末考試八年級英語試卷
- 油脂制取與加工工藝學
- 創(chuàng)新創(chuàng)業(yè)指導把握創(chuàng)業(yè)機會課件
- 部編版道德與法治五(下)第三單元百年追夢復興中華教學課件
- 第三章工程師的責任 工程倫理學課件
- 2022年湖南省普通高中學業(yè)水平考試語文試卷及參考答案
- 傳統(tǒng)節(jié)日端午節(jié)主題班會PPT模板
- 木材采購合同參考
- 1389國開電大本科《理工英語4》網(wǎng)上形考任務(單元自測1至8)試題及答案(精華版)
- 設備供貨投標實施方案
評論
0/150
提交評論