Java小玩意:模拟ATM
一、写在前面
本来是其他班的作业,原本是用来练习集合存储对象,正好最近学了JDBC,缺个小玩意练习,这不缺啥来啥吗,然后就写了这个小屎山😅
纯手写,欢迎指正。
二、功能介绍
- 注册&登录
- 存取款
- 查询
- 转账
- 修改信息
三、基本逻辑
- 登录:获取输入的ID,到数据库中查询此ID的用户信息,查询成功则创建此用户对象,否则提示错误
- 注册:用户需要输入用户名,性别,登录密码等信息,然后自动生成ID,然后将用户信息存储到数据库中
- 存取款:用户输入需要存取的金额,在此对象对应的“余额”属性中对应增加或减少相应数额,然后再更新数据库
- 转账:用户输入转向的ID后,在数据库中查询此ID对应的用户信息,查询成功则创建此用户对象没否则提示错误。输入转账金额后,两个用户对象的“余额”属性同时增加和减少转账的数额,然后更新数据库
- 修改信息:将此用户对象的属性更改后,更新数据库。
四、数据库结构
CREATE TABLE `account` (
`uid` varchar(18) COLLATE utf8mb4_general_ci NOT NULL,
`userName` varchar(10) COLLATE utf8mb4_general_ci NOT NULL,
`passwd` varchar(12) COLLATE utf8mb4_general_ci NOT NULL,
`balance` double DEFAULT NULL,
`xianE` double DEFAULT NULL,
`gender` varchar(2) COLLATE utf8mb4_general_ci NOT NULL,
PRIMARY KEY (`uid`)
)
五、关键代码
注册
public void createAccount() throws SQLException { Account account = new Account(); account.createName(); account.createGender(); account.createPasswd(); account.createBalance(); account.createXianE(); // 随机生成3个3位数字 Random ran = new Random(); int ID1 = ran.nextInt(100, 1000); int ID2 = ran.nextInt(100, 1000); int ID3 = ran.nextInt(100, 1000); String ID = "" + ID1 + ID2 + ID3; Integer uID = Integer.parseInt(ID); account.setuID(uID); Connection conn; { try { conn = DriverManager.getConnection(url, user, password); PreparedStatement ps; { try { String sql = "insert into account (uid,userName,gender, passwd, balance, xianE) values ( ?,?, ?, ?, ?, ?)"; ps = conn.prepareStatement(sql); ps.setString(1, String.valueOf(account.getuID())); ps.setString(2, account.getUserName()); ps.setString(3, account.getGender()); ps.setString(4, account.getPasswd()); ps.setDouble(5, account.getBalance()); ps.setDouble(6, account.getXianE()); ps.executeUpdate(); }; catch (SQLException e) { throw new RuntimeException(e); }; try { ps.close(); conn.close(); }; catch (SQLException e) { throw new RuntimeException(e); }; }; }; catch (SQLException e) { throw new RuntimeException(e); }; }; System.out.println("注册成功!"); System.out.println(account); System.out.println("即将返回主界面..."); };
登录
public Account login() { Account account = new Account(); String[] userInfo; System.out.println("请输入您的ID:"); int uID = new Scanner(System.in).nextInt(); // 接收用户输入后去数据库里查询信息 JDBC jdbc = new JDBC(); String sql = "select * from account where uid = " + uID; try { // 将查询到的用户信息保存到userInfo中 userInfo = jdbc.getUserInfo(sql); if (userInfo == null) { System.out.println("无此用户,请重新输入"); account = login(); }; else { // 密码认证 boolean autFlag = autPasswd(userInfo); for (int i = 0; true; i++) { // 若认证密码返回true,将查询到的信息赋给account对象 if (autFlag) { account.setuID(Integer.parseInt(userInfo[0])); account.setUserName(userInfo[1]); account.setPasswd(userInfo[2]); account.setBalance(Double.parseDouble(userInfo[3])); account.setXianE(Double.parseDouble(userInfo[4])); account.setGender(userInfo[5]); System.out.println("登录成功..."); break; }; else { System.out.println("密码错误,请重新输入!"); System.out.println("您还有" + (2 - i) + "次机会"); autFlag = autPasswd(userInfo); }; if (i == 2) { System.out.println("密码错误次数过多,程序退出,感谢您的使用!"); System.exit(0); }; }; }; }; catch (SQLException e) { throw new RuntimeException(e); }; return account; };
登录成功后显示菜单
public void showMenu(Account account) { if (this.getGender().equals("男")) { System.out.println("您好," + account.getUserName() + "先生,欢迎使用ATM机!"); }; else if (this.getGender().equals("女")) { System.out.println("您好," + account.getUserName() + "女士,欢迎使用ATM机!"); }; else { System.out.println("您好," + account.getUserName() + ",欢迎使用ATM机!"); }; 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("6.退出"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); };
这里会根据用户信息显示“某某先生”还是”某某女士“;
转账
public void transferMoney(Account account) { Account transferAccount = new Account(); System.out.println("请输入您要转账的账户ID:"); int uID = new Scanner(System.in).nextInt(); String[] transferUserInfo; JDBC jdbc = new JDBC(); String sql = "select * from account where uid = " + uID; try { transferUserInfo = jdbc.getUserInfo(sql); if (transferUserInfo == null) { System.out.println("无此用户,请重新输入"); transferMoney(account); }; else { transferAccount.setuID(Integer.parseInt(transferUserInfo[0])); transferAccount.setUserName(transferUserInfo[1]); transferAccount.setPasswd(transferUserInfo[2]); transferAccount.setBalance(Double.parseDouble(transferUserInfo[3])); transferAccount.setXianE(Double.parseDouble(transferUserInfo[4])); }; }; catch (SQLException e) { throw new RuntimeException(e); }; System.out.println("请确认您要转账的账户信息:"); System.out.println("账户ID:" + transferAccount.getuID()); System.out.println("用户名:" + transferAccount.getUserName()); System.out.println("请输入您要转账的金额:"); double money = new Scanner(System.in).nextDouble(); double thisBalance = account.getBalance(); double thatBalance = transferAccount.getBalance(); thisBalance -= money; thatBalance += money; account.setBalance(thisBalance); transferAccount.setBalance(thatBalance); sql = "update account set balance = " + thisBalance + " where uid = " + account.getuID(); jdbc.update(sql); sql = "update account set balance = " + thatBalance + " where uid = " + transferAccount.getuID(); jdbc.update(sql); System.out.println("转账成功,您的余额为:" + thisBalance); showMenu(account); };
修改信息
private void updateAccount(Account account) { JDBC jdbc = new JDBC(); System.out.println("更新账户信息:"); System.out.println(account.getUserName()); System.out.println(account.getPasswd()); System.out.println(account.getXianE()); System.out.println("请输入您要更新的信息:"); System.out.println("1.用户名"); System.out.println("2.密码"); System.out.println("3.取款限额"); System.out.println("4.返回上级菜单"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); switch (choice) { case 1: System.out.println("请输入您的新用户名:"); String newUserName = scanner.next(); account.setUserName(newUserName); String sql = "update account set username = '" + account.getUserName() + "' where uid = " + account.getuID(); jdbc.update(sql); System.out.println("更新成功!"); updateAccount(account); break; case 2: System.out.println("请输入您的新密码:"); String newPasswd = scanner.next(); account.setPasswd(newPasswd); String sql1 = "update account set passwd = '" + account.getPasswd() + "' where uid = " + account.getuID(); jdbc.update(sql1); System.out.println("更新成功!"); updateAccount(account); break; case 3: System.out.println("请输入您的新取款限额:"); double newXianE = scanner.nextDouble(); account.setXianE(newXianE); String sql2 = "update account set xiane = " + account.getXianE() + " where uid = " + account.getuID(); jdbc.update(sql2); System.out.println("更新成功!"); updateAccount(account); break; case 4: showMenu(account); break; default: System.out.println("输入错误,请重新输入!"); updateAccount(account); break; }; };
六、源码
源码已上传Github,Cikian/Simulated_ATM-Java-: java+MySQL模拟ATM
- 克隆代码到本地,在本地或者服务器新建MySQL数据库:atm_account
- 导入sql目录下的sql文件建表及插入测试数据
- 修改cfg.properties文件中的各字段即可