• Java测验总结


    今天上课进行了Java的测试,让写一个模拟ATM程序。在课上没有写出来。特别的遗憾。我在写的时候没有先构建好整体的框架,读题不仔细。导致我后面在写具体功能的时候出现了牵一发而动全身的情况。在出现了问题之后,修改一处就会涉及到其他多个功能。造成了程序的修改困难。

    此外,我对文件的操作依旧不太熟悉,导致我在考虑读取文件数据到内存的时候没有具体的思路,导致了我在编写读取文件的时候浪费了大量的时间。此外我对AccountManager类的思考并不全面,陷入了选择数组还是ArrayList两难的处境,使用ArrayList对这个程序固然有它的可变长的优点,即可以不用过度考虑数据的多少。但是由于我对它的熟悉程度不够导致了后期使用它的时候陷入了困难。以及日期的赋值,由于不熟悉Date类和SimpleDateFormat类导致在写的过程中部分精力用到了对date的实现上。

    之后,就是我对主程序的调用顺序没有弄清楚。以至于此程序的运行常常出现问题。出现变量未赋值为null的错误。这点可以归结为我没有构建起一个正确的、相对简洁的框架和没有理清各功能之间的关系。

    还有,没有对重复的代码进行整合到一个方法里比如输出,导致后期在写相关的相同的输出的时候出现了代码的冗余。

    针对这些问题,如果我能在拿到题目之后可以认真的读完要求,并整理好大致的框架,把大部分容易实现的功能先完成的话,或许会有好的改善。此外,就是要进行注释,虽然在写代码的时候对自己想要完成的大致功能,大部分代码都是可以看懂的。但是事后再进行代码的维护或者进一步的完善的时候如果没有注释的话这就成为了一种痛苦。还有一方面是代码问价写的过于集中,导致一个文件里有两个类,造成了代码查看上的困难。

    经过这次的测试,我清楚地看到了自身的不足。.在看到长篇大论地题目之后产生一步登天地或者着慌地心理确实对我有很大的影响。这样的心理让我无法专注于题目地阅读,框架地思考,进而导致了后面写代码时的各种障碍。所以这种贪天之功的心理对我来说是极需要克服的。只有静下心来才能对问题进行详细的思考、分析。

    此外,我还有对相关问题的经历太少,固执于原来的思维的问题。这也是导致我见到题目乱了思路的原因之一。所以,要吃下编程这碗饭,有活跃的思维是很重要的。以后,我也会努力扩宽自己的思维,只有思维宽广了,在遇到问题的时候才能在短时间内想到相关的解决方法。

    在这次测验中我还认识到了对功能拆分的重要性,拆分功能,分而治之会使编程过程中的思路更加清晰,同时还能增强代码的复用。可以省去很多不必要的麻烦。这对代码结构也是有好处的,能够减少各功能之间的联系。让每个功能相对独立存在。方便对各功能的查找和修改。

    //信1905-2班20194033马玉凯

    Account.java :

    package ReTest;

    public class Account {

           private String accountID;

           private String accountname;

           private String operatedate;

           private int operatetype; // 具体描述为“1”表示存款,“2”表示取款,“3”表示转账汇款,“4”表示修改账户密码,“5”表示查询余额。

           private String accountpassword;

           private int accountbalance = 0;

           private int amount = 0;

           public Account() {

           }

           public Account(String accountID, String accountname, String accountpassword, int accountbalance) {

                  super();

                  this.accountID = accountID;

                  this.accountname = accountname;

                  this.accountpassword = accountpassword;

                  this.accountbalance = accountbalance;

           }

           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 String getAccountpassword() {

                  return accountpassword;

           }

           public void setAccountpassword(String 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;

           }

           @Override

           public String toString() {

                  String str = "" + accountID + ", " + accountname + ", " + accountpassword + ", " + accountbalance + " ";

                  return str;

           }

           public String getAmountString() {

                  String str = "" + accountID + ", " + accountname + ", " + operatedate + ", ";

                  return str;

           }

    }

    AccountManager.java :

    package ReTest;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.RandomAccessFile;

    import java.text.SimpleDateFormat;

    import java.util.Date;

    import java.util.Scanner;

    public class AccountManager {

           private Account a = new Account("10010000", "Ming", "1006111", 1000000);

           private Account b = new Account();

           private Scanner scanner = new Scanner(System.in);

           private long informationPointer;

           public void welcome() {

                  while (true) {

                         String account;

                         System.out.println("***************************************************************");

                         System.out.println(" 欢迎使用中国工商银行自动柜员系统");

                         System.out.println("***************************************************************");

                         System.out.println(" 请输入您的账号:");

                         System.out.println("***************************************************************");

                         account = scanner.next();

                         if (account.length() != 8) {

                                System.out.println("该卡不是工行卡");

                         } else if (!readInformationFile(account, true)) {

                                System.out.println("该账户不存在");

                         } else {

                                break;

                         }

                  }

                  Date date = new Date();

                  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                  a.setOperatedate(sdf.format(date));

                  login();

           }

           public void showName() {

                  System.out.println("****************************************************************");

                  System.out.println(" 欢迎" + a.getAccountname() + "使用中国工商银行自助柜员系统");

                  System.out.println("****************************************************************");

           }

           public void login() {

                  int i;

                  String passwd;

                  showName();

                  System.out.println(" 请输入您的密码:");

                  System.out.println("***************************************************************");

                  for (i = 0; i < 3; i++) {

                         passwd = scanner.next();

                         if (passwd.equals(a.getAccountpassword())) {

                                loginSuccess();

                         } else {

                                System.out.println("密码录入错误");

                         }

                  }

                  if (i >= 2) {

                         System.out.println("该账号三次录入密码错误,该卡已被系统没收,请与工行及时联系处理");

                  }

           }

           public void loginSuccess() {

                  int i = 0;

                  while (true) {

                         showName();

                         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("***************************************************************");

                         i = scanner.nextInt();

                         switch (i) {

                         case 1:

                                saving();

                                break;

                         case 2:

                                withdraw();

                                break;

                         case 3:

                                transfer();

                                break;

                         case 4:

                                passwdUpdate();

                                break;

                         case 5:

                                displayList();

                                break;

                         default:

                                if (i == 'q') {

                                       return;

                                }

                                System.out.println("错误的输入");

                                break;

                         }

                  }

           }

           // 1.存款

           public void saving() {

                  while (true) {

                         showName();

                         System.out.println("请输入存款金额:");

                         if (scanner.hasNextInt()) {

                                int money = scanner.nextInt();

                                if (money <= 0) {

                                       System.out.println("输入金额有误");

                                } else {

                                       a.setAccountbalance(a.getAccountbalance() + money);

                                       a.setAmount(money);

                                       a.setOperatetype(1);

                                       savingSuccess();

                                }

                         } else {

                                char c = scanner.next().charAt(0);

                                if (c == 'q' || c == 'Q') {

                                       return;

                                } else {

                                       System.out.println("错误的选择");

                                }

                         }

                  }

           }

    //存款成功

           public void savingSuccess() {

                  writeToInformationFile(a.toString());

                  writeToListFile();

                  showName();

                  System.out.println(" 当前账户存款操作成功");

                  System.out.println("当前账户余额为:" + a.getAccountbalance() + "元");

           }

           // 2.取钱

           public void withdraw() {

                  int c;

                  char b;

                  while (true) {

                         showName();

                         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、返回");

                         if (scanner.hasNextInt()) {

                                c = scanner.nextInt();

                                int money = 0;

                                switch (c) {

                                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: {

                                       money = self();

                                       break;

                                }

                                case 8:

                                       welcome();

                                       break;

                                case 9:

                                       loginSuccess();

                                       break;

                                default:

                                       System.out.println("错误的输入");

                                       break;

                                }

                                if (a.getAccountbalance() < money) {

                                       System.out.println("账户余额不足");

                                } else {

                                       a.setAccountbalance(a.getAccountbalance() - money);

                                       writeToInformationFile(a.toString());

                                       a.setAmount(money);

                                       a.setOperatetype(2);

                                       withdrawSuccess(money);

                                }

                         } else {

                                b = scanner.next().charAt(0);

                                if (b == 'q' || b == 'Q') {

                                       return;

                                } else {

                                       System.out.println("错误的选择");

                                }

                         }

                  }

           }

           // 自助取款

           public int self() {

                  int money = 0;

                  while (true) {

                         showName();

                         System.out.println("请输入取款金额:");

                         if (scanner.hasNextInt()) {

                                money = scanner.nextInt();

                                return money;

                         } else {

                                char c = scanner.next().charAt(0);

                                if (c == 'q' || c == 'Q') {

                                       break;

                                } else {

                                       System.out.println("错误的选择");

                                }

                         }

                  }

                  return money;

           }

           // 取款成功

           public void withdrawSuccess(int money) {

                  writeToInformationFile(a.toString());

                  writeToListFile();

                  showName();

                  System.out.println("当前账户取款操作" + money + "元成功");

                  System.out.println("当前账户余额为:" + a.getAccountbalance() + "元");

           }

           // 3.转账

           public void transfer() {

                  String account;

                  while (true) {

                         showName();

                         System.out.println("请输入转账账户");

                         if (scanner.hasNextInt()) {

                                account = scanner.next();

                                if (account.length() != 8) {

                                       System.out.println("该卡不是工行卡");

                                } else if (!readInformationFile(account, false)) {

                                       System.out.println("该账户不存在");

                                } else {

                                       transferIDFind();

                                }

                         } else {

                                char c = scanner.next().charAt(0);

                                if (c == 'q' || c == 'Q') {

                                       break;

                                } else {

                                       System.out.println("错误的选择");

                                }

                         }

                  }

           }

           public void transferIDFind() {

                  while (true) {

                         showName();

                         System.out.println("请输入转账金额");

                         if (scanner.hasNextInt()) {

                                int money = scanner.nextInt();

                                if (money > a.getAccountbalance()) {

                                       System.out.println("账户余额不足");

                                } else {

                                       char c;

                                       System.out.println("****************************************************************");

                                       System.out.println(" 请确认是否向" + b.getAccountname() + "转账" + money + "元。");

                                       System.out.println("****************************************************************");

                                       c = scanner.next().charAt(0);

                                       if (c == 'Y' || c == 'y') {

                                              a.setAccountbalance(a.getAccountbalance() - money);

                                              a.setAmount(money);

                                              a.setOperatetype(3);

                                              transferSuccess(money);

                                              break;

                                       } else {

                                              loginSuccess();

                                              break;

                                       }

                                }

                         } else {

                                char c = scanner.next().charAt(0);

                                if (c == 'q' || c == 'Q') {

                                       break;

                                } else {

                                       System.out.println("错误的选择");

                                }

                         }

                  }

           }

           public void transferSuccess(int money) {

                  writeToInformationFile(a.toString());

                  writeToListFile();

                  showName();

                  System.out.println("当前账户向" + b.getAccountname() + "成功转账" + money + "成功");

                  System.out.println("当前账户余额为" + a.getAccountbalance() + "元");

           }

           // 4.修改密码

           public void passwdUpdate() {

                  String oldpasswd;

                  showName();

                  System.out.println(" 请输入当前密码");

                  oldpasswd = scanner.next();

                  if (oldpasswd.equals(a.getAccountpassword())) {

                         System.out.println(" 请输入修改密码");

                         String nowpasswd = scanner.next();

                         System.out.println(" 请输入确认密码");

                         String nowpasswd2 = scanner.next();

                         if (!nowpasswd.equals(nowpasswd2)) {

                                System.out.println(" 修改密码与确认密码不一致");

                                passwdUpdate();

                         } else {

                                a.setAccountpassword(nowpasswd);

                                a.setOperatetype(4);

                                updateSuccess();

                         }

                  } else {

                         System.out.println(" 当前密码录入错误");

                         passwdUpdate();

                  }

           }

           public void updateSuccess() {

                  writeToInformationFile(a.toString());

                  showName();

                  System.out.println(" 当前账户密码修改成功");

                  System.out.println("****************************************************************");

           }

           public void displayList() {

                  showName();

                  System.out.println(" 当前账户余额为:" + a.getAccountbalance() + "元");

                  readListFile();

           }

           public void writeToInformationFile(String newStr) {

                  try {

                         RandomAccessFile fout = new RandomAccessFile("accountinformation.txt", "rw");

                         fout.seek(informationPointer);

                         fout.write(newStr.getBytes());

                         fout.close();

                  } catch (IOException e) {

                         e.printStackTrace();

                  }

           }

           public boolean readInformationFile(String findStr, boolean self) {

                  try {

                         Account temp = new Account();

                         RandomAccessFile fin = new RandomAccessFile("accountinformation.txt", "r");

                         String tempstr;

                         while ((tempstr = fin.readLine()) != null) {

                                if (tempstr.contains(findStr)) {

                                       String[] information = tempstr.split(" |,| ");

                                       temp.setAccountID(information[0]);

                                       temp.setAccountname(information[2]);

                                       temp.setAccountpassword(information[4]);

                                       temp.setAccountbalance(Integer.valueOf(information[6]));

                                       if (self) {

                                              a = temp;

                                              informationPointer = fin.getFilePointer() - tempstr.length() - 1;

                                       } else {

                                              b = temp;

                                       }

                                       fin.close();

                                       return true;

                                }

                         }

                         fin.close();

                  } catch (IOException e) {

                         // TODO: handle exception

                  }

                  return false;

           }

           public void writeToListFile() {

                  try {

                         FileOutputStream fout = new FileOutputStream("accountlist.txt", true);

                         String str = null;

                         switch (a.getOperatetype()) {

                         case 1:

                                str = a.getAmountString() + "存款, " + a.getAmount();

                                break;

                         case 2:

                                str = a.getAmountString() + "取款, " + a.getAmount();

                                break;

                         case 3:

                                str = a.getAmountString() + "转账汇款, " + a.getAmount();

                         default:

                                break;

                         }

                         str += " ";

                         fout.write(str.getBytes("UTF-8"));

                         fout.close();

                  } catch (IOException e) {

                         // TODO: handle exception

                  }

           }

           public void readListFile() {

                  long[] pointer = new long[2];

                  pointer[0] = -1;

                  pointer[1] = -1;

                  try {

                         RandomAccessFile fin = new RandomAccessFile("accountlist.txt", "r");

                         byte[] b = new byte[1024];

                         int length;

                         String str;

                         while ((length = fin.read(b)) > 0) {

                                str = new String(b);

                                if (str.contains(a.getAccountID())) {

                                       System.out.println(str);

                                }

                         }

                         fin.close();

                  } catch (IOException e) {

                         e.printStackTrace();

                  } catch (Exception e1) {

                         e1.printStackTrace();

                  }

           }

    }

    Test.java(main):

    package ReTest;

    public class Test {

           public static void main(String[] args) {

                  AccountManager am = new AccountManager();

                  am.welcome();

           }

    }

  • 相关阅读:
    [JZOJ3386] 守卫者的挑战
    [JZOJ3385] 黑魔法师之门
    [JZOJ3383] 太鼓达人
    [JZOJ3382] 七夕祭
    NOIP模拟测试on 2019.9.27
    数据结构测试2 on 2019.9.25
    数据结构测试1 on 2019.9.24
    P2047 [NOI2007]社交网络
    P2286 [HNOI2004]宠物收养场
    P1342 请柬 建反图+dijkstra
  • 原文地址:https://www.cnblogs.com/MXming/p/13715559.html
Copyright © 2020-2023  润新知