First let’s take a look at the running effect
Let’s analyze it:
In Java, through Interaction between objects solves the problem, so let’s take a look at what objects there are
The first two obvious objects are: user and book, so create two packages book and user
pass As you can see in the picture above: different users have the same operations, but also different operations, so you might as well put all the operations in a package, and just call the required operations directly. I named this package operate
First of all, there must be a Book class in the package, which records various information, including book title, author, book type, price and borrowing status, all I use private modifications for member variables in all classes in the package, and only provide external methods, which will not be repeated later. The code is as follows (only part of the code is shown):
package book; public class Book { private String name; private String author; private String type; private int price; private boolean borrow; 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; } }
Since there is a book, then it is necessary. Bookshelf is used to store books, so define a class called BookShelf in the book package to store books and record the number of books. Book storage can be implemented using the Book class array
public class BookShelf { private Book[] books=new Book[1000]; private int count; //记录书的数量 }
Since the array cannot be accessed directly, so in the class Add a method to get the array subscript, and the method of accessing the array generally operates on the book, so the setBook method is also modified
package book; public class BookShelf { private Book[] books=new Book[1000]; private int count; //记录书的数量 public void setBooks(int pos,Book book) { books[pos] = book; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public BookShelf() { books[0]=new Book("呐喊","鲁迅","小说集",30); //默认初始有一本书,写不写随你 this.count=1; } //获取下标对应的书 public Book pos(int i) { return books[i]; } }
The preliminary code of the book package ends here, continue to the next package
There will definitely be two classes: normal user (NormalUser) and administrator (AdminUser) in the package, but the focus is how to implement different operations for different users in the picture
Then let’s write out the login method first, and then analyze:
public static void login() { System.out.println("输入姓名:"); Scanner scanner=new Scanner(System.in); String name=scanner.nextLine(); System.out.println("输入你身份:1.管理员 2.普通用户"); int i=scanner.nextInt(); if(i==1) { //管理员 } else { //普通用户 } }
The method determines what kind of user it is based on different numbers, so the return value should be to return a class, but there are two classes here , to solve this problem, we can let NormalUser and AdminUser both inherit the same parent class, and the method only needs to return the parent class.
The parent class code is as follows:
public abstract class User { private String name; public User(String name) { this.name = name; } public abstract void menu(); //菜单抽象方法,子类实现 }
Subclass code:
public class AdminUser extends User{ public AdminUser(String name) { super(name); } @Override public void menu() { System.out.println("欢迎使用"); System.out.println("1.查找图书"); System.out.println("2.新增图书"); System.out.println("3.删除图书"); System.out.println("4.显示图书"); System.out.println("0.退出系统"); System.out.println("选择操作"); } } public class NormalUser extends User{ public NormalUser(String name) { super(name); } @Override public void menu() { System.out.println("欢迎使用"); System.out.println("1.查找图书"); System.out.println("2.借阅图书"); System.out.println("3.归还图书"); System.out.println("0.退出系统"); System.out.println("选择操作"); } }
Different users correspond to different operations, but have the same operands, so the two classes need an array to store their respective methods to prevent errors, and this array must be prepared before the user selects it. Therefore, the array should be in the construction method. The modified class is as follows:
public abstract class User { private String name; public IOperate[] ioperate; public void chooseOperate(int choice,BookShelf bookShelf) { ioperate[choice].work(bookShelf); } public User(String name) { this.name = name; } public abstract int menu(); } public class AdminUser extends User{ public AdminUser(String name) { super(name); this.ioperate=new IOperate[] { new Exit(), new FindBook(), new AddBook(), new DelBook(), new DisplayBook(), }; } @Override public int menu() { System.out.println("欢迎使用"); System.out.println("1.查找图书"); System.out.println("2.新增图书"); System.out.println("3.删除图书"); System.out.println("4.显示图书"); System.out.println("0.退出系统"); System.out.println("选择操作"); Scanner scanner=new Scanner(System.in); int choice=scanner.nextInt(); return choice; } } public class NormalUser extends User{ public NormalUser(String name) { super(name); this.ioperate=new IOperate[] { new Exit(), new FindBook(), new BorrowBook(), new ReturnBook(), }; } @Override public int menu() { System.out.println("欢迎使用"); System.out.println("1.查找图书"); System.out.println("2.借阅图书"); System.out.println("3.归还图书"); System.out.println("0.退出系统"); System.out.println("选择操作"); Scanner scanner=new Scanner(System.in); int choice=scanner.nextInt(); return choice; } }
Operations such as adding, deleting, modifying, and checking books are actually operating on the Book array. , so the parameters of these methods are all BookShelf classes, then we can define an interface, define this method in the interface, and then other classes can implement this interface
Interface code:
package operate; import book.BookShelf; public interface IOperate { public void work (BookShelf bookShelf); }
As for the Book operations, here is an example of adding books
We need to know the book information, and then have a Book class to receive it, and finally put it into the array
According to this logic, the previous Book The class needs to be modified: add a constructor method to the Book class to receive the book information. Note that the position of the method in the array should correspond to the position in the menu you wrote
public Book(String name, String author, String type, int price) { this.name = name; this.author = author; this.type = type; this.price = price; }
Add the book method code:
public class AddBook implements IOperate { @Override public void work(BookShelf bookShelf) { System.out.println("请输入图书的名字:"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); System.out.println("请输入图书的作者:"); String author = scanner.nextLine(); System.out.println("请输入图书的类型:"); String type = scanner.nextLine(); System.out.println("请输入图书的价格:"); int price = scanner.nextInt(); Book book = new Book(name,author,type,price); int count=bookShelf.getCount(); bookShelf.setBooks(count,book); bookShelf.setCount(count+1); System.out.println("新增成功"); } }
Note:
1. Since the reference variable outputs the modified address by default, if you want to print the content of the book, you need to override the toString method in the Book class
2 .The exit method needs to traverse the array before exiting and change all the objects it points to to null to facilitate recycling. Although the system will also recycle after the program ends, you must know that it is not so easy to stop some programs in large projects once they start running. of
The above is the detailed content of How to write basic function code for Java book management. For more information, please follow other related articles on the PHP Chinese website!