JDBC 数据库阶段性项目:ATM系统
如果需要项目源码和数据库的数据,请前往我的资源页下载(但建议还是自己敲出来,整个项目完成后基本能掌握这一章节的所有内容):ATM源码
需求分析
一、ATM机
1、要求使用插卡(这里使用卡号)和密码登录ATM系统;
2、能够根据用户账户的冻结状态判断用户是否能够进入系统并给出提示;
3、用户进入系统后可以查看其账户所有信息;
4、用户可以存取款与转账;
5、用户可以更改自己的密码;
6、退卡(注销);
二、管理端
1、登录前同样进行输入判断,输错三次密码则删除管理员;
2、要求可以查询所有的用户信息,可以根据用户提供的卡号查询;最后可以查询ATM机的余额并选择是否添加余额;
3、要求可以为用户开户,根据用户提供的姓名,密码与金额,随机生成一个六位数卡号进行开户;
4、要求可以后台更改用户的状态,也可以为用户修改密码,还可以更改管理员自己的密码;
5、可以对存在的用户进行销户操作;
6、退出;
详细设计
一、ATM机
三张表
用户表
user_card | user_password | user_name | user_money | user_action |
---|---|---|---|---|
用户卡号 | 用户密码 | 用户姓名 | 用户余额 | 用户状态 |
ATM余额表
ATM_money | ID |
---|---|
管理员表
admin_id | admin_password | admin_action |
---|---|---|
管理员卡号 | 管理员密码 | 管理员状态 |
被删除的管理员表(公司炒掉员工还是会有记录的吧)
admin |
---|
管理员卡号 |
二、管理端
终端机功能模块
账户认证
1、判断卡号和密码是否正确
2、判断账户是否冻结
3、判断输入类型是否出错
4、密码错误三次冻结该账户
账户信息查询
1、查询用户信息:卡号,姓名,余额,状态
取款功能
取款:
(1)请输入取款金额:XXXXX
(2)判断取款额度是否小于ATM额度
(3)判断取款是否小于银行卡余额
(4)判断取款是否小于ATM余额
(5)取款成功
(6)是否打印凭条
(7)返回上一级
存款功能
存款:
(1)请输入存款金额:XXXXX
(2)判断存款额度是否小于ATM额度
(3)存款成功!
(4)是否打印凭条
(5)返回上一级
转账功能
转账:
(1)请输入转账卡号:XXXXXX
(2)判断卡号类型是否有误
(3)判断是否存在该卡号
(4)判断是否为相同账户
(5)打印对方账号信息,等待确认转账
(6)确认账号信息
(7)请输入转账金额:XXXXXX
(8)判断金额输入类型是否有误
(9)判断金额是否大于0
(10)转账请输入密码
(11)判断密码类型
(12)判断密码是否正确
(13)判断对方账户是否冻结
(14)转账成功
(15)是否打印凭条、是否继续转账
(16)返回上一级
修改密码功能
修改密码:
(1)请输入旧密码:XXXXXX
(2)判断旧密码是否正确
(3)请输入新密码:XXXXXX
(4)判断密码类型输入是否出错
(5)判断密码是否超过六位或不足六位
(6)请再次输入新密码:XXXXXX
(7)判断两次密码输入是否相同
(8)修改成功
(9)返回上一级
注销功能
注销之后,根据用户输入判断是否继续使用系统
管理端
账户认证
用户认证
1、判断账号和密码是否正确
2、判断输入类型是否出错
3、密码错误三次删除账号
账户信息总览
1、查询:
(1)、查询除密码外所有用户信息
(2)、根据卡号查询用户信息
(3)、查询ATM余额
2、查询ATM余额
(1)、是否增加ATM余额
(2)、退出
用户开户及增加管理员
用户开户及增加管理员:
(1)、用户开户
(2)、增加管理员
(3)、返回上一级
1、用户开户:(贺程)
(1)、请输入姓名:XXXX
(2)、请输入密码:XXXXXX
(3)、判断密码类型输入是否有误
(4)、判断密码是否为六位
(5)、请输入存储金额:XXXX
(6)、判断金额是否大于0
(7)、开户成功,是否打印凭条
(8)、返回上一级
2、增加管理员:
(1)、请输入账号:XXXX
(2)、请输入密码:XXXXXX
(3)、判断密码类型输入是否有误
(4)、判断密码是否为六位
(5)、增加成功
(6)、返回上一级
修改用户信息及管理员密码
修改用户及管理密码:
(1)修改用户密码
(2)修改用户状态
(3)修改管理员密码
销户
销户:
(1)请输入需要销户的卡号:XXXXXX
(2)判断卡号类型是否有误
(3)判断是否存在该卡号
(4)请输入密码:XXXXXX
(5)判断密码输入类型是否有误
(6)销户成功
(7)返回上一级
注销功能
注销之后,根据用户输入判断是否继续使用系统
服务器端
存钱存储过程,取钱存储过程,开户存储过程,ATM存/取钱存储过程,修改管理员密码存储过程,增加管理员过程,销户存储过程。
具体实现
首先我们需要引入一个MySQL的jar包,我的是 mysql-connector-java-5.1.40-bin.jar,下面提供jar包链接与MySQL navcat 12版本的破解下载,要拿的留个言,谢谢
链接:https://pan.baidu.com/s/12x32V8I2kt7D7IVZUHU37w 密码:lb5j
这里就默认大家都会MySQL了,如果不会,请参考我前面的章节,MySQL快速开始
然后,把我前面说的几个必要的表都建立好久万事俱备了
用户表录入数据
管理员表录入数据
ATM余额表录入数据
被删除的管理员表
下面我们进入代码
一、我们先建立数据库中需要用到的存储过程
1、转账存储过程
CREATE DEFINER=`all`@`%` PROCEDURE `bank_acounts`(in one int,in two int,in money int) //这里的 DEFINER=`all`@`%`是给哪一个用户创建,大家可以改成自己的用户,或者不写,默认为root账户
BEGIN
declare one_action int default 0;//转出方用户状态
declare two_action int default 0;//转入方用户状态
declare one_id int default 0;//转出方用户卡号
declare two_id int default 0;//转入方用户卡号
declare m int default 0;//转出方用户剩余金额
declare continue handler for SQLEXCEPTION ROLLBACK;
start transaction;
SELECT user_card into one_id from user where user_card = one;
SELECT user_card into two_id from user where user_card = two;
if(one_id = two_id)
then
SELECT "相同账户不能转账";
else
SELECT user_action into two_action from user where user_card = two;
if(two_action = 1)
then
SELECT user_money into m from user where user_card = one;
if(m-money>0)
then
update user set user_money = m-money where user_card = one;
SELECT user_money into m from user where user_card = two;
update user set user_money = m+money where user_card = two;
SELECT "转账成功";
else
SELECT "对不起,您的账户余额不足";
END if;
else
SELECT "对方的账户被冻结";
end if;
COMMIT;
end if;
END
2、用户存款存储过程
CREATE DEFINER=`all`@`%` PROCEDURE `save`(in u_card int,in s_money int,out rs varchar(128))
begin
declare atm_lit int default 10000;
declare exist_money int default 0;
declare exit handler for sqlexception rollback;
if (s_money <= atm_lit && s_money%100=0)
then
select atm_money into exist_money from atm where atm_id = 1;
if(s_money+exist_money<=1000000)
then
update user set user_money = user_money + s_money where user_card = u_card;
update atm set atm_money = atm_money + s_money where atm_id = 1;
set rs = '存款成功';
select rs;
else
set rs = '超过ATM容纳量';
end if;
elseif(s_money%100 != 0)
then
set rs = '存款只能存100倍数的现金';
select rs;
else
set rs = '存款金额大于存款额度';
select rs;
end if;
commit;
end
3、用户取款
CREATE DEFINER=`all`@`%` PROCEDURE `user_remove`(in u_card int,in u_money int,out print varchar(128))
begin
declare atm_lit int default 10000;
declare a int default 0;
declare b int default 0;
set @a = (select user_money from user where user_card = u_card);
set @b = (select atm_money from atm where atm_id = 1);
if(u_money>0) then
if(u_money%100=0) then
if(u_money>atm_lit) then
set print = '取款额度超额,请输入10000元以下';
select print;
else if(u_money>@a) then
set print = '银行卡余额不足';
select print;
else if(u_money>@b) then
set print = 'ATM机余额不足';
select print;
else
update user set user_money=@a-u_money where user_card = u_card;
update atm set atm_money=@b-u_money where atm_id = 1;
set print = '取款成功';
select print;
end if;
end if;
end if;
else
set print = '请取出100的整数';
select print;
end if;
else
set print = '输入不正确,不能输入负数';
select print;
end if;
end
4、管理员修改密码存储过程
CREATE DEFINER=`all`@`%` PROCEDURE `changeadminpwd`(in passwd int,in id varchar(128) )
begin
update admin set admin_password=passwd where admin_id= id;
end
5、增加管理员存储过程
CREATE DEFINER=`all`@`%` PROCEDURE `insert_admin`(in admin_id varchar(128),in pass int)
BEGIN
insert into admin values(admin_id,pass);
END
6、用户开户存储过程
CREATE DEFINER=`all`@`%` PROCEDURE `insert_user`(in user_card int,in user_password int,in user_name varchar(128),in user_money int,in user_action int)
begin
insert into user values(user_card,user_password,user_name,user_money,user_action);
end
7、用户销户存储过程
CREATE DEFINER=`all`@`%` PROCEDURE `user_delete`(in u_card int, out print varchar(128))
begin
declare exit handler for sqlexception rollback;
start transaction;
delete from user where user_card = u_card;
set print = '删除账户成功';
select print;
commit;
end
8、放钱到ATM的存储过程
CREATE DEFINER=`all`@`%` PROCEDURE `increase_m`(in money int,out rs varchar(128))
begin
declare all_money int default 0;
declare exist_money int default 0;
declare continue handler for SQLEXCEPTION ROLLBACK;
start transaction;
select atm_money into exist_money from atm where atm_id=1;
set all_money = money + exist_money;
if money>0 then
if all_money > 1000000 then
set rs ='总现金超过容纳量';
select rs;
else
update atm set atm_money=all_money where atm_id=1;
set rs='存入成功';
select rs;
end if;
else
set rs = '金额不能小于0';
end if;
commit;
end
9、从ATM退钱的存储过程
CREATE DEFINER=`all`@`%` PROCEDURE `decrease_m`(in money int,out rs varchar(128))
begin
declare exist_money int default 0;
declare all_money int default 0;
declare continue handler for SQLEXCEPTION ROLLBACK;
start transaction;
select atm_money into exist_money from atm where atm_id =1;
if money>0 then
if exist_money>=money then
set all_money=exist_money-money;
update atm set atm_money=all_money where atm_id=1;
set rs = '退钱成功';
select rs;
else
set rs= 'ATM内存款不足';
select rs;
end if;
else
set rs = '金额不能小于0';
end if;
commit;
end
二、下面我们开始写Java代码
我们将之前下载的 mysql-connector-java-5.1.40-bin.jar 导入eclipse中
1、数据库连接类
这是最原生的连接数据库的方法,为了初学者都能看懂,这里就不写db.properties的配置文件了,会的同学可以写
package com.jdbc.bank;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException;
public class JdbcConn {
//注册驱动
private static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
//数据库连接
private static Connection conn = null;
static {
try {
//加载驱动类
Class.forName(JDBC_DRIVER).newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection(String ip) throws SQLException{
//数据库地址,这里我们动态传入数据库的ip地址,用户名与密码写大家自己数据库的用户名和密码
conn = DriverManager.getConnection("jdbc:mysql://"+ip+":3306/bank?user=all&password=123456&useUnicode=true&characterEncoding=utf8");
return conn;//返回连接
}
}
2、Atm终端类
package com.jdbc.bank;
import java.sql.*;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.regex.Pattern;
import static java.lang.Thread.sleep;
public class Atm {
public static int user_card;
public static boolean old_pass;
public Atm() {
}
// 用户认证
public static boolean login(Connection conn) {//传入Connection链接连接数据库
while (true) {
boolean card = false;
boolean password = false;
boolean flag = false;
try {
Scanner console = new Scanner(System.in);
System.out.println("请输入您的卡号:");
while (true) {
try {
user_card = console.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入正确类型的六位卡号:");//判断卡号输入类型
String input = console.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
}else continue;
}
break;
}
Statement stmt = conn.createStatement();
stmt.execute("select * from user");//卡号类型输入正确后去数据库查找卡号
ResultSet rs = stmt.getResultSet();
while (rs.next()) {
if (user_card == rs.getInt(1)) {
card = true;//卡号正确,card值为true
break;
} else {
card = false;
}
}
if (card == true) {
int count = 3;//密码输入次数
while (true) {
System.out.println("请输入您的密码:");
stmt.execute("select * from user where user_card = '" + user_card + "'");//卡号正确后去数据库查找卡号对应的密码
rs = stmt.getResultSet();
int user_password = 0;
while (true) {
try {
user_password = console.nextInt();
} catch (InputMismatchException e) {//判断密码输入类型
System.out.print("请输入正确类型的六位密码:");
String input = console.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
}else continue;
}
break;
}
while (rs.next()) {
if (user_password == rs.getInt(2)) {
password = true;//密码输入正确,password值为true
stmt.execute("select user_action from user where user_card = '" + user_card + "'");
rs = stmt.getResultSet();
while (rs.next()) {
if (rs.getInt(1) == 1) {//判断用户状态是否为冻结
flag = true;
break;
} else {
System.out.println("您的账户已被冻结");
System.out.println();
}
}
break;
} else {
password = false;
}
break;
}
if (count == 1 && password == false) {//输入密码错误三次,用户冻结
stmt.execute("update user set user_action = 0 where user_card = '" + user_card + "'");
System.out.println("输入密码错误超过三次,您的账户已被冻结,滚吧");
break;
}
if (password == false) {
System.out.println("密码错误,请重新输入");
count--;
System.out.println("剩余输入次数:" + count);
continue;
}
break;
}
} else {
System.out.println("卡号错误");
continue;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
}
// 终端菜单
public static void menu1() {
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("请根据菜单序号进行操作:");
}
// 查询用户信息与余额
public static void select(Connection conn) {
while (true) {
try {
Scanner console = new Scanner(System.in);
Statement statement = conn.createStatement();
statement.execute("select * from user where user_card = " + user_card);
ResultSet rs = statement.getResultSet();
System.out.println("正在查询......");
Thread.currentThread().sleep(1000);
rs.next();
System.out.println("查询成功!");
String user_action;
if(rs.getInt(5)==1){
user_action = "正常";
}else{
user_action = "冻结";
}
System.out.println("卡号:" + rs.getInt(1) + " 姓名:" + rs.getString(3) + " 余额:" + rs.getInt(4)
+ " 账户状态:" + user_action);
System.out.println();
System.out.println("是否继续查询?Y/任意键退出");
String str = console.next();
if (str.toUpperCase().equals("Y")){
continue;
}else{
System.out.println("正在退出查询系统......");
sleep(1000);
break;
}
} catch (SQLException | InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
// 存款
public static void save(Connection conn) {
int count = 3;//输入金额次数
while (true) {
try {
System.out.println("请输入存钱金额: ");
Scanner sc = new Scanner(System.in);
int money = 0;
while (true) {
try {
money= sc.nextInt();
} catch (InputMismatchException e) {//判断输入金额类型
System.out.print("请输入正确的数字金额:");
String input = sc.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
}else continue;
}
break;
}
if (money < 0) {
if (count != 0) {
count--;
System.out.println("存款不能小于零!再捣乱" + count + "次退出存款系统");
continue;
} else {
System.out.println("正在退出存款系统.....");
Thread.currentThread().sleep(1000);
break;
}
} else {
CallableStatement cs = conn.prepareCall("{call save(?,?,?)}");//调用存款存储过程
cs.setInt(1, user_card);
cs.setInt(2, money);
cs.execute();
System.out.println("正在存款......");
Thread.currentThread().sleep(1000);
String res = cs.getString(3);
if (res.equals("存款成功")) {
Scanner console = new Scanner(System.in);
System.out.println("存款成功,是否打印凭条Y/任意键退出");//打印凭条即打印用户信息
String in = console.next();
if (in.toUpperCase().equals("Y")) {
System.out.println("正在打印凭条......");
Thread.currentThread().sleep(1000);
Statement stmt = conn.createStatement();
stmt.execute("select user_money from user where user_card = '" + user_card + "'");
ResultSet rs = stmt.getResultSet();
rs.next();
System.out.println("此次存入金额:" + money);
System.out.println("账户余额:" + rs.getInt(1));
System.out.println();
System.out.println("是否继续存款:Y/任意键退出 ?");
String in_in_yn = console.next();
if (in_in_yn.toUpperCase().equals("Y")) {
continue;
} else {
System.out.println("正在退出存款系统......");
Thread.currentThread().sleep(1000);
break;
}
} else {
System.out.println();
System.out.println("是否继续存款:Y/任意键退出 ?");
String in_in_yn = console.next();
if (in_in_yn.toUpperCase().equals("Y")) {
continue;
} else {
System.out.println("正在退出存款系统......");
Thread.currentThread().sleep(1000);
break;
}
}
} else {
Scanner console = new Scanner(System.in);
System.out.println(res);
System.out.println();
System.out.println("是否继续存款:Y/任意键退出 ?");
String in_in_yn = console.next();
if (in_in_yn.toUpperCase().equals("Y")) {
continue;
} else {
System.out.println("正在退出存款系统......");
Thread.currentThread().sleep(1000);
break;
}
}
}
} catch (SQLException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 取款
public static void remove(Connection conn) {
while (true) {
try {
System.out.println("请输入取款金额");
Scanner sc = new Scanner(System.in);
int money = 0;
while (true) {
try {
money= sc.nextInt();
} catch (InputMismatchException e) {//判断金额输入类型
System.out.print("请输入正确的数字金额:");
String input = sc.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
}else continue;
}
break;
}
CallableStatement cs = conn.prepareCall("{call user_remove(?,?,?)}");//调用取款存储过程
cs.setInt(1, user_card);
cs.setInt(2, money);
cs.execute();
System.out.println("正在取款......");
Thread.currentThread().sleep(1000);
String res = cs.getString(3);
if (res.equals("取款成功")) {
Scanner console = new Scanner(System.in);
System.out.println("取款成功,是否打印凭条Y/任意键退出");
String in = console.next();
if (in.toUpperCase().equals("Y")) {//打印凭条
System.out.println("正在打印凭条......");
Thread.currentThread().sleep(1000);
Statement stmt = conn.createStatement();
stmt.execute("select user_money from user where user_card = '" + user_card + "'");
ResultSet rs = stmt.getResultSet();
rs.next();
System.out.println("此次取款金额:" + money);
System.out.println("账户余额:" + rs.getInt(1));
System.out.println();
System.out.println("是否继续取款:Y/任意键退出 ?");
String in_in_yn = console.next();
if (in_in_yn.toUpperCase().equals("Y")) {
continue;
} else {
System.out.println("正在退出取款系统......");
Thread.currentThread().sleep(1000);
break;
}
} else {
System.out.println();
System.out.println("是否继续取款:Y/任意键退出 ?");
String in_in_yn = console.next();
if (in_in_yn.toUpperCase().equals("Y")) {
continue;
} else {
System.out.println("正在退出取款系统......");
Thread.currentThread().sleep(1000);
break;
}
}
} else {
System.out.println(res);
System.out.println();
System.out.println("是否继续取款:Y/任意键退出 ?");
Scanner console = new Scanner(System.in);
String in_in_yn = console.next();
if (in_in_yn.toUpperCase().equals("Y")) {
continue;
} else {
System.out.println("正在退出取款系统......");
Thread.currentThread().sleep(1000);
break;
}
}
} catch (SQLException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 转账
public static void acounts(Connection conn) {
while (true) {
try {
boolean out_card_flag = false;
Scanner console = new Scanner(System.in);
System.out.println("请输入转账的账号:");
int out_card = 0;
while (true) {
try {
out_card = console.nextInt();
} catch (InputMismatchException e) {//判断输入卡号
System.out.print("请输入正确类型的六位数卡号:");
String input = console.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
}else continue;
}
break;
}
Statement stmt = conn.createStatement();
stmt.execute("select * from user");
ResultSet rs = stmt.getResultSet();
while (rs.next()) {
if (out_card == rs.getInt(1)) {
out_card_flag = true;
break;
} else {
out_card_flag = false;
}
}
if (out_card_flag == true) {
boolean out_yn = true;
stmt.execute("select * from user where user_card ="+out_card);
rs = stmt.getResultSet();
rs.next();
System.out.println("正在查找转账账户......");
sleep(1000);
System.out.println("卡号:"+out_card+" 姓名:"+rs.getString(3));
System.out.println("是否确认转账账户?Y/任意键重新输入");
String in_money = console.next();
if (!(in_money.toUpperCase().equals("Y"))) {
out_yn = false;
}
if(out_yn == false){
System.out.println();
System.out.println("是否继续转账:Y/任意键退出 ?");
String in_in_yn = console.next();
if (in_in_yn.toUpperCase().equals("Y")) {
continue;
} else {
System.out.println("正在退出转账系统......");
Thread.currentThread().sleep(1000);
break;
}
}
System.out.println("请输入转账的金额:");
int out_money = 0;
while (true) {
try {
out_money = console.nextInt();
} catch (InputMismatchException e) {//金额输入类型判断
System.out.print("请输入正确的数字金额:");
String input = console.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
}else continue;
}
if(out_money>0){
break;
}else{
System.out.println("金额必须大于0,请重新输入:");
continue;
}
}
int count = 3;
boolean password = false;
while (true) {//密码判断,与登录判断一致
System.out.println("请输入您的密码:");
stmt.execute("select * from user where user_card = '" +user_card + "'");
rs = stmt.getResultSet();
int user_password = 0;
while (true) {
try {
user_password = console.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入正确类型的六位密码:");
String input = console.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
}else continue;
}
break;
}
while (rs.next()) {
if (user_password == rs.getInt(2)) {
password = true;
break;
} else {
password = false;
}
break;
}
if (count == 1 && password == false) {
System.out.println("输入密码错误超过三次,强制退出转账系统......");
sleep(1000);
break;
}
if (password == false) {
System.out.println("密码错误,请重新输入");
count--;
System.out.println("剩余输入次数:" + count);
continue;
}
break;
}
if(password == false){
System.out.println("正在退出系统......");
Thread.currentThread().sleep(1000);
break;
}
CallableStatement cstmt = conn.prepareCall("{call bank_acounts(?,?,?)}");//调用转账存储过程
cstmt.setInt(1, user_card);
cstmt.setInt(2, out_card);
cstmt.setInt(3, out_money);
System.out.println("正在转账......");
Thread.currentThread().sleep(1000);
cstmt.execute();
rs = cstmt.getResultSet();
rs.next();
System.out.println(rs.getString(1));
if (rs.getString(1).equals("转账成功")) {
System.out.println("是否打印凭条: Y/任意键退出 ?");
String in_yn = console.next();
if (in_yn.toUpperCase().equals("Y")) {
System.out.println("正在打印凭条......");
Thread.currentThread().sleep(1000);
System.out.println("转出账号:" + user_card + " 转入账号:" + out_card + " 转账金额: " + out_money);
System.out.println();
System.out.println("是否继续转账:Y/任意键退出 ?");
String in_in_yn = console.next();
if (in_in_yn.toUpperCase().equals("Y")) {
continue;
} else {
System.out.println("正在退出转账系统......");
Thread.currentThread().sleep(1000);
break;
}
} else {
System.out.println();
System.out.println("是否继续转账:Y/任意键退出 ?");
String in_in_yn = console.next();
if (in_in_yn.toUpperCase().equals("Y")) {
continue;
} else {
System.out.println("正在退出转账系统......");
Thread.currentThread().sleep(1000);
break;
}
}
}else{
System.out.println();
System.out.println("是否继续转账:Y/任意键退出 ?");
String in_in_yn = console.next();
if (in_in_yn.toUpperCase().equals("Y")) {
continue;
} else {
System.out.println("正在退出转账系统......");
Thread.currentThread().sleep(1000);
break;
}
}
} else {
System.out.println("查无此人");
System.out.println("正在重新加载转账系统......");
Thread.currentThread().sleep(2000);
continue;
}
} catch (SQLException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 修改密码
public static void changeUserPassword(Connection conn) {
//修改密码需要输入旧密码,新密码与确认新密码,每个密码输错三次就退出系统
try {
Statement st = conn.createStatement();
boolean b = st.execute("select user_password from user where user_card = " + user_card + "");
if (b) {
ResultSet rs2 = st.getResultSet();
rs2.next();
int oldPassword = rs2.getInt("user_password");
boolean flag = true;
int login_count = 3;//旧密码次数
int new_pass_count = 3;//新密码次数
int new_one_pass = 3;//确认新密码次数
while (flag) {
Scanner sc = new Scanner(System.in);
System.out.print("输入6位数字旧密码 ");
int passwd = 0;
boolean isDigit = true;
while (isDigit) {
try {
passwd = sc.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入六位数字密码 ");
String input = sc.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
}else continue;
}
break;
}
if (oldPassword == passwd) {
while (true) {
System.out.print("新密码 ");
int newpassword = 0;
while (isDigit) {
try {
newpassword = sc.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入六位数字密码 ");
String input1 = sc.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input1).matches();
if (digit) {
break;
}else continue;
}
break;
}
int len = Integer.toString(newpassword).length();//密码位数判断
if (len != 6) {
--new_one_pass;
System.out.println("密码位数错误,剩余次数" + new_one_pass);
if (new_one_pass == 0) {
System.out.println("密码输入次数用完,正在退出...");
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
continue;
} else {
System.out.print("确认新密码 ");
int newpassword2 =0;
while (isDigit) {
try {
newpassword2 = sc.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入六位数字密码 ");
String input1 = sc.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input1).matches();
if (digit) {
break;
}else continue;
}
break;
}
if (newpassword == newpassword2) {
System.out.println("正在修改您的密码......");
sleep(1000);
st.execute("update user set user_password='" + newpassword + "' where user_card = " + user_card + "");
System.out.println("修改成功");
flag = false;
break;
} else {
System.out.println("密码两次不相同");
--new_pass_count;
if (new_pass_count == 0) {
flag = false;
break;
}
continue;
}
}
}
} else {
--login_count;
if (login_count == 0) {
try {
System.out.println("旧密码输入次数用完,正在退出...");
sleep(1000);
old_pass = false;
break;
} catch (InterruptedException e) {
e.printStackTrace();
}
flag = false;
}
System.out.println("密码错误,请重新输入" + " " + "剩余次数" + login_count);
continue;
}
break;
}
}
} catch (SQLException | InterruptedException e) {
e.printStackTrace();
}
}
}
3、用户调用Atm界面类
package com.jdbc.bank;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.regex.Pattern;
import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException;
public class UserStart {
static Connection conn = null;
static ResultSet rs = null;
public static void UserBegin(Connection conn){
// 得到数据库连接
while(true){
try {
Scanner console = new Scanner(System.in);
while (true) {
System.out.println("----欢迎使用澳门赌场 ATM 系统----");
boolean flag = Atm.login(conn);//调用Atm类login方法进行登录验证
if (flag == true) {
System.out.println("----正在进入系统......----");
Thread.currentThread().sleep(1000);
System.out.println();
System.out.println("----成功进入系统!----");
while (true) {
int in_num=0;
Atm.menu1();//调用Atm类menu1方法菜单页面
while (true) {
try {
in_num = console.nextInt();
} catch (InputMismatchException e) {//输入指令判断
System.out.print("请输入正确的数字指令:");
String input = console.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
}else continue;
}
break;
}
switch (in_num) {
case 1://查询信息
Atm.select(conn);
continue;
case 2://存款
Atm.save(conn);
continue;
case 3://取款
Atm.remove(conn);
continue;
case 4://转账
Atm.acounts(conn);
continue;
case 5://修改密码
Atm.changeUserPassword(conn);
if(Atm.old_pass){
continue;
}else
break;
case 6://注销
System.out.println("正在退卡......");
Thread.currentThread().sleep(1000);
System.out.println("请拿走您的银行卡!");
System.out.println();
break;
default:
System.out.println("输入指令有误,请重新输入");
continue;
}
break;
}
}
System.out.println("是否继续使用澳门赌场 ATM 系统?Y/任意键退出");
String enter = console.next();
if (enter.toUpperCase().equals("Y")){
continue;
}else{
System.out.println("正在退出终端系统......");
Thread.currentThread().sleep(1000);
System.out.println("欢迎再来赌哦~~~");
break;
}
}
break;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
4、管理端类
package com.jdbc.bank;
import java.sql.*;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.regex.Pattern;
import static java.lang.Thread.sleep;
public class Admin {
private static String admin_card;
public Admin() {
}
// 用户认证,与Atm类登录验证大致相同
public static boolean login(Connection conn) {
while (true) {
boolean card = false;
boolean password = false;
boolean flag = false;
try {
Scanner console = new Scanner(System.in);
System.out.println("请输入管理员账号:");
while (true) {//账号输入类型判断
admin_card = console.next();
String reg = "^[a-z]+$";
if (admin_card.matches(reg)) {
break;
} else {
System.out.println("账号只能是小写字母的组合,请重新输入:");
continue;
}
}
Statement stmt = conn.createStatement();
stmt.execute("select * from admin");
ResultSet rs = stmt.getResultSet();
while (rs.next()) {
if (admin_card.equals(rs.getString(1))) {
card = true;
break;
} else {
card = false;
}
}
if (card == true) {
int count = 3;
while (true) {
System.out.println("请输入您的密码:");
int user_password = 0;
while (true) {
try {
user_password = console.nextInt();
} catch (InputMismatchException e) {//密码类型判断
System.out.print("请输入正确类型的六位密码:");
String input = console.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
} else
continue;
}
break;
}
stmt.execute("select * from admin where admin_id = '" + admin_card + "'");
rs = stmt.getResultSet();
while (rs.next()) {
if (user_password == rs.getInt(2)) {
password = true;
flag = true;
break;
} else {
password = false;
}
break;
}
if (count == 1 && password == false) {
// stmt.execute("update user set user_action = 0
// where user_card = '" + admin_card + "'");//应该删除他
stmt.execute("insert into admin_old values ('"+admin_card+"')");
stmt.execute("delete from admin where admin_id = '" + admin_card + "'");
System.out.println("正在删除你的管理账户......");
sleep(1000);
System.out.println("输入密码错误超过三次,你被炒了,滚吧");
break;
}
if (password == false) {
System.out.println("密码错误,请重新输入");
count--;
System.out.println("剩余输入次数:" + count);
continue;
}
break;
}
} else {
System.out.println("查无此人");
continue;
}
} catch (SQLException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
}
// 管理端首页菜单
public static void menu1() {
System.out.println(" *-------澳门赌场管理--------");
System.out.println(" * 1、查询用户信息及ATM ");
System.out.println(" * 2、用户开户及增加管理员 ");
System.out.println(" * 3、修改信息及密码 ");
System.out.println(" * 4、销户 ");
System.out.println(" * 5、注销 ");
System.out.println(" *----------版本 1.0.0-------");
System.out.println("请根据菜单序号进行操作:");
}
// 查询用户信息及ATM的主菜单
public static void menu_by_select(Connection conn) {
while (true) {
System.out.println(" *---------查询-------------");
System.out.println(" * 1、查询所有用户信息 ");
System.out.println(" * 2、根据卡号查询 ");
System.out.println(" * 3、查询ATM机余额 ");
System.out.println(" * 4、返回上一级 ");
System.out.println(" *--------------------------");
System.out.println("请输入查询指令:");
Scanner console = new Scanner(System.in);
int in_num = console.nextInt();
switch (in_num) {
case 1:
select_all(conn);//调用查询所有用户信息的方法
continue;
case 2:
select_by_card(conn);//根据卡号查询
continue;
case 3:
update_atm_menu(conn);//查询ATM余额
continue;
case 4:
System.out.println("正在退出......");//返回上一级
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
System.out.println("输入指令有误,请重新输入");
continue;
}
break;
}
}
// ATM主菜单
public static void update_atm_menu(Connection conn) {
while (true) {
System.out.println("正在查询当前ATM余额:");
ResultSet rs = null;
try {
sleep(1000);
Statement stmt = conn.createStatement();
stmt.execute("select * from atm");
rs = stmt.getResultSet();
rs.next();
System.out.println(" *------ATM余额菜单--------");
System.out.println(" * 当前ATM余额: " + rs.getInt(2) + "元 *");
System.out.println(" * 1、往ATM放钱 ");
System.out.println(" * 2、从ATM取钱 ");
System.out.println(" * 3、返回上一级 ");
System.out.println(" *--------------------------");
System.out.println("请输入指令:");
Scanner console = new Scanner(System.in);
int in_num = console.nextInt();
switch (in_num) {
case 1:
dealCashIn(conn);//往Atm放钱方法
continue;
case 2:
dealCashOut(conn);//从Atm取钱方法
continue;
case 3:
System.out.println("正在退出......");//返回上一级
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
System.out.println("输入指令有误,请重新输入");
continue;
}
break;
} catch (InterruptedException | SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
// 从ATM取钱
public static void dealCashOut(Connection conn) {
boolean flag = true;
while (flag) {
Scanner sc = new Scanner(System.in);
int input_m = 0;
System.out.println("要取出的现金金额:");
while (true) {
try {
input_m = sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("请输入数字:");
String input = sc.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
} else {
continue;
}
}
if (input_m % 100 == 0) {
break;
} else {
System.out.println("请输入100的正整数倍数:");
continue;
}
}
try {
CallableStatement cs = conn.prepareCall("{call decrease_m(?,?)}");//调用数据库中从ATM取钱存储过程
cs.setInt(1, input_m);
cs.execute();
String rs = cs.getString(2);
System.out.println("正在从ATM取钱.....");
sleep(1000);
System.out.println(rs);
Scanner console = new Scanner(System.in);
System.out.println("是否继续取钱?Y/任意键退出系统");
String in_yn = console.next();
if (in_yn.toUpperCase().equals("Y")) {
System.out.println("正在重新载入......");
Thread.currentThread().sleep(1000);
continue;
} else {
System.out.println("正在返回上一级......");
Thread.currentThread().sleep(1000);
break;
}
} catch (SQLException | InterruptedException e) {
System.out.println("调用存储过程发生异常");
}
}
}
// 存钱到ATM
public static void dealCashIn(Connection conn) {
boolean flag = true;
while (flag) {
Scanner sc = new Scanner(System.in);
int input_m = 0;
System.out.println("要存入的现金金额:");
while (true) {
try {
input_m = sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("请输入数字:");
String input = sc.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
} else {
continue;
}
}
if (input_m % 100 == 0) {
break;
} else {
System.out.println("请输入100的正整数倍数:");
continue;
}
}
try {
CallableStatement cs = conn.prepareCall("{call increase_m(?,?)}");//调用数据库中放钱到Atm的存储过程
cs.setInt(1, input_m);
cs.execute();
String rs = cs.getString(2);
System.out.println(rs);
Scanner console = new Scanner(System.in);
System.out.println("是否继续放钱?Y/任意键退出系统");
String in_yn = console.next();
if (in_yn.toUpperCase().equals("Y")) {
System.out.println("正在重新载入......");
Thread.currentThread().sleep(1000);
continue;
} else {
System.out.println("正在返回上一级......");
Thread.currentThread().sleep(1000);
break;
}
} catch (SQLException | InterruptedException e) {
System.out.println("调用存储过程发生异常");
}
}
}
// 查询所有用户信息
public static void select_all(Connection conn) {
while (true) {
try {
System.out.println("正在查询所有用户信息......");
Scanner console = new Scanner(System.in);
Thread.currentThread().sleep(1000);
Statement stmt = conn.createStatement();
stmt.execute("select * from user");
ResultSet rs = stmt.getResultSet();
System.out.println("查询成功");
while (rs.next()) {
int card = rs.getInt("user_card");
String name = rs.getString("user_name");
int money = rs.getInt("user_money");
int user_action = rs.getInt("user_action");
String action;
if (user_action == 1) {
action = "正常";
} else {
action = "冻结";
}
System.out.println("卡号:" + card + " 姓名:" + name + " 余额:" + money + " 状态:" + action);
}
System.out.println("是否继续查询?Y/任意键退出系统");
String in_yn = console.next();
if (in_yn.toUpperCase().equals("Y")) {
System.out.println("正在重新载入查询系统......");
Thread.currentThread().sleep(1000);
continue;
} else {
System.out.println("正在退出查询系统......");
Thread.currentThread().sleep(1000);
break;
}
} catch (SQLException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 更改用户状态
public static void change_action(Connection conn) {
while (true) {
boolean card = false;
try {
Scanner console = new Scanner(System.in);
System.out.println("请输入您的卡号:");
int user_card = 0;
while (true) {
try {
user_card = console.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入正确类型的卡号:");
String input = console.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
} else
continue;
}
break;
}
Statement stmt = conn.createStatement();
stmt.execute("select * from user");
ResultSet rs = stmt.getResultSet();
while (rs.next()) {
if (user_card == rs.getInt(1)) {
card = true;
break;
} else {
card = false;
}
}
if (card == true) {
while (true) {
System.out.println("请更正用户的状态(冻结/正常):");
String action = console.next();
int user_action = 0;
if (action.equals("正常")) {
user_action = 1;
} else if (action.equals("冻结")) {
user_action = 0;
} else {
System.out.println("输入有误,请重新输入");
continue;
}
System.out.println("正在修改用户状态......");
sleep(1000);
stmt.execute("update user set user_action =" + user_action + " where user_card =" + user_card);
System.out.println("修改成功!");
System.out.println("卡号:" + user_card + " 状态:" + action);
break;
}
} else {
System.out.println("账号不存在");
System.out.println("是否继续修改用户状态?Y/任意键退出系统");
String in_yn = console.next();
if (in_yn.toUpperCase().equals("Y")) {
System.out.println("正在重新载入修改用户状态系统......");
Thread.currentThread().sleep(1000);
continue;
} else {
System.out.println("正在退出修改用户状态系统......");
Thread.currentThread().sleep(1000);
break;
}
}
System.out.println("是否继续修改用户状态?Y/任意键退出系统");
String in_yn = console.next();
if (in_yn.toUpperCase().equals("Y")) {
System.out.println("正在重新载入修改用户状态系统......");
Thread.currentThread().sleep(1000);
continue;
} else {
System.out.println("正在退出修改用户状态系统......");
Thread.currentThread().sleep(1000);
break;
}
} catch (SQLException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 开户及增加管理员主菜单
public static void menu_by_insert(Connection conn) {
while (true) {
System.out.println(" *----用户开户及增加管理员---");
System.out.println(" * 1、用户开户 ");
System.out.println(" * 2、增加管理员 ");
System.out.println(" * 3、返回上一级 ");
System.out.println(" *--------------------------");
System.out.println("请输入查询指令:");
Scanner console = new Scanner(System.in);
int in_num = console.nextInt();
switch (in_num) {
case 1:
insert_account(conn);//用户开户方法
continue;
case 2:
insert_admin(conn);//增加管理员菜单
continue;
case 3:
System.out.println("正在退出......");//返回上一级
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
System.out.println("输入指令有误,请重新输入");
continue;
}
break;
}
}
// 用户开户
public static void insert_account(Connection conn) {
try {
while (true) {
Scanner console = new Scanner(System.in);
System.out.println("请输入您的姓名:");
String user_name = console.next();
String reg = "[\\u4e00-\\u9fa5]+";//正则表达式判断输入类型
if (!(user_name.matches(reg))) {
System.out.println("请输入汉字!");
continue;
} else {
while (true) {
System.out.println("请输入您的密码:");
int user_password = 0;
while (true) {
try {
user_password = console.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入正确类型的六位密码:");
String input = console.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
} else
continue;
}
break;
}
int len = Integer.toString(user_password).length();
if (len != 6) {
System.out.println("密码位数出错,请重新输入:");
continue;
} else {
System.out.println("请输入您存款的金额:");
int money = 0;
while (true) {
try {
money = console.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入正确的数字金额:");
String input = console.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
} else
continue;
}
break;
}
int user_card = (int) (Math.random() * 999999) + 100000;//随机生成六位卡号
System.out.println("正在开户......");
Thread.currentThread().sleep(1000);
CallableStatement cstmt = conn.prepareCall("{call insert_user(?,?,?,?,?)}");//调用数据库中开户存储过程
cstmt.setInt(1, user_card);
cstmt.setInt(2, user_password);
cstmt.setString(3, user_name);
cstmt.setInt(4, money);
cstmt.setInt(5, 1);
cstmt.execute();
System.out.println("开户成功");
System.out.println("正在打印用户信息......");
Thread.currentThread().sleep(1000);
System.out
.println("卡号:" + user_card + " 姓名:" + user_name + " 余额:" + money + " 状态:正常");
break;
}
}
}
System.out.println("是否继续开户?Y/任意键退出");
String in_yn = console.next();
if (in_yn.toUpperCase().equals("Y")) {
System.out.println("正在重新载入开户系统......");
Thread.currentThread().sleep(1000);
continue;
} else {
System.out.println("正在退出系统......");
Thread.currentThread().sleep(1000);
break;
}
}
} catch (SQLException | InterruptedException e) {
}
}
// 增加管理员
public static void insert_admin(Connection conn) {
try {
while (true) {
Scanner console = new Scanner(System.in);
System.out.println("请输入要添加的管理员账号:");
String more_card = null;
while (true) {
more_card = console.next();
String reg = "^[a-z]+$";
if (more_card.matches(reg)) {
break;
} else {
System.out.println("账号只能是小写字母的组合,请重新输入:");
continue;
}
}
while (true) {
System.out.println("请输入您的密码:");
int admin_password = 0;
while (true) {
try {
admin_password = console.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入正确类型的六位密码:");
String input = console.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
} else
continue;
}
break;
}
int len = Integer.toString(admin_password).length();
if (len != 6) {
System.out.println("密码位数出错,请重新输入:");
continue;
} else {
System.out.println("正在增加管理员......");
Thread.currentThread().sleep(1000);
CallableStatement cstmt = conn.prepareCall("{call insert_admin(?,?)}");
cstmt.setString(1, more_card);
cstmt.setInt(2, admin_password);
cstmt.execute();
System.out.println("增加成功");
break;
}
}
System.out.println("是否继续增加管理员?Y/任意键退出");
String in_yn = console.next();
if (in_yn.toUpperCase().equals("Y")) {
System.out.println("正在重新载入......");
Thread.currentThread().sleep(1000);
continue;
} else {
System.out.println("正在退出系统......");
Thread.currentThread().sleep(1000);
break;
}
}
} catch (SQLException | InterruptedException e) {
}
}
// 修改管理员密码
public static void changeAdminPassword(Connection conn) {
int admin_password_old = 0;
Scanner sc = new Scanner(System.in);
try {
Statement statement = conn.createStatement();//先查询账户
boolean b = statement.execute("SELECT admin_password from admin where admin_id = '" + admin_card + "'");
if (b) {
ResultSet rs = statement
.executeQuery("SELECT admin_password from admin where admin_id = '" + admin_card + "'");
rs.next();
admin_password_old = rs.getInt(1);
CallableStatement cs = conn.prepareCall("call changeadminpwd(?,?)");//调用数据库中修改管理员密码存储过程
boolean flag = true;
int admin_try_count_left = 3;
while (flag) {
System.out.print("输入6位数字旧密码 ");
int new_f_pass_input = 3;
int new_f_password = 0;
int new_pass_count = 3;
while (true) {
try {
new_f_password = sc.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入六位数字密码 ");
String input = sc.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
} else
continue;
}
break;
}
if (admin_password_old == new_f_password) {
while (true) {
System.out.println("新的管理员密码:");
int new_f_admin_password = 0;
while (true) {
try {
new_f_admin_password = sc.nextInt();
} catch (InputMismatchException e) {
System.out.print("请重新输入六位数字管理密码: ");
String input1 = sc.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input1).matches();
if (digit) {
break;
} else
continue;
}
break;
}
int len = Integer.toString(new_f_admin_password).length();
if (len != 6) {
--new_f_pass_input;
System.out.println("密码位数错误,剩余次数: " + new_f_pass_input);
if (new_f_pass_input == 0) {
System.out.println("密码输入次数用完,正在退出...");
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
continue;
} else {
System.out.println("确认新的管理员密码: ");
int new_s_admin_password = 0;
while (true) {
try {
new_s_admin_password = sc.nextInt();
} catch (InputMismatchException e) {
System.out.print("请重新输入六位数字管理密码:");
String input1 = sc.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input1).matches();
if (digit) {
break;
} else
continue;
}
break;
}
if (new_s_admin_password == new_f_admin_password) {
cs.setInt(1, new_s_admin_password);
cs.setString(2, admin_card);
cs.execute();
System.out.println("修改成功");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
flag = false;
break;
} else {
System.out.println("密码两次不相同");
--new_pass_count;
if (new_pass_count == 0) {
flag = false;
break;
}
continue;
}
}
}
} else {
--admin_try_count_left;
if (admin_try_count_left == 0) {
try {
System.out.println("旧密码输入次数用完,正在退出...");
sleep(1000);
break;
} catch (InterruptedException e) {
e.printStackTrace();
}
flag = false;
}
System.out.println("密码错误,请重新输入" + " " + "剩余次数" + admin_try_count_left);
continue;
}
break;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 修改用户密码
//与Atm类修改密码大致相同
public static void changeUserPassword(Connection conn) {
int user_card = 0;
try {
// 选择账号
while (true) {
boolean card = false;
Scanner console = new Scanner(System.in);
System.out.println("请输入用户账号:");
while (true) {
try {
user_card = console.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入正确类型的账号:");
String input = console.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
} else
continue;
}
break;
}
Statement st = conn.createStatement();
st.execute("select * from user");
ResultSet rs = st.getResultSet();
while (rs.next()) {
if (user_card == rs.getInt(1)) {
card = true;
break;
} else {
card = false;
}
}
if (card) {
// 修改密码
boolean b = st.execute("select user_password from user where user_card = " + user_card + "");
if (b) {
ResultSet rs2 = st.getResultSet();
rs2.next();
int oldPassword = rs2.getInt("user_password");
boolean user_flag = true;
int login_count = 3;
int new_pass_count = 3;
int new_one_pass = 3;
while (user_flag) {
Scanner sc = new Scanner(System.in);
System.out.print("输入6位数字旧密码 ");
int passwd = 0;
boolean isDigit = true;
while (isDigit) {
try {
passwd = sc.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入六位数字密码 ");
String input = sc.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
} else
continue;
}
break;
}
if (oldPassword == passwd) {
while (true) {
System.out.print("新密码 ");
int newpassword = 0;
while (isDigit) {
try {
newpassword = sc.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入六位数字密码 ");
String input1 = sc.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input1).matches();
if (digit) {
break;
} else
continue;
}
break;
}
int len = Integer.toString(newpassword).length();
if (len < 6) {
--new_one_pass;
System.out.println("密码位数错误,剩余次数" + new_one_pass);
if (new_one_pass == 0) {
System.out.println("密码输入次数用完,正在退出...");
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
continue;
} else {
System.out.print("确认新密码 ");
int newpassword2 = 0;
while (isDigit) {
try {
newpassword2 = sc.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入六位数字密码 ");
String input1 = sc.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input1).matches();
if (digit) {
break;
} else
continue;
}
break;
}
if (newpassword == newpassword2) {
st.execute("update user set user_password='" + newpassword
+ "' where user_card = " + user_card + "");
System.out.println("修改成功");
user_flag = false;
break;
} else {
System.out.println("密码两次不相同");
--new_pass_count;
if (new_pass_count == 0) {
user_flag = false;
break;
}
continue;
}
}
}
} else {
--login_count;
if (login_count == 0) {
try {
System.out.println("旧密码输入次数用完,正在退出...");
sleep(1000);
break;
} catch (InterruptedException e) {
e.printStackTrace();
}
user_flag = false;
}
System.out.println("密码错误,请重新输入" + " " + "剩余次数" + login_count);
continue;
}
break;
}
}
} else {
System.out.println("用户卡号错误");
continue;
}
System.out.println("是否继续修改用户密码:Y/任意键退出?");
Scanner sc = new Scanner(System.in);
String inputY = sc.next();
if (inputY.toUpperCase().equals("Y")) {
continue;
} else
break;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//根据卡号查询用户信息
public static void select_by_card(Connection conn) {
while (true) {
boolean card = false;
try {
Scanner console = new Scanner(System.in);
System.out.println("请输入您要查询的卡号:");
int user_card = 0;
while (true) {
try {
user_card = console.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入正确类型的卡号:");
String input = console.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
} else
continue;
}
break;
}
Statement stmt = conn.createStatement();
stmt.execute("select * from user");
ResultSet rs = stmt.getResultSet();
while (rs.next()) {
if (user_card == rs.getInt(1)) {
card = true;
break;
} else {
card = false;
}
}
if (card == true) {
System.out.println("正在查询......");
Thread.currentThread().sleep(1000);
stmt.execute("select * from user where user_card = '" + user_card + "'");
rs = stmt.getResultSet();
rs.next();
String action;
if (rs.getInt(5) == 1) {
action = "正常";
} else {
action = "冻结";
}
System.out.println("查询成功");
System.out.println("卡号:" + rs.getInt(1) + " 姓名:" + rs.getString(3) + " 余额:" + rs.getInt(4)
+ " 账户状态:" + action);
System.out.println("是否继续查询?Y/任意键退出系统");
String in_yn = console.next();
if (in_yn.toUpperCase().equals("Y")) {
System.out.println("正在重新载入查询系统......");
Thread.currentThread().sleep(1000);
continue;
} else {
System.out.println("正在退出查询系统......");
Thread.currentThread().sleep(1000);
break;
}
} else {
System.out.println("账号不存在");
System.out.println("是否继续查询?Y/任意键退出系统");
String in_yn = console.next();
if (in_yn.toUpperCase().equals("Y")) {
System.out.println("正在重新载入查询系统......");
Thread.currentThread().sleep(1000);
continue;
} else {
System.out.println("正在退出查询系统......");
Thread.currentThread().sleep(1000);
break;
}
}
} catch (SQLException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 密码修改主菜单
public static void menuChangePwd(Connection conn) {
while (true) {
System.out.println("*--------密码修改 -------");
System.out.println("*-1、修改当前管理员密码 ");
System.out.println("*-2、修改用户密码");
System.out.println("*-3、修改用户状态");
System.out.println("*-4、返回上一级");
System.out.println();
System.out.print("输入要选择的业务:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
switch (n) {
case 1:
changeAdminPassword(conn);//修改管理员密码方法
continue;
case 2:
changeUserPassword(conn);//修改用户密码方法
continue;
case 3:
change_action(conn);//修改用户状态方法
continue;
case 4:
System.out.println("正在退出密码修改系统......");//返回上一级
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
System.out.println("输入指令有误,请重新输入");
continue;
}
break;
}
}
// 销户
//先查询是否有该账户,再调用数据库中删除用户的存储过程
public static void delete_user(Connection conn) {
//首先判断卡号
boolean card = false;
while (true) {
System.out.println("请输入销户的账户");
Scanner sc = new Scanner(System.in);
int user_card = 0;
boolean isDigit = true;
while (isDigit) {
try {
user_card = sc.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入六位数字卡号:");
String input = sc.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
} else
continue;
}
break;
}
try {
Statement st = conn.createStatement();
st.execute("select * from user");
ResultSet rs = st.getResultSet();
while (rs.next()) {
if (user_card == rs.getInt(1)) {
card = true;
break;
}
}
if (card) {
while (true) {
System.out.println("请输入密码:");
Scanner sc2 = new Scanner(System.in);
int user_password = 0;
boolean isDigit2 = true;
while (isDigit2) {
try {
user_password = sc2.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入六位数字密码:");
String input = sc.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
} else
continue;
}
break;
}
Statement st2 = conn.createStatement();
st2.execute("select user_password from user where user_card =" + user_card);//查询用户
ResultSet rs2 = st2.getResultSet();
rs2.next();
int password = rs2.getInt(1);
if (user_password == password) {
while (true) {
System.out.println("正在查找账户......");
sleep(1000);
System.out.println("找到账户");
st2.execute("select * from user where user_card = " + user_card);
rs2 = st2.getResultSet();
rs2.next();
String action;
if (rs2.getInt(5) == 1) {
action = "正常";
} else {
action = "冻结";
}
System.out.println("卡号:" + rs2.getInt(1) + " 姓名:" + rs2.getString(3) + " 余额:"
+ rs2.getInt(4) + " 状态:" + action);
System.out.println("是否确认销户:Y/任意键退出");
Scanner console = new Scanner(System.in);
String in_yn = console.next();
if (in_yn.toUpperCase().equals("Y")) {
System.out.println("正在销户......");
sleep(1000);
Statement st3 = conn.createStatement();
st3.execute("delete from user where user_card =" + user_card + "");
System.out.println("销户成功");
break;
} else {
System.out.println("正在退出");
Thread.currentThread().sleep(1000);
break;
}
}
} else {
System.out.println("密码错误,重新输入");
continue;
}
break;
}
Scanner console = new Scanner(System.in);
System.out.println("是否继续销户?Y/任意键退出");
String in_yn = console.next();
if (in_yn.toUpperCase().equals("Y")) {
System.out.println("正在重新载入......");
Thread.currentThread().sleep(1000);
continue;
} else {
System.out.println("正在退出系统......");
Thread.currentThread().sleep(1000);
break;
}
} else {
System.out.println("请重新输入正确卡号");
continue;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
5、管理端界面类
package com.jdbc.bank;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.regex.Pattern;
import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException;
public class AdminStart {
static Connection conn = null;
static ResultSet rs = null;
public static void AdminBegin(Connection conn) {
while (true) {
// 得到数据库连接
Scanner console = new Scanner(System.in);
try {
while (true) {
System.out.println("----欢迎使用澳门赌场 管理 系统----");
boolean flag = Admin.login(conn);//调用管理类的认证方法
while (flag) {
System.out.println("----正在进入系统......----");
Thread.currentThread().sleep(1000);
System.out.println();
System.out.println("----成功进入系统!----");
while (true) {
int in_num = 0;
Admin.menu1();//调用菜单
while (true) {
try {
in_num = console.nextInt();
} catch (InputMismatchException e) {//输入指令判断
System.out.print("请输入正确的数字指令:");
String input = console.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
} else
continue;
}
break;
}
switch (in_num) {
case 1:// 查询用户信息及ATM
System.out.println();
Admin.menu_by_select(conn);//调用查询菜单
System.out.println();
continue;
case 2:// 开户
Admin.menu_by_insert(conn);//调用开户菜单
continue;
case 3:// 修改用户及管理密码
Admin.menuChangePwd(conn);//调用修改菜单
continue;
case 4:// 销户
Admin.delete_user(conn);//调用销户方法
continue;
case 5:// 注销
System.out.println("正在退出......");//退出系统
Thread.currentThread().sleep(1000);
System.out.println("一路走好");
System.out.println();
break;
default:
System.out.println("输入指令有误,请重新输入");
continue;
}
break;
}
break;
}
System.out.println("是否继续使用澳门赌场 管理 系统?Y/任意键退出");
String enter = console.next();
if (enter.toUpperCase().equals("Y")) {
continue;
} else {
System.out.println("正在退出管理系统......");
Thread.currentThread().sleep(1000);
System.out.println("欢迎再来哦~~~");
break;
}
}
break;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
6、开始主界面类
package com.jdbc.bank;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.regex.Pattern;
import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException;
public class Start {
static Connection conn = null;
public static void main(String[] args) {
Conn_Start();
}
public static void menu() {
System.out.println("-----欢迎光临澳门赌场-----");
System.out.println("1、进入管理端");
System.out.println("2、进入终端");
System.out.println("3、退出系统");
System.out.println("请输入指令进行操作:");
}
public static void Conn_Start() {
// TODO Auto-generated method stub
while (true) {
try {
Scanner console = new Scanner(System.in);
System.out.println("请输入数据库地址:");
String ip;
while (true) {//判断输入数据库地址
ip = console.next();
String reg = "\\d+\\.\\d+\\.\\d+\\.\\d+";
if (!(ip.matches(reg))) {
System.out.println("请输入正确的IP地址:");
continue;
} else {
break;
}
}
System.out.println("正在连接数据库.....");
Thread.currentThread().sleep(1000);
conn = JdbcConn.getConnection(ip);
while (true) {
menu();//调用开始菜单
int in_num = 0;
while (true) {
try {
in_num = console.nextInt();
} catch (InputMismatchException e) {
System.out.print("请输入正确的数字指令:");
String input = console.next();
Pattern pattern = Pattern.compile("[0-9]*");
boolean digit = pattern.matcher(input).matches();
if (digit) {
break;
}else continue;
}
break;
}
switch (in_num) {
case 1://进入管理端
System.out.println("正在进入管理端......");
Thread.currentThread().sleep(1000);
AdminStart.AdminBegin(conn);
continue;
case 2://进入用户端
System.out.println("正在进入终端......");
Thread.currentThread().sleep(1000);
UserStart.UserBegin(conn);
continue;
case 3://退出系统
System.out.println("正在退出......");
Thread.currentThread().sleep(1000);
System.out.println("再见~~~~~~~");
break;
default:
System.out.println("输入指令有误,请重新输入:");
continue;
}
break;
}
break;
} catch (CommunicationsException e) {//地址出错异常
System.out.println("数据库地址输入有误");
continue;
} catch (SQLException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
测试
这里我是把整个程序打包成了jar包进行测试,大家可以就在eclipse控制台进行测试即可
一、开始
二、终端
这里我把卡号输入错误与冻结账户不能登录演示了
1、查询用户信息
2、存款
3、取款
4、转账
5、修改密码
6、退卡
三、管理端
1、查询所有用户信息及ATM
(1)、查询所有用户信息
(2)根据卡号查询
(3)查询ATM余额
【1】往ATM放钱
【2】从ATM取钱
2、用户开户及增加管理员
(1)用户开户
(2)增加管理员
3、修改用户信息及密码
(1)修改当前管理员密码
(2)修改用户密码
(3)修改用户状态
4、销户
5、注销
注:上面所有的功能都有输入类型的判断,输入指令判断,以及输错密码三次判断,为了截图,所以没有演示这些功能
总结
至此,所有的功能已完成,对于初学者来说可能需要两三天来做这个系统,但是相信我,能做出来这个系统且思路清晰,你的JDBC也算是比较熟练了,当然需要完全掌握JDBC与MySQL还需要更大的项目练习,下次更新SQL命令的常见面试题,谢谢。