JavaSE项目--图书管理系统代码

结构

JavaSE项目--图书管理系统代码_第1张图片

代码展示

Book类代码

package com.xszx.beans;




//实体类
public class Book {
    private int id;
    private String name;
    private int bsum;

    public Book() {
    }

    public Book(int id, String name, int bsum) {
        this.id = id;
        this.name = name;
        this.bsum = bsum;
    }

    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBsum() {
        return bsum;
    }

    public void setBsum(int bsum) {
        this.bsum = bsum;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", bsum=" + bsum +
                '}';
    }
}

Jbook类代码

package com.xszx.beans;

import java.util.Date;

public class Jbook {
    private int id;
    private int uid;//用户id
    private int bid;//书籍id
    private Date jdate;//借书日期
    private Date hdate;//换书日期

    public Jbook() {
    }

    public Jbook(int id, int uid, int bid, Date jdate, Date hdate) {
        this.id = id;
        this.uid = uid;
        this.bid = bid;
        this.jdate = jdate;
        this.hdate = hdate;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public Date getJdate() {
        return jdate;
    }

    public void setJdate(Date jdate) {
        this.jdate = jdate;
    }

    public Date getHdate() {
        return hdate;
    }

    public void setHdate(Date hdate) {
        this.hdate = hdate;
    }

    @Override
    public String toString() {
        return "Jbook{" +
                "id=" + id +
                ", uid=" + uid +
                ", bid=" + bid +
                ", jdate=" + jdate +
                ", hdate=" + hdate +
                '}';
    }
}

User类代码

package com.xszx.beans;

public class User {
    private int id;
    private String username;
    private String password;
    private int role;//1为管理员 2为会员

    public User() {
    }

    public User(int id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public User(int id, String username, String password, int role) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.role = role;
    }

    public int getId() {
        return id;
    }

    public void setId(int 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;
    }

    public int getRole() {
        return role;
    }

    public void setRole(int role) {
        this.role = role;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", role=" + role +
                '}';
    }
}

BookController类代码

package com.xszx.controller;


import com.xszx.beans.Book;
import com.xszx.beans.Jbook;
import com.xszx.beans.User;
import com.xszx.service.BookService;
import com.xszx.service.JbookService;
import com.xszx.service.impl.BookServiceImpl;
import com.xszx.service.impl.JbookServiceImpl;

import java.util.List;
import java.util.Scanner;

//控制层  控制请求转发
public class BookController {
    Scanner sc=new Scanner(System.in);
    BookService bookService = new BookServiceImpl();
    JbookService jbookService = new JbookServiceImpl();

    //主方法管理员界面
    public void menu(User user){
        System.out.println();
        System.out.println("                     ---小张图书管理系统---  " +
                "                        ");
        System.out.println("***【1】查看图书列表 【2】添加图书 【3】修改图书 " +
                "【4】删除图书 【0】返回上一级***");
        System.out.println("请选择您要进行的操作:");
        int n = sc.nextInt();
        if(n==1){
            checkBook();
            menu(null);
        }else if (n==2){
            addBook();
            menu(null);
        }else if(n==3){
            alterBook();
            menu(null);
        }else if (n==4){
            deleteBook();
            menu(null);
        }else if(n==0){
            new UserController().menu(user);
        }else {
            System.out.println("输入错误,请重新输入!");
            menu(null);
        }
    }
    //用户界面
    public void usermenu(User user){
        User user1 = user;
        System.out.println();
        System.out.println("                     ---小张图书系统---  " +
                "                        ");
        System.out.println("***【1】查看图书列表 【2】借书 【3】还书 " +
                "【4】查看借书记录 【0】返回上一级***");
        System.out.println("请选择您要进行的操作:");
        int n = sc.nextInt();
        if(n==1){
            checkBook();
            usermenu(user1);
        }else if (n==2){
            borrowBook(user1);
            usermenu(user1);
        }else if(n==3){
            returnBook(user1);
            usermenu(user1);
        }else if (n==4){
            jmenu(user1);
        }else if(n==0){
            new UserController().menu(user1);
        }else{
            System.out.println("输入错误,请重新输入!");
            usermenu(user1);
        }
    }
    public void jmenu(User user){
        User user1 = user;
        System.out.println();
        System.out.println("                              ---借书记录---" +
                "                              ");
        System.out.println("***【1】查看所有 【2】查看已归还的借书记录 " +
                "【3】查看未归还的借书记录 【0】返回上一级***");
        System.out.println("请选择您要进行的操作:");
        int n = sc.nextInt();
        if(n==1){
            checkJbook(user1);
            jmenu(user1);
        }else if (n==2){
            checkHbook(user1);
            jmenu(user1);
        }else if(n==3){
            checkWHbook(user1);
            jmenu(user1);
        }else if (n==0){
            usermenu(user1);
        }else{
            System.out.println("输入错误,请重新输入!");
            jmenu(user1);
        }
    }

    private void checkWHbook(User user) {
        List jbookList = jbookService.checkWHbook(user);
        for (Jbook jbook : jbookList){
            System.out.println(jbook);
        }

    }

    private void checkHbook(User user) {
        List jbookList = jbookService.checkHbook(user);;
        for (Jbook jbook : jbookList){
            System.out.println(jbook);
        }

    }

    //借书记录
    private void checkJbook(User user) {
        List jbookList = jbookService.checkJbook(user);
        for (Jbook jbook : jbookList){
            System.out.println(jbook);
        }
    }
    //还书
    private void returnBook(User user) {
        System.out.println("请输入您要归还的书籍编号:");
        int bid= sc.nextInt();
        int id = bookService.getbookuidbid(user,bid);
        if(id>0){
            jbookService.returnBook(id);
            bookService.addbooksum(bid);
            bookService.alterhbook(bid);
            System.out.println("还书成功!");
        }else{
            System.out.println("该书无需归还!");
        }
    }

    //借书
    private void borrowBook(User user) {
        System.out.println("请输入您要借的书籍编号:");
        int bid= sc.nextInt();
        int i= bookService.getbookid(bid);
        if(i==1){
            int id= bookService.getbookuidbid(user,bid);
            if(id>0){
                System.out.println("您所要借的书未还!");
            }else{
                jbookService.borrowBook(user,bid);
                bookService.delbooksum(bid);
                bookService.alterjbook(bid);
                System.out.println("借书成功!");
            }
        }else{
            System.out.println("该图书已全部借出或该图书不存在,请查询图书列表!");
        }
    }

    //删除图书
    private void deleteBook() {
        Book book = new Book();
        System.out.println("请输入您要删除的图书名称:");
        book.setName(sc.next());
        //判断名字是否存在 存在则删除 不存在则提示不存在
        Book b=bookService.getbookname(book);
        if(b!=null){
            if (bookService.deleteBook(book)==2){
                System.out.println("删除成功!");
            }else if(bookService.deleteBook(book)==1){
                System.out.println("书借出还未还,无法删除!");
            }
        } else{
            System.out.println("您要删除的图书不存在!");
        }
    }
    //修改图书
    private void alterBook() {
        Book book = new Book();
        System.out.println("请输入您要修改的图书名称:");
        book.setName(sc.next());
        //判断名字是否存在 存在则继续修改 不存在则提示不存在
        Book b=bookService.getbookname(book);
        if(b!=null){
            Book book1 = new Book();
            System.out.println("请输入修改后的图书名称:");
            book1.setName(sc.next());
            int i = bookService.alterBook(book,book1);
            if (i==0){
                System.out.println("修改成功!");
            }
            else{
                System.out.println("您要修改的图书名称已存在,请重新操作!");
            }
        } else{
            System.out.println("您要修改的图书不存在!");
        }
    }
    //增加图书
    private void addBook() {
        Book book = new Book();
        System.out.println("请输入您要添加的图书名称:");
        book.setName(sc.next());
        //判断名字是否存在 存在则提示名字冲突 不存在则继续执行

        Book b=bookService.getbookname(book);
        if(b!=null){
            System.out.println("添加的图书名字冲突");
            menu(null);
        }else{
            System.out.println("请输入您要添加的图书库存:");
            book.setBsum(sc.nextInt());


            //后台添加完成,是否成功

            int i = bookService.addbook(book);
            if (i==1){
                System.out.println("添加成功!!!");
                menu(null);
            }else{
                System.out.println("添加失败!!!");
                menu(null);
            }
        }

    }

    //查看图书
    private void checkBook() {
        List list = bookService.getbook();
        for (Book book : list){
            System.out.println(book);
        }
    }
}

UserController类代码

package com.xszx.controller;




import com.xszx.beans.User;
import com.xszx.service.UserService;
import com.xszx.service.impl.UserServiceImpl;

import java.util.Scanner;

public class UserController {
    Scanner sc=new Scanner(System.in);
    BookController bookController = new BookController();
    UserService userService = new UserServiceImpl();
    User user = new User();
    public void menu(User user) {
        System.out.println();
        System.out.println("       ---小张图书管理系统---       ");
        System.out.println("***【1】登录 【2】注册 【0】退出系统***");
        System.out.println("请选择您要进行的操作:");
        int n = sc.nextInt();
        if (n==1){
            login();
        }else if (n==2){
            register();
        }else if (n==0){
            System.out.println("系统已退出,欢迎下次使用!");
            System.exit(0);
        }else{
            System.out.println("输入错误,请重新输入!");
            menu(null);
        }
    }

    private void register() {
        User user = new User();
        System.out.println("请输入账号:");
        user.setUsername(sc.next());

        boolean register = userService.register(user);
        if (register){
            System.out.println("该账户已存在,请重新注册");
            menu(null);
        }else{
            System.out.println("请输入密码:");
            user.setPassword(sc.next());
             userService.addUser(user);
                 System.out.println("注册成功!");
                 menu(null);
        }
    }

    private User login() {
        User user = new User();
        System.out.println("请输入账号:");
        user.setUsername(sc.next());
        System.out.println("请输入密码:");
        user.setPassword(sc.next());
        user = userService.login(user);
        if (user!=null){
            System.out.println("登陆成功!!!");
            if (user.getRole()==1){
                new BookController().menu(user);
            }else{
                new BookController().usermenu(user);
            }
            return user;
        }else{
            System.out.println("账号密码错误,登陆失败!");
            menu(null);
        }
        return null;
    }
}

BookDao类代码

JbookDao类代码

UserDao类代码

BookService接口代码

package com.xszx.service;


import com.xszx.beans.Book;
import com.xszx.beans.User;

import java.util.List;

//业务层  处理业务
public  interface BookService {
    List getbook();

    int addbook(Book book);

    Book getbookname(Book book);

    int alterBook(Book book, Book book1);

    int deleteBook(Book book);

    int getbookid(int bid);

    int getbookuidbid(User user, int bid);

    void delbooksum(int bid);

    void alterjbook(int bid);

    void addbooksum(int bid);

    void alterhbook(int bid);
}

JbookService接口代码

package com.xszx.service;

import com.xszx.beans.Jbook;
import com.xszx.beans.User;

import java.util.List;

public interface JbookService {

    void returnBook(int id);

    List checkJbook(User user);

    List checkHbook(User user);

    List checkWHbook(User user);

    void borrowBook(User user, int bid);
}

UserService接口代码

package com.xszx.service;

import com.xszx.beans.User;

public interface UserService {
    User login(User user);

    boolean register(User user);

    void addUser(User user);
}

BookServiceImpl类代码

JbookServiceImpl类代码

UserServiceImpl类代码

Test类代码

JDBCUtil类代码

注意数据库需自行创建

package com.xszx.util;

import java.sql.*;

public class JDBCUtil {
    static String url = "jdbc:mysql://127.0.0.1:3306/booksystem";
    static String user = "root";
    static String password = "123456";
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        Connection conn=null;
        try {
            conn= DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void close(Connection conn, PreparedStatement ps, ResultSet rs){
        try {
            if(conn!=null){
                conn.close();
            }
            if(ps!=null){
                ps.close();
            }
            if(rs!=null){
                rs.close();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

该系统可作为大二大三Java课设使用,也可进行定制,需要全部源码请后台私信我!!!!

你可能感兴趣的:(Java学习,java)