preface
Core requirements of the project:
Implementing simple login
The administrator provides the following functions
① Find books ② Add books ③ Delete books ④ Show all books ⑤ Exit the system
Through the user to achieve the following functions
① Check books ② Borrow books ③ Return books ④ Exit the system
Project class design presentation
Book related classes
Book: Information that defines a Book
package book; /** * Created with IntelliJ IDEA * Details about unstoppable_t: * User: Administrator * Date: 2021 -01-19 * Time: 16:54 */ public class Book {private String name; private String author; private int price; private String type; private boolean isBorrowed = false; Public Book(String name, String author, int price, String type) {this.name = name; this.author = author; this.price = price; this.type = type; // this.isBorrowed = isBorrowed; } public String getName() { return name; } public String getAuthor() { return author; } public int getPrice() { return price; } public String getType() { return type; } public boolean isBorrowed() { return isBorrowed; } public void setName(String name) { this.name = name; } public void setAuthor(String author) { this.author = author; } public void setPrice(int price) { this.price = price; } public void setType(String type) { this.type = type; } public void setBorrowed(boolean borrowed) { isBorrowed = borrowed; } // @Override // public String toString() { // return "Book{" + // "name='" + name + '\'' + // ", author='" + author + '\'' + // ", price=" + price + // ", type='" + type + '\'' + // ", isBorrowed=" + isBorrowed + // '}'; // } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", type='" + type + '\'' + ((isBorrowed == true) ? + '}'; }}Copy the code
package book; /** * Created with IntelliJ IDEA * Details about unstoppable_t: * User: Administrator * Date: 2021 -01-19 * Time: 16:54 */ public class BookList { public int usedSize = 3; Public Book[] books = new Book[10]; Public BookList() {books[0] = new Book(" romance of The Three Kingdoms "," Ruoguizhong ", 100, "novel "); Books [1] = New Book(" Water Margin ", "Shi Naian ", 100," novel "); Books [2] = New Book(" Journey to the West ", "Wu Cheng 'en ", 100," Fiction "); Public void setBooks(int pos,Book Book) {this.books[pos] = Book; } public Book getBooks(int pos){return this.books[pos]; } public int getUsedSize() { return usedSize; } public void setUsedSize(int usedSize) { this.usedSize = usedSize; }}Copy the code
Classes that operate on libraries (sequence tables)
Public implements IOperation{@override public void work(BookList BookList) { System.out.println(" Add book "); Scanner sc = new Scanner(System.in); System.out.println(" Please enter a title "); String name = sc.nextLine(); System.out.println(" Please enter the author of the book "); String author = sc.nextLine(); System.out.println(" Please enter the price of the book "); int price = sc.nextInt(); System.out.println(" Please enter the type of book "); String type = sc.next(); Book newBook = new Book(name,author,price,type); Int size = booklist.getusedSize (); Booklist.setbooks (size,newBook); Booklist.setusedsize (size+1); // After placing the new books in the order table, Public class BorrowOperation implements IOperation{@override public void work(BookList BookList) { System.out.println(" borrow books "); Scanner sc = new Scanner(System.in); System.out.println(" Please enter a title "); String name = sc.nextLine(); For (int I = 0; i < booklist.getUsedSize(); I ++) {booklist = booklist.getBooks(I); If (book.getName().equals(name)){// If (book-.getName ().equals(name)){// If (book-.getName ().equals(name)){// If (book-.getName (). // True indicates that the book has already produced a return; }} system.out.println (" sorry, we don't have any books you want to borrow!" ); Public implements IOperation{@override public void work(BookList BookList) { System.out.println(" Delete book "); Scanner sc = new Scanner(System.in); System.out.println(" Please enter a title "); String name = sc.nextLine(); Int I = 0; for (; i < booklist.getUsedSize(); i++) { Book book = booklist.getBooks(i); if(book.getName().equals(name)){ break; }} if(I >= booklist.getusedSize ()){system.out.println (" There is no book to delete! ); return ; } for (int j = I; j < booklist.getUsedSize()-1; j++) { Book book = booklist.getBooks(j+1); Booklist.setbooks (j,book); } int size = booklist.getusedSize (); Booklist.setusedsize (size-1); // After deleting the book, the length is subtracted by 1 system.out.println (" The book has been deleted!" ); Public implements IOperation{@override public void work(BookList BookList) { System.out.println(" display books "); for (int i = 0; i < booklist.getUsedSize(); i++) { Book book = booklist.getBooks(i); System.out.println(book); Public class implements IOperation{@override public void work(BookList BookList) { System.out.println(" Find book "); Scanner sc = new Scanner(System.in); System.out.println(" Please enter a title "); String name = sc.nextLine(); For (int I = 0; i < booklist.getUsedSize(); I ++) {booklist = booklist.getBooks(I); If (book.getName().equals(name)){system.out.println (" The book exists! ); System.out.println(book); // Print the book information directly, toString method has been overridden return; }} system.out.println (" There is no such book! ") ); Public class ReturnOperation implements IOperation{@override public void work(BookList BookList) { System.out.println(" return book "); Scanner sc = new Scanner(System.in); System.out.println(" Please enter a title "); String name = sc.nextLine(); For (int I = 0; i < booklist.getUsedSize(); i++) { Book book = booklist.getBooks(i); if(book.getName().equals(name)){ book.setBorrowed(false); System.out.println(book); // Print the book information directly, toString method has been overridden return; }} system.out.println (" There is no book you want to return!" ); Public class implements IOperation{@override public void work(BookList BookList) { System.out.println(" exit System "); System.exit(1); Public interface IOperation {void work(BookList BookList); }Copy the code
User dependent classes
User classes
package user; import book.BookList; import operation.IOperation; /** * Created with IntelliJ IDEA * Details about unstoppable_t: * User: Administrator * Date: 2021 -01-19 * Time: 16:55 */ abstract public class User { public String name; public IOperation[] operations; public User(String name){ this.name = name; } /* Since SpecialPerson and OrdinaryPerson inherit from User, let the two subclasses override the menu method. The two subclasses display different menus. Menu can't be implemented, so let's make it abstract. So the User class becomes abstract */ abstract public int menu(); // What operations are stored in operations, Public void doOperation(int choice, BookList BookList){this.operations[choice]. Work (BookList); }}Copy the code
The administrator
package user; import operation.*; import java.util.Scanner; /** * Created with IntelliJ IDEA * Details about unstoppable_t: * User: Administrator * Date: 2021 -01-19 * Time: Public class SpecialPerson extends User{// Constructor public SpecialPerson(String name){super(name); This.operations = new IOperation[]{new ExitOperation(), //0 exit system new FindOperation(), new AddOperation(), new DelOperation(), new DisplayOperation() }; } /* Override public int menu() {system.out.println (" admin menu! ") {Override public int menu() {system.out.println (" admin menu! "); ); System.out.println("================================="); System.out.println("hello "+ this.name +" welcome to use our library management System!" ); System.out.println("1. System.out.println("2. System.out.println("3. Delete book "); System.out.println("4. Display all books "); System.out.println("0. System.out.println("================================"); System.out.println(" Please select the corresponding operation as prompted: "); Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); return choice; }}Copy the code
The average user
package user; import operation.*; import java.util.Scanner; /** * Created with IntelliJ IDEA * Details about unstoppable_t: * User: Administrator * Date: 2021 -01-19 * Time: Public class OrdinaryPerson extends User{// Constructor public OrdinaryPerson(String name){super(name); This.operations = new IOperation[]{new ExitOperation(), //0 new FindOperation(), new BorrowOperation(), new ReturnOperation() }; } // Override public int menu() {system.out.println (" ordinary User menu!" ); System.out.println("================================="); System.out.println("hello" + this.name + "welcome to use our library management System!" ); System.out.println("1. System.out.println("2. System.out.println("3. System.out.println("0. System.out.println("================================"); System.out.println(" Please select the corresponding operation as prompted: "); Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); return choice; }}Copy the code
Main function program
import book.BookList; import user.OrdinaryPerson; import user.SpecialPerson; import user.User; import java.util.Scanner; /** * Created with IntelliJ IDEA * Details about unstoppable_t: * User: Administrator * Date: 2021 -01-19 * Time: 16:30 */ public class TrqTest {// the login method returns the user instance, The type is User public static User login(){Scanner sc = new Scanner(system.in); System.out.println(" Please enter your name: "); String name = sc.nextLine(); System.out.println(" please input your identity > 1: administrator 0: common user "); int number = sc.nextInt(); if(number == 1){ return new SpecialPerson(name); }else{ return new OrdinaryPerson(name); } } public static void main(String[] args) { BookList bookList = new BookList(); // login User User = login(); While (true) {//user.menu() which menu method to call, depending on the login identity int choice = user.menu(); // Choice selects the corresponding class in the operations array of the current user. The object generated by this class calls its own work method user.doOperation(choice, bookList); }}}Copy the code
Let’s run it and see what happens
Thank you for reading here, the article has any shortcomings please correct, feel the article is helpful to you remember to give me a thumbs up, every day will share Java related technical articles or industry information, welcome to pay attention to and forward the article! Welcome to pay attention to the public number: the future has light, receive a line of large factory Java interview questions summary + the knowledge points learning thinking guide + a 300 page PDF document Java core knowledge points summary!