9月20的周四的Java课堂第一节课上就是有关于实现ATM的考试内容,在实现的过程中我了解到自己本身还是有很多的不足之处,例如在实现工程方面的相似性上面还有些许就的欠缺,再者就是回宿舍拿电源的原因导致了我再最后写完全没有经过测试之后就直接交上去给师兄检查成绩,最后的成绩不算是特别理想,也许就是由于这个方面的原因导致了我在3个小时的时间内不能够完成这个ATM基本操作的全部功能,只能写到大概仅仅八成左右的功能实现,不过在3个小时之后再回宿舍经过半个小时的修改之后,能够实现完全的功能。当然在实现功能的时候还是有一些小激动的,这一方面表明了我写代码的速度还需要再提高,另一方面就表示我还需要继续努力,这是一门之前我没有接触过的语言,这次的测试过程中我始终感觉到有一种莫名其妙的紧张的感觉,大概是由于有考试的这个原因吧,这在之前的考试之中都是没有的,在大一的时候更多的是纸卷上的考试,但是次的考试让我觉得,我还需要再实践的方面上有所历练才行。
上面是我对那第一堂测试的感悟。代码中出现的问题,大概就是存钱的时候,文档在判断行数的时候忘记减去一行,导致在存钱的时候存到了下一个人的账户里面,我也是意想不到的,本来是直接想着想着就写到了那里,还有就是转账汇款的那一项, 由于没有经过测试的原因,导致了我在转账汇款的时候,再设置接收人的余额的这个功能里面写成了设置接收人密码的这个函数,这应该是粗心大意所导致的,我应该还要继续努力,在写代码的时候继续努力,继续提高自己的专注能力,防止错误的再次出现,这些都说明了我应该继续历练自己,在实践中继续了解编程之中可能出现的问题,然后逐渐增强自己的能力,不然在以后的竞争之后我不可能超越自己,使自己成为一个优秀的人。
可能吧,在以后的学习生涯中,不仅要和别人竞争,更重要的是要超越自己本身,学无止境这是一种原因,但语言本身博大精深,我认为这是一门不可能精通的语言,只能接近熟悉,在这一个信息飞逝流淌的时代,代码更新换代的速度是飞快的,而且每一次更新换代都基本上要求我们重新掌握那些新的东西,这在我现在这个年龄的阶段尚且还是有可能的去学习的,但是想想到了以后的时间,自己不再有足够的精力去学习,那个时候,我们凭的是我们的经验,在于那个方面的经验吧。
所以我贴上了我第一个实现的Java的程序的测试图。
这是查看流水账的功能,从文件accountlist.txt中读取所有信息,然后判断是不是用户的操作,如果是用户的操作,那么输出用户的信息,而且每天用户的流水金额设置为0
每次的登录界面会从另一个文件accountinformation.txt中读取信息,然后判断是不是有该用户的信息,如果有的话,就显示密码输入界面,就如图一所示
这是用户信息的管理界面,第一项是用户的账号,第二项是用户的名字,第三项是用户的密码,第四项目是用户的账户余额
其他的功能就不加以描述了...
ATM的实现的代码如下:
Account.java文件中:
package account; public class Account { private String accountID; private String accountname; private String operatedate; private int operatetype; private int accountpassword; private int accountbalance; private int amount; public String getAccountID() { return accountID; } public void setAccountID(String accountID) { this.accountID = accountID; } public String getAccountname() { return accountname; } public void setAccountname(String accountname) { this.accountname = accountname; } public String getOperatedate() { return operatedate; } public void setOperatedate(String operatedate) { this.operatedate = operatedate; } public int getOperatetype() { return operatetype; } public void setOperatetype(int operatetype) { this.operatetype = operatetype; } public int getAccountpassword() { return accountpassword; } public void setAccountpassword(int accountpassword) { this.accountpassword = accountpassword; } public int getAccountbalance() { return accountbalance; } public void setAccountbalance(int accountbalance) { this.accountbalance = accountbalance; } public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } public void set(String id,String name,int password,int yuer) { accountID=id; accountname=name; accountpassword=password; accountbalance=yuer; } public void set_1(String data,int liushui,int type) { operatedate=data; operatetype=type; amount=liushui; } }
AccountManager.java文件中:
package account; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.text.SimpleDateFormat; import java.util.Iterator; import java.util.Scanner; import java.util.Vector; import java.util.*; //洪鼎淇 20173627 信1705-3 public class AccountManager { private static Scanner can=new Scanner(System.in); private Vector<Account> map=new Vector<Account>(); private Vector<Account> maplist=new Vector<Account>(); private final String szM[]=new String[]{"存款","取款","转账","修改密码","查询"}; private Iterator<Account> it; private String cnum; private int go; private int index; private int an; private Account me=new Account(); public static void main(String[] args) { AccountManager a=new AccountManager(); try { a.daoru_1(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } a.go=3; a.start(); } public void start() { index=0; an=0; System.out.println("***************************************************************"); System.out.println(" 欢迎使用中国工商银行自动柜员系统"); System.out.println("***************************************************************"); System.out.println(" 请输入您的账号:"); if(can.hasNext()) { cnum=can.next(); } if(cnum.length()==8) { // TODO 自动生成的方法存根 if(isfind(cnum)) { index=lookp(cnum)-1; showpass(); } else { System.out.println(" 没有找到相关账户信息,请重新输入"); start(); } } else { System.out.println(" 输入的账户信息不是八位数,请重新输入"); start(); } } public void showpass() { try { daoru_2(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } System.out.println("***************************************************************"); System.out.println(" 欢迎 "+me.getAccountname()+" 使用中国工商银行自助柜员系统"); System.out.println("***************************************************************"); System.out.println(" 请输入您的密码:"); if(can.hasNext()) { cnum=can.next(); } if(cnum.length()==6) { if(Integer.parseInt(cnum)==me.getAccountpassword()) { insystem(); } else { if(go>0) { System.out.println(" 密码错误,请重新输入,还剩"+go+"次机会重新输入"); go=go-1; showpass(); } else { System.out.println(" 密码错误输入次数过多,返回账号输入菜单"); go=3; start(); } } } else { System.out.println(" 输入的密码信息不是六位数,请重新输入"); showpass(); } } public void insystem() { System.out.println("***************************************************************"); System.out.println(" 欢迎 "+me.getAccountname()+" 使用中国工商银行自助柜员系统"); 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(" q.登录菜单"); int type=0; if(can.hasNextInt()) { type=can.nextInt(); } else { cnum=can.next(); if(cnum.equals("q")) { System.out.println("已经退出系统,返回账号界面"); start(); go=3; return; } else { System.out.println("输入错误,请重新输入"); insystem(); return; } } func(type); } public void func(int type) { if(type==1) { cunkuang(); } else if(type==2) { qukuang(); } else if(type==3) { zhuangzhang(); } else if(type==4) { xiugai(); } else if(type==5) { chaxun(); } } public void zhuangzhang() { System.out.println("***************************************************************"); System.out.println(" 欢迎 "+me.getAccountname()+" 使用中国工商银行自助柜员系统"); System.out.println("***************************************************************"); System.out.println(" 请输入你要转账的账号:"); String type=null; if(can.hasNext()) { type=can.next(); } if(isfind_1(type)) { if((an=lookp(type)-1)!=-1) { jiner(); } } else { System.out.println(" 查无此账号"); System.out.println(" 按q输入回车回到账号输入菜单,按其他任意键回到主菜单 "); if(can.hasNext()) { cnum=can.next(); if(cnum.equals("q")) { System.out.println("已经退出系统,返回账号界面 "); start(); go=3; return; } else insystem(); } } } public void jiner() { int c=0; System.out.println(" 请输入你要转账的金额:"); if(can.hasNextInt()) { c=can.nextInt(); } if(me.getAccountbalance()>=c) { Account ss=new Account(); me.setAccountbalance(me.getAccountbalance()-c); me.setAmount(me.getAmount()-c); me.setOperatetype(3); ss=me; map.get(an).setAccountbalance(map.get(an).getAccountbalance()+c); map.get(an).setAmount(map.get(an).getAmount()+c); map.get(an).setOperatetype(1); map.setElementAt(ss, index); //1 ss.set_1(me.getOperatedate(), me.getAmount(), me.getOperatetype()); ss.setAccountID(me.getAccountID()); maplist.add(ss); try { daochu_2(); daochu_1(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } System.out.println("***************************************************************"); System.out.println(" 欢迎 "+me.getAccountname()+" 使用中国工商银行自助柜员系统"); System.out.println("***************************************************************"); System.out.println(" 当前账户向"+map.get(an).getAccountname()+"成功转账"+c+"元。"); System.out.println(" 当前账户余额为:"+me.getAccountbalance()); System.out.println("*************************************************************** "); insystem(); } else { System.out.println("金额不够,不能转"); zhuangzhang(); } } public void xiugai() { System.out.println("***************************************************************"); System.out.println(" 欢迎 "+me.getAccountname()+" 使用中国工商银行自助柜员系统"); System.out.println("***************************************************************"); System.out.println(" 请输入你原来的密码:"); int type=0,g_old=0,g_new=0; if(can.hasNextInt()) { type=can.nextInt(); } if(type==me.getAccountpassword()) { System.out.println(" 请输入你要修改的密码:"); if(can.hasNextInt()) { g_old=can.nextInt(); } if(!(g_old>=100000&&g_old<=999999)) { System.out.println(" 密码输入格式错误,请重新输入"); xiugai(); return; } System.out.println(" 请再次你输入你要修改的密码:"); if(can.hasNextInt()) { g_new=can.nextInt(); } if(g_new==g_old) { Account ss=new Account(); me.setAccountpassword(g_old); me.setOperatetype(4); ss=me; map.setElementAt(ss, index); //1 ss.set_1(me.getOperatedate(), me.getAmount(), me.getOperatetype()); ss.setAccountID(me.getAccountID()); maplist.add(ss); try { daochu_2(); daochu_1(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } System.out.println("***************************************************************"); System.out.println(" 欢迎 "+me.getAccountname()+" 使用中国工商银行自助柜员系统"); System.out.println("***************************************************************"); System.out.println(" 密码修改成功"); System.out.println("*************************************************************** "); System.out.println(" 按q输入回车回到账号输入菜单,按其他任意键回到主菜单 "); if(can.hasNext()) { cnum=can.next(); if(cnum.equals("q")) { System.out.println("已经退出系统,返回账号界面 "); start(); go=3; return; } else insystem(); } } } } public void chaxun() { me.setOperatetype(5); System.out.println("***************************************************************"); System.out.println(" 欢迎 "+me.getAccountname()+" 使用中国工商银行自助柜员系统"); System.out.println("***************************************************************"); System.out.println(" 当前账户余额为:"+me.getAccountbalance()); System.out.println(" 账号清单信息为:"); it=maplist.iterator(); int i=1; Account st=new Account(); while(it.hasNext()) { st=it.next(); if(st.getAccountID().equals(me.getAccountID())) { System.out.println(" "+i+"、"+st.getOperatedate()+" "+szM[st.getOperatetype()-1]+" "+st.getAmount()); i++; } } if(i==1) System.out.println("用户尚无操作"); System.out.println("*************************************************************** "); Account ss=new Account(); ss.set_1(me.getOperatedate(), me.getAmount(), me.getOperatetype()); ss.setAccountID(me.getAccountID()); //1 maplist.add(ss); try { daochu_2(); daochu_1(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } insystem(); } public void qukuang() { System.out.println("***************************************************************"); System.out.println(" 欢迎 "+me.getAccountname()+" 使用中国工商银行自助柜员系统"); 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.返回"); int type=0; if(can.hasNextInt()) { type=can.nextInt(); } cunkuangi(type); } public void cunkuangi(int type) { boolean g_test=false; Account ss=new Account(); if(type==1) { if(me.getAccountbalance()>=100) { g_test=true; me.setAccountbalance(me.getAccountbalance()-100); me.setAmount(me.getAmount()-100); ss=me; map.setElementAt(ss, index); } } else if(type==2) { if(me.getAccountbalance()>=500) { g_test=true; me.setAccountbalance(me.getAccountbalance()-500); me.setAmount(me.getAmount()-500); ss=me; map.setElementAt(ss, index); } } else if(type==3) { if(me.getAccountbalance()>=1000) { g_test=true; me.setAccountbalance(me.getAccountbalance()-1000); me.setAmount(me.getAmount()-1000); ss=me; map.setElementAt(ss, index); } } else if(type==4) { if(me.getAccountbalance()>=1500) { g_test=true; me.setAccountbalance(me.getAccountbalance()-1500); me.setAmount(me.getAmount()-1500); ss=me; map.setElementAt(ss, index); } } else if(type==5) { if(me.getAccountbalance()>=2000) { g_test=true; me.setAccountbalance(me.getAccountbalance()-2000); me.setAmount(me.getAmount()-2000); ss=me; map.setElementAt(ss, index); } } else if(type==6) { if(me.getAccountbalance()>=5000) { g_test=true; me.setAccountbalance(me.getAccountbalance()-5000); me.setAmount(me.getAmount()-5000); ss=me; map.setElementAt(ss, index); } } else if(type==7) { int num=0; if(can.hasNextInt()) { type=can.nextInt(); } if(me.getAccountbalance()>=num) { g_test=true; me.setAccountbalance(me.getAccountbalance()-num); me.setAmount(me.getAmount()-num); ss=me; map.setElementAt(ss, index); } } else if(type==8) { System.out.println(" 8.退卡成功"); start(); go=3; return; } else if(type==9) { insystem(); return; } if(g_test) { me.setOperatetype(2); ss.setOperatetype(2); System.out.println("***************************************************************"); System.out.println(" 欢迎 "+me.getAccountname()+" 使用中国工商银行自助柜员系统"); System.out.println("***************************************************************"); System.out.println(" 当前账户取款成功"); System.out.println(" 当前账户余额为:"+me.getAccountbalance()); System.out.println("*************************************************************** "); //1 ss.set_1(me.getOperatedate(), me.getAmount(), me.getOperatetype()); ss.setAccountID(me.getAccountID()); maplist.add(ss); try { daochu_2(); daochu_1(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } System.out.println(" 输入q退出到账号菜单,按其他任意键回到主菜单 "); if(can.hasNext()) { cnum=can.next(); if(cnum.equals("q")) { System.out.println("已经退出系统,返回账号界面"); start(); go=3; return; } else insystem(); } } } public void cunkuang() { System.out.println("***************************************************************"); System.out.println(" 欢迎 "+me.getAccountname()+" 使用中国工商银行自助柜员系统"); System.out.println("***************************************************************"); System.out.println(" 请输入存款金额:"); int num=0; if(can.hasNextInt()) { num=can.nextInt(); } else { if(can.hasNext()) { cnum=can.next(); if(cnum.equals("q")) { System.out.println("已经退出系统,返回账号界面"); start(); go=3; return; } else { System.out.println("输入错误,请重新输入"); cunkuang(); } } } if(num>0) { Account ss=new Account(); me.setAccountbalance(me.getAccountbalance()+num); me.setAmount(me.getAmount()+num); ss=me; map.setElementAt(ss, index); qukuangchenggong(); } else { System.out.println("输入错误,请重新输入"); cunkuang(); } } void qukuangchenggong() { me.setOperatetype(1); System.out.println("***************************************************************"); System.out.println(" 欢迎 "+me.getAccountname()+" 使用中国工商银行自助柜员系统"); System.out.println("***************************************************************"); System.out.println(" 当前账户存款成功"); System.out.println(" 当前账户余额为:"+me.getAccountbalance()); System.out.println("*************************************************************** "); System.out.println(" 输入q退出到账号菜单,按其他任意键回到主菜单 "); //1 Account ss=new Account(); ss.set_1(me.getOperatedate(), me.getAmount(), me.getOperatetype()); ss.setAccountID(me.getAccountID()); maplist.add(ss); try { daochu_2(); daochu_1(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } if(can.hasNext()) { cnum=can.next(); if(cnum.equals("q")) { System.out.println("已经退出系统,返回账号界面"); start(); go=3; return; } else insystem(); } } public void daoru_1() throws IOException { File a=new File("accountinformation.txt"); FileInputStream b = new FileInputStream(a); map.clear(); InputStreamReader c=new InputStreamReader(b,"UTF-8"); { BufferedReader bufr =new BufferedReader(c); String line = null; boolean g_tr=false; while((line = bufr.readLine())!=null){ if(!g_tr) { g_tr=true; continue; } Account st=new Account(); String ook[]=line.split(" "); st.set(ook[0],ook[1], Integer.parseInt(ook[2]),Integer.parseInt(ook[3])); map.add(st); } bufr.close(); //System.out.println(sb.toString()); } c.close(); b.close(); } public void daoru_2() throws IOException { File a=new File("accountlist.txt"); FileInputStream b = new FileInputStream(a); InputStreamReader c=new InputStreamReader(b,"UTF-8"); { BufferedReader bufr =new BufferedReader(c); String line = null; maplist.clear(); boolean g_tr=false; String ostr="",temp_str=""; while((line = bufr.readLine())!=null){ if(!g_tr) { g_tr=true; continue; } Account st=new Account(); String ook[]=line.split(" "); st.setAccountID(ook[3]); st.set_1(ook[0],Integer.parseInt(ook[1]),Integer.parseInt(ook[2])); if(me.getAccountID().equals(ook[3])) { ostr=ook[0]; me.setAmount(Integer.parseInt(ook[1])); me.setOperatedate(ook[0]); } maplist.add(st); } Date dt = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); temp_str=sdf.format(dt); if(!ostr.equals(temp_str)) { me.setAmount(0); me.setOperatedate(temp_str); } bufr.close(); } c.close(); b.close(); } public void daochu_1() throws IOException { Account st=new Account(); File a=new File("accountinformation.txt"); FileOutputStream b = new FileOutputStream(a); OutputStreamWriter c=new OutputStreamWriter(b,"UTF-8"); it=map.iterator(); while(it.hasNext()) { c.append(" "); st=it.next(); c.append(st.getAccountID()+" "+st.getAccountname()+" "+st.getAccountpassword()+" "+st.getAccountbalance()); } c.close(); b.close(); } public void daochu_2() throws IOException { File a=new File("accountlist.txt"); FileOutputStream b = new FileOutputStream(a,true); String temp_str=""; Date dt = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); temp_str=sdf.format(dt); OutputStreamWriter c=new OutputStreamWriter(b,"UTF-8"); { c.append(" "); c.append(temp_str+" "+me.getAmount()+" "+me.getOperatetype()+" "+me.getAccountID()); } c.close(); b.close(); } //----------------------------- public int lookp(String xuehao) { Account st=new Account(); int i=0; it=map.iterator(); while(it.hasNext()) { i++; st=it.next(); if(st.getAccountID().equals(xuehao)) break; } return i; } public boolean isfind(String xuehao) { Account st=new Account(); it=map.iterator(); boolean ok=false; while(it.hasNext()) { st=it.next(); if(st.getAccountID().equals(xuehao)) { me=st; ok=true; break; } } return ok; } public boolean isfind_1(String xuehao) { Account st=new Account(); it=map.iterator(); boolean ok=false; while(it.hasNext()) { st=it.next(); if(st.getAccountID().equals(xuehao)) { ok=true; break; } } return ok; } }