ATM简易模拟更新 2.0 控制台输出 SQL数据库

刚学完数据库和JDBC  又重写了一遍ATM  以后再也不写ATM了 在写就要吐了   

数据库使用sql的 语句如下:

create database ATM
use ATM
create table TBL_ACCOUNT
(
ID   int,
PASSWORD  int,
BALENCT  decimal(18,2)
)

就是建一个数据库 里面有一个表 表里有三个字段分别是 账号 密码 余额 余额的单词打错了  将错就错 不改了


java语句如下:

package com.hyjy.atm;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;


public class Atm {


Scanner in = new Scanner(System.in);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
Integer idx = 0;


public void welcom() throws Exception { // 欢迎界面
System.out.println("——————————欢迎光临民生银行——————————");
System.out.println("*******************************");
System.out.println("请选择操作:【1】 登陆    【2】 注册   【3】退出");
int num = in.nextInt();
while (true) { // 判断输入的操作编号是否正确
if (num >= 1 && num <= 3) {
switch (num) { // 编号正确 按照编号调用相应的方法
case 1:
logIn();
break; // 进入登录界面
case 2:
sign();
break; // 进入注册界面
case 3:
System.out.println("谢谢使用,欢迎下次光临 ,再见!");
break; // 退出程序
}
break;
} else {
System.out.println("输入错误 ,请重新输入:");
num = in.nextInt();
continue;
}
}
}


public void logIn() throws Exception {
System.out.println("——————————欢迎光临民生银行——————————");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 加载数据库驱动
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=ATM"; // 创建url字符串
conn = DriverManager.getConnection(url, "sa", "sa"); // 连接数据库
stmt = conn.createStatement(); // 创建数据库操作对象
System.out.println("请输入账号:");
System.out.println("或按【ESC】返回欢迎界面"); // 退出按钮
String id = in.next();
idx = idx.valueOf(id);
if (id.equals("ESC")) {
welcom();
}
int i = 1;
while (true) {
rs = stmt
.executeQuery("select PASSWORD from TBL_ACCOUNT where ID= "
+ id); // 判断账号是否存在
if (rs.next()) { // 返回的实例中有元素
System.out.println("请输入密码:"); // 进行密码判断
System.out.println("或按【ESC】返回欢迎界面");
String password = in.next();
if (password.equals("ESC")) {
welcom();
}
while (true) {
if (rs.getString("PASSWORD").equals(password))
// 判断密码是否正确
{
System.out.println("登陆成功!");
rs.close();
stmt.close();
conn.close();
menu();
break;
} else { // 输入错误的处理
i++;
while (i == 4) {
System.out
.println("密码三次输入错误,该账号已经锁定,请带本人身份证到当地营业厅办理解锁手续。");
welcom();
}
System.out.println("密码输入错误,三次输入错误账号将锁定,请重新输入:");
System.out.println("或按【ESC】返回欢迎界面");
password = in.next();
if (password.equals("ESC")) {
welcom();
}
System.out.println("已经输入错误" + i + "次, " + "还有 "
+ (3 - i) + "次机会。");
continue;
}
}
break;
} else {
System.out.println("该账户不存在,请重新输入:");
System.out.println("或按【ESC】返回欢迎界面");
id = in.next();
idx = idx.valueOf(id);
if (id.equals("ESC")) {
welcom();
}
continue;
}
}


}


public void sign() throws Exception {
System.out.println("——————————欢迎光临民生银行——————————");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 加载数据库驱动
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=ATM"; // 创建url字符串
conn = DriverManager.getConnection(url, "sa", "sa"); // 连接数据库
stmt = conn.createStatement(); // 创建数据库操作对象


System.out.println("请输入账号:");
int id = in.nextInt();
while (true) {
rs = stmt.executeQuery("select ID from TBL_ACCOUNT where ID = "
+ id);
if (rs.next()) {
System.out.println("账号已存在,请重新输入:");
id = in.nextInt();
continue;
} else {
System.out.println("请输入密码:");
int password = in.nextInt();
System.out.println("请再输一次密码:");
int password1 = in.nextInt();
while (true) {
if (password == password1) {
stmt.executeUpdate("insert into TBL_ACCOUNT values('"
+ id + "','" + password + "','0')");
rs.close();
stmt.close();
conn.close();
System.out.println("注册成功!");
welcom();
break;
} else {
System.out.println("两次输入的密码不一致,请重新输入密码:");
password = in.nextInt();
System.out.println("请再输一次密码:");
password1 = in.nextInt();
continue;
}
}
break;
}
}


}


public void menu() throws Exception {
System.out.println("——————————欢迎光临民生银行——————————");
System.out.println("请选择操作:【1】取款  【2】存款 【3】查询 【4】修改密码 【5】转账 【6】退出");
int num = in.nextInt();
while (true) {
if (num > 6 || num < 1) {
System.out.println("输入有误,请重新输入:");
num = in.nextInt();
continue;
} else {
switch (num) {
case 1:
draw();
break;
case 2:
deposit();
break;
case 3:
select();
break;
case 4:
changePassword();
break;
case 5:
transfer();
break;
case 6:
welcom();
break;
}
break;
}
}
}


public void draw() throws Exception {
System.out.println("——————————欢迎光临民生银行——————————");
System.out
.println("请选择取款金额:【1】100   【2】200  【3】500 【4】1000  【5】2000 【6】其他金额 【7】退出");
int num = in.nextInt();
while (true) {
if (num > 7 || num < 1) {
System.out.println("输入有误,请重新输入:");
num = in.nextInt();
continue;
} else {
switch (num) {
case 1: {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 加载数据库驱动
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=ATM"; // 创建url字符串
conn = DriverManager.getConnection(url, "sa", "sa"); // 连接数据库
stmt = conn.createStatement(); // 创建数据库操作对象
rs = stmt
.executeQuery("select * from TBL_ACCOUNT where ID = "
+ idx);
if (rs.next()) {
if (rs.getInt("BALENCT") - 100 > 0) {
stmt.executeUpdate("update TBL_ACCOUNT set BALENCT = BALENCT - 100 where ID = "
+ idx);
System.out.println("取款成功");
rs.close();
stmt.close();
conn.close();
menu();
} else {
System.out.println("余额不足,取款失败");
draw();
}
break;
}
}
case 2: {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 加载数据库驱动
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=ATM"; // 创建url字符串
conn = DriverManager.getConnection(url, "sa", "sa"); // 连接数据库
stmt = conn.createStatement(); // 创建数据库操作对象
rs = stmt
.executeQuery("select * from TBL_ACCOUNT where ID = "
+ idx);
if (rs.next()) {
if (rs.getInt("BALENCT") - 100 > 0) {
stmt.executeUpdate("update TBL_ACCOUNT set BALENCT = BALANCT - 100 where ID = "
+ idx);
System.out.println("取款成功");
rs.close();
stmt.close();
conn.close();
menu();
} else {
System.out.println("余额不足,取款失败");
draw();
}
break;
}
}
case 3: {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 加载数据库驱动
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=ATM"; // 创建url字符串
conn = DriverManager.getConnection(url, "sa", "sa"); // 连接数据库
stmt = conn.createStatement(); // 创建数据库操作对象
rs = stmt
.executeQuery("select * from TBL_ACCOUNT where ID = "
+ idx);
if (rs.next()) {
if (rs.getInt("BALENCT") - 100 > 0) {
stmt.executeUpdate("update TBL_ACCOUNT set BALENCT = BALANCT - 100 where ID = "
+ idx);
System.out.println("取款成功");
rs.close();
stmt.close();
conn.close();
menu();
} else {
System.out.println("余额不足,取款失败");
draw();
}
break;
}
}
case 4: {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 加载数据库驱动
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=ATM"; // 创建url字符串
conn = DriverManager.getConnection(url, "sa", "sa"); // 连接数据库
stmt = conn.createStatement(); // 创建数据库操作对象
rs = stmt
.executeQuery("select * from TBL_ACCOUNT where ID = "
+ idx);
if (rs.next()) {
if (rs.getInt("BALENCT") - 100 > 0) {
stmt.executeUpdate("update TBL_ACCOUNT set BALENCT = BALENCT - 100 where ID = "
+ idx);
System.out.println("取款成功");
rs.close();
stmt.close();
conn.close();
menu();
} else {
System.out.println("余额不足,取款失败");
draw();
}
break;
}
}
case 5: {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 加载数据库驱动
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=ATM"; // 创建url字符串
conn = DriverManager.getConnection(url, "sa", "sa"); // 连接数据库
stmt = conn.createStatement(); // 创建数据库操作对象
rs = stmt
.executeQuery("select * from TBL_ACCOUNT where ID = "
+ idx);
if (rs.next()) {
if (rs.getInt("BALENCT") - 100 > 0) {
stmt.executeUpdate("update TBL_ACCOUNT set BALENCT = BALENCT - 100 where ID = "
+ idx);
System.out.println("取款成功");
rs.close();
stmt.close();
conn.close();
menu();
} else {
System.out.println("余额不足,取款失败");
draw();
}
break;
}
}
case 6: {
System.out.println("请输入取款金额:");
System.out.println("只能取100的整数倍的金额");
int outNum = in.nextInt();
while (true) {
if (outNum >= 100 && outNum % 100 == 0) {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 加载数据库驱动
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=ATM"; // 创建url字符串
conn = DriverManager.getConnection(url, "sa", "sa"); // 连接数据库
stmt = conn.createStatement(); // 创建数据库操作对象
rs = stmt
.executeQuery("select * from TBL_ACCOUNT where ID = "
+ idx);
if (rs.next()) {
if (rs.getInt("BALENCT") - outNum > 0) {
stmt.executeUpdate("update TBL_ACCOUNT set BALENCT = BALENCT - "
+ outNum + " where ID = " + idx);
System.out.println("取款成功,当前余额为:"
+ rs.getInt("BALENCT"));
rs.close();
stmt.close();
conn.close();
menu();
} else {
System.out.println("余额不足,取款失败");
draw();
}
break;
} else {
System.out.println("金额输入有误,请输入100的倍数:");
outNum = in.nextInt();
continue;
}
}


}
break;
}
case 7:
menu();
break;
}
break;
}
}
}


public void deposit() throws Exception {
System.out.println("——————————欢迎光临民生银行——————————");
System.out.println("请输入存款金额(100的整数倍):");
int inNum = in.nextInt();
while (true) {
if (inNum >= 100 && inNum % 100 == 0) {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 加载数据库驱动
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=ATM"; // 创建url字符串
conn = DriverManager.getConnection(url, "sa", "sa"); // 连接数据库
stmt = conn.createStatement(); // 创建数据库操作对象


stmt.executeUpdate("update TBL_ACCOUNT set BALENCT = BALENCT + "
+ inNum + " where ID = " + idx);
rs = stmt.executeQuery("select * from TBL_ACCOUNT where ID = "
+ idx);
if (rs.next()) {
System.out.println("存款成功,当前余额为:" + rs.getInt("BALENCT"));
}
rs.close();
stmt.close();
conn.close();
menu();
break;
} else {
System.out.println("金额输入有误,请输入100的倍数:");
inNum = in.nextInt();
continue;
}
}
}


public void select() throws Exception {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 加载数据库驱动
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=ATM"; // 创建url字符串
conn = DriverManager.getConnection(url, "sa", "sa"); // 连接数据库
stmt = conn.createStatement(); // 创建数据库操作对象
rs = stmt.executeQuery("select * from TBL_ACCOUNT where ID = " + idx);
if (rs.next()) {
System.out.println("账户当前余额为:" + rs.getInt("BALENCT"));
}
rs.close();
stmt.close();
conn.close();
menu();


}


public void changePassword() throws Exception {
System.out.println("请输入新的密码:");
int num = in.nextInt();
System.out.println("请输再输入一遍:");
int num1 = in.nextInt();
while (true) {
if (num == num1) {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 加载数据库驱动
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=ATM"; // 创建url字符串
conn = DriverManager.getConnection(url, "sa", "sa"); // 连接数据库
stmt = conn.createStatement(); // 创建数据库操作对象
stmt.executeUpdate("update TBL_ACCOUNT set PASSWORD = " + num
+ "where ID= " + idx);
System.out.println("密码修改成功!请牢记新密码。");
rs.close();
stmt.close();
conn.close();
menu();
break;
} else {
System.out.println("两次密码输入不一致,请重新输入:");
num = in.nextInt();
System.out.println("请再输入一遍:");
num1 = in.nextInt();
continue;
}
}


}


public void transfer() throws Exception {
System.out.println("请输入对方账号:");
int num1 = in.nextInt();
while (true) {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 加载数据库驱动
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=ATM"; // 创建url字符串
conn = DriverManager.getConnection(url, "sa", "sa"); // 连接数据库
stmt = conn.createStatement(); // 创建数据库操作对象
rs = stmt.executeQuery("select ID from TBL_ACCOUNT where ID = "
+ num1);
if (rs.next()) {
System.out.println("请输入转账金额:");
int tra = in.nextInt();
stmt.executeUpdate("update TBL_ACCOUNT set BALENCT = BALENCT + "
+ tra + "where ID = " + num1);

stmt.executeUpdate("update TBL_ACCOUNT set BALENCT = BALENCT - "
+ tra + "where ID = " + idx);
System.out.println("转账成功!");
rs.close();
stmt.close();
conn.close();
menu();
break;
} else {
System.out.println("该账号不存在,请重新输入:");
num1 = in.nextInt();
continue;
}
}
}
}




注释不多  还没养成三行一注释的习惯 以后要多加注释!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值