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
1.book package
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
2.user 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; } }
3.operate package
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!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment