Java程序设计 实验七.docx
- 文档编号:10288979
- 上传时间:2023-05-24
- 格式:DOCX
- 页数:58
- 大小:36.09KB
Java程序设计 实验七.docx
《Java程序设计 实验七.docx》由会员分享,可在线阅读,更多相关《Java程序设计 实验七.docx(58页珍藏版)》请在冰点文库上搜索。
Java程序设计实验七
《Java程序设计》实验七
假定银行系统有两类客户,一类是现金用户,一类是信用卡用户,每种客户都可以实现存、取、查询余额和交易历史(信用卡用户还可以查询透支情况和信用情况)要求。
对于现金用户,每次取款操作只能在账户实际额度内操作,而信用卡用户则根据其信用级别有一定的透支限额,不论是哪种用户都必须在密码正确的情况下进行操作,请根据你的理解,运用所学的抽象、接口、继承等面向对象的概念建立模型。
(即给出所需类、接口的设计,包括类之间的继承关系,类和接口之间的关系,属性给出定义,方法给出方法头的定义和功能描述)
Account.java
importjava.util.InputMismatchException;
importjava.util.Vector;
importjava.util.Scanner;
importjava.util.Random;
publicabstractclassAccountimplementsBankService{
privateintid;
privateStringname;
privateintbalance;
privateStringpassword;
privateVector
publicAccount(Stringname,Stringpwd){
Randomrd=newRandom();
this.id=rd.nextInt(10000);
this.name=name;
this.password=pwd;
}
publicAccount(Stringname,Stringpwd,intbalance){
this(name,pwd);
Randomrd=newRandom();
this.id=rd.nextInt();
this.balance=balance;
}
publicintgetId(){
returnid;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetName(){
returnname;
}
//存款
publicintsetBalance(intamount){
returnbalance+=amount;
}
//修改余额
publicintsetBalance1(intamount){
returnbalance=amount;
}
publicintgetBalance(){
returnbalance;
}
publicStringgetPassword(){
returnthis.password;
}
publicvoidsetPassword(Stringpwd){
this.password=pwd;
}
publicbooleancheckPwd()throwsWrongpwdException{
Scannersc=newScanner(System.in);
Stringpwd;
intn=0;
do{
System.out.println("请输入密码:
");
pwd=sc.next();
try{
if(this.password.equals(pwd)){
returntrue;
}
else{
thrownewWrongpwdException();
}
}
catch(WrongpwdExceptione){
System.out.println(e.getMessage());
}
catch(InputMismatchExceptione){
System.out.println("输入不匹配");
}
catch(Exceptione){
System.out.println(e.getMessage());
}
n++;
}while(n<5);
thrownewWrongpwdException();
}
publicvoidaddHistory(Historyhistory){
this.history.add(history);
}
publicvoidgetHistory(){
for(Historyhistory1:
history){
System.out.println(history1);
}
}
publicStringtoString(){
returnthis.name+""+this.id+"当前余额是:
"+this.balance;
CashAccount.java
importjava.util.Scanner;
importjava.util.InputMismatchException;
publicclassCashAccountextendsAccount{
publicCashAccount(Stringname,Stringpwd,intbalance){
super(name,pwd,balance);
}
publicintwithdraw(){
Scannersc=newScanner(System.in);
System.out.println("请输入取款金额");
do{
try{
intamount=sc.nextInt();
intbalance=this.getBalance();
if(amount<=balance){
returnthis.setBalance1(balance-amount);
}
else
thrownewInsufficientFundException();
}
catch(InputMismatchExceptione){
System.out.println("输入数据类型不合法");
}
catch(InsufficientFundExceptione){
System.out.println(e.getMessage());
}
catch(Exceptione){
System.out.println(e.getMessage());
}
System.out.println("继续取款吗?
");
if(sc.next().equalsIgnoreCase("N")){
returnthis.getBalance();
}
}while(true);
}
}
CreditAccount.java
importjava.util.Scanner;
importjava.util.InputMismatchException;
publicclassCreditAccountextendsAccount{
privateintlimit;
CreditAccount(Stringname,Stringpwd,intlimit){
super(name,pwd);
this.limit=limit;
}
CreditAccount(Stringname,Stringpwd,intlimit,intbalance){
super(name,pwd,balance);
this.limit=limit;
}
publicvoidsetLimit(intlimit){
this.limit=limit;
}
publicintgetLimit(){
returnlimit;
}
publicintsetBalance(intamount){
returnsuper.setBalance(amount)+limit;
}
publicintgetBalance(){
returnthis.limit+super.getBalance();
}
publicintwithdraw(){
Scannersc=newScanner(System.in);
intamount,balance;
do{
try{
System.out.println("请输入取款金额");
amount=sc.nextInt();
balance=super.getBalance();
if(amount<=balance+limit){
balance-=amount;
this.setBalance1(balance);
returnbalance+limit;
}
else{
thrownewInsufficientFundException();
}
}catch(InputMismatchExceptione){
System.out.println("输入数据类型不合法");
}
catch(InsufficientFundExceptione){
System.out.println(e.getMessage());
}
catch(Exceptione){
System.out.println(e.getMessage());
}
System.out.println("继续取款吗?
");
if(sc.next().equalsIgnoreCase("N")){
returnthis.getBalance();
}
}while(true);
}
}
CreditDataBase.java
importjava.util.ArrayList;
importjava.util.ListIterator;
publicclassCreditDataBase{
privateArrayList
publicvoidadd(CreditAccountc){
this.credit.add(c);
}
publicArrayList
returnthis.credit;
}
publicCreditAccountfind(Stringname){
for(CreditAccountc:
credit){
if(c.getName().equalsIgnoreCase(name)){
returnc;
}
}
returnnull;
}
publicCreditAccountremove(Stringname){
for(CreditAccountc:
credit){
if(c.getName().equalsIgnoreCase(name)){
this.credit.remove(c);
returnc;
}
}
returnnull;
}
publicvoidset(CreditAccountc){
ListIterator
while(ci.hasNext()){
if(ci.next().getName().equalsIgnoreCase(c.getName())){
ci.set(c);
break;
}
}
}
publicvoidprint(){
ListIterator
System.out.println("账户\t用户名\t余额\t透支额度");
while(ci.hasNext()){
CreditAccountc=ci.next();
System.out.println(c.getId()+"\t"+c.getName()+"\t"+c.getBalance()+"\t"+c.getLimit());
}
}
publicvoidprint1(){
for(CreditAccountc:
this.credit){
System.out.println(c);
}
}
}
History.java
importjava.util.Calendar;
publicclassHistory{
Calendaropttime;
Stringoptr;
publicHistory(Stringoptr){
this.optr=optr;
opttime=Calendar.getInstance();
}
publicStringtoString(){
return"操作时间:
"+this.opttime.getTime()+",操作类型:
"+this.optr;
}
}
MainClass1.java
importjava.util.Scanner;
publicclassMainClass1{
publicstaticvoidmain(String[]args){
Scannersc=newScanner(System.in);
CreditDataBaseallcredit=newCreditDataBase();
CashDataBaseallcash=newCashDataBase();
do{
System.out.println("请选择操作类型:
1、开户2、已有账户");
intselect0=sc.nextInt();
if(select0==1){
//开户操作
System.out.println("请选择开户用户类型:
0(信用卡账户)\\1(现金账户)");
intselect1=sc.nextInt();
if(select1==0){
//创建信用卡账户
System.out.println("请输入用户的信息(用户名,密码,透支金额,存款金额)");
CreditAccountcredit=newCreditAccount(sc.next(),sc.next(),sc.nextInt(),sc.nextInt());
Historyhistory1=newHistory("创建信用卡账户");
credit.addHistory(history1);
allcredit.add(credit);
}
elseif(select1==1){
//创建现金账户
System.out.println("请输入用户的信息(用户名,密码,存款金额)");
CashAccountcash=newCashAccount(sc.next(),sc.next(),sc.nextInt());
Historyhistory=newHistory("创建现金账户");
cash.addHistory(history);
allcash.add(cash);
}
}
elseif(select0==2){
//对已有账户进行操作
System.out.println("请选择用户类型:
0(信用卡账户)\\1(现金账户)");
intselect2=sc.nextInt();
if(select2==0){
//信用卡账户的操作
System.out.println("请输入已有账户名");
Stringname=sc.next();
booleanis=false;
for(CreditAccountcredit1:
allcredit.get()){
if(credit1.getName().equalsIgnoreCase(name)){
is=true;//说明该用户存在
try{
if(credit1.checkPwd()){
do{
System.out.println("请选择操作类型(1、查询账户余额2、存款3、取款4、查询历史信息5、打印账户信息)");
inti=sc.nextInt();
switch(i){
case1:
System.out.println(credit1.getBalance());
Historyhistory2=newHistory("查询余额");
credit1.addHistory(history2);
break;
case2:
System.out.println("请输入存款金额");
System.out.println(credit1.setBalance(sc.nextInt()));
Historyhistory3=newHistory("存款");
credit1.addHistory(history3);
break;
case3:
System.out.println(credit1.withdraw());
Historyhistory4=newHistory("取款");
credit1.addHistory(history4);
break;
case4:
credit1.getHistory();
Historyhistory5=newHistory("查询历史信息");
credit1.addHistory(history5);
break;
case5:
System.out.println(credit1);
Historyhistory6=newHistory("打印账户信息");
credit1.addHistory(history6);
break;
default:
System.out.println("无效输入,请重新选择1/2/3/4/5");
}
System.out.println("继续其他操作吗?
(YorN)");
if(sc.next().equalsIgnoreCase("N"))
break;
}while(true);
}
}catch(WrongpwdExceptione){
System.out.println(e.getMessage());
}
catch(Exceptione){
System.out.println(e.getMessage());
}
break;
}
}
if(is==false){
System.out.println("该用户不存在");
}
}
elseif(select2==1){
//现金账户的操作
System.out.println("请输入已有账户名");
Stringname=sc.next();
booleanis=false;
for(CashAccountcash1:
allcash.get()){
if(cash1.getName().equalsIgnoreCase(name)){
is=true;
try{
if(cash1.checkPwd()){
do{//当该账户的当前操作完成,可以选择其他操作
System.out.println("请选择操作类型(1、查询账户余额2、存款3、取款4、查询历史信息5、打印账户信息)");
inti=sc.nextInt();
switch(i){
case1:
System.out.println(cash1.getBalance());
Historyhistory2=newHistory("查询余额");
cash1.addHistory(history2);
break;
case2:
System.out.println("请输入存款金额");
System.out.println(cash1.setBalance(sc.nextInt()));
Historyhistory3=newHistory("存款");
cash1.addHistory(history3);
break;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java程序设计 实验七 Java 程序设计 实验