package run;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import bean.*;
import dao.db;
import java.util.Date;
import java.util.regex.Pattern;
public class BookSystem {
public static final db d = new db();
public static final HashMap<String, User> ALL_User = new HashMap<>();
public static HashMap<String, Reader> ALL_Reader = new HashMap<>();
public static HashMap<String, Manager> ALL_Manager = new HashMap<>();
public static final Scanner sc = new Scanner(System.in);
public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss EE a");
static {
//数据库注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");//Java会自动将驱动程序注册到DriverManager类中
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
m();
}
//小马图书系统
public static void m() {
while (true) {
System.out.println("==============欢迎来到小马图书管理系统=============");
System.out.println("1.注册,2.登录");
System.out.println("请输入您的操作:");
String command = sc.nextLine();
switch (command) {
case "1":
//注册
Register();
break;
case "2":
//登录
Login();
return;
default:
System.out.println("操作有误!请检查你的操作!");
}
}
}
//用户注册
public static void Register() {
System.out.println("============用户注册界面=========");
while (true) {
System.out.println("请输入登录名:(登录名一经注册不可修改!)");
String loginName = sc.nextLine();
if (Pattern.matches("^[a-zA-Z0-9]{3,10}$", loginName)) {
if (getPasswordByLoginNameFromDb(loginName) == null) {
System.out.println("请输入用户名:");
String username = sc.nextLine();
System.out.println("请输入密码:");
String ps = sc.nextLine();
if (Pattern.matches("^[a-zA-Z0-9]{6}$", ps)) {
System.out.println("请输入您的性别:");
String gender = sc.nextLine();
System.out.println("请输入您的生日:(x-x-x)");
String birthday = sc.nextLine();
Date date = new Date();
System.out.println("当前的注册时间是:" + sdf.format(date));
//将数据添加到数据库中去
d.addReaderToDb(loginName, ps, username, gender, birthday, date);
System.out.println(username + (gender.equals("男") ? "先生" : "女士") + "注册成功!");
break;
} else {
System.out.println("密码格式错误!");
}
} else {
System.out.println("该登录名已被注册!请重新输入");
}
} else {
System.out.println("账号格式有误!请检查!!");
}
}
}
//用户登录功能
public static void Login() {
System.out.println("==========用户登录界面===========");
while (true) {
System.out.println("请输入登录名:");
String loginName = sc.nextLine();
System.out.println("请输入密码:");
String ps = sc.nextLine();
User r1 = getPasswordByLoginNameFromDb(loginName);
if (r1 != null) {
//找到了用户
//判断密码是否正确
if (r1 instanceof Reader) {
Reader r = (Reader) r1;
if (r.getPassword().equals(ps)) {
//密码正确
System.out.println(r.getLoginName() + "(先生/女士)" + "登录成功!");
System.out.println("读者界面");
ReaderWin.run(r);
break;
} else {
System.out.println("登录失败!密码错误!");
}
} else if (r1 instanceof Manager) {
Manager r = (Manager) r1;
if (r.getLoginName().equals("admin")) {
System.out.println("超级管理员界面");
SuperManager();
break;
} else {
if (r.getPassword().equals(ps)) {
//密码正确
System.out.println(r.getLoginName() + "(先生/女士)" + "登录成功!");
System.out.println("管理员界面");
ManagerWin.run(r);
break;
} else {
System.out.println("登录失败!密码错误!");
}
}
}
} else {
System.out.println("用户不存在!");
}
}
}
/*
超级管理员界面
*/
private static void SuperManager() {
System.out.println("===============超级管理员界面=============");
System.out.println("1.管理读者 2.管理管理员 3.退出系统");
while (true) {
System.out.println("请输入您的操作:");
String command = sc.nextLine();
switch (command) {
case "1":
GReader();
break;
case "2":
GManager();
break;
case "3":
m();
default:
System.out.println("指令有误!");
}
}
}
/*
管理管理员
*/
private static void GManager() {
System.out.println("1.增加管理员 2.删除管理员 3.退出");
System.out.println("请输入您的操作:");
String command = sc.nextLine();
switch (command) {
case "1":
addManager();
break;
case "2":
deleteManager();
break;
case "3":
SuperManager();
break;
default:
System.out.println("指令有误!");
}
}
//增加管理员
private static void addManager() {
while (true) {
System.out.println("请输入登录名:");
String loginName = sc.nextLine();
if (Pattern.matches("^[a-zA-Z0-9]{3,10}$", loginName)) {
if (getPasswordByLoginNameFromDb(loginName) == null) {
System.out.println("请输入用户名:");
String username = sc.nextLine();
System.out.println("请输入密码:");
String ps = sc.nextLine();
d.addManagerToDb(loginName, username, ps);
break;
} else {
System.out.println("该登录名已被注册!请重新输入:");
}
} else {
System.out.println("登录名格式错误!");
}
}
}
/*
删除管理员
*/
private static void deleteManager
评论0