Java实现简易的图书管理系统(只有后端)

实现本系统的初衷是为了加深我对java语法的一些理解和练习,所以只实现了几个简单的操作。

本系统共分为两个角色,分别为用户和管理员,都只能进行最基本图书操作。

共四个文件:(1)User:记录用户操作的一些方法;(2)Manager:记录管理员操作的一些方法;(3)Book:定义了每本书的结构;(4)BookList:存储所有书籍信息;

1、Book类

 1 public class Book {
 2     // 用来表示一本书的信息
 3     private String name;
 4     private String author;
 5     private double price;
 6     private String type;
 7     private boolean isBorrowed = false;
 8 
 9     Book(String name, String author, double price, String type, boolean isBorrowed) {
10         this.name = name;
11         this.author = author;
12         this.price = price;
13         this.type = type;
14         this.isBorrowed = isBorrowed;
15     }
16 
17     public String getName() {
18         return name;
19     }
20     public void setName(String name) {
21         this.name = name;
22     }
23     public String getAuthor() {
24         return author;
25     }
26     public void setAuthor(String author) {
27         this.author = author;
28     }
29     public double getPrice() {
30         return price;
31     }
32     public void setPrice(double price) {
33         this.price = price;
34     }
35     public String getType() {
36         return type;
37     }
38     public void setType(String type) {
39         this.type = type;
40     }
41     public boolean isBorrowed() {
42         return isBorrowed;
43     }
44     public void setBorrowed(boolean borrowed) {
45         isBorrowed = borrowed;
46     }
47 
48     @Override
49     public String toString() {
50         return "Book{" +
51                 "name='" + name + '\'' +
52                 ", author='" + author + '\'' +
53                 ", price=" + price +
54                 ", type='" + type + '\'' +
55                 ", isBorrowed=" + isBorrowed +
56                 '}';
57     }
58 }

2、BookList类

 1 public class BookList {
 2     // 书籍列表
 3     private Book[] books = new Book[100];
 4     private int size = 0;
 5     public BookList(){
 6         books[0] = new Book("水浒传", "施耐庵", 25, "文学", false);
 7         books[1] = new Book("红楼梦", "曹雪芹", 30, "文学", false);
 8         books[2] = new Book("三国演义", "罗贯中", 28, "文学", false);
 9         books[3] = new Book("西游记", "吴承恩", 35, "文学", false);
10         books[4] = new Book("数据结构", "严蔚敏", 35, "计算机", false);
11     }
12 
13     public Book getBooks(int index) {
14         return books[index];
15     }
16     public void setBooks(int index, Book book) {
17         books[index] = book;
18     }
19     public int getSize() {
20         return size;
21     }
22     public void setSize(int size) {
23         this.size = size;
24     }
25 
26     public void deleteBook(int index){
27         for(int i = index; i < size-1; i++){
28             books[i] = books[i+1];
29         }
30         size--;
31     }
32 }

3、User类

 1 import java.util.Scanner;
 2 public class User {
 3     // 普通用户
 4 //    private String name;
 5 //        ----- 1.查阅书籍信息 -----
 6 //        -----   2.借阅书籍   -----
 7 //        -----   3.归还书籍   -----
 8 //        -----   0.退出程序   -----
 9     public void inquireBook(BookList bookList) {
10         System.out.println("请输入要查询书籍的名字!");
11         Scanner scanner = new Scanner(System.in);
12         String bookName = scanner.next();
13         int result = inquire(bookList, bookName);
14         if(result >= 0){
15             System.out.println(bookList.getBooks(result));
16         }else{
17             System.out.println("查询的书籍不存在!");
18         }
19     }
20 
21     public void borrowBook(BookList bookList) {
22         System.out.print("请输入想要借阅书籍的名字:>> ");
23         Scanner scanner = new Scanner(System.in);
24         String bookName = scanner.next();
25         int result = inquire(bookList, bookName);
26         if(result >= 0)
27             bookList.getBooks(result).setBorrowed(true);
28         else
29             System.out.println("抱歉借阅的书籍不存在!");
30     }
31 
32     public void returnBook(BookList bookList) {
33         // 归还书籍
34         System.out.print("请输入想要归还书籍的名字:>> ");
35         Scanner scanner = new Scanner(System.in);
36         String BookName = scanner.next();
37         int result = inquire(bookList, BookName);
38         if(result >= 0)
39             bookList.getBooks(result).setBorrowed(false);
40         else
41             System.out.println("抱歉,归还的书名不存在!");
42     }
43 
44     public int inquire(BookList bookList, String name){
45         int result = -1;
46         for(int i = 0; i < bookList.getSize(); i++){
47             if(name.equals(bookList.getBooks(i).getName())){
48                 result = i;
49                 break;
50             }
51         }
52         return result;
53     }
54 }

4、Manager类

 1 import java.util.Scanner;
 2 public class Manager {
 3 
 4 //            ----- 1.查阅书籍信息 -----
 5 //            ----- 2.增加书籍信息 -----
 6 //            ----- 3.删除书籍信息 -----
 7 //            ----- 4.查看书籍列表 -----
 8 //            -----   0.退出程序   -----
 9 
10     public void look(BookList bookList) {
11         // 查看书籍列表
12         for(int i = 0; i < bookList.getSize(); i++){
13             System.out.println(bookList.getBooks(i));
14         }
15     }
16 
17     public void inquireBook(BookList bookList) {
18         // 查询书籍信息
19         System.out.println("请输入要查询书籍的名字!");
20         Scanner scanner = new Scanner(System.in);
21         String bookName = scanner.next();
22         for(int i = 0; i < bookList.getSize(); i++){
23             if(bookName.equals(bookList.getBooks(i).getName())){
24                 System.out.println(bookList.getBooks(i));
25                 return;
26             }
27         }
28         System.out.println("查询的书籍不存在!");
29     }
30 
31     public void addBook(BookList bookList) {
32         System.out.println("请按照 书籍名 作者 价格 类型 的顺序输入增加书籍的信息!");
33         Scanner scanner = new Scanner(System.in);
34         String name = scanner.next();
35         String author = scanner.next();
36         double price = scanner.nextDouble();
37         String type = scanner.next();
38         Book book = new Book(name, author, price, type,false);
39         bookList.setBooks(bookList.getSize(), book);
40         bookList.setSize(bookList.getSize()+1);
41     }
42 
43     public void deleteBook(BookList bookList) {
44         System.out.print("请输入想要删除书籍的名字:>> ");
45         Scanner scanner = new Scanner(System.in);
46         String bookName = scanner.next();
47         for(int i = 0; i < bookList.getSize(); i++){
48             if(bookName.equals(bookList.getBooks(i).getName())){
49                 bookList.deleteBook(i);
50                 break;
51             }
52         }
53     }
54 }

 

 

 

 

 

posted @ 2021-03-14 14:14  一帆小白  阅读(880)  评论(0编辑  收藏  举报