赞
踩
本系统是控制台版java图书管理系统,实现了用户的注册登录,用户管理员的登录,图书管理员的登录,用户可以购买图书和查看图书购买记录,用户管理员可以对用户信息进行增删查改,图书管理员可以查看图书信息,以及上架新的图书和修改图书库存。
本系统中存在的已知bug已解决,如遇到到其它bug请在评论区留言告知。
新建数据库lib_dtb,然后在桌面建立lib_dtb.sql文件,写入以下sq语句,然后点击lib_dtb数据库中的表右键运行该sql文件,数据库表即导入成功,数据库成功创建。
- /*
- Navicat Premium Data Transfer
- Source Server : MySql
- Source Server Type : MySQL
- Source Server Version : 80036
- Source Host : localhost:3306
- Source Schema : lib_dtb
- Target Server Type : MySQL
- Target Server Version : 80036
- File Encoding : 65001
- Date: 17/05/2024 23:00:29
- */
-
- SET NAMES utf8mb4;
- SET FOREIGN_KEY_CHECKS = 0;
-
- -- ----------------------------
- -- Table structure for admin
- -- ----------------------------
- DROP TABLE IF EXISTS `admin`;
- CREATE TABLE `admin` (
- `id` int UNSIGNED NOT NULL AUTO_INCREMENT,
- `username` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
- `password` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
- PRIMARY KEY (`id`) USING BTREE,
- UNIQUE INDEX `username`(`username` ASC) USING BTREE
- ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '管理员' ROW_FORMAT = Dynamic;
-
- -- ----------------------------
- -- Records of admin
- -- ----------------------------
- INSERT INTO `admin` VALUES (1, 'admin', '123456');
-
- -- ----------------------------
- -- Table structure for buy_table
- -- ----------------------------
- DROP TABLE IF EXISTS `buy_table`;
- CREATE TABLE `buy_table` (
- `name` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '用户姓名',
- `library` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '图书名字',
- `buy_time` datetime NOT NULL COMMENT '购买时间'
- ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '用户购买图书记录表' ROW_FORMAT = Dynamic;
-
- -- ----------------------------
- -- Records of buy_table
- -- ----------------------------
- INSERT INTO `buy_table` VALUES ('zs', '平凡世界', '2024-04-15 22:20:02');
- INSERT INTO `buy_table` VALUES ('zs', '平凡世界', '2024-04-16 23:25:57');
- INSERT INTO `buy_table` VALUES ('zs', '平凡世界', '2024-04-18 18:24:22');
-
- -- ----------------------------
- -- Table structure for libadmin
- -- ----------------------------
- DROP TABLE IF EXISTS `libadmin`;
- CREATE TABLE `libadmin` (
- `id` int UNSIGNED NOT NULL AUTO_INCREMENT,
- `username` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
- `password` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
- PRIMARY KEY (`id`) USING BTREE
- ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '图书管理员' ROW_FORMAT = Dynamic;
-
- -- ----------------------------
- -- Records of libadmin
- -- ----------------------------
- INSERT INTO `libadmin` VALUES (1, 'admin', '654321');
-
- -- ----------------------------
- -- Table structure for library
- -- ----------------------------
- DROP TABLE IF EXISTS `library`;
- CREATE TABLE `library` (
- `id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键id',
- `name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '图书名字',
- `author` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '作者',
- `price` decimal(5, 2) NOT NULL COMMENT '图书价格,可以保留两位小数,总共5位',
- `count` int UNSIGNED NOT NULL COMMENT '图书数量',
- `Shelf_time` datetime NOT NULL,
- PRIMARY KEY (`id`) USING BTREE
- ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '图书表' ROW_FORMAT = Dynamic;
-
- -- ----------------------------
- -- Records of library
- -- ----------------------------
- INSERT INTO `library` VALUES (1, '平凡世界', '路遥', 46.00, 0, '2024-04-15 22:02:59');
-
- -- ----------------------------
- -- Table structure for user
- -- ----------------------------
- DROP TABLE IF EXISTS `user`;
- CREATE TABLE `user` (
- `id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键id',
- `username` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '用户名,唯一',
- `password` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '密码',
- PRIMARY KEY (`id`) USING BTREE,
- UNIQUE INDEX `username`(`username` ASC) USING BTREE
- ) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '用户表' ROW_FORMAT = Dynamic;
-
- -- ----------------------------
- -- Records of user
- -- ----------------------------
- INSERT INTO `user` VALUES (1, 'zs', '20021015');
- INSERT INTO `user` VALUES (2, 'lisi', '111');
-
- SET FOREIGN_KEY_CHECKS = 1;
- package com.java.main.sqlConnect;
-
- import java.sql.*;
-
- /*
- 数据库的连接
- */
- public class Connect {
- //查询表中的数据
- public static ResultSet queryAll(String sql) throws ClassNotFoundException, SQLException {
- Class.forName("com.mysql.cj.jdbc.Driver");
- Connection rt = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lib_dtb", "root", "你的数据库密码");
- Statement st = rt.createStatement();
- ResultSet rs = st.executeQuery(sql);
- return rs;
- }
-
- //添加数据,删除数据,修改数据
- public static void edit(String sql) throws ClassNotFoundException, SQLException {
- Class.forName("com.mysql.cj.jdbc.Driver");
- Connection rt = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lib_dtb", "root", "你的数据库密码");
- Statement st = rt.createStatement();
- st.executeUpdate(sql);
- }
- }
- package com.java.main.buyJavaBean;
-
-
- /*
- 购买记录javabean
- */
- public class UserBuy {
- private String name;
- private String libraryName;
- private String buyTime;
-
- public UserBuy() {
- }
-
- public UserBuy(String name, String libraryName, String buyTime) {
- this.name = name;
- this.libraryName = libraryName;
- this.buyTime = buyTime;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getLibraryName() {
- return libraryName;
- }
-
- public void setLibraryName(String libraryName) {
- this.libraryName = libraryName;
- }
-
- public String getBuyTime() {
- return buyTime;
- }
-
- public void setBuyTime(String buyTime) {
- this.buyTime = buyTime;
- }
-
- @Override
- public String toString() {
- return "UserBuy{" +
- "name='" + name + '\'' +
- ", libraryName='" + libraryName + '\'' +
- ", buyTime='" + buyTime + '\'' +
- '}';
- }
- }
- package com.java.main.libraryJavaBean;
-
- /*
- 图书javabean
- */
- public class Library {
- private Integer id;
- private String name;
- private String author;
- private Double price;
- private Integer count;
- private String time;
-
- public Library() {
- }
-
- public Library(Integer id, String name, String author, Double price, Integer count, String time) {
- this.id = id;
- this.name = name;
- this.author = author;
- this.price = price;
- this.count = count;
- this.time = time;
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getAuthor() {
- return author;
- }
-
- public void setAuthor(String author) {
- this.author = author;
- }
-
- public Double getPrice() {
- return price;
- }
-
- public void setPrice(Double price) {
- this.price = price;
- }
-
- public Integer getCount() {
- return count;
- }
-
- public void setCount(Integer count) {
- this.count = count;
- }
-
- public String getTime() {
- return time;
- }
-
- public void setTime(String time) {
- this.time = time;
- }
- @Override
- public String toString() {
- return "Library{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", author='" + author + '\'' +
- ", price=" + price +
- ", count=" + count +
- ", time='" + time + '\'' +
- '}';
- }
- }
- package com.java.main.perponJavaBean;
-
- /*
- 管理员javabean
- */
-
- public class Admin extends User {
- public Admin() {
- }
-
- public Admin(Integer id, String username, String password) {
- super(id, username, password);
- }
- }
- package com.java.main.perponJavaBean;
-
- /*
- 用户javabean
- */
- public class User {
- private Integer id;
- private String username;
- private String password;
-
- public User() {
- }
-
- public User(Integer id, String username, String password) {
- this.id = id;
- this.username = username;
- this.password = password;
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- @Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", username='" + username + '\'' +
- ", password='" + password + '\'' +
- '}';
- }
- }
- package com.java.main.Screen;
-
- import com.java.main.perponJavaBean.Admin;
- import com.java.main.perponJavaBean.User;
- import com.java.main.sqlConnect.Connect;
- import com.java.main.utils.CodeUtil;
-
- import javax.swing.*;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Scanner;
-
- /**
- * @author leiGood浮生若梦
- * @since 2024-06-13 星期四 18:41:29
- * 用户注册登录
- * 管理员登录
- */
- public class AppRun {
- public AppRun() {
- System.out.println("=======================图书管理=======================");
- System.out.println("=====================欢迎您的到来!====================");
- System.out.println("=====================长风破浪会有时,==================");
- System.out.println("=====================直挂云帆济沧海。==================");
- }
-
- private static ArrayList<User> list = new ArrayList<>();
-
- public static void main(String[] args) throws SQLException, ClassNotFoundException {
- new AppRun();
- //存储用户集合
- list = new ArrayList<User>();
- //用户管理员
- ArrayList<Admin> UserAdmin = new ArrayList<>();
- //图书管理员
- ArrayList<Admin> libraryAdmin = new ArrayList<>();
- //获取数据库中的用户
- ResultSet rs1 = Connect.queryAll("select * from user");
- while (rs1.next()) {
- Integer id = rs1.getInt("id");
- String username = rs1.getString("username");
- String password = rs1.getString("password");
- list.add(new User(id, username, password));
- }
- //获取用户管理员
- ResultSet rs2 = Connect.queryAll("select * from admin");
- while (rs2.next()) {
- Integer id = rs2.getInt("id");
- String username = rs2.getString("username");
- String password = rs2.getString("password");
- UserAdmin.add(new Admin(id, username, password));
- }
- //获取图书管理员
- ResultSet rs3 = Connect.queryAll("select * from libAdmin");
- Scanner sc = new Scanner(System.in);
- while (rs3.next()) {
- Integer id = rs3.getInt("id");
- String username = rs3.getString("username");
- String password = rs3.getString("password");
- libraryAdmin.add(new Admin(id, username, password));
- }
- rs3.close();
- rs2.close();
- rs1.close();
- while (true) {
- System.out.println("请输入您的选择:1.登录 2注册 3用户管理管理员登录 4图书管理管理员登录 5退出");
- String numStr = sc.nextLine();
- try {
- int num = Integer.parseInt(numStr);
- switch (num) {
- case 1:
- Login(list);
- break;
- case 2:
- Register(list);
- break;
- case 3:
- UserAdminLogin(UserAdmin);
- break;
- case 4:
- LibraryAdminLogin(libraryAdmin);
- break;
- case 5:
- System.exit(WindowConstants.EXIT_ON_CLOSE);
- break;
- default:
- System.out.println("没有这个选项");
- break;
- }
- } catch (Exception e) {
- System.out.println("请输入数字,不能输入其它字符");
- }
- }
- }
-
- //图书管理员登录
- private static void LibraryAdminLogin(ArrayList<Admin> libraryAdmin) throws SQLException, ClassNotFoundException {
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("请输入图书管理员账号");
- String username = sc.nextLine();
- int index = getLibraAdminIndex(libraryAdmin, username);
- if (index >= 0) {
- System.out.println("请输入图书管理员密码");
- String pwd = sc.nextLine();
- if (pwd.equals(libraryAdmin.get(index).getPassword())) {
- System.out.println("登陆成功!");
- com.java.main.Screen.LibraryAdmin.main();
- break;
- } else {
- System.out.println("密码输入错误,请重新输入~");
- }
- } else {
- System.out.println("图书管理员账号不存在~");
- }
- }
- }
-
- //用户管理员登录
- private static void UserAdminLogin(ArrayList<Admin> UserAdminList) throws SQLException, ClassNotFoundException {
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("请输入用户管理员账号");
- String username = sc.nextLine();
- System.out.println("请输入用户管理员密码");
- String pwd = sc.nextLine();
- int adminIndex = getAdminIndex(UserAdminList, username);
- if (adminIndex >= 0) {
- if (UserAdminList.get(adminIndex).getUsername().equals(username) &&
- UserAdminList.get(adminIndex).getPassword().equals(pwd)) {
- System.out.println("登录成功!");
- UserAdmin.main();
- break;
- } else {
- System.out.println("密码输入错误!");
- }
- } else {
- System.out.println("该用户管理员账号不存在!");
- }
- }
- }
-
- //注册操作
- private static void Register(ArrayList<User> list) throws SQLException, ClassNotFoundException {
- while (true) {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入用户名:");
- String username = sc.next();
- //判断用户名是否存在
- int index = getIndex(list, username);
- if (index < 0) {
- //用户名不存在,可以执行注册
- System.out.println("请输入密码:");
- String pwd1 = sc.next();
- System.out.println("请再次输入密码:");
- String pwd2 = sc.next();
- if (pwd2.equals(pwd1)) {
- //两次密码输入一致
- String sql = "insert into user(username,password) values('" + username + "','" + pwd2 + "')";
- Connect.edit(sql);
- System.out.println("注册成功!");
- //刷新用户集合中的数据,在次进行查询获取用户数据
- //获取数据库中的用户
- ResultSet rs1 = Connect.queryAll("select * from user");
- while (rs1.next()) {
- Integer id = rs1.getInt("id");
- String uname = rs1.getString("username");
- String password = rs1.getString("password");
- list.add(new User(id, uname, password));
- }
- break;
- } else {
- //两次密码输入不一致
- System.out.println("两次密码输入不一致,请重新输入!");
- }
- } else {
- System.out.println("用户名已经存在,请重新输入~");
- }
- }
- }
-
- //登录操作
- private static void Login(ArrayList<User> list) throws SQLException, ClassNotFoundException {
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("请输入用户名:");
- String username = sc.nextLine();
- //判断用户名是否存在
- int index = getIndex(list, username);
- if (index >= 0) {
- //用户名存在,执行下一步输入密码操作
- System.out.println("请输入密码:");
- String password = sc.nextLine();
- if (password.equals(list.get(index).getPassword())) {
- String codeStr = CodeUtil.getCode();
- System.out.println("当前验证码为:" + codeStr);
- System.out.println("请输入验证码:");
- String code = sc.nextLine();
- if (code.equalsIgnoreCase(codeStr)) {
- //密码输入正确
- System.out.println("登陆成功!");
- //传递用户登录的用户名
- new UserBuyLibrary(username).main();
- break;
- } else {
- System.out.println("验证码输入错误,请重新输入");
- }
- } else {
- System.out.println("密码输入错误,请重新输入");
- }
- } else {
- //用户名不存在
- System.out.println("用户名未注册,请注册后再来使用~");
- break;
- }
- }
- }
-
- //判断用户在登录或者注册时用户名是否存在,存在返回对应的索引,否则返回-1
- public static int getIndex(ArrayList<User> list, String username) {
- for (int i = 0; i < list.size(); i++) {
- if (username.equals(list.get(i).getUsername())) {
- //存在
- return i;
- }
- }
- //不存在
- return -1;
- }
-
- //判断用户管理员登录
- public static int getAdminIndex(ArrayList<Admin> list, String username) {
- for (int i = 0; i < list.size(); i++) {
- if (username.equals(list.get(i).getUsername())) {
- //存在
- return i;
- }
- }
- //不存在
- return -1;
- }
-
- //判断图书管理员登录
- public static int getLibraAdminIndex(ArrayList<Admin> list, String username) {
- for (int i = 0; i < list.size(); i++) {
- if (username.equals(list.get(i).getUsername())) {
- //存在
- return i;
- }
- }
- //不存在
- return -1;
- }
- }
- package com.java.main.Screen;
-
- import com.java.main.libraryJavaBean.Library;
- import com.java.main.sqlConnect.Connect;
-
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Timestamp;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.Scanner;
-
- /*
- 图书管理员操作代码
- */
- public class LibraryAdmin {
- public LibraryAdmin() throws SQLException, ClassNotFoundException {
- //获取图书库存为0的书籍
- ResultSet rs = Connect.queryAll("select * from library where count='0'");
- while (rs.next()) {
- System.out.println("========================图书" + rs.getString("name") + "的库存为0了,请增加库存!!!" +
- "========================");
- }
- rs.close();
- }
-
- public static void main() throws SQLException, ClassNotFoundException {
- new LibraryAdmin();
- Scanner sc = new Scanner(System.in);
- //图书集合
- ArrayList<Library> list = new ArrayList<>();
- ResultSet rs = Connect.queryAll("select * from library");
- while (rs.next()) {
- Integer id = rs.getInt("id");
- String libraryName = rs.getString("name");
- String authorName = rs.getString("author");
- Double price = rs.getDouble("price");
- Integer count = rs.getInt("count");
- //获取上架时间
- Timestamp shelf_time = rs.getTimestamp("Shelf_time");
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String time = sdf.format(shelf_time);
- list.add(new Library(id, libraryName, authorName, price, count, time));
- }
- rs.close();
- System.out.println("请输入你的选择:1查看所有图书信息 2增加图书 3修改图书库存 4退出");
- String numStr = sc.nextLine();
- try {
- switch (Integer.parseInt(numStr)) {
- case 1:
- LookForInfo(list);
- break;
- case 2:
- InsertBook(list);
- break;
- case 3:
- EditBook(list);
- break;
- case 4:
- new AppRun();
- break;
- default:
- System.out.println("没有这个选项~");
- break;
- }
- } catch (Exception e) {
- System.out.println("请输入数字!不能输入其它字符~");
- }
- }
-
- //修改图书库存
- private static void EditBook(ArrayList<Library> list) throws SQLException, ClassNotFoundException {
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("请输入要修改图书库存的图书名字:");
- String name = sc.nextLine();
- int index = getResult(list, name);
- if (index >= 0) {
- //存在
- System.out.println("请输入增加的库存数量:");
- Integer count1 = sc.nextInt();
- Integer count2 = list.get(index).getCount();
- Integer count = count1 + count2;
- String sql = "update library set count='" + count + "' where name='" + name + "'";
- Connect.edit(sql);
- System.out.println("库存添加成功!");
- main();
- break;
- } else {
- System.out.println("不存在该图书!请重新输入~");
- }
- }
- }
-
- //增加图书
- private static void InsertBook(ArrayList<Library> list) throws SQLException, ClassNotFoundException {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入要增加的图书名:");
- String name = sc.next();
- System.out.println("请输入作者名:");
- String author = sc.next();
- System.out.println("请输入价格:");
- Double price = sc.nextDouble();
- System.out.println("请输入图书数量:");
- Integer count = sc.nextInt();
- //获取上架时间
- Date d = new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String time = sdf.format(d);
- String sql = " insert into library(name, author, price, count, Shelf_time) values('" + name + "','" + author + "','" +
- price + "','" + count + "','" + time + "')";
- Connect.edit(sql);
- System.out.println("添加成功!");
- main();
- }
-
- //查询图书信息
- private static void LookForInfo(ArrayList<Library> list) throws SQLException, ClassNotFoundException {
- System.out.println("图书信息如下:");
- for (Library library : list) {
- System.out.println("图书编号:" + library.getId() + " 图书名字:《" + library.getName() + " 》 图书作者:" + library.getAuthor()
- + " 图书价格:" + library.getPrice() + " 图书库存" + library.getCount() + " 图书上架时间:" + library.getTime());
- }
- main();
- }
-
- //判断图书是否存在
- public static Integer getResult(ArrayList<Library> list, String name) {
- for (int i = 0; i < list.size(); i++) {
- if (name.equals(list.get(i).getName())) {
- //存在
- return i;
- }
- }
- return -1;
- }
- }
- package com.java.main.Screen;
-
- import com.java.main.perponJavaBean.User;
- import com.java.main.sqlConnect.Connect;
-
- import javax.swing.*;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.util.function.Consumer;
-
- /*
- 用户管理员
- 实现对用户信息的增删查改
- */
- public class UserAdmin {
- public static void main() throws SQLException, ClassNotFoundException {
- Scanner sc = new Scanner(System.in);
- //用户信息
- ArrayList<User> list = new ArrayList<>();
- String sql = "select * from user";
- //连接数据库
- ResultSet rs = Connect.queryAll(sql);
- while (rs.next()) {
- list.add(new User(rs.getInt("id"), rs.getString("username"), rs.getString("password")));
- }
- rs.close();
- System.out.println("请输入你的选择:1查看用户信息 2删除用户信息 3修改用户信息 4增加用户信息 5退出");
- String numStr = sc.nextLine();
- try {
- int num = Integer.parseInt(numStr);
- switch (num) {
- case 1:
- LookUser(list);
- break;
- case 2:
- Delete(list);
- break;
- case 3:
- UpdateUser(list);
- break;
- case 4:
- InsertUser(list);
- break;
- case 5:
- System.exit(WindowConstants.EXIT_ON_CLOSE);
- break;
- default:
- System.out.println("没有这个选项!");
- main();
- break;
- }
- } catch (Exception e) {
- System.out.println("请输入数字!");
- main();
- }
- }
-
- //修改用户信息
- private static void UpdateUser(ArrayList<User> list) throws SQLException, ClassNotFoundException {
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("请输入要修改的用户id:");
- int id = sc.nextInt();
- boolean flag = getResult(list, id);
- if (flag) {
- //输入的id存在
- System.out.println("请输入修改后的用户名:");
- String username = sc.next();
- System.out.println("请修改后的密码:");
- String pwd = sc.next();
- String sql = "update user set username='" + username + "',password='" + pwd + "' where id=" + id;
- Connect.edit(sql);
- System.out.println("用户信息修改成功!");
- main();
- break;
- } else {
- System.out.println("输入的用户id在数据库中不存在,请重新输入~");
- }
- }
- }
-
- //增加用户信息
- private static void InsertUser(ArrayList<User> list) throws SQLException, ClassNotFoundException {
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("请输入用户名:");
- String username = sc.nextLine();
- boolean flag = getUserFlag(list, username);
- if (!flag) {
- //输入的用户名在数据库不存在,执行增加操作
- System.out.println("请输入密码:");
- String pwd = sc.nextLine();
- String sql = "insert into user(username,password) values('" + username + "','" + pwd + "')";
- Connect.edit(sql);
- System.out.println("添加成功!");
- main();
- break;
- } else {
- System.out.println("输入的用户名在数据库中已经存在,请重新输入!");
- }
- }
- }
-
- //删除用户信息
- private static void Delete(ArrayList<User> list) throws SQLException, ClassNotFoundException {
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("请输入你要删除的用户id");
- int id = sc.nextInt();
- boolean flag = getResult(list, id);
- if (flag) {
- //id存在
- String sql = "delete from user where id=" + id;
- Connect.edit(sql);
- System.out.println("删除成功!");
- main();
- break;
- } else {
- //id不存在
- System.out.println("输入的用户id不存在,请重新输入");
- }
- }
-
- }
-
- //查看用户信息
- private static void LookUser(ArrayList<User> list) throws SQLException, ClassNotFoundException {
- //遍历用户集合
- System.out.println("用户信息如下");
- list.forEach(new Consumer<User>() {
- @Override
- public void accept(User user) {
- System.out.println(user.getId() + " " + user.getUsername() + " " + user.getPassword());
- }
- });
- UserAdmin.main();
- }
-
- //在删除用户信息的时候,判断用户输入的id是否存在
- public static boolean getResult(ArrayList<User> list, int id) {
- for (int i = 0; i < list.size(); i++) {
- if (list.get(i).getId() == id) {
- //存在
- return true;
- }
- }
- //不存在
- return false;
- }
-
- //查询输入的用户名是否存在
- private static boolean getUserFlag(ArrayList<User> list, String username) {
- for (int i = 0; i < list.size(); i++) {
- if (username.equals(list.get(i).getUsername())) {
- //用户名存在
- return true;
- }
- }
- //不存在
- return false;
- }
- }
- package com.java.main.Screen;
-
- import com.java.main.buyJavaBean.UserBuy;
- import com.java.main.libraryJavaBean.Library;
- import com.java.main.sqlConnect.Connect;
-
- import javax.swing.*;
- import java.sql.*;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.Scanner;
- import java.util.function.Consumer;
-
- /*
- 用户进行图书购买查询操作代码
- */
- public class UserBuyLibrary {
- private static String name;
-
- public UserBuyLibrary(String name) {
- this.name = name;
- System.out.println("欢迎来到图书商城");
- System.out.println("当前的登录用户:" + name);
- }
-
- public static void main() throws SQLException, ClassNotFoundException {
- //创建图书集合
- ArrayList<Library> list = new ArrayList<>();
- //用户购买记录
- ArrayList<UserBuy> buyList = new ArrayList<>();
- //连接数据库 将图书信息添加到集合中
- ResultSet rs1 = Connect.queryAll("select * from library");
- while (rs1.next()) {
- Integer id = rs1.getInt("id");
- String libraryName = rs1.getString("name");
- String authorName = rs1.getString("author");
- Double price = rs1.getDouble("price");
- Integer count = rs1.getInt("count");
- //获取上架时间
- Timestamp shelf_time = rs1.getTimestamp("Shelf_time");
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String time = sdf.format(shelf_time);
- list.add(new Library(id, libraryName, authorName, price, count, time));
- }
- rs1.close();
- //获取用户购买记录
- String sql = "select * from buy_table";
- ResultSet rs2 = Connect.queryAll(sql);
- while (rs2.next()) {
- Timestamp buyTime = rs2.getTimestamp("buy_time");
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String time = sdf.format(buyTime);
- buyList.add(new UserBuy(rs2.getString("name"), rs2.getString("library"), time));
- }
- rs2.close();
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入你的选择 1.查看所有图书信息 2.购买书籍 3.查询购买记录 4.退出");
- try {
- int num = sc.nextInt();
- switch (num) {
- case 1:
- LookForLibraryInfo(list);
- break;
- case 2:
- BuyBook(list);
- break;
- case 3:
- FindBuy(buyList);
- break;
- case 4:
- System.exit(WindowConstants.EXIT_ON_CLOSE);
- break;
- default:
- System.out.println("没有这个选项");
- main();
- break;
- }
- } catch (Exception e) {
- System.out.println("请输入数字,不能输入其它字符~");
- main();
- }
- }
-
- //查询当前用户购买的图书信息
- private static void FindBuy(ArrayList<UserBuy> buyList) throws SQLException, ClassNotFoundException {
- System.out.println("购买记录如下所示:");
- int index = getBuyIndex(buyList, name);
- if (index >= 0) {
- buyList.stream().forEach(userBuy -> System.out.println("用户" + userBuy.getName() + "于" + userBuy.getBuyTime() + "购买了" + userBuy.getLibraryName()));
- } else {
- System.out.println("您还没有购买任何书籍");
- }
- main();
- }
-
- //购买书籍
- private static void BuyBook(ArrayList<Library> list) throws SQLException, ClassNotFoundException {
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("请输入你要购买的图书名字");
- String libraryName = sc.next();
- int index = getIndex(list, libraryName);
- if (index >= 0) {
- //图书存在
- //判断图书数量是否大于0
- Integer count = list.get(index).getCount();
- if (count > 0) {
- //图书有剩余容量
- System.out.println("请输入购买数量:");
- Integer amount = sc.nextInt();
- //判断购买数量是否在图书库存范围内
- if (amount > 0 && amount <= list.get(index).getCount()) {
- //在库存范围内
- //获取剩余书籍数量
- Integer remainingCount = list.get(index).getCount() - amount;
- //更新数据库
- String sql = "update library set count='" + remainingCount + "'where name='" + libraryName + "'";
- Connect.edit(sql);
- System.out.println("购买成功!");
- //获取当前购买时间
- Date d = new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String datetime = sdf.format(d);
- //将用户名和图书名字插入数据库中
- Connect.edit("insert into buy_table values ('" + name + "','" + libraryName + "','" + datetime + "')");
- main();
- break;
- } else {
- System.out.println("超出库存范围,请重新输入~");
- }
- } else {
- //图书已经卖完了
- System.out.println("很抱歉,图书已经卖完了,请买别的书籍吧~");
- }
- } else {
- System.out.println("不存在该书籍!请重新输入~");
- }
- }
- }
-
- //查询书籍
- private static void LookForLibraryInfo(ArrayList<Library> list) throws SQLException, ClassNotFoundException {
- System.out.println("图书信息如下:");
- list.stream().forEach(new Consumer<Library>() {
- @Override
- public void accept(Library library) {
- System.out.println("图书编号:" + library.getId() + " 图书名字: 《" + library.getName() + "》 图书作者:" + library.getAuthor()
- + " 图书价格:" + library.getPrice() + " 图书库存" + library.getCount() + " 图书上架时间:" + library.getTime());
- }
- });
- main();
- }
-
- //判断图书名字在集合在是否存在
- public static int getIndex(ArrayList<Library> list, String name) {
- for (int i = 0; i < list.size(); i++) {
- if (name.equals(list.get(i).getName())) {
- return i;
- }
- }
- return -1;
- }
-
- //判断用户在是否存在在购买集合中
- private static int getBuyIndex(ArrayList<UserBuy> buyList, String name) {
- for (int i = 0; i < buyList.size(); i++) {
- if (name.equals(buyList.get(i).getName())) {
- return i;
- }
- }
- return -1;
- }
- }
- package com.java.main.utils;
-
- import java.util.ArrayList;
- import java.util.Random;
-
- /*
- 验证码
- */
- public class CodeUtil {
- public static String getCode() {
- ArrayList<Character> list = new ArrayList<>();
- for (int i = 0; i < 26; i++) {
- list.add((char) ('a' + i));
- }
- for (int i = 0; i < 26; i++) {
- list.add((char) ('A' + i));
- }
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < 4; i++) {
- int index = new Random().nextInt(list.size());
- sb.append(list.get(index));
- }
- for (int i = 0; i < 2; i++) {
- int number = new Random().nextInt(10);
- sb.append(number);
- }
- return sb.toString();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。