The main content of our implementation is the addition, deletion, modification and query of books, which is divided into administrator users and ordinary users.
The flow chart is as follows:
Create book related classes
Since the operation is object-oriented, we can divide it into several pieces and operate them separately
Create a book bag with book related things in it
Create a Book class to put things related to books. Create a Book class to represent a book
Create a BookList class to save N books
Book class
public class Book { private String name; //The name of the book private String author;//author private int price;//Price private String type;//type private boolean isBorrowed;//Is it lent }
Next is encapsulation
public class Book { private String name; private String author; private int price; private String type; private boolean isBorrowed; public Book(String name, String author, int price, String type) { this.name = name; this.author = author; this.price = price; this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getType() { return type; } public void setType(String type) { this.type = type; } public boolean isBorrowed() { return isBorrowed; } public void setBorrowed(boolean borrowed) { isBorrowed = borrowed; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", type='" + type + '\'' + ((isBorrowed == true) ? " Has been lent out" : " Not lent")+ '}'; } }
Shortcut keys: right click, and then
BookList class
public class BookList { private Book[] books = new Book[10]; //An array of books private int usedSize;//How many books are there public BookList(){ //Put some initial values books[0] = new Book("Romance of the Three Kingdoms","Luo Guanzhong",17,"novel"); books[1] = new Book("Journey to the West","Wu Chengen",47,"novel"); books[2] = new Book("Water Margin","Shi Naian",37,"novel"); books[3] = new Book("The Dream of Red Mansion","Cao Xueqin",35,"novel"); this.usedSize = 4; } public int getUsedSize() { return usedSize; } public void setUsedSize(int usedSize) { this.usedSize = usedSize; } public Book getPos(int pos){ //Get a book with pos location if(pos < 0 || pos >getUsedSize()){ System.out.println("Illegal location!"); return null; } return books[pos]; } public void setBooks(int pos,Book book){ //Set the pos subscript to a book if(pos < 0 || pos > getUsedSize()){ return; } books[pos] = book; } }
Create operation related classes
First, create a package operation, which contains classes for related operations
Here, I show the content of each operation to be realized first, so as to feel it more intuitively.
IOperation interface
The purpose of using the interface is that this method is used for subsequent additions, deletions, queries and changes. It is helpful for subsequent administrators and ordinary users to call it separately.
public interface IOperation { Scanner scanner = new Scanner(System.in); public void work(BookList bookList); }
AddOperation new books
public class AddOperation implements IOperation{ public void work(BookList bookList){ //Methods of operating n books System.out.println("New books!"); System.out.println("Please enter the name of the book you want to add: "); String name = scanner.nextLine(); System.out.println("Please enter author: "); String author = scanner.nextLine(); System.out.println("Please enter the type: "); String type = scanner.nextLine(); System.out.println("Please enter price: "); int price = scanner.nextInt(); Book book = new Book(name,author,price,type); //Create a Book Object int size = bookList.getUsedSize(); //How many books are there bookList.setBooks(size,book); //Add this book at the end of the array (we will add it from the last position by default) bookList.setUsedSize(size + 1); //Book plus one System.out.println("Successfully added books!"); } }
The implementation is as follows:
DelOperation delete book
public class DelOperation implements IOperation{ public void work(BookList bookList){ System.out.println("Delete books!"); //1. Find the location of the book by its title System.out.println("Please enter the name of the book you want to delete: "); String name = scanner.nextLine(); int currentSize = bookList.getUsedSize(); int index = 0;//Store found Subscripts int i = 0; for (; i < currentSize; i++) { Book book = bookList.getPos(i); //Book with i subscript if(book.getName().equals(name)) { //Is the book to be deleted (the equal function is used here to judge whether the values are the same, regardless of the address) index = i; break; } } if(currentSize <= i){ System.out.println("There is no book you want to delete!"); return; } //2. Delete for (int j = index;j < currentSize - 1;j++) { Book book = bookList.getPos(j + 1); //Move the back book forward bookList.setBooks(j,book); } bookList.setBooks(currentSize,null); bookList.setUsedSize(currentSize - 1); System.out.println("Delete succeeded!"); } }
The implementation is as follows:
FindOperation find books
public class FindOperation implements IOperation{ public void work(BookList bookList){ System.out.println("Find books!"); System.out.println("Please enter the name of the book you are looking for: "); String name = scanner.nextLine(); int size = bookList.getUsedSize(); int i = 0; for(;i < size;i++){ Book book = bookList.getPos(i); if(name.equals(book.getName())){ System.out.println("I found the book. The information is as follows: "); System.out.println(book); return; } } System.out.println("There's no book you're looking for!"); } }
The implementation is as follows:
DisplayOperation display books
public class DisplayOperation implements IOperation{ public void work(BookList bookList){ System.out.println("Show books!"); int size = bookList.getUsedSize(); for (int i = 0; i < size; i++) { Book book = bookList.getPos(i); System.out.println(book); } } }
Borrow books from borrow operation
public class BorrowOperation implements IOperation{ public void work(BookList bookList){ System.out.println("Borrow books!"); System.out.println("Please enter the name of the book you want to borrow: "); String name = scanner.nextLine(); int size = bookList.getUsedSize(); int i = 0; for(;i < size;i++){ Book book = bookList.getPos(i); if(name.equals(book.getName())){ System.out.println("The information you want to borrow books is as follows: "); System.out.println(book); book.setBorrowed(true); System.out.println("Borrowing succeeded!"); System.out.println(book); return; } } System.out.println("There are no books you want to borrow!"); } }
The implementation is as follows:
ReturnOperation return books
public class ReturnOperation implements IOperation{ public void work(BookList bookList){ System.out.println("Return the books!"); System.out.println("Please enter the name of the book you want to return: "); String name = scanner.nextLine(); int i = 0; int size = bookList.getUsedSize(); for(;i < size;i++){ Book book = bookList.getPos(i); if(name.equals(book.getName())){ book.setBorrowed(false); System.out.println("Return succeeded!"); System.out.println(book); return; } } System.out.println("There are no books you want to return!"); } }
The implementation is as follows:
ExitOperation exit system
public class ExitOperation implements IOperation{ public void work(BookList bookList){ System.out.println("Exit the system!"); System.exit(0); } }
The implementation is as follows:
Create user related classes
Create package user first
Create User class, which is an abstract class. Create ordinary User class, which is a subclass of User, and create administrator User class
User parent class
public abstract class User { protected String name; protected IOperation[] iOperations; //Create interface array not instantiated public User(String name){ this.name = name; } public abstract int menu(); //Abstract menu method (it is better to use abstraction here because the parent class does not need to instantiate it) public void doWork(int choice, BookList bookList){ //Choice of what to do iOperations[choice].work(bookList); //Call the action you selected } }
AdminUser subclass
public class AdminUser extends User{ public AdminUser(String name){ super(name); this.iOperations = new IOperation[]{ //Create interface array instantiation (inherit the method of the parent class) new ExitOperation(), new FindOperation(), new AddOperation(), new DelOperation(), new DisplayOperation() }; } public int menu(){ System.out.println("================Administrator menu=================="); System.out.println("hello "+this.name+" welcome back!"); System.out.println("1.Find books:"); System.out.println("2.New books:"); System.out.println("3.Delete book:"); System.out.println("4.Show books:"); System.out.println("0.Exit the system!"); System.out.println("=========================================="); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); return choice; //Returns the operation you selected, corresponding to the array subscript } }
NormalUser subclass
public class NormalUser extends User{ public NormalUser(String name){ super(name); this.iOperations = new IOperation[]{ new ExitOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperation(), }; } public int menu(){ System.out.println("================General user menu=================="); System.out.println("hello "+this.name+" welcome back!"); System.out.println("1.Find books:"); System.out.println("2.Borrow books:"); System.out.println("3.Return books:"); System.out.println("0.Exit the system!"); System.out.println("============================================"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); return choice; } }
TestDemo test class
public class TestDemo { public static User login() { //Landing page System.out.println("Please enter your name: "); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); System.out.println("Please select your identity: 1-> administrators || 0-> Ordinary users"); int choice = scanner.nextInt(); if(choice == 1){ //Select administrator user or ordinary user return new AdminUser(name); }else{ return new NormalUser(name); } } public static void main(String[] args) { BookList bookList = new BookList(); User user = login();//Upward transformation while(true){ int choice = user.menu();//Dynamic binding - polymorphism occurred //Call the appropriate operation according to your choice user.doWork(choice,bookList); } } }
summary
Note that each class we implement may call the classes in other packages. When using, we need to import the classes in the corresponding packages to use them normally, like this
This is very important. Don't lead the wrong package.
Well, that's all for today's library management system. If you don't think it's easy to understand, it must be that you don't master the previous knowledge points very firmly. You can take a look at my next two blogs, which are a summary of basic knowledge. It will be better to see this effect after reading it.
https://blog.csdn.net/chenbaifan/article/details/121322372?spm=1001.2014.3001.5501
https://blog.csdn.net/chenbaifan/article/details/121098374?spm=1001.2014.3001.5501
Thanks for watching. Your encouragement is my biggest motivation for learning. Come on! Let's make progress together!