先创建了一个ATM.txt格式的的文档,将卡的账号、密码、内存金额放在此文档
password=123
money=5000
userName=123
然后用类Properties创建一个对象,将银行卡的属性放进对象中,书写一个简单的ATM机
实现简单的登录、存款、取款、查看、修改密码、退出程序等功能
具体代码示例如下:
package unit331; import java.io.FileReader; import java.io.FileWriter; import java.util.Properties; import javax.swing.JOptionPane; public class ATM { public static void main(String[] args) { JOptionPane.showMessageDialog(null, "***欢迎光临***"); boolean item = login(); if (item == false) { JOptionPane.showMessageDialog(null, "你已经输入三次,超次,将退出程序"); System.exit(0); } while (true) { String number = JOptionPane.showInputDialog(null, "1、存款 \n2、取款\n3、查询\n4、改密\n5、退出"); switch (number) { case "1": add(); break; case "2": setout(); break; case "3": find(); break; case "4": change(); break; case "5": System.exit(0); default: JOptionPane.showMessageDialog(null, "请再选项里面选择"); } } }// 主方法 // 修改密码 public static int change() { Properties pro = new Properties(); try { pro.load(new FileReader("src/unit331/ATM.txt")); } catch (Exception e) { JOptionPane.showMessageDialog(null, "查无此文件"); } String password = pro.getProperty("password"); String code = JOptionPane.showInputDialog(null, "请输入原密码"); if (password.equals(code) == false) { JOptionPane.showMessageDialog(null, "密码不正确"); return 0; } int newpassword=Integer.parseInt(JOptionPane.showInputDialog(null, "请输入新密码")) ; int newpassword1=Integer.parseInt(JOptionPane.showInputDialog(null, "请确认新密码")) ; if (newpassword!=newpassword1) { JOptionPane.showConfirmDialog(null, "两次密码不一致"); return 0; } pro.setProperty("password", newpassword+""); try { pro.store(new FileWriter("src/unit331/ATM.txt"), null); } catch (Exception e) { JOptionPane.showMessageDialog(null, "查无此文件"); } return 0; } // 查询页面 public static void find() { Properties p = new Properties(); try { p.load(new FileReader("src/unit331/ATM.txt")); } catch (Exception e) { JOptionPane.showMessageDialog(null, "查无此文件"); } int money = Integer.parseInt(p.getProperty("money")); JOptionPane.showMessageDialog(null, "账号余额为:" + money); } // 取款页面 public static int setout() { int money = Integer.parseInt(JOptionPane.showInputDialog(null, "请输入取款金额")); Properties pro = new Properties(); try { pro.load(new FileReader("src/unit331/ATM.txt")); } catch (Exception e) { JOptionPane.showMessageDialog(null, "查无此文件"); } int AtmMoney = Integer.parseInt(pro.getProperty("money")); if (money != AtmMoney) { JOptionPane.showMessageDialog(null, "账户余额不足"); return 0; } int AllMoney = AtmMoney - money; pro.setProperty("money", AllMoney + ""); try { pro.store(new FileWriter("src/unit331/ATM.txt"), null); } catch (Exception e) { JOptionPane.showMessageDialog(null, "查无此文件"); } JOptionPane.showMessageDialog(null, "请从取款口取出现金"); return 0; } // 存款页面 public static void add() { int money = Integer.parseInt(JOptionPane.showInputDialog(null, "请输入存款金额")); Properties pro = new Properties(); try { pro.load(new FileReader("src/unit331/ATM.txt")); } catch (Exception e) { System.out.println("查无此文件"); } String atmmoney = pro.getProperty("money"); int Atmmoney = Integer.parseInt(atmmoney); int allmoney = Atmmoney + money; pro.setProperty("money", allmoney + ""); try { pro.store(new FileWriter("src/unit331/ATM.txt"), null); } catch (Exception e) { JOptionPane.showMessageDialog(null, "查无此文件"); } } // 登录页面 public static boolean login() { Properties pro = new Properties(); try { pro.load(new FileReader("src/unit331/ATM.txt")); } catch (Exception e) { System.out.println("查无此文件"); } String userName = pro.getProperty("userName"); String password = pro.getProperty("password"); for (int i = 0; i < 3; i++) { String name = JOptionPane.showInputDialog(null, "请输入用户名"); String pwd = JOptionPane.showInputDialog(null, "请输入密码"); if (userName.equals(name) && password.equals(pwd)) { return true; } else { JOptionPane.showMessageDialog(null, "用户名和密码错误,请重新输入"); } } return false; } }
欢迎广大朋友指正,共同交流