开学第一周系主任安排了一项测试,测试要求:模拟ATM自助取款机用文件进行存储账户信息,密码等,并进行存款取款,转账,查询记录等操作,而且要进行文件的读取与录入。
这是一个ATM自助取款的操作系统,进行了文件的存储和读取,以及对文件信息的操作。先说说我的感想:在刚开始测试得时候,我对文件的读取和存储只有初步的认识只知道将信息输入到文件中但是读取并利用这方面不太会运用,通过网上一些信息的查找我知道了一种文件的读取和存储:Scanner 的存储。
PrintWriter out = new PrintWriter("accountinformation.txt");
文件的创建:PrintWriter out = new PrintWriter("XXXXX");输出到文件中是out.println();或out.print();前者自动换行,后者不会自动换行;我选用的是后者,因为在读取时如果输出之后有回车就会报错,是因为我在读入的时候运用了next()。会产生一些错误。在文件的使用时还需要在方法后加上throws IOException语句抛出的声明。
文件的读取:Scanner in = new Scanner(Paths.get("XXXXX"));;打开文件,运用while(in.hasNextLine())来进行遍历文件结尾,其中的数据我采用in.next()或in.nextLine()来进行读取,两者的区别:前者是遇到空格回车就会停止,输入的是一个类型,后者则是输入的一整行数据,换句话说就是可以带有空格的字符串。 在文件读取之后,我创建了对象数组,是一个大小为100的类数组,判断它其中是否为null来进行相应的选择。
do { Atm.display(); switch(Atm.getoperatetye()) { case 1:{ //存款 a[j].add(); a[j].addfinsh(); str[ti]=a[j].getoperate(); money[ti]=a[j].getamount(); a[j].setamount(0); };break; case 2:{ //取款 f=a[j].withdraw(); if(f) { a[j].withdrawfinsh(); str[ti]=a[j].getoperate(); money[ti]=a[j].getamount(); a[j].setamount(0); } };break; case 3:{ //转账 f=a[j].zhuan(a,len); if(f) { str[ti]=a[j].getoperate(); money[ti]=a[j].getamount(); a[j].setamount(0); } };break; case 4:{ //修改密码 a[j].changepassword(); str[ti]=a[j].getoperate(); };break; case 5:a[j].showbalace();break; //查询余额 case 6:{ //查询记录 Atm.outlist(st, str,money); Atm.inlist(); } } if(Atm.getoperatetye()!=7&&Atm.getoperatetye()!=6) //将操作记录先保存到数组中,在录入到文件中去 { st[ti]=Atm.getoperatetye(); ti++; } }while(Atm.getoperatetye()!=7);
对于各种操作我选择运用do-while循环加上switch语句来进行选择操作。每种操作之后都有一些数组的赋值,来记录操作类型,时间以及金额。相应的操作我选择了运用布尔类型来判断是否成功,以及相应的输出界面,存款比较简单我将流水金额设置成一个含有参数的方法,在调用时对其调用,在每个操作结束将其的值设为零。
void settime() { //自动读取操作时的系统日期 Date date = new Date(); String year = String.format("%tY", date); String month = String.format("%tB", date); String day = String.format("%te", date); setoperate(year+"-"+month+"-"+day); }
在设置时间的时候我查阅了网上的资料对时间进行赋值并储存在一个数组中,这个数组与操作类型的数组还有存储流水金额的数组一一对应,达到匹配的效果。在转账、取款等操作时都会有判断判断用户余额是否充足。在不足的时候将返回选择操作界面。
System.out.println("请确认是否向"+a.getname().replaceFirst(".", "*")+"转账"+k+"元。");
对于字符串替换我从网上查阅到s.repalace(“.”,“#”);s.replaceAll(".","#");s.replaceFirst(".","#")三种字符穿替换语句。第一个试将字符串中所有的“.”替换成“#”第二个则是将字符串中所有的字符替换成“#”;最后一个则是将字符串中第一个字符替换成“#”,在本程序中我运用的是最后一中将用户的姓名第一个字进行替换。
String s; String [] str; str =new String [100]; //用于存储操作记录的时间 int i = 0,j=0,len,ti=0; int [] st; //存储操作类型记录到文件中 int [] money; //存储操作金额记录到文件中 money=new int [100]; st =new int [100];
对于用户操作记录的几觉方法我选择的是建立几个不同类型的数组来进行存储相应的操作类型,操作时间,操作金额,并且输入到文件中去,在现实的时候将文件类型进行修改改成对应的操作,来输出。
以下是这个程序的完整的源代码:
/* * 这是Main方法的源代码 * 信1705-1班 * 20173425 陈欢 * */ import java.io.IOException; import java.util.*; import java.util.ArrayList; public class Main { public static void main(String[] args) throws IOException { Scanner reader=new Scanner (System.in); String s; String [] str; str =new String [100]; //用于存储操作记录的时间 int i = 0,j=0,len,ti=0; int [] st; //存储操作类型记录到文件中 int [] money; //存储操作金额记录到文件中 money=new int [100]; st =new int [100]; for(i=0;i<100;i++) { st[i]=-1; } boolean k=false,f=false,m=false; AccountManger Atm; AccountManger [] a; a=new AccountManger[100]; Atm=new AccountManger(); Atm.infile(a); len=a.length; System.out.println(len); for(i=0;i<len;i++) { if(a[i]!=null)System.out.println(a[i].getID()+" "+a[i].getname()+" "+a[i].getpassword()+" "+a[i].getbalance()); } k=Atm.judgeID(a,len); j=Atm.getit(); if(k) { do { Atm.display(); switch(Atm.getoperatetye()) { case 1:{ //存款 a[j].add(); a[j].addfinsh(); str[ti]=a[j].getoperate(); money[ti]=a[j].getamount(); a[j].setamount(0); };break; case 2:{ //取款 f=a[j].withdraw(); if(f) { a[j].withdrawfinsh(); str[ti]=a[j].getoperate(); money[ti]=a[j].getamount(); a[j].setamount(0); } };break; case 3:{ //转账 f=a[j].zhuan(a,len); if(f) { str[ti]=a[j].getoperate(); money[ti]=a[j].getamount(); a[j].setamount(0); } };break; case 4:{ //修改密码 a[j].changepassword(); str[ti]=a[j].getoperate(); };break; case 5:a[j].showbalace();break; //查询余额 case 6:{ //查询记录 Atm.outlist(st, str,money); Atm.inlist(); } } if(Atm.getoperatetye()!=7&&Atm.getoperatetye()!=6) //将操作记录先保存到数组中,在录入到文件中去 { st[ti]=Atm.getoperatetye(); ti++; } }while(Atm.getoperatetye()!=7); } else k=Atm.judgeID(a,len); Atm.outfile(a,len); //结束程序的时候将数组中的用户信息进行更新 Atm.outlist(st, str,money); //结束程序时将数组中的操作记录进行更新 } }
/* * 这是ATM相关类的源代码 * 信1705-1班 * 20173425 陈欢 * */ import java.io.IOException; import java.io.PrintWriter; import java.nio.file.Paths; import java.util.Date; import java.util.Scanner; public class Account { private String accountID; //账号 private String accountname; //账户名称 private String operate; //储存操作时间 private int operatetype; //操作类型 private String accountpassword; //用户密码 private int accountbalance; //账户余额 private int amount; //操作流水金额 private static int it=0; //用于记录第几个对象进行操作 /* * 各个数据的设置以及读取*/ String getID() { return accountID; } String getname() { return accountname; } String getoperate() { return operate; } int getoperatetye() { return operatetype; } String getpassword() { return accountpassword; } int getbalance() { return accountbalance; } int getamount() { return amount; } int getit() { return it; } void setID(String s) { accountID = s; } void setname(String s) { accountname =s; } void setoperate(String s) { operate =s; } void setoperatetype() { Scanner reader=new Scanner (System.in); operatetype=reader.nextInt(); } void setpassword(String s) { accountpassword =s; } void setbalance(int s) { accountbalance=s; } void setamount(int i) { amount=i; } void setit(int k) { it=k; } void outfile(AccountManger a[],int len) throws IOException { //将数组中的对相关信息进行输入到文件中 PrintWriter out = new PrintWriter("accountinformation.txt"); int i=0; for(i=0;i<len-1;i++) { if(a[i]!=null)out.print(a[i].getID()+" "+a[i].getname()+" "+a[i].getpassword()+" "+a[i].getbalance()); if(a[i+1]!=null)out.print(" "); } out.flush(); out.close(); } void outlist(int st[], String str[], int[] money)throws IOException { //将数组中的操作信息输入到文件中 PrintWriter out = new PrintWriter("accountlist.txt"); int j=0; do { out.print(st[j]+" "+str[j]+" "+money[j]); if(st[j+1]!=-1)out.print(" "); j++; }while(st[j]!=-1); out.flush(); out.close(); } void inlist() throws IOException{ //对文件中的操作记录进行翻译输出 Scanner in = new Scanner(Paths.get("accountlist.txt")); int i=1; while(in.hasNextLine()) { int data1=in.nextInt(); String data2 = in.next(); int data3=in.nextInt(); System.out.print(i+"、"); switch(data1) { case 1:System.out.println("操作时间:"+data2+"操作类型;存款 操作金额:"+data3);break; case 2:System.out.println("操作时间:"+data2+"操作类型;取款 操作金额:"+data3);break; case 3:System.out.println("操作时间:"+data2+"操作类型;转账 操作金额:"+data3);break; case 4:System.out.println("操作时间:"+data2+"操作类型;修改密码 ");break; default: } i++; } } void settime() { //自动读取操作时的系统日期 Date date = new Date(); String year = String.format("%tY", date); String month = String.format("%tB", date); String day = String.format("%te", date); setoperate(year+"-"+month+"-"+day); } void infile(AccountManger a[])throws IOException{ //对文件中的用户信息进行分类录入 Scanner in = new Scanner(Paths.get("accountinformation.txt")); int i=0; while(in.hasNextLine()) { a[i]=new AccountManger(); String data1 = in.next(); String data2 = in.next(); String data3 = in.next(); int data4=in.nextInt(); a[i].setID(data1); a[i].setname(data2); a[i].setpassword(data3); a[i].setbalance(data4); i++; } in.close(); } Account(){ //对象的初始化 accountID=null; accountname=null; operate=null; operatetype=0; accountpassword=null; accountbalance=0; amount=0; } } class AccountManger extends Account { AccountManger(){ super(); } boolean judgeID(AccountManger a[],int i) { //判断账户信息及相应界面 String s; boolean j=false,k=false; Scanner reader=new Scanner (System.in); System.out.println("*******************************"); System.out.println("欢迎使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("请输入你的账号:"); s=reader.next(); for(int m=0;m<i;m++) { if(a[m]!=null&&s.compareTo(a[m].getID())==0) { j=a[m].judgepassword();k=true; setit(m); } } if(k==false)System.out.println("您输入的账号不存在"); return j; } boolean judgepassword() { //对相应的账户进行判断密码操作及界面 int j=0,i=0; String s; Scanner reader=new Scanner (System.in); System.out.println("*******************************"); System.out.println("欢迎"+getname()+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("请输入你的密码:"); do { s=reader.next(); if(s.compareTo(getpassword())==0) {i=1;break;} else { j++; System.out.println("您输入的密码不正确请重新输入"); } }while(j<3); if(i==1)return true; else {System.out.println("您输入的密码错误超过3次,系统跳转至主界面");return false;} } public void display() { //用户选择操作类型的主界面 System.out.println("*******************************"); System.out.println("欢迎使用中国工商银行自动柜员系统"); 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("6、查询记录;"); System.out.println("7、退卡;"); System.out.println("*******************************"); System.out.println("请输入你要操作的编号:"); setoperatetype(); } void add() { //存款及相应界面 int k,i; Scanner reader=new Scanner (System.in); System.out.println("*******************************"); System.out.println("欢迎"+getname()+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("请输入存款金额:"); i=reader.nextInt(); setamount(i); k=getbalance()+getamount(); setbalance(k); settime(); } void addfinsh() { //判断存款成功输出界面 System.out.println("*******************************"); System.out.println("欢迎"+getname()+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("当前账户存款操作成功。"); System.out.println("当前账户余额:"+getbalance()); } void showbalace() { //查询余额界面 System.out.println("您的余额为:"+getbalance()); } public boolean withdraw() { //取款及界面 int k,i,j; boolean m=false; Scanner reader=new Scanner (System.in); System.out.println("*******************************"); System.out.println("欢迎"+getname()+"使用中国工商银行自动柜员系统"); 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、返回"); i=reader.nextInt(); switch(i) { case 1:{ setamount(100); };break; case 2:{ setamount(500); };break; case 3:{ setamount(1000); };break; case 4:{ setamount(1500); };break; case 5:{ setamount(2000); };break; case 6:{ setamount(5000); };break; case 7:{ System.out.println("*******************************"); System.out.println("欢迎"+getname()+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("请输入取款金额"); j=reader.nextInt(); setamount(j); };break; case 8:System.exit(1);break; case 9:break; } if(getbalance()>=getamount()) { k=getbalance()-getamount(); setbalance(k); settime(); } else {System.out.println("账户余额不足");return false;} return true; } void withdrawfinsh() { //判断取款成功及相应界面安 System.out.println("*******************************"); System.out.println("欢迎"+getname()+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("当前账户取款操作成功。"); System.out.println("当前账户余额:"+getbalance()); } public void changepassword() { //修改密码界面 String s,s1,s2; Scanner reader=new Scanner (System.in); System.out.println("*******************************"); System.out.println("欢迎"+getname()+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("请输入当前密码:"); s=reader.next(); if(s.compareTo(getpassword())==0) { do { System.out.println("请输入新密码:"); s1=reader.next(); System.out.println("请再次输入新密码:"); s2=reader.next(); if(s1.compareTo(s2)!=0)System.out.println("两次密码不一致!请重新输入:"); }while(s1.compareTo(s2)!=0); setpassword(s1); System.out.println("*******************************"); System.out.println("欢迎"+getname()+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("当前账户密码修改成功!"); settime(); } } public boolean zhuan(AccountManger[] a,int i) { //判断转账对象账户 String s; boolean k=false; Scanner reader=new Scanner (System.in); System.out.println("*******************************"); System.out.println("欢迎"+getname()+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("请输入转账账户:"); s=reader.next(); for(int m=0;m<i;m++) { if(a[m]!=null&&s.compareTo(a[m].getID())==0&&m!=a[m].getit()) { k=a[getit()].zhuangmoney(a[m]); settime(); } } return k; } public boolean zhuangmoney(AccountManger a) { //输入转账金额 int k; Scanner reader=new Scanner (System.in); System.out.println("*******************************"); System.out.println("欢迎"+getname()+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("请输入转账金额:"); k=reader.nextInt(); setamount(k); System.out.println("*******************************"); System.out.println("欢迎"+getname()+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("请确认是否向"+a.getname().replaceFirst(".", "*")+"转账"+k+"元。"); System.out.println("单击“N”表示不确认转账,单击“Y”表示确定转账"); String s=reader.next(); if(s.compareTo("Y")==0&&getbalance()>=getamount()) { //确定是否转账 k=getbalance()-getamount(); setbalance(k); a.setbalance(a.getbalance()+getamount()); System.out.println("*******************************"); System.out.println("欢迎"+getname()+"使用中国工商银行自动柜员系统"); System.out.println("*******************************"); System.out.println("当前账户向"+a.getname().replaceFirst(".", "*")+"成功转账"+getamount()+"元。"); System.out.println("当前账户余额为:"+getbalance()); } else if(getbalance()<getamount()){System.out.println("账户余额不足");return false;} else return false; return true; } }