JAVA的第一堂课,我们如约到教室测试,一共四节课时间,测试内容是做银行提款机的程序。
我先把题目中要求的类和主函数写出来,然后把之前写的学生管理系统的开始界面的语句改了改,然后按照题目中的顺序写程序。
首先是按照要求写了私有变量,以及私有变量的set和get函数(后加的,当时根本没时间写),然后就是主函数在主函数中我写的不多,主要是调用核对账号、密码以及显示主界面的函数。
在核对账号的函数中,用一个变量存储客户输入的账号,然后打开储存客户账号的文件,按行查找账号,当找到账号就把那一行的数据赋值给相应的变量,然后主函数调用核对密码的函数,如果账号不对,回到主函数依旧调用核对账号的函数,直到账号输入正确。
在核对密码的函数中,用一个变量存储客户输入的密码,如果密码输入正确,则回到主函数,调用主界面函数,如果不正确,则由time变量存储输入账号次数,若达到三次则锁定,回到输入账号的界面。
进入主界面函数后,显示可以进行的选项。在主函数中利用switch语句判断、调用对应的函数。
在存款函数中用户输入错误金额会提示出错,然后回到存款界面,如果输入q则回到输入账号界面,输入正确金额后,找到对应的账号,加金额写道临时文件中,最后把临时文件写入到存储文件中,然后在流水文件中写入账户和操作。
取款函数与存款函数类似,用switch语句判断取款金额。
转账汇款函数与前面几个函数基本相同,需要注意的是两个账户的金额都要修改。
修改密码,确认原密码和两次新密码之后将修改后的密码写到文件中,若其中出现错误则跳回修改密码界面。
查询函数,在流水文件中找到相应账户的信息并输出。
本次测试成绩为19分,原因在于没有将代码完善,一些功能还会出错,所以我认识到敲代码不仅仅要准确也要有速度。而这些的基础就在于程序员对语言、结构的掌握。我以后要经常敲代码,多熟悉代码。
以下是完整的程序代码。
package test; import java.util.*; //信 1905-1 20194171 常金悦 import java.io.*; public class AccountManager { private String accountID; //用户账号 private String accountname; //账户名称 private String operatedate; //操作时间 2018-09-20 private int operatetype; //操作类型 private String accountpassword; //用户密码 private int accountbalance; //账户余额 private int amount; //流水金额 String getaccountID(){return accountID;} void setaccountID(String s){accountID=s;} String getaccountname(){return accountname;} void setaccountname(String s){accountname=s;} String getoperatedate(){return operatedate;} void setoperatedate(String s){operatedate=s;} int getoperatetype(){return operatetype;} void setoperatetype(int s){operatetype=s;} String getaccountpassword(){return accountpassword;} void setaccountpassword(String s){accountpassword=s;} int getaccountbalance(){return accountbalance;} void setaccountbalance(int s){accountbalance=s;} int getamount(){return amount;} void setamount(int s){amount=s;} public static void main(String[] args)throws IOException{ AccountManager acc=new AccountManager(); System.out.println("*******************************"); System.out.println(" 欢迎使用中国工商银行自动柜员系统"); acc.mmaaiinn(); } void mmaaiinn()throws IOException{ int a=0; while(a==0){ System.out.println("*******************************"); System.out.println("请输入您的账号:"); String Id=zhanghao(); if(Id.equals("false")){} else { a=mima(Id); if(a==1) jiemian(); } } } public String zhanghao()throws IOException{ Scanner input = new Scanner(System.in,"UTF-8"); String ID=input.next(); InputStream is = new FileInputStream("accountinformation.txt"); Scanner ss = new Scanner(is,"UTF-8"); int t=0; while(ss.hasNext()){ String line = ss.nextLine(); String[] txt = line.split(","); String id = txt[0]; if(id.equals(ID)){ accountID=txt[0]; accountpassword=txt[1]; accountbalance=Integer.valueOf(txt[2]); t=-1; break; } } if(t==0){ System.out.println("该卡不是工行卡"); return "false"; } else return ID; } public int mima(String Id)throws IOException{ System.out.println("*******************************"); System.out.println(" 欢迎"+Id+"使用中国工商银行自动柜员系统"); int time=0; int a=0; while(time!=3){ System.out.println("*******************************"); System.out.println("请输入您的密码:"); Scanner input = new Scanner(System.in,"UTF-8"); String mm=input.next(); if(accountpassword.equals(mm)){ a=1; return a; } else{ System.out.println("*******************************"); System.out.println("密码输入错误"); time++; } } if(time==3){ System.out.println("该账号三次录入密码错误,该卡已被系统没收,请与工行及时联系处理"); a=0; } return a; } public void jiemian()throws IOException{ System.out.println("*******************************"); System.out.println(" 欢迎"+accountID+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("1、存款"); System.out.println("2、取款"); System.out.println("3、转账汇款"); System.out.println("4、修改密码"); System.out.println("5、查询余额"); System.out.println("*******************************"); Scanner input = new Scanner(System.in,"UTF-8"); int a=0; a = input.nextInt(); input.nextLine(); switch (a) { case 1: cunkuan(); break; case 2: qukuan(); break; case 3: zhuanzhang(); break; case 4: xiugai(); break; case 5: chaxun(); break; default: System.out.println("*******************************"); System.out.println("Wrong number!"); break; } input.close(); } public void cunkuan()throws IOException{ System.out.println("*******************************"); System.out.println(" 欢迎"+accountID+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("请输入存款金额:"); Scanner input = new Scanner(System.in,"UTF-8"); String a; int e=0; a = input.nextLine(); if(a.equals("q")) mmaaiinn(); else {e=Integer.valueOf(a);} if((e<=0)){ System.out.println("输入金额有误"); cunkuan(); } else{ int money=0; InputStream is = new FileInputStream("accountinformation.txt"); Scanner ss = new Scanner(is,"UTF-8"); File f2 = new File("account.txt"); FileWriter fw2 = new FileWriter(f2); PrintWriter pw2 = new PrintWriter(fw2); while(ss.hasNext()){ String line = ss.nextLine(); String[] txt = line.split(","); String id = txt[0]; if(id.equals(accountID)){ money=accountbalance+e; pw2.println(id+","+accountpassword+","+money); } else{ pw2.println(line); } } //12345678,787878,0 pw2.close(); is = new FileInputStream("account.txt"); ss = new Scanner(is,"UTF-8"); File f = new File("accountinformation.txt"); fw2 = new FileWriter(f); PrintWriter w = new PrintWriter(fw2); while (ss.hasNext()) { String line = ss.nextLine(); w.println(line); } w.close(); System.out.println("*******************************"); System.out.println(" 欢迎"+accountID+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("当前账户存款操作成功"); System.out.println("当前账户余额为:"+money+"元"); f=new File("accountlist.txt"); FileWriter fw = new FileWriter(f,true); PrintWriter pw=new PrintWriter(fw); pw.println(accountID+",take in"+a+"RMB"); pw.close(); } } public void qukuan()throws IOException{ System.out.println("*******************************"); System.out.println(" 欢迎"+accountID+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println(" 当前账户每日可以支取2万元。"); System.out.println(" 1、100元"); System.out.println(" 2、500元"); System.out.println(" 3、1000元"); System.out.println(" 4、1500元"); System.out.println(" 5、2000元"); System.out.println(" 6、5000元"); System.out.println(" 7、其他金额"); System.out.println(" 8、退卡"); System.out.println(" 9、返回"); Scanner input = new Scanner(System.in,"UTF-8"); int a=0; int money=0; a = input.nextInt(); input.nextLine(); switch (a) { case 1: money=100; break; case 2: money=500; break; case 3: money=1000; break; case 4: money=1500; break; case 5: money=2000; break; case 6: money=5000; break; case 7:{ System.out.println("*******************************"); System.out.println(" 欢迎"+accountID+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("请输入取款金额"); money=input.nextInt(); } break; case 8:{mmaaiinn();}break; case 9:{jiemian();}break; default: System.out.println("*******************************"); System.out.println("Wrong number!"); break; } if(money>0){ if(money<=accountbalance){ System.out.println("*******************************"); System.out.println(" 欢迎"+accountID+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println(" 当前账户取款操作"+money+"元成功"); accountbalance=accountbalance-money; System.out.println(" 当前账户余额为"+accountbalance+"元"); InputStream is = new FileInputStream("accountinformation.txt"); Scanner ss = new Scanner(is,"UTF-8"); File f2 = new File("account.txt"); FileWriter fw2 = new FileWriter(f2); PrintWriter pw2 = new PrintWriter(fw2); while(ss.hasNext()){ String line = ss.nextLine(); String[] txt = line.split(","); String id = txt[0]; if(id.equals(accountID)){ pw2.println(id+","+accountpassword+","+accountbalance); } else{ pw2.println(line); }} pw2.close(); is = new FileInputStream("account.txt"); ss = new Scanner(is,"UTF-8"); File f = new File("accountinformation.txt"); fw2 = new FileWriter(f); PrintWriter w = new PrintWriter(fw2); while (ss.hasNext()) { String lin = ss.nextLine(); w.println(lin); } w.close(); f=new File("accountlist.txt"); FileWriter fw = new FileWriter(f,true); PrintWriter pw=new PrintWriter(fw); pw.println(accountID+",take out"+money+"RMB"); pw.close(); fw.close(); }else{ System.out.println("账户余额不足"); } } input.close(); } public void zhuanzhang()throws IOException{ System.out.println("*******************************"); System.out.println(" 欢迎"+accountID+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println(" 请输入转账用户"); int t=0; Scanner input = new Scanner(System.in,"UTF-8"); String id=input.nextLine(); InputStream is = new FileInputStream("accountinformation.txt"); Scanner ss = new Scanner(is,"UTF-8"); while(ss.hasNext()){ String line = ss.nextLine(); String[] txt = line.split(","); String idd = txt[0]; if(idd.equals(id)){ t=-1; System.out.println("*******************************"); System.out.println(" 欢迎"+accountID+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println(" 请输入转账金额"); int money=input.nextInt(); if(money<=accountbalance){ System.out.println("*******************************"); System.out.println(" 欢迎"+accountID+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println(" 请确认是否向"+idd+"转账"+money+"元"); String s=input.next(); if(s.equals("Y")){ System.out.println("*******************************"); System.out.println(" 欢迎"+accountID+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println(" 当前账户向"+idd+"成功转账"+money+"元"); System.out.println(" 当前账户余额为"+(accountbalance-money)+"元"); File f2 = new File("account.txt"); FileWriter fw2 = new FileWriter(f2); PrintWriter pw2 = new PrintWriter(fw2); InputStream iis = new FileInputStream("accountinformation.txt"); Scanner sss = new Scanner(iis,"UTF-8"); f2 = new File("account.txt"); fw2 = new FileWriter(f2); pw2 = new PrintWriter(fw2); while(sss.hasNext()){ line = sss.nextLine(); txt = line.split(","); if(txt[0].equals(accountID)||txt[0].equals(idd)){ if(txt[0].equals(idd)){ pw2.println(idd+","+txt[1]+","+(money+Integer.valueOf(txt[2]))); } else{ pw2.println(accountID+","+accountpassword+","+(accountbalance-money)); } } else{ pw2.println(line); }} pw2.close(); is = new FileInputStream("account.txt"); ss = new Scanner(is,"UTF-8"); File f = new File("accountinformation.txt"); fw2 = new FileWriter(f); PrintWriter w = new PrintWriter(fw2); while (ss.hasNext()) { String lin = ss.nextLine(); w.println(lin); } w.close(); f=new File("accountlist.txt"); FileWriter fw = new FileWriter(f,true); PrintWriter pw=new PrintWriter(fw); pw.println(accountID+",take "+money+"RMB to"+idd); pw.close(); fw.close(); }else if(s.equals("N")){ jiemian(); } }else{ System.out.println("账户余额不足"); } } }if(t!=-1){ System.out.println("该账户不存在"); } } public void xiugai()throws IOException{ System.out.println("*******************************"); System.out.println(" 欢迎"+accountID+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("请输入当前密码"); Scanner input = new Scanner(System.in,"UTF-8"); String ma=input.nextLine(); if(ma.equals(accountpassword)){ System.out.println("请输入修改密码"); String ma1=input.nextLine(); System.out.println("请输入确认密码"); String ma2=input.nextLine(); if(ma1.equals(ma2)){ System.out.println("*******************************"); System.out.println(" 欢迎"+accountID+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("当前账户密码修改成功"); File f2 = new File("account.txt"); FileWriter fw2 = new FileWriter(f2); PrintWriter pw2 = new PrintWriter(fw2); InputStream is = new FileInputStream("accountinformation.txt"); Scanner ss = new Scanner(is,"UTF-8"); while(ss.hasNext()){ String lin = ss.nextLine(); String txt[] = lin.split(","); if(txt[0].equals(accountID)){ pw2.println(txt[0]+","+ma2+","+txt[2]); } else{ pw2.println(lin); } } pw2.close(); is = new FileInputStream("account.txt"); ss = new Scanner(is,"UTF-8"); File f = new File("accountinformation.txt"); fw2 = new FileWriter(f); PrintWriter w = new PrintWriter(fw2); while (ss.hasNext()) { String lin = ss.nextLine(); w.println(lin); } w.close(); f=new File("accountlist.txt"); FileWriter fw = new FileWriter(f,true); PrintWriter pw=new PrintWriter(fw); pw.println(accountID+",change password into"+ma2); pw.close(); fw.close(); }else{ xiugai(); } }else{ System.out.println("密码输入错误"); xiugai(); } } public void chaxun()throws IOException{ System.out.println("*******************************"); System.out.println(" 欢迎"+accountID+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("当前账户余额为"+accountbalance+"元"); InputStream is = new FileInputStream("accountlist.txt"); Scanner ss = new Scanner(is,"UTF-8"); int x=0; while(ss.hasNext()){ String line = ss.nextLine(); String[] txt = line.split(","); if(txt[0].equals(accountID)){ x++; System.out.println(x+"."+txt[1]); } } } }