TOP

Java初级进阶_面向对象练习

 

  1 import java.io.*;
  2 import java.rmi.AccessException;
  3 
  4 
  5 //txt存储格式   账号,密码,级别,金额,状态
  6 class Rootx {
  7     String username;
  8     String password;
  9     String level;
 10     String money;
 11     String stauts;
 12     BufferedReader input;
 13     String s1 = System.getProperty("user.dir") + "\\user_info.txt";
 14     String s2 = System.getProperty("user.dir") + "\\user_info2.txt";
 15 
 16     //初始化信息
 17     public Rootx(String[] args) throws IOException {
 18         this.username = args[0]; // id
 19         this.password = args[1]; // 密码
 20         this.level = args[2];// 级别
 21         this.money = args[3]; // 金额
 22         this.stauts = args[4]; // 状态
 23 //        System.out.printf("%s %s %s %s %s ", this.username, this.password, this.level, this.money, this.stauts); //展示信息
 24         System.out.println("1: 添加新成员, 2: 冻结账号, 3: 解冻账号, 4: 查询所有用户信息, 5: 查询指定用户信息, 6: 退出系统 ");
 25 
 26         this.input = new BufferedReader(new InputStreamReader(System.in));
 27         String chose = (String) input.readLine();
 28         while (!chose.toUpperCase().equals("6")) {
 29             switch (chose) {
 30                 case "1":
 31                     this.add();
 32                     break;
 33                 case "2":
 34                     this.dongjie();
 35                     break;
 36                 case "3":
 37                     this.jiedong();
 38                     break;
 39                 case "4":
 40                     this.all();
 41                     break;
 42                 case "5":
 43                     this.geren();
 44                     break;
 45                 case "6":
 46                     break;
 47             }
 48             System.out.println("1: 添加新成员, 2: 冻结账号, 3: 解冻账号, 4: 查询所有用户信息, 5: 查询指定用户信息, 6: 退出系统 ");
 49             chose = (String) input.readLine();
 50         }
 51 //        input.close();
 52     }
 53 
 54 
 55     public void add() throws IOException {
 56         File f1 = new File(s1);
 57         System.out.println("输入新账号: ");
 58         BufferedWriter txt2 = new BufferedWriter(new FileWriter(f1, true));
 59         String al = new BufferedReader(new InputStreamReader(System.in)).readLine();
 60         String info = al + ",123,1,5000,1";
 61         txt2.write(info);
 62         try {
 63             txt2.close();
 64         } catch (IOException x) {
 65             System.out.println("关闭报错!!!!");
 66         }
 67         System.out.println(al + " 添加成功! ");
 68 
 69     }
 70 
 71     public void dongjie() throws IOException {
 72         System.out.println("输入冻结的账号: ");
 73         BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
 74         String zhanghao = input.readLine();
 75 
 76         File f1 = new File(s1);
 77         File f2 = new File(s2);
 78         BufferedReader txt1 = new BufferedReader(new FileReader(f1));
 79         BufferedWriter txt2 = new BufferedWriter(new FileWriter(f2));
 80         while (txt1.ready()) {
 81             String al = txt1.readLine();
 82             String[] al_arr = al.split(",");
 83             if (al_arr[0].equals(zhanghao)) {
 84                 al_arr[4] = "0";
 85                 al = String.join(",", al_arr);
 86             }
 87             txt2.write(al);
 88             txt2.write("\r\n");
 89         }
 90         txt1.close();
 91         txt2.close();
 92 //        input.close();
 93         boolean d = f1.delete();
 94         boolean r = f2.renameTo(f1);
 95         System.out.println(zhanghao + " 冻结成功!");
 96     }
 97 
 98     public void jiedong() throws IOException {
 99         System.out.println("输入解冻的账号: ");
100         BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
101         String zhanghao = input.readLine();
102 
103         File f1 = new File(s1);
104         File f2 = new File(s2);
105         BufferedReader txt1 = new BufferedReader(new FileReader(f1));
106         BufferedWriter txt2 = new BufferedWriter(new FileWriter(f2));
107         while (txt1.ready()) {
108             String al = txt1.readLine();
109             String[] al_arr = al.split(",");
110             if (al_arr[0].equals(zhanghao)) {
111                 al_arr[4] = "1";
112                 al = String.join(",", al_arr);
113             }
114             txt2.write(al);
115             txt2.write("\r\n");
116         }
117         txt1.close();
118         txt2.close();
119 //        input.close();
120         boolean d = f1.delete();
121         boolean r = f2.renameTo(f1);
122         System.out.println(zhanghao + " 解冻成功!");
123     }
124 
125     public void all() throws IOException {
126         File f1 = new File(s1);
127         BufferedReader txt1 = new BufferedReader(new FileReader(f1));
128         while (txt1.ready()) {
129             String al = txt1.readLine();
130             System.out.println(al);
131         }
132         txt1.close();
133     }
134 
135     public void geren() throws IOException {
136         File f1 = new File(s1);
137         BufferedReader txt1 = new BufferedReader(new FileReader(f1));
138 
139         System.out.println("输入查询账号: ");
140         BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
141         String zhanghao = input.readLine();
142         while (txt1.ready()) {
143             String al = txt1.readLine();
144             String[] al_arr = al.split(",");
145             if (al_arr[0].equals(zhanghao)) {
146                 System.out.println(al);
147             }
148         }
149         try {
150 //            input.close();
151             txt1.close();
152         } catch (IOException x) {
153             System.out.println("关闭报错 ");
154         }
155     }
156 
157 
158 }
159 
160 
161 class Client {
162     String username;
163     String password;
164     String level;
165     int money;
166     String stauts;
167     BufferedReader input;
168     String s1 = System.getProperty("user.dir") + "\\user_info.txt";
169     String s2 = System.getProperty("user.dir") + "\\user_info2.txt";
170 
171     //初始化信息
172     public Client(String[] args) throws IOException {
173         this.input = new BufferedReader(new InputStreamReader(System.in));
174         System.out.print("请选择: ");
175         this.username = args[0]; // id
176         this.password = args[1]; // 密码
177         this.level = args[2];// 级别
178         this.money = Integer.parseInt(args[3]); // 金额
179         this.stauts = args[4]; // 状态
180 //        System.out.printf("%s %s %s %s %s :", this.username, this.password, this.level, this.money, this.stauts); //展示信息
181         System.out.println("1: 查询余额, 2: 取款, 3: 存款, 4: 转账, 5: 修改个人密码, 6: 退出系统 ");
182         String chose = (String) input.readLine();
183         while (!chose.toUpperCase().equals("6")) {
184             switch (chose) { //"1: 查询余额, 2: 取款, 3: 存款, 4: 转账, 5: 修改个人密码, 6: 退出系统 "
185                 case "1":
186                     System.out.println("余额: " + this.money);
187                     break;
188                 case "2":
189                     this.qukuan();
190                     break;
191                 case "3":
192                     this.cunkuan();
193                     break;
194                 case "4":
195                     this.zhuanzhang();
196                     break;
197                 case "5":
198                     this.xgmm();
199                     break;
200                 case "6":
201                     break;
202             }
203             System.out.println("1: 查询余额, 2: 取款, 3: 存款, 4: 转账, 5: 修改个人密码, 6: 退出系统 ");
204             chose = (String) input.readLine();
205         }
206 //        input.close();
207     }
208 
209     public void qukuan() throws IOException {
210         System.out.print("输入取款金额(最多5000,100的整数):");
211         int qian_int = 0;
212         String mon = (String) this.input.readLine();
213         try {
214             qian_int = Integer.parseInt(mon);
215         } catch (NumberFormatException e) {
216             System.out.print("(输入数字)");
217         }
218         while (qian_int % 100 != 0 || qian_int <= 0 || qian_int > 5000 || qian_int > this.money) {
219             System.out.print("金额输入错误(最多5000,100的整数):");
220             mon = (String) this.input.readLine();
221             try {
222                 qian_int = Integer.parseInt(mon);
223             } catch (NumberFormatException e) {
224                 System.out.print("(输入数字)");
225             }
226         }
227         this.money = this.money - qian_int;
228         System.out.println("取款" + mon + " ,余额: " + this.money);
229         File f1 = new File(s1);
230         File f2 = new File(s2);
231         BufferedReader txt1 = new BufferedReader(new FileReader(f1));
232         BufferedWriter txt2 = new BufferedWriter(new FileWriter(f2));
233         while (txt1.ready()) {
234             String al = txt1.readLine();
235             String[] al_arr = al.split(",");
236             if (al_arr[0].equals(this.username)) {
237                 al_arr[3] = Integer.toString(this.money);
238                 al = String.join(",", al_arr);
239             }
240             txt2.write(al);
241             txt2.write("\r\n");
242         }
243         txt1.close();
244         txt2.close();
245         boolean d = f1.delete();
246         boolean r = f2.renameTo(f1);
247     }
248 
249     public void cunkuan() throws IOException {
250         System.out.print("输入存款金额(100的整数,不能为负数):");
251         int qian_int = 0;
252         String mon = (String) this.input.readLine();
253         try {
254             qian_int = Integer.parseInt(mon);
255         } catch (NumberFormatException e) {
256             System.out.print("(输入数字)");
257         }
258         while (qian_int % 100 != 0 || qian_int <= 0) {
259             System.out.print("金额输入错误(100的整数,不能为负数):");
260             mon = (String) this.input.readLine();
261             try {
262                 qian_int = Integer.parseInt(mon);
263             } catch (NumberFormatException e) {
264                 System.out.print("(输入数字)");
265             }
266         }
267         this.money = this.money + qian_int;
268         System.out.println("存" + mon + " ,余额: " + this.money);
269         File f1 = new File(s1);
270         File f2 = new File(s2);
271         BufferedReader txt1 = new BufferedReader(new FileReader(f1));
272         BufferedWriter txt2 = new BufferedWriter(new FileWriter(f2));
273         while (txt1.ready()) {
274             String al = txt1.readLine();
275             String[] al_arr = al.split(",");
276             if (al_arr[0].equals(this.username)) {
277                 al_arr[3] = Integer.toString(this.money);
278                 al = String.join(",", al_arr);
279             }
280             txt2.write(al);
281             txt2.write("\r\n");
282         }
283         txt1.close();
284         txt2.close();
285         boolean d = f1.delete();
286         boolean r = f2.renameTo(f1);
287     }
288 
289     public void zhuanzhang() throws IOException {
290         int tag = 1;
291         System.out.print("输入他人账号:");
292         String other = (String) this.input.readLine();
293         System.out.print("输入转账金额(100的整数,不能为负数):");
294 
295         int qian_int = -2;
296         String mon = null;
297         mon = (String) this.input.readLine();
298         try {
299             qian_int = Integer.parseInt(mon);
300         } catch (NumberFormatException e) {
301             System.out.print("(输入数字)");
302         }
303         while (qian_int % 100 != 0 || qian_int <= 0) {
304             System.out.print("金额输入错误(100的整数,不能为负数):");
305             mon = (String) this.input.readLine();
306             try {
307                 qian_int = Integer.parseInt(mon);
308             } catch (NumberFormatException e) {
309                 System.out.print("金额输入错误(100的整数,不能为负数):");
310             }
311         }
312 
313         File f1 = new File(s1);
314         File f2 = new File(s2);
315         BufferedReader txt1 = new BufferedReader(new FileReader(f1));
316         BufferedWriter txt2 = new BufferedWriter(new FileWriter(f2));
317         while (txt1.ready()) {
318             String al = txt1.readLine();
319             String[] al_arr = al.split(",");
320             if (al_arr[0].equals(other)) {
321                 tag = 2;
322                 break;
323 
324             }
325         }
326         txt1.close();
327         if (other.equals(this.username)) {
328             tag = 3;
329         }
330         if (tag == 2) {
331             txt1 = new BufferedReader(new FileReader(f1));
332             while (txt1.ready()) {
333                 String al = txt1.readLine();
334                 String[] al_arr = al.split(",");
335                 if (al_arr[0].equals(this.username)) {
336                     this.money = this.money - qian_int;
337                     al_arr[3] = Integer.toString(this.money);
338                     al = String.join(",", al_arr);
339                 } else if (al_arr[0].equals(other)) {
340                     int other_amt = Integer.parseInt(al_arr[3]);
341                     al_arr[3] = Integer.toString(other_amt + qian_int);
342                     al = String.join(",", al_arr);
343                 }
344                 txt2.write(al);
345                 txt2.write("\r\n");
346             }
347         } else if (tag == 3) {
348             System.out.println("不能转给自己");
349         } else System.out.println("对方ID不存在");
350         txt1.close();
351         txt2.close();
352         boolean d = f1.delete();
353         boolean r = f2.renameTo(f1);
354         System.out.println("转账成功,当前余额: " + this.money);
355     }
356 
357     public void xgmm() throws IOException {
358         System.out.print("输入新密码:");
359         String new1 = (String) this.input.readLine();
360         System.out.print("再次确认密码:");
361         String new2 = (String) this.input.readLine();
362 
363         while (!new1.equals(new2) || new1.length() <= 6) {
364             System.out.println("提示: 确保2次密码一致且长度不能小于6位");
365             System.out.print("输入新密码:");
366             new1 = (String) this.input.readLine();
367             System.out.print("再次确认密码:");
368             new2 = (String) this.input.readLine();
369         }
370         File f1 = new File(s1);
371         File f2 = new File(s2);
372         BufferedReader txt1 = new BufferedReader(new FileReader(f1));
373         BufferedWriter txt2 = new BufferedWriter(new FileWriter(f2));
374         while (txt1.ready()) {
375             String al = txt1.readLine();
376             String[] al_arr = al.split(",");
377             if (al_arr[0].equals(this.username)) {
378                 al_arr[1] = new1;
379                 al = String.join(",", al_arr);
380             }
381             txt2.write(al);
382             txt2.write("\r\n");
383         }
384         txt1.close();
385         txt2.close();
386         boolean d = f1.delete();
387         boolean r = f2.renameTo(f1);
388         System.out.println("修改密码成功,当前密码: " + new1);
389     }
390 }
391 
392 
393 class ATM {
394     String s1 = System.getProperty("user.dir") + "\\user_info.txt";
395     String s2 = System.getProperty("user.dir") + "\\user_info2.txt";
396     int root; // 1 root用户
397     int tag;  //1 登录成功
398     int status = 2; //0 冻结
399     String username;
400     String password;
401     String[] info;
402     BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
403     BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
404 
405     public ATM() throws IOException {
406         System.out.print("输入账号:");
407         this.username = (String) br1.readLine();
408 
409         System.out.print("输入密码:");
410         this.password = (String) br2.readLine();
411 
412         BufferedReader bline1 = new BufferedReader(new FileReader(s1));
413         while (bline1.ready()) {
414             String line = bline1.readLine();
415             String[] line_arr = line.split(",");
416             this.info = line_arr;
417             //root
418             if (line_arr[0].equals(this.username) && line_arr[4].equals("0")) {
419 //                System.out.println("账号已冻结!");
420                 this.status = 0;
421                 break;
422 //                System.exit(0);
423 //                System.exit(0);
424             } else if (line_arr[0].equals(this.username) && line_arr[1].equals(this.password) && line_arr[2].equals("0") && line_arr[4].equals("1")) {
425                 this.root = 1;
426                 this.tag = 1;
427                 break;
428                 //普通用戶
429             } else if (line_arr[0].equals(this.username) && line_arr[1].equals(this.password) && line_arr[4].equals("1")) {
430                 this.tag = 1;
431                 break;
432                 //普通用戶
433             } else if (line_arr[0].equals(this.username) && line_arr[4].equals("1")) {
434                 int num = 1;
435                 while (num <= 3) {
436                     System.out.print("密码错误重新输入(错3次冻结,目前" + num + "次):");
437                     this.password = (String) br2.readLine();
438                     if (line_arr[1].equals(this.password) && line_arr[2].equals("0")) {
439                         this.root = 1;
440                         this.tag = 1;
441                         break;
442                     } else if (line_arr[1].equals(this.password)) {
443                         this.tag = 1;
444                         break;
445                     }
446                     num++;
447                 }
448                 if (num > 3) {
449                     bline1.close();
450                     this.dongjie();
451 //                    System.exit(0);
452                 }
453                 break;
454             }
455         }
456         try {
457             bline1.close();
458 //            br2.close();
459 //            br1.close();
460 
461         } catch (Exception x) {
462             System.out.println(" ");
463         }
464     }
465 
466     public void dongjie() throws IOException {
467         File f1 = new File(s1);
468         File f2 = new File(s2);
469         BufferedReader txt1 = new BufferedReader(new FileReader(f1));
470         BufferedWriter txt2 = new BufferedWriter(new FileWriter(f2));
471         while (txt1.ready()) {
472             String al = txt1.readLine();
473             String[] al_arr = al.split(",");
474             if (al_arr[0].equals(this.username)) {
475                 al_arr[4] = "0";
476                 al = String.join(",", al_arr);
477             }
478             txt2.write(al);
479             txt2.write("\r\n");
480         }
481         txt1.close();
482         txt2.close();
483         boolean d = f1.delete();
484         boolean r = f2.renameTo(f1);
485         this.status = 0;
486     }
487 }
488 
489 public class ATM_test {
490     public static void main(String[] args) throws IOException {
491         String s1 = System.getProperty("user.dir") + "\\user_info.txt";
492         File f1 = new File(s1);
493         if (!f1.exists()) {
494             try {
495                 boolean x = f1.createNewFile();
496                 BufferedWriter w1 = new BufferedWriter(new FileWriter(f1,true));
497                 w1.write("a1,123,0,100000000,1");
498                 w1.write("\r\n");
499                 w1.write("a2,123,1,10000,1");
500                 w1.write("\r\n");
501                 w1.write("a3,123,1,10000,0");
502                 w1.write("\r\n");
503                 w1.close();
504             } catch (IOException x) {
505                 System.out.println("xx");
506             }
507         }
508 
509 
510         Rootx r1;
511         Client c1;
512         ATM atm1 = new ATM();
513 // 判断登录
514         if (atm1.tag == 1) {
515             if (atm1.root == 1) {
516                 System.out.println("Root用户 登录成功!");
517                 r1 = new Rootx(atm1.info);
518             } else {
519                 System.out.println("普通用户 登录成功!");
520                 c1 = new Client(atm1.info);
521             }
522         } else if (atm1.status == 0) {
523             System.out.println("账号已冻结,请联系管理员!");
524         } else System.out.println("账号或密码错误!");
525         atm1.br1.close();
526         atm1.br2.close();
527     }
528 }
529 
530 //碰到问题
531 //main方法是静态方法 不能使用this
532 //txt修改文件内容不会写  (用复制到文件2 ,然后替换原文件代替)
533 // 转账部分可以优化
View Code

 

posted on 2022-02-16 15:55  hui_T  阅读(19)  评论(0编辑  收藏  举报