第一次学Java,写的比较拙劣
test类
package bank;
import java.util.Scanner;
public class test {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("0.退出\t1.管理员\t2.普通用户\n请选择你的身份:");
int identity = sc.nextInt();
MySQL mysql = new MySQL();
SqlUser sqluser = new SqlUser();
mysql.runOneTime();//载入全部信息只运行一次
if (identity == 1) {
mysql.SqlAdminLogin();
mysql.SqlAdminQuery();
} else if(identity == 2){
sqluser.SqlUserLogin();
}else if(identity == 0) {
System.out.println("****感谢使用,期待下次合作!****");
System.exit(0);
}else {
System.out.println("出错误啦~");
}
sc.close();
}
}
SqlUser类
package bank;
import java.sql.*;
import java.util.*;
public class SqlUser {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
Scanner sc = new Scanner(System.in);
int CardID = 0;
public void SqlUserLogin() throws SQLException {
int psw;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/BankDB?useSSL=false&&serverTimezone=GMT%2B8";
String username = "root";
String password = "078772";
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
System.out.print("请输入你的卡号:");
CardID = sc.nextInt();
System.out.print("请输入你的密码:");
psw = sc.nextInt();
String sqlSingle = "select * from BankDB where CardID = " + CardID + " AND Password =" + psw + "";
rs = stmt.executeQuery(sqlSingle);
String name = null;
String sex = null;
while (rs.next()) {
name = rs.getString("Username");
sex = rs.getString("Sex");
}
while (true) {
if (name == null) {
System.out.println("**您还没有办理服务或输入有错哦!**\n您要创建新用户吗?\n1.不,我再试试\t2.好\t3.直接退出");
int choice = sc.nextInt();
switch (choice) {
case 1: {
SqlUserLogin();
break;
}
case 2: {
inputWrite();
break;
}
case 3: {
System.out.println("***即将退出系统***");
System.exit(0);
}
default: {
System.out.println("***输入错误***");
}
}
} else {
System.out.println("Yuelin智能ATMer欢迎" + name + sex + "士!");
System.out.print("1.存款\n2.取款\n3.查询\n4.修改密码\n0.退出\n请输入您的选择:");
int userChoice = sc.nextInt();
switch (userChoice) {
case 1: {
SqlUserSave();
break;
}
case 2: {
SqlUserWithdraw();
break;
}
case 3: {
SqlUserQuery();
break;
}
case 4: {
SqlUserChangePsw();
break;
}
case 0: {
System.exit(0);
}
}
}
}
} catch (Exception e) {
System.out.println("这里有一些错误哦~");
e.printStackTrace();
} finally {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
}
}
public void inputWrite() throws SQLException {
String name = null, sex = null;
int psw, id = 0;
int count = 0, newcardId = 0;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/BankDB?useSSL=false&&serverTimezone=GMT%2B8";
String username = "root";
String password = "078772";
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
String sqlFirst = "select count(1) from BankDB ID";
rs = stmt.executeQuery(sqlFirst);
while (rs.next()) {
count = rs.getInt(1);
}
if (count == 0) {
newcardId = 67400;
} else {
id = count + 1;
newcardId = 67400 + id;
}
String blanckFirst = sc.nextLine();
System.out.println("请输入你要创建的用户名:");
name = sc.nextLine();
System.out.println("请输入你要设置的密码:");
psw = sc.nextInt();
String blankSecond = sc.nextLine();
System.out.println("请输入你的性别:");
sex = sc.nextLine();
System.out.println("您的卡号为" + newcardId);
String sql = "Insert into BankDB (ID,CardID,Username,Sex,Password) values (" + id + "," + newcardId + ",'"
+ name + "','" + sex + "'," + psw + ")";
stmt.executeUpdate(sql);
System.out.println("******您的信息已录入!******");
} catch (Exception e) {
System.out.println("******这里有一些错误哦~******");
e.printStackTrace();
} finally {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
}
}
public void SqlUserSave() throws SQLException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/BankDB?useSSL=false&&serverTimezone=GMT%2B8";
String admin = "root";
String psw = "078772";
conn = DriverManager.getConnection(url, admin, psw);
stmt = conn.createStatement();
String sql = "select * from BankDB CardID WHERE CardID = " + CardID + "";
rs = stmt.executeQuery(sql);
double balance = 0.00;
double money = 0.00;
while (rs.next()) {
balance = rs.getDouble("Balance");
}
System.out.print("请输入您要存入的金额:");
money = sc.nextDouble();
if (money > 0) {
balance = balance + money;
String sqlChange = "update BankDB set Balance = " + balance + " WHERE CardID = " + CardID + " ";
stmt.executeUpdate(sqlChange);
System.out.println("******" + balance + "元已存入******");
} else {
System.out.println("****您的输入有误!****");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null)
conn.close();
if (stmt != null)
stmt.close();
if (rs != null)
rs.close();
}
}
public void SqlUserWithdraw() throws SQLException {
double money = 0.00;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/BankDB?useSSL=false&&serverTimezone=GMT%2B8";
String admin = "root";
String psw = "078772";
conn = DriverManager.getConnection(url, admin, psw);
stmt = conn.createStatement();
String sql = "select * from BankDB CardID WHERE CardID = " + CardID + "";
rs = stmt.executeQuery(sql);
String name = null, password = null, sex = null;
double balance = 0;
while (rs.next()) {
name = rs.getString("Username");
password = rs.getString("Password");
sex = rs.getString("Sex");
balance = rs.getDouble("Balance");
}
System.out.println("****************************\n您的账户情况如下:");
System.out.println("姓名:" + name);
System.out.println("卡号:" + CardID);
System.out.println("密码:" + password);
System.out.println("性别:" + sex);
System.out.println("余额:" + balance);
System.out.println("***************************");
System.out.print("请输入您要取出的金额:");
money = sc.nextDouble();
balance = balance - money;
if (balance >= 0) {
String sqlChange = "update BankDB set Balance = " + balance + " WHERE CardID = " + CardID + " ";
stmt.executeUpdate(sqlChange);
System.out.println("******" + balance + "元已取出******");
} else {
System.out.println("****您的账户里还没有这么多钱哦~努力挣钱吧~****");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null)
conn.close();
if (stmt != null)
stmt.close();
if (rs != null)
rs.close();
}
}
public void SqlUserQuery() throws SQLException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/BankDB?useSSL=false&&serverTimezone=GMT%2B8";
String admin = "root";
String psw = "078772";
conn = DriverManager.getConnection(url, admin, psw);
stmt = conn.createStatement();
String sql = "select * from BankDB CardID WHERE CardID = " + CardID + "";
rs = stmt.executeQuery(sql);
String name = null, sex = null;
double balance = 0;
int password = 0;
while (rs.next()) {
name = rs.getString("Username");
password = rs.getInt("Password");
sex = rs.getString("Sex");
balance = rs.getDouble("Balance");
}
System.out.println("****************************\n您的账户情况如下:");
System.out.println("姓名:" + name);
System.out.println("卡号:" + CardID);
System.out.println("密码:" + password);
System.out.println("性别:" + sex);
System.out.println("余额:" + balance);
System.out.println("***************************");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null)
conn.close();
if (stmt != null)
stmt.close();
if (rs != null)
rs.close();
}
}
public void SqlUserChangePsw() throws SQLException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/BankDB?useSSL=false&&serverTimezone=GMT%2B8";
String admin = "root";
String psw = "078772";
conn = DriverManager.getConnection(url, admin, psw);
stmt = conn.createStatement();
String sql = "select * from BankDB CardID WHERE CardID = " + CardID + "";
rs = stmt.executeQuery(sql);
String name = null, sex = null;
int password = 0;
while (rs.next()) {
name = rs.getString("Username");
password = rs.getInt("Password");
sex = rs.getString("Sex");
}
System.out.println("****************************");
while (true) {
System.out.println("请重新输入您的密码:");
int passwordtwice = sc.nextInt();
if (passwordtwice == password) {
System.out.println("***************************\n请输入您的新密码:");
password = sc.nextInt();
String sqlChange = "update BankDB set Password = " + password + " WHERE CardID = " + CardID + " ";
stmt.executeUpdate(sqlChange);
System.out.println("******尊敬的" + name + sex + "士您的密码已修改******");
break;
} else {
System.out.println("有错误哦~请重新输入:");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null)
conn.close();
if (stmt != null)
stmt.close();
if (rs != null)
rs.close();
}
}
}
MYSQL类
package bank;
import java.io.RandomAccessFile;
import java.sql.*;
import java.util.Map;
import java.util.Scanner;
public class MySQL {
Map<Integer, Users> users = BankDB.getDb().getUsers();
public Users user;
Object[] cardID = users.keySet().toArray();
Scanner sc = new Scanner(System.in);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public void sqlStart() throws SQLException{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/BankDB?useSSL=false&&serverTimezone=GMT%2B8";
String username = "root";
String password = "078772";
conn = DriverManager.getConnection(url,username,password);
stmt = conn.createStatement();
for (int i = 1;i<=users.size()-1;i++) {
int cardId = Integer.parseInt(cardID[i].toString());
user = BankDB.getDb().getUser(cardId);
String sql = "Insert into BankDB (ID,CardID,Username,Sex,Password,Balance) values ("+i+","+cardID[i]+",'"+user.getName()+"','"+user.getSex()+"',"+user.getPassWord()+","+user.getBalance()+")";
stmt.executeUpdate(sql);
}
//executeUpdate 增删改
//查用executeQuery
}catch(Exception e) {
System.out.println("未能成功加载驱动程序,请检查是否导入驱动程序!");
e.printStackTrace();
}finally {
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}
}
public void SqlAdminLogin() {
System.out.println("查看全部成员的信息");
//输入root账号密码查看全部信息
}
public void SqlAdminQuery() throws Exception {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/BankDB?useSSL=false&&serverTimezone=GMT%2B8";
System.out.print("我们要先获得权限\n请输入您的用户名:");// root
String name = sc.nextLine();
System.out.print("请输入您的密码:");// 078772
String psw = sc.nextLine();
if (name.equals("root") && psw.equals("root")) {
conn = DriverManager.getConnection(url, name, psw);
stmt = conn.createStatement();
String sqlSingle = "select * from BankDB ";
System.out.println("亲爱的管理员,感谢您的到来!\n以下是数据库内的全部信息:");
rs = stmt.executeQuery(sqlSingle);
System.out.println("id\t|username\t|sex\t|cardId\t|password\t|balance\t|");
while (rs.next()) {
int id = rs.getInt(1);
int password = rs.getInt("Password");
String username = rs.getString("Username");
String sex = rs.getString("Sex");
int cardId = rs.getInt("CardID");
int balance = rs.getInt("Balance");
System.out.println(id + "\t|" + username + "\t|" + sex + "\t|" + cardId + "\t|" + password + "\t|"
+ balance + "\t|");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
stmt.close();
rs.close();
}
}
public void runOneTime() throws Exception{
try {
RandomAccessFile raf = new RandomAccessFile("D:/a电子书/java/time/time.txt","rw");
int times = Integer.parseInt(raf.readLine());
if (times == 1) {
sqlStart();
System.out.println("成员信息已载入!");
raf.seek(0);
times = times - 1;
raf.write((times+"").getBytes());
raf.close();
}else {
System.out.println("****成员信息早就载入啦!****");
raf.close();
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
BankDB类
package bank;
import java.util.HashMap;
import java.util.Map;
public class BankDB {
Map<Integer,Users> users = new HashMap<>();
private BankDB() {
Users u1 = new Users("Emiya","女",67400,123456,1000);
Users u2 = new Users("Reese","男",67401,654321,13000);
Users u3 = new Users("Finch","男",67402,654321,100000);
Users u4 = new Users("Cheshire","女",67403,18554,5200);
Users u5 = new Users("Carter","女",67404,111111,1000);
Users u6 = new Users("Root","女",67405,222222,2000);
Users u7 = new Users("Shane","女",67406,333333,2000);
Users u8 = new Users("Sunshine","女",67407,444444,9000);
Users u9 = new Users("Morrian","女",67409,121212,1212);
users.put(67400, u1);
users.put(67401, u2);
users.put(67402, u3);
users.put(67403, u4);
users.put(67404, u5);
users.put(67405, u6);
users.put(67406, u7);
users.put(67407, u8);
users.put(67408, u9);
}
static private BankDB db = new BankDB();
public static BankDB getDb() {
return db;
}
public static void setDb(BankDB db) {
BankDB.db = db;
}
public Map<Integer, Users> getUsers() {
return users;
}
public void setUsers(Map<Integer, Users> users) {
this.users = users;
}
public Users getUser(Integer cardId) {//由cardId获得用户对象
Users user = users.get(cardId);
return user;
}
}
Users类
package bank;
public class Users {
private String name;
private String sex;
private int passWord;
private int cardId;
private int balance;
private int interestType;
private int time;
public Users() {}
public Users(String name,String sex,int cardId,int passWord,int balance) {
//super();或许不需要
this.balance = balance;
this.passWord = passWord;
this.name = name;
this.sex = sex;
this.cardId = cardId;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCardId() {
return cardId;
}
public void setCardId(int cardId) {
this.cardId = cardId;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getPassWord() {
return passWord;
}
public void setPassWord(int passWord) {
this.passWord = passWord;
}
public int getInterestType() {
return interestType;
}
public void setInterestType(int interestType) {
this.interestType = interestType;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
}
*******************************************以下是纯HashMap**********************************************
BankDb、Users类都一样就不贴了
test类
package bank;
public class test {
public static void main(String[] args) {
operate o = new operate();
o.login();
o.menuSelect();
}
}
operate类
package bank;
import java.util.*;
public class operate {
Map<Integer, Users> users = BankDB.getDb().getUsers();
Scanner sc = new Scanner(System.in);
public Users user;
public void login() {
System.out.println("欢迎来到银行服务系统");
while (true) {
System.out.print("输入卡号:");
int cardId = sc.nextInt();
System.out.print("输入密码:");
int passWord = sc.nextInt();
user = BankDB.getDb().getUser(cardId);
if (users.containsKey(cardId) && user.getPassWord() == passWord) {// equals是字符串
System.out.println("欢迎" + user.getName() + user.getSex() + "士!");
break;
} else {
System.out.println("或许你输入了什么奇怪的东西哦~");
}
}
}// login
public void menuSelect() {
while (true) {
System.out.println("1.查詢\n2.存款\n3.取款\n請輸入你的選擇:");
int choice = sc.nextInt();
switch (choice) {
case 0: {
System.out.println("真的要退出嗎?那就好好道個別吧bye~\n===============The end===============");
return;
}
case 1: {
query();
break;
}
case 2:{
saveMoney();
break;
}
case 3:{
withdrawMoney();
break;
}
default:{
System.out.println("您的輸入有問題哦~");
break;
}
}
}
}
public void query() {
System.out.println("您當前的餘額:" + user.getBalance() + "元\n" + "特殊時期請保管好自己的口袋!");
}
public void saveMoney() {
while (true) {
System.out.print("想要存入多少钱呐~悄悄告诉我哦:");
int num = sc.nextInt();
if (num <= 0) {
System.out.println("请检查你的输入~");
} else {
user.setBalance(num + user.getBalance());
System.out.println("成功存入" + num + "元\n" + "目前余额为:" + user.getBalance());
}
}
}
public void withdrawMoney() {
System.out.println("怎么,这么早就想把你的钱要回去了吗?说吧,想要多少?:");
int num = sc.nextInt();
if (num > user.getBalance()) {
System.out.println("你可要挣更多钱哦~目前没有这么多钱躺在你的钱包里哦~");
} else {
user.setBalance(user.getBalance() - num);
System.out.println("成功取出" + num + "元\n" + "目前余额为:" + user.getBalance());
}
}
}
就只是留个纪念啦~有一个比较好一些的类似的是小组作业,但毕竟不是我个人所有啦~
反正还有很多要进步的地方,加油!