catalogue
Organize books (this function is extensible)
1. Create book related classes
2. Create operation related classes
Create package operation first
3. Create user related classes
Create the Main method and build the overall logic
5. Implement each specific Operation
Relevant operation class framework code:
Library management system framework code
Improvement of library management system interface code
1, Core requirements
1. Simple login
2. Management end
-
Organize books (this function is extensible)
-
Consult books
-
Add books
-
Delete books
-
Print book list
-
sign out
3. Client
-
Query books
-
Borrow books
-
Return books
-
sign out
2, Class design
1. Create book related classes
Create a package book first
Create a Book class to represent a book; Create a BookList class to save N books
2. Create operation related classes
Create package operation first
Next, create a group of operation classes. Each class corresponds to a user's actions AddOperation, DeleteOperation, FindOperation, DisplayOperation, BorrowOperation, ReturnOperation and UpdateOperation
Create the empty class first, and don't worry about the implementation details Abstract the benefits of Operation: low coupling between operations and between operations and users
3. Create user related classes
Create package user first
Create User class, which is an abstract class; Create a common User class, which is a subclass of User; Create administrator User class
4. Integration
Create the Main method and build the overall logic
5. Implement each specific Operation
3, Frame modeling
4, Basic framework code
book class:
package book_management; //Book entity class public class Book { //title private String bookName; //author private String author; //Price private double price; //Book category private String type; //Borrowing status: false by default private boolean isBorrowed; public Book(String bookName,String author,double price,String type){ this.bookName = bookName; this.author = author; this.price = price; this.type = type; } //Only the attributes that need to be modified need to provide setter - price, type and borrowing status public void setPrice(double price){ this.price = price; } public void setType(String type){ this.type = type; } public void setBorrowed(boolean borrowed){ isBorrowed = borrowed; } public String getBookName(){ return bookName; } public String getAuthor(){ return author; } public double getPrice(){ return price; } public String getType(){ return type; } public boolean isBorrowed(){ return isBorrowed; } @Override public String toString() { return "Book{" + "bookName='" + bookName + '\'' + ", author='" + author + '\'' + ", price=" + price + ", type='" + type + '\'' + ", isBorrowed=" + isBorrowed + '}'; } }
bookList class:
package book_management; import come.B; import java.util.ArrayList; import java.util.List; //Bookshelves public class BookList { // Books stores all the books in the current bookshelf, and each Book object is an entity of a Book private static List<Book> books = new ArrayList<>(); // booksName stores all book names - query books, etc. are operated through book names private static List<String> bookName = new ArrayList<>(); // Initialize books and booksName, and put the four famous works into the bookshelf by default // Initializing static variables using static code blocks static { books.add(new Book("Journey to the West","Wu Chengen",99.99,"novel")); books.add(new Book("The Dream of Red Mansion","Cao Xueqin",108.9,"novel")); books.add(new Book("Water Margin","Shi Naian",199.89,"novel")); books.add(new Book("Romance of the Three Kingdoms","Luo Guanzhong",145.39,"novel")); bookName.add("Journey to the West"); bookName.add("The Dream of Red Mansion"); bookName.add("Water Margin"); bookName.add("Romance of the Three Kingdoms"); } public void displayBooks(){ for (Book book : books){ System.out.println(book); } } public boolean contains(String booksName){ return BookList.bookName.contains(booksName); } public void add(Book book){ books.add(book); bookName.add(book.getBookName()); } }
Main method:
package book_management; import java.util.Scanner; //The entrance of the whole program public class Main { private static Scanner scanner = new Scanner(System.in); public static void main (String[] args){ User user = login(); BookList bookList = new BookList(); while(true){ int choice = user.menu(); if(choice == -1){ System.out.println("ByeBye!"); break; } user.doOperation(choice,bookList); } } private static User login() { System.out.println("Please enter user name:"); String name = scanner.next(); System.out.println("Please select your role. 1 is an ordinary user and 0 is an administrator"); int choice = scanner.nextInt(); if (choice == 1){ return new NormalUser(name); } return new AdminUser(name); } }
User class:
package book_management; //User class - abstract class //I don't know whether it's an ordinary user or an administrator public abstract class User { //user name protected String name; //Method of authorized operation protected IOperation[] operations; // Menu. Only specific subclasses know what the menu looks like~~ public abstract int menu(); public void doOperation(int choice,BookList bookList){ operations[choice - 1].work(bookList); } }
Relevant operation class framework code:
package book_management; import java.util.Scanner; //Interface for operating bookshelf // Add, delete, modify and check - borrow books - return books // There are only global constants and abstract methods in the interface public interface IOperation { // Global constant, shared by subclasses of all interfaces // static + final co modification Scanner scanner = new Scanner(System.in); //Operate in the corresponding bookshelf class void work(BookList bookList); } //Add books public class AddOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("Adding books is now in progress..."); } } //Borrow books public class BorrowOperation implements IOperation{ @Override public void work(BookList bookList) { } } //Delete books public class DeleteOperation implements IOperation{ @Override public void work(BookList bookList) { } } //Show all books public class DisplayAllBooks implements IOperation{ @Override public void work(BookList bookList) { System.out.println("What you are doing now is to view all books!"); bookList.displayBooks(); } } //Find books public class FindOperation implements IOperation{ @Override public void work(BookList bookList) { } } //Return books public class ReturnOperation implements IOperation{ @Override public void work(BookList bookList) { } } //Update books public class UpdateOperation implements IOperation{ @Override public void work(BookList bookList) { } }
5, Source code
Library management system framework code
Improvement of library management system interface code
End of this section^_^