CharonD

导航

完善Java图书管理系统

原本功能:

  1. 用户登录
  2. 增加图书
  3. 修改图书信息
  4. 删除图书信息
  5.查询图书
  6. 查看所有的图书

扩充功能:

  1. 借阅图书(输入书名和借阅人姓名,库存量为0的图书不能再借)

  2. 还书功能(根据书名进行还书)

源码:

BookManager是和用户进行交互,在控制台显示信息提示用户如何操作,并且调用dao查询数据库拿到结果返回给用户。

  1 public class BookManager{
  2     LinkedHashMap<String, Book> stu = new LinkedHashMap<String, Book>();
  3     Book book = new Book();
  4     Scanner sc = new Scanner(System.in);
  5         
  6     public void Manage(int choice) throws Exception{            
  7         switch (choice) {
  8             case 0:
  9                 list();
 10                 break;
 11             case 1:
 12                 find();
 13                 break;
 14             case 2:
 15                 add();
 16                 break;
 17             case 3:
 18                 modify();
 19                 break;
 20             case 4:
 21                 delete();
 22                 break;
 23             default: {
 24                 throw new Exception("没有该功能,请重新选择!");
 25             }        
 26         }        
 27     }
 28     
 29     //获取所有书籍
 30     public void list() {
 31         Dao dao = new Dao();
 32         List<Book> list = dao.getAllBooks();
 33         System.out.println("书籍列表如下:");
 34         for(Book book : list) {
 35             System.out.println("书名:"+book.getName()+" 作者:"+book.getAuthor()+" 库存量:"+book.getNum());
 36         }    
 37     }
 38         
 39     //搜索书籍
 40     public void find() {
 41         System.out.println("输入要搜索的书名:");
 42         String name = sc.next();
 43         Dao dao = new Dao();
 44         if(!dao.judgeExist(name))
 45             System.out.println("该书籍不存在!");
 46         else{
 47             Book book = dao.find(name);
 48             System.out.println("书名:"+book.getName()+" 作者:"+book.getAuthor()+" 库存量:"+book.getNum());
 49         }
 50     }
 51     
 52     //添加书籍
 53     public void add() {
 54         Dao dao = new Dao();
 55         System.out.println("输入要添加的书名:");
 56         String name = sc.next();
 57         if(dao.judgeExist(name))
 58             System.out.println("该书籍已存在!");
 59         else{
 60             System.out.println("输入作者:");
 61             String author = sc.next();
 62             System.out.println("输入数量:");
 63             int num = sc.nextInt();
 64             dao.addBook(new Book(name,author,num));
 65             System.out.println("添加成功!");
 66             list();
 67         }
 68     }
 69  
 70     //修改书籍
 71     public void modify(){
 72         System.out.println("输入要修改的书名:");
 73         String name = sc.next();
 74         Dao dao = new Dao();
 75         if(!dao.judgeExist(name))
 76             System.out.println("该书籍不存在!");
 77         else{
 78             Book book = dao.find(name);
 79             System.out.println("书名:"+book.getName()+" 作者:"+book.getAuthor()+" 库存量:"+book.getNum());
 80             System.out.println("输入修改后的书名:");
 81             String newname = sc.next();
 82             System.out.println("输入修改后的作者:");
 83             String newauthor = sc.next();
 84             System.out.println("输入修改后的库存量:");
 85             int newnum = sc.nextInt();
 86             dao.modifyBook(name, new Book(newname,newauthor,newnum));
 87             System.out.println("修改成功!");
 88         }
 89     }
 90  
 91     //删除书籍
 92     public void delete() {
 93         System.out.println("请输入要删除的书名:");
 94         String name = sc.next();
 95         Dao dao = new Dao();
 96         if(!dao.judgeExist(name)) {
 97             System.out.println("书籍不存在!");
 98         }
 99         else{ 
100             dao.deleteBook(name);
101             System.out.println("删除成功!");
102         }
103     }
104 }

 Dao类就是和数据库交互,执行sql语句从数据库取得结果。

  1 public class Dao {
  2     
  3     /**
  4      * 根据书名判定书籍是否已存在
  5      */
  6     public boolean judgeExist(String targetName){
  7     Connection conn = null; 
  8     PreparedStatement stmt = null;    
  9     ResultSet rs = null;
 10     int count = 0;
 11     try {
 12         conn = MySQLUtils.getConn();
 13         String sql = "select count(*) as count from book where book_name = ?";
 14         stmt = conn.prepareStatement(sql);
 15         stmt.setString(1, targetName);
 16         rs = stmt.executeQuery();
 17         while(rs.next()){
 18             count = rs.getInt("count");
 19         }
 20     } 
 21     catch (SQLException e) {
 22         e.printStackTrace();
 23     }
 24     finally{
 25         MySQLUtils.closeConn(conn, stmt, rs);
 26     }
 27     if(count==0) return false;
 28     else
 29         return true;
 30    }
 31     
 32     
 33     /**
 34      * 获取所有书籍列表
 35      */
 36     public List<Book> getAllBooks(){
 37         Connection conn = null; 
 38         Statement stmt = null;    
 39         ResultSet rs = null;
 40         List<Book> bookList = new ArrayList<>();
 41         try {
 42             
 43             conn = MySQLUtils.getConn();
 44             String sql = "select * from book";
 45             stmt = conn.createStatement();
 46             rs = stmt.executeQuery(sql);
 47             while(rs.next()){
 48                 String name = rs.getString("book_name");
 49                 String author = rs.getString("book_author");
 50                 int num = rs.getInt("book_num");
 51                 Book book = new Book(name, author, num);
 52                 bookList.add(book);
 53             }
 54         } 
 55         catch (SQLException e) {
 56             e.printStackTrace();
 57         }
 58         finally{
 59             MySQLUtils.closeConn(conn, stmt, rs);
 60         }
 61         return bookList;
 62     }
 63     
 64     /**
 65      * 根据书名查找书籍详细信息
 66      */
 67     public Book find(String bookName){
 68         Connection conn = null; 
 69         PreparedStatement stmt = null;    
 70         Book book = null;
 71         try {
 72             conn = MySQLUtils.getConn();
 73             String sql = "select * from book where book_name = ?";
 74             stmt = conn.prepareStatement(sql);
 75             stmt.setString(1, bookName);
 76             ResultSet tmpres = stmt.executeQuery();
 77             book = new Book();
 78             while(tmpres.next()){
 79                 book.setName(tmpres.getString("book_name"));
 80                 book.setAuthor(tmpres.getString("book_author"));
 81                 book.setNum(tmpres.getInt("book_num"));
 82             }
 83         } 
 84         catch (SQLException e) {
 85             e.printStackTrace();
 86         }
 87         finally{
 88             MySQLUtils.closeConn(conn, stmt);
 89         }
 90         return book;
 91     }
 92     
 93     /**
 94      * 添书
 95      */
 96     public void addBook(Book book){
 97         Connection conn = null; 
 98         PreparedStatement stmt = null;    
 99         try {
100             conn = MySQLUtils.getConn();
101             String sql = "insert into book(book_name, book_author, book_num) values(?,?,?)";
102             stmt = conn.prepareStatement(sql);
103             stmt.setString(1, book.getName());
104             stmt.setString(2, book.getAuthor());
105             stmt.setInt(3, book.getNum());
106             stmt.executeUpdate();
107         } 
108         catch (SQLException e) {
109             e.printStackTrace();
110         }
111         finally{
112             MySQLUtils.closeConn(conn, stmt);
113         }
114     }
115     
116     /**
117      * 修改书
118      */
119     public void modifyBook(String targetName, Book book){
120         Connection conn = null;
121         PreparedStatement stmt = null;
122         try {
123             conn = MySQLUtils.getConn();
124             String sql = "update book set book_name = ?, book_author = ?,  book_num = ? where book_name = ?";
125             stmt = conn.prepareStatement(sql);
126             stmt.setString(1, book.getName());
127             stmt.setString(2, book.getAuthor());
128             stmt.setInt(3, book.getNum());
129             stmt.setString(4, targetName);
130             stmt.executeUpdate();
131         }
132         catch (SQLException e) {
133             e.printStackTrace();
134         }
135         finally{
136             MySQLUtils.closeConn(conn, stmt);
137         }
138     }
139     
140     /**
141      * 删书
142      */
143     public void deleteBook(String bookName){
144         Connection conn = null; 
145         PreparedStatement stmt = null;    
146         try {
147             conn = MySQLUtils.getConn();
148             String sql = "delete from book where book_name = ?";
149             stmt = conn.prepareStatement(sql);
150             stmt.setString(1, bookName);
151             stmt.executeUpdate();
152         } 
153         catch (SQLException e) {
154             e.printStackTrace();
155         }
156         finally{
157             MySQLUtils.closeConn(conn, stmt);
158         }
159     }
160 }

扩充:

借阅功能(在Dao中增加下列函数)

 1     // 检查书是否存在馆里和是否已借出
 2     public int testReader(String bookname) {
 3  
 4         // 馆里有此书,查找此书的索引,有此书a>=0,无返-1
 5         int a = selectIndex(bookname);
 6         // 馆里有此书
 7         if (a >= 0) {
 8             if ((book.borrowreaders[a].equals("无") || book.borrowreaders[a].equals("")))// 无人借此书,""为扫描器的直接回车值
 9                 return 0;//未借
10             else
11                 return 1;//已借
12         } else {
13             return -1;// 馆里没有此书
14         }
15  
16     }
17     //将借阅者设置为无或者为空
18     public void setBorrowReader(String bookname){
19         int a = selectIndex(bookname);
20         book.borrowreaders[a]="无";
21     }
22     //查询指定书的借阅者
23     public String getBorrowReader(String bookname){
24         int a = selectIndex(bookname);
25         return book.borrowreaders[a];
26     }
27  
28     //书籍借阅功能
29     public void borrow(String bookname,String readername){
30         int a = selectBook(bookname);
31         book.borrowreaders[a]=readername;
32     }
33 }

还书功能(在BookManager类中增加下列函数)

 1 // 还书功能
 2     public void returnBook() {
 3         System.out.print("请输入要还的书籍名称:");
 4         Scanner scanner = new Scanner(System.in);
 5         String bookname = scanner.nextLine();
 6         int flag = bo.testReader(bookname);
 7         if (flag == 0) {
 8             System.out.println(bookname + "暂未借出。");
 9             System.out.print("输入0回车返回至主菜单,输入其他键则继续还书");
10             String a = scanner.nextLine();
11             if(a.equals("0")){
12                 showMenu();
13             }else {
14                 returnBook();
15             }
16         } else if (flag == -1) {
17             System.out.println(bookname + "不存在该图书馆中。");
18             System.out.print("输入0回车返回至主菜单,输入其他键则继续还书");
19             String a = scanner.nextLine();
20             if(a.equals("0")){
21                 showMenu();
22             }else {
23                 returnBook();
24             }
25         } else if (flag == 1) {
26  
27             System.out.println(bo.getBorrowReader(bookname) + "已经还书成功!");
28             bo.setBorrowReader(bookname);
29             System.out.print("输入0回车返回至主菜单,输入其他键则继续还书");
30             String a = scanner.nextLine();
31             if(a.equals("0")){
32                 showMenu();
33             }else {
34                 returnBook();
35             }
36         }
37     }
38  
39     // 书籍借阅功能
40     public void borrowBook() {
41         System.out.print("请输入要借的书名:");
42         Scanner scanner = new Scanner(System.in);
43         String bookname = scanner.nextLine();
44         int flag = bo.testReader(bookname);
45         switch (flag) {
46         case 0://存在此书并无人借的情况
47  
48             System.out.println("请输入借阅者的名字:");
49             String readername = scanner.nextLine();
50             bo.borrow(bookname,readername);
51             System.out.println(readername+"从该图书馆里借出了以"+bookname+"为名的书籍。");
52             System.out.print("输入0回车返回至主菜单,输入1回车则继续借书:");
53             int a0 = scanner.nextInt();
54                 if(a0==0)
55                     showMenu();
56                 if(a0==1)
57                     borrowBook();
58             break;
59  
60         case 1://存在此书被借的情况
61             System.out.println("借书失败," + bookname + "已被借!");
62             System.out.print("输入0回车返回至主菜单,输入1回车则继续借书:");
63             int a = scanner.nextInt();
64             switch (a) {
65             case 0:
66                 showMenu();
67                 break;
68  
69             case 1:
70                 borrowBook();
71                 break;
72                 
73                 }
74         case -1://不存在此书的情况
75             System.out.println("借书失败," + bookname + "不存在该图书馆中。");
76             System.out.print("输入0回车返回至主菜单,输入1回车则继续借书:");
77             int a1 = scanner.nextInt();
78             switch (a1) {
79             case 0:
80                 showMenu();
81                 break;
82  
83             case 1:
84                 borrowBook();
85                 break;
86  
87             }
88         }
89     }
90 }

 

posted on 2021-03-12 13:47  CharonD  阅读(130)  评论(0编辑  收藏  举报