package Memu; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Scanner; public class BankATM { public static void main(String[] args) throws ClassNotFoundException, SQLException { //加载驱动 Class.forName("com.mysql.jdbc.Driver"); //获取连接 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/atm?characterEncoding=utf-8", "root", "root"); System.out.println("正在连接数据库......"); mainMemu(conn); } public static void mainMemu(Connection conn) throws SQLException { boolean flag = true; Scanner sc = new Scanner(System.in); do { System.out.println("------欢迎光临银行系统------"); System.out.println("1.进入管理端"); System.out.println("2.进入终端"); System.out.println("3.退出系统"); System.out.println("请输入指令进行 *** 作:"); String str = sc.next(); if ("1".equals(str)) { //1.进入管理端 Manager.manager(conn); } else if ("2".equals(str)) { //2.进入终端 Terminal.terminal(conn); } else if ("3".equals(str)) { //3.退出系统 System.out.println("银行系统已退出!"); flag = false; } else { System.out.println("输入的指令有误请重新输入......"); } } while (flag); } }
package Memu; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class Terminal { public static void terminal(Connection conn) throws SQLException { //终端 Scanner sc = new Scanner(System.in); for (int i = 1; i <= 3; i++) { System.out.println("---欢迎使用ATM系统---"); System.out.println("请输入您的卡号:"); String user_card = sc.next(); System.out.println("请输入您的密码:"); String user_password = sc.next(); //进行判断 card_pwdIF(conn, user_card, user_password, i); } } //判断卡号和密码是否正确 public static void card_pwdIF(Connection conn, String user_card, String user_password, int i) throws SQLException { //获取sql语句对象 String sql = "select user_card,user_password from userinfo where user_card = ? and user_password = ?"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, user_card); pst.setString(2, user_password); ResultSet rs = pst.executeQuery(); if (rs.next()) { forzenIF(conn, user_card); //判断账户是否被冻结 } else { //密码错误三次冻结用户 System.out.println("您输入的密码错误!"); //System.out.println("i = " + i); if (i == 3) { System.out.println("您输入的密码已经错误三次,账户将被冻结!"); String sql1 = "update userinfo set user_action = '冻结' where user_card = ?"; PreparedStatement pst1 = conn.prepareStatement(sql1); pst1.setString(1, user_card); pst1.executeUpdate(); } } } //判断账户是否被冻结 public static void forzenIF(Connection conn, String user_card) throws SQLException { String str = "正常"; String sql = "select user_action,user_card from userinfo where user_action = ? and user_card = ?"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, str); pst.setString(2, user_card); ResultSet rs = pst.executeQuery(); if (rs.next()) { System.out.println("----成功进入系统!----"); //进入子菜单 UserMemu.usermemu(conn,user_card); } else { System.out.println("用户已被冻结!"); } } }
package Memu; import Method.*; import java.sql.Connection; import java.sql.SQLException; import java.util.Scanner; public class UserMemu { public static void usermemu(Connection conn, String user_card) throws SQLException { Scanner sc = new Scanner(System.in); boolean bool = true; do { System.out.println("*--------银行ATM--------"); System.out.println("* 1.查询用户信息"); System.out.println("* 2.存款"); System.out.println("* 3.提现"); System.out.println("* 4.转账"); System.out.println("* 5.修改密码"); System.out.println("* 6.退卡"); System.out.println("*-------版本1.0.0-------"); System.out.println("请根据菜单序号进行 *** 作:"); String str = sc.next(); if ("1".equals(str)) { //1.查询用户信息 LookUser.lookuser(conn, user_card); } else if ("2".equals(str)) { //2.存款 Deposit.deposit(conn, user_card); } else if ("3".equals(str)) { //3.提现 Withdrawal.withdrawal(conn, user_card); } else if ("4".equals(str)) { //4.转账 Transfer.transfer(conn, user_card); } else if ("5".equals(str)) { //5.修改密码 Update_pwd.update_pwd(conn, user_card); } else if ("6".equals(str)) { //6.退卡 ReturnCard.returnCard(conn); } else { System.out.println("您输入的指令有误,请重新输入..."); } } while (bool); } }
package Method; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class LookUser { public static void lookuser(Connection conn, String user_card) throws SQLException { //查询用户信息 Scanner sc = new Scanner(System.in); String str; do { String sql = "select user_card,user_name,user_money,user_action from userinfo where user_card = ?"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, user_card); ResultSet rs = pst.executeQuery(); while (rs.next()) { System.out.println("正在查询..."); System.out.println("卡号:" + rs.getString("user_card") + " 姓名:" + rs.getString("user_name") + " 余额:" + rs.getString("user_money") + " 账户状态:" + rs.getString("user_action")); System.out.println("查询成功..."); } System.out.println("是否继续查询?Y/任意键退出"); str = sc.next(); } while ("Y".equals(str)); } }
package Method; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class Deposit { public static void deposit(Connection conn, String user_card) throws SQLException { Scanner sc = new Scanner(System.in); String str1; //存款 do { System.out.println("请输入存款的金额:"); double money = sc.nextDouble(); //判断存款额度是否小于ATM额度 quotaIF(conn, money, user_card); System.out.println("是否继续存款:Y/任意键退出?"); str1 = sc.next(); } while ("Y".equals(str1)); } //判断存款额度是否小于ATM额度(单次只能存储30000元) public static void quotaIF(Connection conn, double money, String user_card) throws SQLException { String sql = "select ATM_quota from balance where ATM_quota >= ?"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, String.valueOf(money)); ResultSet rs = pst.executeQuery(); if (rs.next()) { //小于进行存款 System.out.println("正在存款....."); String sql1 = "update userinfo set user_money = user_money + ? where user_card = ?"; PreparedStatement pst1 = conn.prepareStatement(sql1); pst1.setString(1, String.valueOf(money)); pst1.setString(2, user_card); int i = pst1.executeUpdate(); if (i > 0) { System.out.println("存款成功,是否打印凭条(Y/任意键退出)"); print(conn, money, user_card); } } else { System.out.println("存款额度大于ATM额度!"); } } public static void print(Connection conn, double money, String user_card) throws SQLException { Scanner sc = new Scanner(System.in); String str = sc.next(); if ("Y".equals(str)) { System.out.println("正在打印凭条....."); System.out.println("此次存入的金额为:" + money); String sql = "select user_money from userinfo where user_card = ?"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, user_card); ResultSet rs = pst.executeQuery(); if (rs.next()) { System.out.println("账户余额:" + rs.getString("user_money")); } } } }
package Method; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class Withdrawal { public static void withdrawal(Connection conn, String user_card) throws SQLException { //提现(取款) Scanner sc = new Scanner(System.in); String str; do { System.out.println("请输入取款的金额:"); double getMoney = sc.nextDouble(); //判断取款额度是否小于ATM额度 getIF(conn, user_card, getMoney); System.out.println("是否继续取款:Y/任意键退出?"); str = sc.next(); } while ("Y".equals(str)); } //判断取款额度是否小于ATM额度(单次只能取30000元) public static void getIF(Connection conn, String user_card, double getMoney) throws SQLException { String sql = "select ATM_quota from balance where ATM_quota >= ?"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, String.valueOf(getMoney)); ResultSet rs = pst.executeQuery(); if (rs.next()) { //判断取款是否小于yhk余额 getMoney_balanceIF(conn, user_card, getMoney); } else { System.out.println("取款额度大于ATM额度!"); //重新输入取款金额 Withdrawal.withdrawal(conn, user_card); } } //判断取款是否小于yhk余额 public static void getMoney_balanceIF(Connection conn, String user_card, double getMoney) throws SQLException { String sql = "select user_money,user_card from userinfo where user_money >= ? and user_card = ?"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, String.valueOf(getMoney)); pst.setString(2, user_card); ResultSet rs = pst.executeQuery(); if (rs.next()) { //判断取款是否小于ATM余额 ATM_moneyIF(conn, user_card, getMoney); } else { System.out.println("取款金额大于yhk余额!"); //重新输入取款金额 Withdrawal.withdrawal(conn, user_card); } } //判断取款是否小于ATM余额 public static void ATM_moneyIF(Connection conn, String user_card, double getMoney) throws SQLException { String sql = "select ATM_money from balance where ATM_money >= ?"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, String.valueOf(getMoney)); ResultSet rs = pst.executeQuery(); if (rs.next()) { //进行存款 go(conn, user_card, getMoney); } else { System.out.println("取款额度大于ATM余额!"); //重新输入取款金额 Withdrawal.withdrawal(conn, user_card); } } public static void go(Connection conn, String user_card, double getMoney) throws SQLException { System.out.println("正在取款....."); String sql1 = "update userinfo set user_money = user_money - ? where user_card = ?"; PreparedStatement pst1 = conn.prepareStatement(sql1); pst1.setString(1, String.valueOf(getMoney)); pst1.setString(2, user_card); int i = pst1.executeUpdate(); if (i > 0) { System.out.println("存款成功,是否打印凭条(Y/任意键退出)"); print(conn, getMoney, user_card); } else { System.out.println("取款失败!"); } } public static void print(Connection conn, double getMoney, String user_card) throws SQLException { Scanner sc = new Scanner(System.in); String str = sc.next(); if ("Y".equals(str)) { System.out.println("正在打印凭条....."); System.out.println("此次取款的金额为:" + getMoney); String sql = "select user_money from userinfo where user_card = ?"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, user_card); ResultSet rs = pst.executeQuery(); if (rs.next()) { System.out.println("账户余额:" + rs.getString("user_money")); } } } }
package Method; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class Transfer { public static void transfer(Connection conn, String user_card) throws SQLException { //转账 Scanner sc = new Scanner(System.in); String str; do { System.out.println("请输入转账卡号:"); String cardID = sc.next();//要转的那个账户的卡号 //进行判断 if (cardID.matches("^[0-9]*$")) { cardExistIF(conn, cardID, user_card); } else { System.out.println("卡号类型有误!"); } System.out.println("是否继续转账:Y/任意键退出?"); str = sc.next(); } while ("Y".equals(str)); } public static void cardExistIF(Connection conn, String cardID, String user_card) throws SQLException { //判断卡号是否存在,是否为相同用户 String sql = "select user_card from userinfo where user_card = ?"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, cardID); ResultSet rs = pst.executeQuery(); if (rs.next()) { if (cardID.equals(user_card)) { System.out.println("转账的用户为相同用户,无法转账!"); } else { //打印对方账号信息,等待确认转账 System.out.println("正在查找转账账户......"); printInfo(conn, cardID, user_card); } } else { System.out.println("该卡号不存在无法转账!"); } } public static void printInfo(Connection conn, String cardID, String user_card) throws SQLException { //打印和确认对方账户信息 Scanner sc = new Scanner(System.in); String sql = "select user_card,user_name,user_action from userinfo where user_card = ?"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, cardID); ResultSet rs = pst.executeQuery(); while (rs.next()) { System.out.println("卡号:" + rs.getString("user_card") + " 姓名:" + rs.getString("user_name")); } System.out.println("是否确认转账用户?Y/任意键重新输入"); String str = sc.next(); if ("Y".equals(str)) { //开始进行转账 start_tra(conn, user_card, cardID); } else { transfer(conn, cardID); } } public static void start_tra(Connection conn, String user_card, String cardID) throws SQLException { //开始转账 Scanner sc = new Scanner(System.in); System.out.println("请输入转账金额:"); double traMoney = sc.nextDouble(); if (traMoney > 0) { System.out.println("请输入您的密码:"); String user_password = sc.next(); if (user_password.matches("^\d{6}$")) { //判断密码是否正确 passwordIF(conn, user_password, user_card, cardID, traMoney); } else { System.out.println("密码类型有误!"); } } else { System.out.println("您输入的金额小于0!"); } } public static void passwordIF(Connection conn, String user_password, String user_card, String cardID, double traMoney) throws SQLException { String sql = "select user_password from userinfo where user_password = ? and user_card = ?"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, user_password); pst.setString(2, user_card); ResultSet rs = pst.executeQuery(); if (rs.next()) { user_frozen(conn, cardID, user_card, traMoney); } else { System.out.println("您输入的密码有误!"); } } public static void user_frozen(Connection conn, String cardID, String user_card, double traMoney) throws SQLException { //判断对方账号是否被冻结 String sql = "select user_action from userinfo where user_action = '正常' and user_card = ?"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, cardID); ResultSet rs = pst.executeQuery(); if (rs.next()) { System.out.println("正在转账......"); update_balance(conn, cardID, user_card, traMoney); } else { System.out.println("该账户被冻结,无法进行转账!"); } } public static void update_balance(Connection conn, String cardID, String user_card, double traMoney) throws SQLException { String sql = "update userinfo set user_money = user_money - ? where user_card = ?"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, String.valueOf(traMoney)); pst.setString(2, user_card); pst.executeUpdate(); String sql1 = "update userinfo set user_money = user_money + ? where user_card = ?"; PreparedStatement pst1 = conn.prepareStatement(sql1); pst1.setString(1, String.valueOf(traMoney)); pst1.setString(2, cardID); int i = pst1.executeUpdate(); if (i > 0) { System.out.println("转账成功!"); print(traMoney, user_card, cardID); } } public static void print(double traMoney, String user_card, String cardID) { Scanner sc = new Scanner(System.in); System.out.println("转账成功,是否打印凭条(Y/任意键退出)"); String str = sc.next(); if ("Y".equals(str)) { System.out.println("正在打印凭条....."); System.out.println("转出账号:" + user_card + " 转入账号:" + cardID + " 转账金额:" + traMoney); } } }
package Method; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class Update_pwd { public static void update_pwd(Connection conn, String user_card) throws SQLException { Scanner sc = new Scanner(System.in); String str; do { System.out.println("请输入6位数字旧密码:"); String oldPassword = sc.next(); //判断旧密码是否正确 oldPasswordIF(conn, oldPassword, user_card); str = sc.next(); } while ("Y".equals(str)); } //判断旧密码是否正确 public static void oldPasswordIF(Connection conn, String oldPassword, String user_card) throws SQLException { String sql = "select user_password from userinfo where user_password = ? and user_card = ?"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, oldPassword); pst.setString(2, user_card); ResultSet rs = pst.executeQuery(); if (rs.next()) { updatePassword(conn, user_card); } else { System.out.println("您的6位旧密码输入有误!"); update_pwd(conn, user_card); } } //输入新密码并判断新密码是否超过六位或不足六位 public static void updatePassword(Connection conn, String user_card) throws SQLException { Scanner sc = new Scanner(System.in); System.out.println("请输入新密码:"); String newPassword = sc.next(); if (newPassword.matches("^\d{6}$")) { System.out.println("请再次确认密码:"); String newPassword1 = sc.next(); //判断两次输入的密码是否相同 if (newPassword.equals(newPassword1)) { System.out.println("正在修改您的密码....."); String sql = "update userinfo set user_password = ? where user_card = ?"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, newPassword1); pst.setString(2, user_card); int i = pst.executeUpdate(); if (i > 0) { System.out.println("密码修改成功!"); System.out.println("是否继续使用ATM系统?Y/任意键退出"); } } else { System.out.println("您两次输入的密码不一致!"); update_pwd(conn, user_card); } } else { System.out.println("您输入的密码不足六位,请重新输入!"); update_pwd(conn, user_card); } } }
package Method; import Memu.Terminal; import java.sql.Connection; import java.sql.SQLException; import java.util.Scanner; public class ReturnCard { public static void returnCard(Connection conn) throws SQLException { //退卡返回登录界面 Scanner sc = new Scanner(System.in); System.out.println("您是否继续使用ATM系统?Y/任意键退出"); String str = sc.next(); if ("Y".equals(str)) { Terminal.terminal(conn); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)