java开学考试有感以及源码

一.感想

Java开学测试有感

九月二十号,王老师给我们上的第一节java课,测试。

说实话,不能说是十分有自信,但还好,直到看见了开学测试的题目,之前因为已经做过了王老师发的16级的题目,所以当时还是有些底气的。遇到题目的时候是的确蒙了。因为小学期已经做过学生的通讯录。所以16级的试题显得好做一些,考试前学委说可能老师会加上文件操作,所以自己也相应的添加了文件的读取和写入操作,但是王老师显然是老江湖,觉得这些题目还不够有挑战性,换成了现在的ATM程序。

变量的定义,按照老师的说法就是送分,的确,这算是基本的操作了,不过令人尴尬的是,我在用eclipse的对成员变量进行构造set和get方法时也出现了问题。但是在后来的编程过程当中,这个问题也是无缘无故地消失了,我也是莫名其妙。

整个程序基本上可以说是建立在文件的基础上的,除了开头的操作,大多都要用到文件功能。

再说具体的功能,第一个账号的辨别,现在对我来说,这个功能的实现已经很简单了,但是当时确实花了不少的时间去解决这个问题,导致当时的进度一直无法推进,因为自己的文件功能还算比较陌生的,识别账号的关键就是读取文件之后对于没一行的数据进行遍历,对比,从而来达到这个功能。这个地方对比的时候,必须要注意不可以直接用==来对比,因为两者的地址不同,对比的结果肯定是不一样的,可以用equal方法来解决这个问题。

对于密码的功能的实现,我是在之前账号的基础上,对账号的下一行数据保存到对象的密码成员数据中,然后在输入密码时和密码进行对比。

目前程序已经完成,所有的功能大致已经都可以很好的实现,但是还有地方有欠缺,就是账号和密码这连个功能是分开实现的,没有连成一个整体,也就是说,账号功能无论是否完成都会进行密码阶段,我个人觉得这连个功能和之后的五个功能都要在一个大的while循环中完成就可以解决问题,或者是思考如何返回界面这个问题,当然我也觉得这个问题是本程序的难点之一,可惜自己现在还没有完成。

存款等功能是在while循环中做出来的,正整数可以用除余的方法来实现。下面的几个程序都能够很正常的实现。我觉得大多数同学也觉得这个地方比较简单,最重要问题的就是文件的读取和写入。

在修改密码的功能中就需要文件的写入,因为我在密码功能实现的时候是把账号的下一行数值当做密码赋值给成员数据的,所以在写入新密码的时候也需要将新密码写在账号的后一行,这样才能确保程序下一次运行时密码和账号匹配。所以我的解决方案是先将文件中的数据读到一个String数组当中,然后再次覆盖写入,在覆盖写入的时候对写入的进行辨别,如果是当前账号,就在账号后面先插入修改完成的密码,之后再将剩下的数据依次插入即可完成。

最后要求完成的功能就是对操作过程的记录,这个即使简单的对文件的写入,唯一的尴尬就是我没有办法实现排序,即不能1.2.这样。。。。

到这里,所有功能的讲解已全部结束,以上讲解了我的解决方案还有自己程序的不足之处。

很显然,自己的这次开学考试并不理想,从ATM这个程序已经看出了自己的不住,以及需要改进之处。文件的读写操作需要联系,自己还是太生疏,还有一些小的地方还需改进和完善。在软件工程专业,我还有很长的路要走,也感谢老师,让我明白自己的编程技术是多么的菜。

在查看别人的博客园资料时,看到别人引用了老子的话,觉得很中二,但是也感觉有那么点认同:道可道,非常道;名可名,非常名。每个人都需要领悟,不管是程序还是人生,我现在还是有些迷茫。对人生还是没有什么太大的认识,我希望未来我也可以领悟。。。。

二.源码

  1 //信1705-3  20173579  陈阳
  2 package atm0;
  3 import java.io.IOException;
  4 import java.util.*;
  5 public class mynode {
  6 
  7     public static void main(String[] args) throws Exception {
  8         // TODO Auto-generated method stub
  9         Scanner in =new Scanner(System.in);
 10         jiemian dang0=new jiemian();
 11         int count=0;
 12         atmbianliang dang1=new atmbianliang();
 13         atmbianliang dang2=new atmbianliang();
 14         while(true) 
 15         {
 16             dang0.jiemiankaishi();
 17             String Id=in.nextLine();
 18             dang1.setAccountID(Id);
 19             dang1.bianbie();
 20             if(dang1.bianbie()==true)
 21             {
 22                 dang1.bianbie2();
 23                 break;
 24             }
 25             count++;
 26             if(count>2)
 27             {
 28                 System.out.println("该账号三次录入错误,该卡已被系统没收,请与工行及时联系处理");
 29                 break;
 30             }
 31         }
 32         while(true)
 33         {
 34             dang0.shumima();
 35             String shupassword=in.nextLine();
 36             if(dang1.bianbiemima1(shupassword)==true)
 37             {
 38                 String password=dang1.getAccountpassword();
 39                 if((password.equals(shupassword)))
 40                     break;
 41             }
 42             System.out.println("密码输入错误,请重新输入");
 43             count++;
 44             if(count>2)
 45             {
 46                 System.out.println("该账号三次录入密码错误,该卡已被系统没收,请与工行及时联系处理");
 47                 break;
 48             }
 49         }
 50         String xuanze1="1";
 51         String xuanze2="2";
 52         String xuanze3="3";
 53         String xuanze4="4";
 54         String xuanze5="5";
 55         String xuanze6="q";
 56         String xuanze;
 57 
 58         while(true)
 59         {
 60             dang1.shuzijiemian();
 61             xuanze=in.nextLine();
 62             if((xuanze.equals(xuanze1)))
 63             {
 64                 dang1.cunkuan();
 65                 int cunkuanshuzhi=in.nextInt();
 66                 if(cunkuanshuzhi%100!=0)
 67                 {
 68                     System.out.println("输入金额有误,返回操作界面");
 69                 }
 70                 else
 71                 {
 72                     dang1.cunkuanshezhi(cunkuanshuzhi);
 73                     dang1.cunkuanchengongjiemian();
 74                 }
 75             }
 76             else if((xuanze.equals(xuanze2)))
 77             {
 78                 dang1.qukuanjiemian();
 79                 int qukuanxuanxiang=in.nextInt();
 80                 switch(qukuanxuanxiang)
 81                 {
 82                 case 1:
 83                 {
 84                     dang1.bianbieluru(100);
 85                 }break;
 86                 case 2:
 87                 {
 88                     dang1.bianbieluru(500);
 89                 }break;
 90                 case 3:
 91                 {
 92                     dang1.bianbieluru(1000);
 93                 }break;
 94                 case 4:
 95                 {
 96                     dang1.bianbieluru(1500);
 97                 }break;
 98                 case 5:
 99                 {
100                     dang1.bianbieluru(2000);
101                 }break;
102                 case 6:
103                 {
104                     dang1.bianbieluru(5000);
105                 }break;
106                 case 7:
107                 {
108                     System.out.print("请输入其他金额:");
109                     int luruyue=in.nextInt();
110                     dang1.bianbieluru(luruyue);
111                 }break;
112                 case 8:
113                 {
114                     System.out.println("感谢使用!");
115                     System.exit(0);
116                 }break;
117                 case 9:
118                 {
119                     break;
120                 }
121                 }
122             }
123             else if((xuanze.equals(xuanze3)))
124             {
125                 dang1.zhuanzhangjiemian();
126                 String zhuanzhangzhanghu=in.nextLine();
127                 dang1.zhuanzhangzhanghupanding(zhuanzhangzhanghu);      
128                 dang2.bianbie();
129                 if(dang2.bianbie()==true)
130                 {
131                     dang2.bianbie2();
132                     dang1.zhuanzhangjinejiemian();
133                     int zhuanzhangjine=in.nextInt();
134                     if(dang1.zhuanzhangbianbie(zhuanzhangjine, zhuanzhangzhanghu)==true)
135                         dang2.zhnaghu2zenyue(zhuanzhangjine);
136                 }
137             }
138             else if((xuanze.equals(xuanze4)))
139             {
140                 dang1.xiugaimimajiemian();
141                 String shurumima=in.nextLine();
142                 dang1.bianbiemima(shurumima);
143                 dang1.mimacunruwenjian();
144                 dang1.mimachongxinxieru();
145                 
146             }
147             else if((xuanze.equals(xuanze5)))
148             {
149                 dang1.chaxungongneng();
150             }
151             else if((xuanze.equals(xuanze6)))
152             {
153             }
154         }
155         
156 
157     }
158 }
159 
160 
161 
162 //信1705-3  20173579  陈阳
163 package atm0;
164 import java.io.IOException;
165 import java.util.Date;
166 import java.text.SimpleDateFormat;
167 import java.io.Reader;
168 import java.util.*;
169 import java.io.BufferedReader;
170 import java.io.File;
171 import java.io.FileReader;
172 import java.io.FileNotFoundException;
173 import java.io.FileOutputStream;
174 import java.io.FileWriter;
175 import java.io.PrintStream;
176 import java.io.PrintWriter;
177 import java.io.RandomAccessFile;
178 public class atmbianliang {
179     Scanner in =new Scanner(System.in);
180     private static String accountID;
181     private String accountname;
182     private String operatedate;
183     private int operatetype;
184     private static String accountpassword;
185     public String getAccountname() {
186         return accountname;
187     }
188     private int accountbalance;
189     private int amount;
190     static String[] content2=new String[6];
191     public atmbianliang()
192     {
193         accountID=null;
194         accountname=null;
195         operatedate=null;
196         operatetype=0;
197         accountpassword=null;
198         accountbalance=0;
199         amount=0;
200     }
201     public void yue()
202     {
203         System.out.println("***************************************************************");
204         System.out.println("                 欢迎"+accountID+"使用中国工商银行自助柜员系统                            ");
205         System.out.println("***************************************************************");
206         System.out.println("                       当前账户存款操作成功。                            ");
207         System.out.print("                       当前账户余额为:");
208         System.out.print(accountbalance);
209         System.out.print("元");
210         System.out.println("***************************************************************");
211         
212     }
213     public  static String getAccountID() {
214         return accountID;
215     }
216     public void setAccountID(String accountID) {
217         this.accountID = accountID;
218     }
219 
220 
221     public String getOperatedate() {
222         return operatedate;
223     }
224     public void setOperatedate(String operatedate) {
225         this.operatedate = operatedate;
226     }
227     public int getOperatetype() {
228         return operatetype;
229     }
230     public void setOperatetype(int operatetype) {
231         this.operatetype = operatetype;
232     }
233     public String getAccountpassword() {
234         return accountpassword;
235     }
236     public void setAccountpassword(String accountpassword) {
237         this.accountpassword = accountpassword;
238     }
239     public int getAccountbalance() {
240         return accountbalance;
241     }
242     public void setAccountbalance(int accountbalance) {
243         this.accountbalance = accountbalance;
244     }
245     public int getAmount() {
246         return amount;
247     }
248     public void setAmount(int amount) {
249         this.amount = amount;
250     }
251     public boolean bianbie()throws IOException                                 //辨别账户位数
252     {
253         if(accountID.length()!=8)
254         {
255             System.out.println("该卡不是工行卡");
256             return false;
257         }
258         else
259             return true;
260     }
261     public boolean bianbiemima1(String ps)throws IOException                                //辨别密码位数
262     {
263         if(ps.length()!=6)
264         {
265             System.out.println("密码格式错误");
266             return false;
267         }
268         else
269             return true;
270     }
271     public void bianbie2()throws Exception                                      //辨别账户信息
272      {
273             String fileContent = readFileContent("D:/accountinformation.txt");
274             System.out.println(fileContent);
275          }
276         private static String readFileContent(String fileName) throws IOException {
277          File file = new File(fileName);
278          BufferedReader bf = new BufferedReader(new FileReader(file));
279          String content = "";
280          StringBuilder sb = new StringBuilder();
281          String Idjia1=atmbianliang.getAccountID();
282          while(content != null){
283          content = bf.readLine();
284          if((Idjia1.equals(content)))
285          {
286              System.out.println("账户正确");
287              String zhanghumima=bf.readLine();
288              accountpassword=zhanghumima;
289          }
290          if(content == null){
291              break;
292          }
293 //         sb.append(content.trim());
294          
295          }
296 
297         bf.close();
298         System.out.println("识别账户结束");
299          return sb.toString();
300          }
301         public void bianbie001()throws Exception                                            //辨别转账账户
302         {
303                 String fileContent2 = readFileContent2("D:/accountinformation.txt");
304                  System.out.println(fileContent2);
305         }
306         private static String readFileContent2(String fileName) throws IOException {
307 
308              File file = new File(fileName);
309 
310              BufferedReader bf = new BufferedReader(new FileReader(file));
311 
312              String content = "";
313              StringBuilder sb = new StringBuilder();
314              String Idjia1=atmbianliang.getAccountID();
315              while(content != null){
316              content = bf.readLine();
317              if((Idjia1.equals(content)))
318              {
319                  System.out.println("账户正确");
320                  String zhanghumima=bf.readLine();
321                  accountpassword=zhanghumima;
322              }
323              if(content == null){
324                  break;
325              }
326 //             sb.append(content.trim());
327              
328              }
329 
330             bf.close();
331             System.out.println("识别账户结束");
332              return sb.toString();
333              }
334 
335         public void cunkuanshezhi(int a) throws FileNotFoundException
336         {
337             accountbalance=accountbalance+a;
338             cunkuanjilu(a);
339         }
340         public void zhuanzhangzhanghupanding(String c)
341         {
342             if((c.equals(accountID)))
343             {
344                 System.out.println("账户不能是自己");
345             }
346             else
347                 return;
348         }
349         public void bianbieluru(int b)throws FileNotFoundException 
350         {
351             if(accountbalance>=b)
352             {
353                 accountbalance=accountbalance-b;
354                 System.out.println("***************************************************************");
355                 System.out.println("                 欢迎"+accountID+"使用中国工商银行自助柜员系统                            ");
356                 System.out.println("***************************************************************");
357                 System.out.println("                          当前账户取款"+b+"元操作成功                            ");
358                 System.out.println("                 当前账户余额为:"+accountbalance+"元                            ");
359                 System.out.println("***************************************************************");
360                 qukuanjilu(b);
361             }
362                 
363             else
364                  System.out.println("账户余额不足");
365         }
366         public void qukuanjilu(int a)throws FileNotFoundException                   //取款记录写入
367         {
368             String filePath = "D:/accountlist.txt";
369             SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
370              try
371              {        
372                  File file = new File(filePath);
373                  PrintStream qk = new PrintStream(new FileOutputStream(file));
374                  qk.append("操作日期"+df.format(new Date())+"取款  "+a+"元");
375              }catch(FileNotFoundException e)
376              {
377                  e.printStackTrace();
378              }
379         }
380         public boolean zhuanzhangbianbie(int a,String b)throws FileNotFoundException
381         {
382             if(accountbalance>=a)
383             {
384                 System.out.println("***************************************************************");
385                 System.out.println("                 欢迎"+accountID+"使用中国工商银行自助柜员系统                            ");
386                 System.out.println("***************************************************************");
387                 System.out.println("                 请确认是否向"+b+"转账"+a+"元 (Y确认转账,N不转账)               ");
388                 String zhuanzhangqueren=in.nextLine();
389                 if((zhuanzhangqueren.equals("Y")))
390                 {
391                     accountbalance=accountbalance-a;
392                     System.out.println("***************************************************************");
393                     System.out.println("                 欢迎"+accountID+"使用中国工商银行自助柜员系统                            ");
394                     System.out.println("***************************************************************");
395                     System.out.println("                     当前账户向"+b+"成功转账"+a+"元");
396                     System.out.println("                     当前账户余额为"+accountbalance+"元");
397                     System.out.println("***************************************************************");
398                     zhuanzhangjilu(a);
399                     return true;
400                 }
401                 else
402                 {
403                     return false;
404                 }
405                     
406             }
407             return false;
408         }
409         public void zhuanzhangjilu(int a)throws FileNotFoundException
410         {
411             String filePath = "D:/accountlist.txt";
412             SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
413              try
414              {        
415                  File file = new File(filePath);
416                  PrintStream zz = new PrintStream(new FileOutputStream(file));
417                  zz.append("操作日期"+df.format(new Date())+"转账  "+a+"元");
418              }catch(FileNotFoundException e)
419              {
420                  e.printStackTrace();
421              }
422         }
423         public void zhnaghu2zenyue(int zz2)
424         {
425             accountbalance=accountbalance+zz2;
426         }
427         public void bianbiedangqianmima()
428         {
429             String dangqianmima=in.nextLine();
430             if((dangqianmima.equals(accountpassword)))
431                 return;
432             else
433                 System.out.println("当前密码录入错误");
434         }
435         public void bianbiemima(String a)
436         {
437             if((a.equals(accountpassword)))
438             {
439                 String xiugaimima=in.nextLine();
440                 String querenmima=in.nextLine();
441                 if((xiugaimima.equals(querenmima)))
442                 {
443                     accountpassword=querenmima;
444                     System.out.println("***************************************************************");
445                     System.out.println("                 欢迎"+accountID+"使用中国工商银行自助柜员系统                            ");
446                     System.out.println("***************************************************************");
447                     System.out.println("                   当前账户密码修改成功                          ");
448                     System.out.println("***************************************************************");
449                     
450                 }
451                 else
452                 {
453                     System.out.println("                修改密码与确认密码不一致            ");
454                     return;
455                 }
456             }
457             else
458             {
459                 System.out.println("                  当前密码录入错误                 ");
460                 return;
461             }    
462         }
463         public void xiugaimimaluru()throws FileNotFoundException
464         {
465             String filePath = "D:/accountlist.txt";
466             SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
467              try
468              {        
469                  File file = new File(filePath);
470                  PrintStream xmm = new PrintStream(new FileOutputStream(file));
471                  xmm.append("操作日期"+df.format(new Date())+"  修改密码");
472              }catch(FileNotFoundException e)
473              {
474                  e.printStackTrace();
475              }
476         }
477         public void mimacunruwenjian()throws Exception                                   //将文件中的数据写出
478         {
479             String fileContent3 = readFileContent3("D:/accountinformation.txt");
480             System.out.println(fileContent3);
481         }
482         private static String readFileContent3(String fileName) throws IOException {
483              File file = new File(fileName);
484              BufferedReader bf = new BufferedReader(new FileReader(file)); 
485              StringBuilder sb = new StringBuilder();
486              int zzi=0;
487              while(zzi<6) {
488              content2[zzi] = bf.readLine();
489              zzi++;
490 
491              }
492             bf.close();
493              return sb.toString();
494              }
495         public void mimachongxinxieru() throws FileNotFoundException                                     //再将密码写入文件
496         {
497              int i=0;
498              String filePath = "D:/accountinformation.txt";
499              try
500              {        
501                  File file = new File(filePath);
502                  PrintStream ps = new PrintStream(new FileOutputStream(file));
503                  while(i<6)
504                  {
505                      
506                       ps.println(content2[i]);
507                      if((content2[i].equals(accountID)))
508                      ps.println(accountpassword);
509                       i++;
510                  }
511 
512              }catch(FileNotFoundException e)
513              {
514                  e.printStackTrace();
515              }
516         }
517         public void cunkuanjilu(int a)throws FileNotFoundException                //存款记录记录
518         {
519             String filePath = "D:/accountlist.txt";
520             SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
521             
522              try
523              {        
524                  File file = new File(filePath);
525                  PrintStream ck = new PrintStream(new FileOutputStream(file));
526                  ck.append("操作日期"+df.format(new Date())+"存款  "+a+"元");
527              }catch(FileNotFoundException e)
528              {
529                  e.printStackTrace();
530              }
531             
532         }
533         
534         public void cunkuan()                                                       //存款界面
535          {
536                 System.out.println("***************************************************************");
537                 System.out.println("                 欢迎"+accountID+"使用中国工商银行自助柜员系统 ");
538                 System.out.println("***************************************************************");
539                 System.out.println("                 请输入存款金额;                            ");
540                 System.out.println("***************************************************************");
541          }
542         public void shuzijiemian()
543             {
544                  System.out.println("***************************************************************");
545                 System.out.print("                 欢迎");
546                 System.out.print(accountID);
547                 System.out.println("使用中国工商银行自助柜员系统                            ");
548                 System.out.println("***************************************************************");
549                 System.out.println("                       1.存款                            ");
550                 System.out.println("                       2.取款                            ");
551                 System.out.println("                       3.转账汇款                            ");
552                 System.out.println("                       4.修改密码                            ");
553                 System.out.println("                       5.查询余额                            ");
554                 System.out.println("***************************************************************");
555             }
556         public void cunkuanchengongjiemian()
557         {
558             System.out.println("***************************************************************");
559             System.out.println("                 欢迎"+accountID+"使用中国工商银行自助柜员系统                            ");
560             System.out.println("***************************************************************");
561             System.out.println("                          当前账户存款操作成功                            ");
562             System.out.println("                 当前账户余额为:"+accountbalance+"元                            ");
563             System.out.println("***************************************************************");
564         }
565         public void qukuanjiemian()
566         {
567             System.out.println("***************************************************************");
568             System.out.println("                 欢迎"+accountID+"使用中国工商银行自助柜员系统                            ");
569             System.out.println("***************************************************************");
570             System.out.println("                       当前账户每日可以支取2万元                            ");
571             System.out.println("                       1、100元                            ");
572             System.out.println("                       2、500元                            ");
573             System.out.println("                       3、1000元                            ");
574             System.out.println("                       4、1500元                            ");
575             System.out.println("                       5、2000元                            ");
576             System.out.println("                       6、5000元                            ");
577             System.out.println("                       7、其他金额                            ");
578             System.out.println("                       8、退卡                            ");
579             System.out.println("                       9、返回                            ");
580             System.out.println("***************************************************************");
581         }
582         public void zhuanzhangjiemian()
583         {
584             System.out.println("***************************************************************");
585             System.out.println("                 欢迎"+accountID+"使用中国工商银行自助柜员系统                            ");
586             System.out.println("***************************************************************");
587             System.out.println("                       请输入转账账户                            ");
588             System.out.println("***************************************************************");
589         }
590         public void zhuanzhangjinejiemian()
591         {
592             System.out.println("***************************************************************");
593             System.out.println("                 欢迎"+accountID+"使用中国工商银行自助柜员系统                            ");
594             System.out.println("***************************************************************");
595             System.out.println("                       请输入转账金额                            ");
596             System.out.println("***************************************************************");
597         }
598         public void xiugaimimajiemian()
599         {
600             System.out.println("***************************************************************");
601             System.out.println("                 欢迎"+accountID+"使用中国工商银行自助柜员系统                            ");
602             System.out.println("***************************************************************");
603             System.out.println("                       请输入当前密码:                        ");
604             System.out.println("                       请输入修改密码:                        ");
605             System.out.println("                       请输入确认密码:                        ");
606             System.out.println("***************************************************************");
607         }
608         public void chaxungongneng()
609         {
610             System.out.println("***************************************************************");
611             System.out.println("                 欢迎"+accountID+"使用中国工商银行自助柜员系统                            ");
612             System.out.println("***************************************************************");
613             System.out.println("                       当前账户余额为:"+accountbalance+"元           ");
614             System.out.println("                       账户清单信息为:                     ");
615             System.out.println("***************************************************************");
616             
617             
618         }
619 }
620 
621 
622 
623 
624 package atm0;
625 public class jiemian {
626     public void jiemiankaishi()
627     {
628             System.out.println("***************************************************************");
629             System.out.println("                 欢迎使用中国工商银行自动柜员系统                            ");
630             System.out.println("***************************************************************");
631             System.out.println("                        请输入您的账号:                            ");
632             System.out.println("***************************************************************");
633     }
634     public void shumima()
635     {
636             System.out.println("***************************************************************");
637             System.out.println("                 欢迎使用中国工商银行自动柜员系统                            ");
638             System.out.println("***************************************************************");
639             System.out.println("                        请输入您的密码:                            ");
640             System.out.println("***************************************************************");
641     }
642 }

 

原文地址:https://www.cnblogs.com/heiyang/p/9694686.html