java bank项目.docx
《java bank项目.docx》由会员分享,可在线阅读,更多相关《java bank项目.docx(24页珍藏版)》请在冰豆网上搜索。
![java bank项目.docx](https://file1.bdocx.com/fileroot1/2022-12/13/b01b7e80-ed64-4eef-b8d2-b324bb2fe791/b01b7e80-ed64-4eef-b8d2-b324bb2fe7911.gif)
javabank项目
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类。
可以看到下列输出结果:
Creatinganaccountwitha500.00balance
Withdraw150.00
Deposit22.50
Withdraw47.62
Theaccounthasabalanceof324.88
//TestBanking.java文件
/*
*Thisclasscreatestheprogramtotestthebankingclasses.
*ItcreatesanewBank,setstheCustomer(withaninitialbalance),
*andperformsaseriesoftransactionswiththeAccountobject.
*/
packagetest;
importbanking.*;
publicclassTestBanking{
publicstaticvoidmain(String[]args){
Account account;
//Createanaccountthatcanhasa500.00balance.
System.out.println("Creatinganaccountwitha500.00balance.");
//code
System.out.println("Withdraw150.00");
//code
System.out.println("Deposit22.50");
//code
System.out.println("Withdraw47.62");
//code
//Printoutthefinalaccountbalance
System.out.println("Theaccounthasabalanceof"+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程序。
应该看到如下输出结果:
CreatingthecustomerJaneSmith.
Creatingheraccountwitha500.00balance.
Withdraw150.00
Deposit22.50
Withdraw47.62
Customer[Smith,Jane]hasabalanceof324.88
//TestBanking.java文件
/*
*Thisclasscreatestheprogramtotestthebankingclasses.
*ItcreatesanewBank,setstheCustomer(withaninitialbalance),
*andperformsaseriesoftransactionswiththeAccountobject.
*/
importbanking.*;
publicclassTestBanking{
publicstaticvoidmain(String[]args){
Customercustomer;
Account account;
//Createanaccountthatcanhasa500.00balance.
System.out.println("CreatingthecustomerJaneSmith.");
//code
System.out.println("Creatingheraccountwitha500.00balance.");
//code
System.out.println("Withdraw150.00");
//code
System.out.println("Deposit22.50");
//code
System.out.println("Withdraw47.62");
//code
//Printoutthefinalaccountbalance
System.out.println("Customer["+customer.getLastName()
+","+customer.getFirstName()
+"]hasabalanceof"+account.getBalance());
}
}
Java基础实战—Bank项目
实验题目3:
修改withdraw方法以返回一个布尔值,指示交易是否成功。
实验目的:
使用有返回值的方法。
提示:
1.修改Account类
a.修改deposit方法返回true(意味所有存款是成功的)。
b.修改withdraw方法来检查提款数目是否大于余额。
如果amt小于balance,则从余额中扣除提款数目并返回true,否则余额不变返回false。
2.在exercise3主目录编译并运行TestBanking程序,将看到下列输出;
CreatingthecustomerJaneSmith.
Creatingheraccountwitha500.00balance.Withdraw150.00:
true
Deposit22.50:
trueWithdraw47.62:
trueWithdraw400.00:
false
Customer[Smith,Jane]hasabalanceof324.88
//TestBanking.java文件
/*
*Thisclasscreatestheprogramtotestthebankingclasses.
*ItcreatesanewBank,setstheCustomer(withaninitialbalance),
*andperformsaseriesoftransactionswiththeAccountobject.
*/
importbanking.*;
publicclassTestBanking{
publicstaticvoidmain(String[]args){
Customercustomer;
Account account;
//Createanaccountthatcanhasa500.00balance.
System.out.println("CreatingthecustomerJaneSmith.");
//code
System.out.println("Creatingheraccountwitha500.00balance.");
//code
//Performsomeaccounttransactions
System.out.println("Withdraw150.00:
"+account.withdraw(150.00));
System.out.println("Deposit22.50:
"+account.deposit(22.50));
System.out.println("Withdraw47.62:
"+account.withdraw(47.62));
System.out.println("Withdraw400.00:
"+account.withdraw(400.00));
//Printoutthefinalaccountbalance
System.out.println("Customer["+customer.getLastName()
+","+customer.getFirstName()
+"]hasabalanceof"+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]isSimms,Jane
Customer[2]isBryant,Owen
Customer[3]isSoley,Tim
Customer[4]isSoley,Maria
//TestBanking.java文件
/*
*Thisclasscreatestheprogramtotestthebankingclasses.
*ItcreatesanewBank,setstheCustomer(withaninitialbalance),
*andperformsaseriesoftransactionswiththeAccountobject.
*/
importbanking.*;
publicclassTestBanking{
publicstaticvoidmain(String[]args){
Bank bank=newBank();
//AddCustomerJane,Simms
//code
//AddCustomerOwen,Bryant
//code
//AddCustomerTim,Soley
//code
//AddCustomerMaria,Soley
//code
for(inti=0;i Customercustomer=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程序。
输出应为:
CreatingthecustomerJaneSmith.
CreatingherSavingsAccountwitha500.00balanceand3%interest.
CreatingthecustomerOwenBryant.
CreatinghisCheckingAccountwitha500.00balanceandnooverdraftprotection.
CreatingthecustomerTimSoley.
CreatinghisCheckingAccountwitha500.00balanceand500.00inoverdraftprotection.
CreatingthecustomerMariaSoley.
MariasharesherCheckingAccountwithherhusbandTim.
RetrievingthecustomerJaneSmithwithhersavingsaccount.
Withdraw150.00:
true
Deposit22.50:
true
Withdraw47.62:
true
Withdraw400.00:
false
Customer[Simms,Jane]hasabalanceof324.88
RetrievingthecustomerOwenBryantwithhischeckingaccountwithnooverdraftprotection.
Withdraw150.00:
true
Deposit22.50:
true
Withdraw47.62:
true
Withdraw400.00:
false
Customer[Bryant,Owen]hasabalanceof324.88
RetrievingthecustomerTimSoleywithhischeckingaccountthathasoverdraftprotection.
Withdraw150.00:
true
Deposit22.50:
true
Withdraw47.62:
true
Withdraw400.00:
true
Customer[Soley,Tim]hasabalanceof0.0
RetrievingthecustomerMariaSoleywithherjointcheckingaccountwithhusbandTim.
Deposit150.00:
true
Withdraw750.00:
false
Customer[Soley,Maria]hasabalanceof150.0
//TestBanking程序
/*
*Thisclasscreatestheprogramtotestthebankingclasses.
*ItcreatesanewBank,setstheCustomer(withaninitialbalance),
*andperformsaseriesoftransactionswiththeAccountobject.
*/
importbanking.*;
publicclassTestBanking{
publicstaticvoidmain(String[]args){
Bank bank=newBank();
Customercustomer;
Accountaccount;
//
//Createbankcustomersandtheiraccounts
//
System.out.println("CreatingthecustomerJaneSmith.");
bank.addCustomer("Jane","Simms");
//code
System.out.println("CreatingherSavingsAccountwitha500.00balanceand3%interest.");
//code
System.out.println("CreatingthecustomerOwenBryant.");
//code
customer=bank.getCustomer
(1);
System.out.println("CreatinghisCheckingAccountwitha500.00balanceandnooverdraftprotection.");
//code
System.out.println("CreatingthecustomerTimSoley.");
bank.addCustomer("Tim","Soley");
customer=bank.getCustomer
(2);
System.out.println("CreatinghisCheckingAccountwitha500.00balanceand500.00inoverdraftprotection.");
//code
System.out.println("Creatingthecu