demand an introduction
Make use of the previous knowledge: use classes and objects, inheritance, encapsulation, polymorphism, abstract classes, interfaces and sequence tables to carry out a simple code practice, so as to master the application of these knowledge points.
Core requirements
1. Simple login
2. Management end
- Consult books
- Add books
- Delete books
- Print book list
- sign out
3. Client
- Query books
- Borrow books
- Return books
- sign out
Project analysis
First, you need to find entities: books and users
- The attributes contained in the book entity are name, author, price, type, and browed
- Attribute contained in the user entity: name
What to do after finding the entity
This project is about three related directions: books, some behavior operations on books, and users.
- Step 1: first build three packages, namely book, operation and user. The outermost layer is given to a program's main function (TestMain)
- Step 2: implement the interface provided to complete each operation
- Step 3: integrate the classes after writing them
Class design
1. Create book related classes
Step 1: design database
Create package book first
Code example:
package com.xiaoba.book; /** * Description:Design a Book class to represent a book and complete the construction */ /*1,Design a book class and complete the construction * 2,alt+insert: Provide getter and setter methods * 3,alt+insert: Provide toString method*/ public class Book { private String name; private String auther; private int price; private String type; private boolean isBorrowed;//Default not lent //Alt + insert -- > constructor provides construction methods public Book(String name, String auther, int price, String type) { this.name = name; this.auther = auther; this.price = price; this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuther() { return auther; } public void setAuther(String auther) { this.auther = auther; } 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 + '\'' + ", auther='" + auther + '\'' + ", price=" + price + ", type='" + type + '\'' + ", isBorrowed=" + isBorrowed + '}'; } }
Step 2: design the list of books. The booklist class. The booklist is actually a sequential table, which can hold a lot of books. The design of the sequential table is that there is an integer array, so the design of the booklist is that there is an array of book type, which can hold a lot of books.
package com.xiaoba.book; /** * Description:Book list: create a BookList class to save N books */ //The list of books is equivalent to a sequential table public class BookList { private Book[] books=new Book[100]; private int usedSize=0; //Call the provided constructor public BookList(){ books[0]=new Book("Journey to the West","Luo Guanzhong",99,"Famous novels");//Put a book in the subscript 0 of books books[1]=new Book("Water Margin","Shi Naian",99,"Famous novels"); books[2]=new Book("The Dream of Red Mansion","Cao Xueqin", 99,"Famous novels"); books[3]=new Book("Romance of the Three Kingdoms","Wu Chengen",99,"Famous novels"); this.usedSize=4; } //We can write all operations into the BookList class, because each operation is the operation books, but I don't want to write it in, because I want to use the interface, // It is more convenient to write an interface, so these operations are not written to this class, but to a separate package (operation) public void setBooks(int pos,Book book){ this.books[pos]=book; } public Book getBook(int pos){//Get a book and give it a pos position return this.books[pos];//Returns the book in the pos position of the current books } public int getUsedSize() { return usedSize; } public void setUsedSize(int usedSize) { this.usedSize = usedSize; } }
2. Create operation related classes
Create a package operation first
The benefits of abstracting Operation: low coupling between operations and between operations, and low coupling between operations and users
Implement the interface provided to complete each operation
Implementation of AddOperation
package com.xiaoba.operation; import com.xiaoba.book.Book; import com.xiaoba.book.BookList; import java.util.Scanner; /** * Description:Add operation, add books */ /*Because each operation requires public void work (Booklist){ } So simply provide an interface, let every operation implement this interface, and then rewrite the work method*/ public class AddOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("New books"); //By default, it is placed in the form of tail interpolation //1. Get the types of all books first Scanner scanner=new Scanner(System.in); System.out.println("Please enter the name of the book:"); String name=scanner.nextLine(); System.out.println("Please enter the author of the book:"); String author=scanner.nextLine(); System.out.println("Please enter the price of the book:"); int price=scanner.nextInt(); System.out.println("Please enter the type of book"); String type=scanner.next(); //Note: nextInt() and nextLine() cannot be used together //2. Put the book type in the corresponding bookList Book book=new Book(name,author,price,type); int curSize=bookList.getUsedSize(); bookList.setBooks(curSize,book); bookList.setUsedSize(curSize+1); System.out.println("Successfully added!"); } public static void main(String[] args) { } }
Implementation of BorrowOperation
package com.xiaoba.operation; import com.xiaoba.book.Book; import com.xiaoba.book.BookList; import java.util.Scanner; /** * Description:Borrowing operation */ public class BorrowOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("Borrow books"); Scanner scanner=new Scanner(System.in); System.out.println("Please enter the name of the book to borrow:"); String name=scanner.nextLine(); //Before borrowing a book, you need to traverse to see if the book you want to borrow exists for(int i=0;i< bookList.getUsedSize();i++){ Book book=bookList.getBook(i);//Subscript i if(book.getName().equals(name)){//After getting the book, judge whether the name of the current book is consistent with the name of the book you entered //If it is true, it means that you have this book and can borrow it book.setBorrowed(true);//The book has been lent out System.out.println("Borrowing succeeded!"); return; } } System.out.println("There is no book you want to borrow"); } }
Implementation of DelOperation
package com.xiaoba.operation; import com.xiaoba.book.Book; import com.xiaoba.book.BookList; import java.util.Scanner; /** * Description:Delete operation */ public class DelOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("Delete book"); Scanner scanner=new Scanner(System.in); System.out.println("Please enter the name of the book you want to delete:"); String name=scanner.nextLine(); //Before deleting a book, you need to traverse to see if the book to be deleted exists int i=0; for(;i< bookList.getUsedSize();i++){ Book book=bookList.getBook(i);//Subscript i if(book.getName().equals(name)){//After getting the book, judge whether the name of the current book is consistent with the name of the book you entered //If true, it indicates that there is this book, which can be deleted break; } } if (i==bookList.getUsedSize()){ System.out.println("There is no book!"); return; } //Start deletion. The deletion method is similar to the sequence table for(int pos=i;pos<bookList.getUsedSize()-1;pos++){ //[pos]=[pos+1]; Book book= bookList.getBook(pos+1);//Get the book in bookList, pos+1 first bookList.setBooks(pos,book);//Then set the pos position to book } //After deletion, you need to set the size bookList.setUsedSize(bookList.getUsedSize()-1);//Subtract one from the original basis System.out.println("Delete succeeded!"); } }
Implementation of DisplayOperation
package com.xiaoba.operation; import com.xiaoba.book.Book; import com.xiaoba.book.BookList; /** * Description:Display operation */ public class DisplayOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("Show books"); for(int i=0;i<bookList.getUsedSize();i++){ Book book=bookList.getBook(i); System.out.println(book); } } }
Implementation of ExitOperation
package com.xiaoba.operation; import com.xiaoba.book.BookList; /** * Description:Exit operation */ public class ExitOperation implements IOperation { @Override public void work(BookList bookList) { System.out.println("Exit the system"); System.exit(1);//Exit system code } }
Implementation of FindOperation
package com.xiaoba.operation; import com.xiaoba.book.Book; import com.xiaoba.book.BookList; import java.util.Scanner; /** * Description:Find operation */ public class FindOperation implements IOperation { @Override public void work(BookList bookList) { System.out.println("Find books"); Scanner scanner=new Scanner(System.in); System.out.println("Please enter the name of the book to borrow:"); String name=scanner.nextLine(); //Before borrowing a book, you need to traverse to see if the book you want to borrow exists for(int i=0;i< bookList.getUsedSize();i++){ Book book=bookList.getBook(i);//Subscript i if(book.getName().equals(name)){//After getting the book, judge whether the name of the current book is consistent with the name of the book you entered //If true, it means there is this book, you can find it System.out.println(book); System.out.println("Search succeeded!"); return; } } System.out.println("There is no book you are looking for"); } }
Implementation of IOperation
package com.xiaoba.operation; import com.xiaoba.book.BookList; /** * Description:IOperation */ /*Because each operation needs to target the bookList object, this code public void work (bookList) should be written for each operation{ } So simply provide an interface, let every operation implement this interface, and then rewrite the work method*/ public interface IOperation {//Provide an interface, which is an abstract method by default void work(BookList bookList); }
Implementation of ReturnOperation
import com.xiaoba.book.BookList; import java.util.Scanner; /** * Description:Return operation */ public class ReturnOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println("Return books"); Scanner scanner=new Scanner(System.in); System.out.println("Please enter the name of the book to return:"); String name=scanner.nextLine(); //Before returning a book, you need to traverse to see if the book to be returned exists for(int i=0;i< bookList.getUsedSize();i++){ Book book=bookList.getBook(i);//Subscript i if(book.getName().equals(name)){//After getting the book, judge whether the name of the current book is consistent with the name of the book you entered book.setBorrowed(false); System.out.println("Return succeeded!"); return; } } System.out.println("There are no books you want to return"); } }
3. Create user related classes
Create package user first
Create the User class, which is an abstract class
package com.xiaoba.user; import com.xiaoba.book.BookList; import com.xiaoba.operation.IOperation; /** * Description: User Class is an abstract class, and each subclass needs to do two things 1. Initialize the corresponding operation array 2. Implement Menu menu */ public abstract class User { protected String name; protected IOperation[] operations;//Define an operations array through protected, public User(String name){ this.name=name; } public abstract int menu();//Provide a menu method to the User public void doOperation(BookList bookList,int choice){//Provide a doOperation method. After passing it, find out what the choice is this.operations[choice].work(bookList);//It indicates the number of subscript of this array, jumps to the object corresponding to the subscript (Admin or NormalUser), and then calls the work method of the object. } }
Create a normal User class, which is a subclass of User
package com.xiaoba.user; import com.xiaoba.operation.*; import java.util.Scanner; /** * Description:Ordinary users */ public class NormalUser extends User{//NormalUser inherits from User and takes name public NormalUser(String name) { super(name);//Call the constructor of the parent class (User) with one parameter this.operations=new IOperation[]{//NormalUser ordinary User inherits User, so it has these operations new ExitOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperation() }; } //NormalUser needs to implement the menu method @Override public int menu() { System.out.println("================"); System.out.println("hello"+" "+this.name+" "+"Welcome to the library management system"); 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("================"); System.out.println("Please enter your choice:"); //Enter data after seeing the menu Scanner scanner=new Scanner(System.in); int choice=scanner.nextInt(); return choice; } }
Create administrator user class
package com.xiaoba.user; import com.xiaoba.operation.*; import java.util.Scanner; /** * Description:administrators */ public class Admin extends User {//Admin inherits the User and takes the name public Admin(String name){ super(name);//Call the constructor of the parent class (User) with one parameter this.operations=new IOperation[]{//The Admin administrator inherits the User, so it has these operations new ExitOperation(), new FindOperation(), new AddOperation(), new DelOperation(), new DisplayOperation() }; } //Admin needs to implement the menu method, so that the subclass and parent class will have a menu @Override public int menu() { System.out.println("================"); System.out.println("hello"+" "+this.name+" "+"Welcome to the library management system"); System.out.println("1.Find books"); System.out.println("2.New books"); System.out.println("3.Delete book"); System.out.println("4.Show all books"); System.out.println("0.Exit the system"); System.out.println("================"); System.out.println("Please enter your choice:"); //Enter data after seeing the menu Scanner scanner=new Scanner(System.in); int choice=scanner.nextInt(); return choice;//Returns the selected value } }
4. Main function of a program (TestMain)
package com.xiaoba; import com.xiaoba.book.BookList; import com.xiaoba.user.Admin; import com.xiaoba.user.NormalUser; import com.xiaoba.user.User; import java.util.Scanner; /** * Description:Library management system, object-oriented programming, building framework, packaging, calling */ public class TestMain { public static User login(){//Set a login, and the return value is User Scanner scanner=new Scanner(System.in); System.out.println("Please enter your name:"); String name=scanner.nextLine(); System.out.println("Please enter your identity:(1 On behalf of the administrator, 2 on behalf of ordinary users)");//Return different objects according to different numbers, //Why return different objects? Because the objects are different, the printed menus are also different int chioce=scanner.nextInt();//Define a chioce to judge whether it is 1 or 2 if(chioce==1){//If you are an administrator, return Admin() to the administrator object return new Admin(name);//The return value of login is User, which is received through User, and an upward transition has taken place }else {//If it is an ordinary user. Return to NormalUser() return new NormalUser(name);//If you want to write the name in, you need to provide a constructor to NormalUser, and so does Admin } } public static void main(String[] args) { //1. Prepare books BookList bookList = new BookList();//Call the BookList constructor without parameters to get the books in //2. Login User user = login();//User may point to an administrator or an ordinary user, and the menu will be called by the object to which it refers while (true) { int choice = user.menu();//Call the menu method through the user, and call the corresponding method through the selected number printed from your menu //After selection, pass it to doOperation, and then jump to the User class user.doOperation(bookList, choice);//Call doOperation. The first parameter is bookList and the second parameter is choice } } }
Supplement: interface and interface are extensions, and class and interface are implementations.