JAVASE图书管理系统
主要业务有:管理员(增删改查),会员(借书还书查看记录)
管理员主要有:查看图书,增加图书,修改图书,会员管理,删除图书,
会员主要有:查看图书,借书,还书,查看记录,返回登陆
项目分层
beans:实体类层
controller:控制层
dao:数据层
service:业务层
impl:实现层
test:测试层
lib:JDBC包
util:工具类
具体代码
beans Book:
package com.xszx.beans;
//实体类层
public class Book {private int id;private String name;private String authon;private int num;public Book(int id, String name, String authon, int num) {this.id = id;this.name = name;this.authon = authon;this.num = num;}public Book() {}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 String getAuthon() {return authon;}public void setAuthon(String authon) {this.authon = authon;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}@Overridepublic String toString() {return "Book{" +"id=" + id +", name='" + name + '\'' +", authon='" + authon + '\'' +", num=" + num +'}';}
}
beans JieBook:
package com.xszx.beans;import java.sql.Timestamp;
import java.util.Date;public class JieBook {private int id;private int uid;private int bid;private Date jdate;private Date hdate;public JieBook() {}public JieBook(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(Timestamp jdate) {this.jdate = jdate;}public Date getHdate() {return hdate;}public void setHdate(Timestamp hdate) {this.hdate = hdate;}@Overridepublic String toString() {return "JieBook{" +"id=" + id +", uid=" + uid +", bid=" + bid +", jdate='" + jdate + '\'' +", hdate='" + hdate + '\'' +'}';}
}
beans User:
package com.xszx.beans;public class User {private int id;private String account;//账号private String pwd;//密码private int role;public User(int id, String account, String pwd, int role) {this.id = id;this.account = account;this.pwd = pwd;this.role = role;}public User() {}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getAccount() {return account;}public void setAccount(String account) {this.account = account;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public int getRole() {return role;}public void setRole(int role) {this.role = role;}@Overridepublic String toString() {return "User{" +"id=" + id +", account='" + account + '\'' +", pwd='" + pwd + '\'' +", role=" + role +'}';}
}
controller BookController:
package com.xszx.controller;import com.xszx.beans.Book;
import com.xszx.beans.JieBook;
import com.xszx.beans.User;
import com.xszx.service.BookService;
import com.xszx.service.impl.BookServiceImpl;import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;//控制层
public class BookController {Scanner sc = new Scanner(System.in);//控制层要告诉业务层,因为不能直接new接口所以要new接口实现类BookService bookService = new BookServiceImpl();//主方法public void show(){System.out.println("【1】查看图书【2】添加图书【3】修改图书【4】删除图书【5】会员管理【6】返回上一层");int n = sc.nextInt();if (n == 1){showbook();}else if (n == 2){addbook();}else if (n == 3){update();}else if (n == 4){delete();}else if (n == 5){huiyuanguanli(null);}else if (n == 6){new UserController().register();}}void huiyuanguanli(User us) {System.out.println("【1】查看图书【2】借书【3】还书【4】查看借书记录【5】返回登录");int n =sc.nextInt();if (n == 1){lookbook(us);}else if (n == 2){// jiebook(us);addjbook(us);}else if (n == 3){upjbook(us);// huanbook(us);}else if (n == 4){// lookjilu1(us);getjbooksuser(us);}else if (n == 5){new UserController().register();}}//还书private void upjbook(User us) {System.out.println("请输入你要还书的编号");int bid = sc.nextInt();//判断图书是否需要归还 uid bid hdateint id =bookService.getjbookuidbid(us,bid);if (id>0){bookService.updatejbook(id,bid);System.out.println("还书成功");huiyuanguanli(us);}else{System.out.println("这本书不需要还");huiyuanguanli(us);}}//借书private void addjbook(User us) {System.out.println("请输入你要借的图书编号");int bid =sc.nextInt();int i = bookService.getbookid(bid);if (i == 1){int id =bookService.getjbookuidbid(us,bid);if (id>0){System.out.println("当前这本书借了没有还");huiyuanguanli(us);}else{bookService.addjbook(us,bid);System.out.println("借书成功");huiyuanguanli(us);}}else{System.out.println("图书不存在");huiyuanguanli(us);}}//查看借书记录private void getjbooksuser(User us) {List<JieBook> jiebookList = bookService.getjboosuser(us);for (JieBook j : jiebookList){System.out.println(j);}huiyuanguanli(us);}private void lookjilu1(User us) {System.out.println("【1】查看所有【2】查看已经归还的完成的借书记录【3】查看没有归还的【4】返回上一层");int n = sc.nextInt();if (n == 1){lookjilu(us);}else if (n == 2){lookyihuan(us);}else if (n == 3){lookguihuan(us);}else if (n == 4){huiyuanguanli(us);}}//查看已经归还的借书记录private void lookyihuan(User us) {List<JieBook> jlist = bookService.look();for (JieBook jb : jlist){System.out.println(jb);huiyuanguanli(us);}}//查看没有归还的借书记录private void lookguihuan(User us) {List<JieBook> huanlist = bookService.getlookbook();for (JieBook jb : huanlist){System.out.println(jb);}huiyuanguanli(us);}//查看记录private void lookjilu(User us) {List<JieBook> jlist = bookService.look();for (JieBook jb : jlist){System.out.println(jb);huiyuanguanli(us);}}//会员查看图书查看图书private void lookbook(User us) {List<Book> list = bookService.getbooks();for (Book book : list){System.out.println(book);}huiyuanguanli(us);}//删除图书private void delete() {System.out.println("请输入你要删除的图书编号");int id = sc.nextInt();bookService.delbook(id);System.out.println("删除成功");show();}//修改图书private void update() {Book book = new Book();System.out.println("请输入你要修改的图书名字");book.setName(sc.next());//提前判断图书名字是否存在,存在则修改,不存在则不修改boolean b = bookService.getbookname(book);if (b){Book book1 = new Book();System.out.println("请输入你要修改后的图书名字");book1.setName(sc.next());System.out.println("请输入你要修改后的图书作者");book1.setAuthon(sc.next());System.out.println("请输入你要修改后的图书数量");book1.setNum(sc.nextInt());//传入两个值,一个判断的值一个修改的值bookService.updatebook(book,book1);System.out.println("修改完成");show();}else{System.out.println("你要修改的图书不存在");show();}}//添加图书private void addbook() {//控制曾告诉业务层,用list调用业务层来创建addbooks抽象方法Book book = new Book();System.out.println("请输入你要添加的图书名字");book.setName(sc.next());//提前判断图书名字是否存在,存在则不添加,不存在则继续添加boolean b = bookService.getbookname(book);if (b){System.out.println("添加的图书你名字冲突");show();}else {System.out.println("请输入你要添加的图书作者");book.setAuthon(sc.next());System.out.println("请输入你要添加的图书数量");book.setNum(sc.nextInt());//后台添加完成是否成功int i = bookService.addbook(book);if (i == 1){System.out.println("添加成功");show();}else{System.out.println("添加失败");show();}}}//查看图书private void showbook() {//控制层告诉业务层,用list调用业务层来创建getbooks抽象方法List<Book> list = bookService.getbooks();for (Book book : list){System.out.println(book);}show();}}
controller UserControlle:
{Scanner sc = new Scanner(System.in);UserService userService = new UserServiceImpl();public void register(){System.out.println("【1】登录【2】注册");int n = sc.nextInt();if (n == 1){denglu();}else if (n == 2){zhuce();}}//注册private void zhuce() {User user = new User();System.out.println("请输入你要注册的账号");user.setAccount(sc.next());System.out.println("请输入你要注册的密码");user.setPwd(sc.next());//判断账号是否重复 如果重复就停止添加,如果不重复就继续添加boolean b = userService.getzhanghao(user);if (b){System.out.println("您输入的账号重复请重新注册");register();}else{userService.zhuce(user);System.out.println("注册成功,请登录");register();}}//登录private void denglu() {User user = new User();System.out.println("请输入你的账号");user.setAccount(sc.next());System.out.println("请输入你的密码");user.setPwd(sc.next());User us = userService.getzm(user);if (us == null){System.out.println("你的账号密码错误");denglu();}else{System.out.println("登陆成功");//判断是管理员还是用户if (us.getRole() == 1){new BookController().show();}else{new BookController().huiyuanguanli(us);}}}
}
service BookService:
package com.xszx.service;import com.xszx.beans.Book;
import com.xszx.beans.JieBook;
import com.xszx.beans.User;
import com.xszx.dao.BookDao;import java.util.List;//业务层
public interface BookService {//创建查看的抽象方法由BookServiceImpl来实现List<Book> getbooks();int addbook(Book book);boolean getbookname(Book book);void updatebook(Book book, Book book1);void delbook( int id);List<JieBook> look();List<JieBook> getlookbook();List<JieBook> getjboosuser(User us);int getbookid(int bid);int getjbookuidbid(User us, int bid);void addjbook(User us, int bid);void updatejbook(int id,int bid);}
service UserService:
package com.xszx.service;import com.xszx.beans.User;public interface UserService {boolean getzhanghao(User user);User getzm(User user);void zhuce(User user);}
serviceimpl BookServiceImpl:
package com.xszx.service.impl;import com.xszx.beans.Book;
import com.xszx.beans.JieBook;
import com.xszx.beans.User;
import com.xszx.dao.BookDao;
import com.xszx.service.BookService;import java.util.List;public class BookServiceImpl implements BookService {//应为要调用数据层,所以new一个数据层的对象BookDao bookDao = new BookDao();//添加public int addbook(Book book) {return bookDao.addbook(book);}@Overridepublic boolean getbookname(Book book) {return bookDao.getbookname(book);}@Overridepublic void updatebook(Book book, Book book1) {bookDao.updatebook( book,book1);}@Overridepublic void delbook(int id) {bookDao.delbook(id);}@Overridepublic List<JieBook> look() {return bookDao.look();}@Overridepublic List<JieBook> getlookbook() {return bookDao.getlookbook();}@Overridepublic List<JieBook> getjboosuser(User us) {return bookDao.getjboosuser(us);}@Overridepublic int getbookid(int bid) {return bookDao.getbookid(bid);}@Overridepublic int getjbookuidbid(User us, int bid) {return bookDao.getjbookuidbid(us,bid);}@Overridepublic void addjbook(User us, int bid) {//添加借书记录bookDao.addjbook(us,bid);//图书数量-1bookDao.delbooksum(bid);}@Overridepublic void updatejbook(int id,int bid) {bookDao.updatejbook(id);//还书bookDao.addbooksum(bid);//+1}//实现查看方法@Overridepublic List<Book> getbooks() {//创建数据层的查看方法,返回一个listList<Book> list = bookDao.getbooks();return list;}}
service UserServiceImpl:
package com.xszx.service.impl;import com.xszx.beans.User;
import com.xszx.dao.UserDao;
import com.xszx.service.UserService;public class UserServiceImpl implements UserService {UserDao userDao = new UserDao();@Overridepublic User getzm(User user) {return userDao.getzm(user);}@Overridepublic void zhuce(User user) {userDao.zhuce(user);}@Overridepublic boolean getzhanghao(User user) {return userDao.getzhanghao(user);}
}
test Test:
package com.xszx.test;
import com.xszx.controller.UserController;//启动方法
public class Test {public static void main(String[] args) {System.out.println("欢迎来到**的图书管");new UserController().register();}}
util (数据库需要自己创建)
package com.xszx.util;import java.sql.*;public class JDBCUtil {static String url = "jdbc:mysql://localhost:3306/work";static String user = "root";static String password = "root";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();}}
}
JDBC需要连接数据库,数据库以及内容需要自己创建
运行结果展示
管理员运行结果:省略
会员运行结果:
欢迎来到**的图书管
【1】登录【2】注册
2
请输入你要注册的账号
777
请输入你要注册的密码
777
注册成功,请登录
【1】登录【2】注册
1
请输入你的账号
777
请输入你的密码
777
登陆成功
【1】查看图书【2】借书【3】还书【4】查看借书记录【5】返回登录
1
Book{id=1, name='三国演义', authon='罗贯中', num=9}
Book{id=2, name='西游记', authon='吴承恩', num=9}
【1】查看图书【2】借书【3】还书【4】查看借书记录【5】返回登录
2
请输入你要借的图书编号
1
借书成功
【1】查看图书【2】借书【3】还书【4】查看借书记录【5】返回登录
1
Book{id=1, name='三国演义', authon='罗贯中', num=8}
Book{id=2, name='西游记', authon='吴承恩', num=9}
【1】查看图书【2】借书【3】还书【4】查看借书记录【5】返回登录
4
JieBook{id=2, uid=13, bid=1, jdate='2024-07-15 10:32:05.0', hdate='null'}
【1】查看图书【2】借书【3】还书【4】查看借书记录【5】返回登录
3
请输入你要还书的编号
1
还书成功
【1】查看图书【2】借书【3】还书【4】查看借书记录【5】返回登录
1
Book{id=1, name='三国演义', authon='罗贯中', num=9}
Book{id=2, name='西游记', authon='吴承恩', num=9}
【1】查看图书【2】借书【3】还书【4】查看借书记录【5】返回登录
4
JieBook{id=2, uid=13, bid=1, jdate='2024-07-15 10:32:05.0', hdate='2024-07-15 10:32:12.0'}
【1】查看图书【2】借书【3】还书【4】查看借书记录【5】返回登录