In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to write the basic function code of Java book management". In the daily operation, I believe that many people have doubts about how to write the basic function code of Java book management. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to write the basic function code of Java book management". Next, please follow the editor to study!
First of all, let's take a look at the effect of running.
Let's analyze it:
In Java, things are solved through the interaction between objects, so let's see what objects there are.
The first two obvious objects are the user and the book, so create two packages book and user
As you can see from the figure above: there are the same operations and different operations between different users, so you might as well put all the operations in one package and call directly what you need. I named this package operate.
1.book package
First of all, there must be a Book class in the package. All kinds of information recorded in this class, including book title, author, book type, price and loan status, I use private modification for member variables in all classes in all packages. Only external methods are provided. 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 you have books, you need bookshelves to store them, so define another class in the book package as BookShelf to store books and record the number of books. You can use Book class arrays to store books.
Public class BookShelf {private Book [] books=new Book [1000]; number of private int count; / / record books}
Because the array cannot be accessed directly, a method to obtain the array subscript is added to the class, and the method to access the array usually operates on the book, so the setBook method is also modified.
Package book;public class BookShelf {private Book [] books=new Book [1000]; number of private int count; / / record books 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 ("Scream", "Lu Xun", "Collection of Fictions", 30); / / there is a book at the beginning by default, whether to write a book corresponding to your this.count=1;} / / get the subscript public Book pos (int I) {return books [I];}}
This is the end of the preliminary code for the book package. Move on to the next package.
2.user package
There will certainly be two classes in the package, ordinary user (NormalUser) and administrator (AdminUser), but the focus is on how to implement different operations for different users in the diagram
Let's first write down the login method, and then analyze:
Public static void login () {System.out.println ("enter name:"); Scanner scanner=new Scanner (System.in); String name=scanner.nextLine (); System.out.println ("enter your identity: 1. Administrator 2. Ordinary user "); int i=scanner.nextInt (); if (iTunes 1) {/ / Administrator} else {/ / ordinary user}}
Method to determine which user is based on different numbers, so the return value should return a class, but there are two classes. To solve this problem, we can make both NormalUser and AdminUser 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 (); / / menu abstract method, subclass implementation}
Subclass code:
Public class AdminUser extends User {public AdminUser (String name) {super (name);} @ Override public void menu () {System.out.println (Welcome); System.out.println ("1. Find books "); System.out.println (" 2. Add books "); System.out.println (" 3. " Delete the book "); System.out.println (" 4. Show Book "); System.out.println (" 0. System.out.println ("Select Action");}} public class NormalUser extends User {public NormalUser (String name) {super (name);} @ Override public void menu () {System.out.println ("Welcome"); System.out.println ("1. Find books "); System.out.println (" 2. Borrow books "); System.out.println (" 3. Return the book "); System.out.println (" 0. Exit the system "); System.out.println (" Select Action ");}}
Different users correspond to different operations, but have the same operands, so the two classes need to have an array to store their own methods to prevent errors, and this array should be prepared before the user chooses it. So the array should be in the constructor, and the modified class is as follows:
Public abstract class User {private String name; public IOperate [] ioperate; public void chooseOperate (int choice,BookShelf bookShelf) {ioperate.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 (Welcome); System.out.println ("1. Find books "); System.out.println (" 2. Add books "); System.out.println (" 3. " Delete the book "); System.out.println (" 4. Show Book "); System.out.println (" 0. Exit the system "); System.out.println (" Select Action "); 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 (Welcome); System.out.println ("1. Find books "); System.out.println (" 2. Borrow books "); System.out.println (" 3. Return the book "); System.out.println (" 0. Exit the system "); System.out.println (" Select Action "); Scanner scanner=new Scanner (System.in); int choice=scanner.nextInt (); return choice;}} 3.operate package
Operations such as adding, deleting, changing and querying books are actually operated on the Book array, so the parameters of these methods are all BookShelf classes, so 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 operation of books, take the addition of books as an example.
We need to know the information about the book, and then we need a Book class to receive it, and finally put it in the array.
According to this logic, the previous Book class needs to be modified: add a constructor to the Book class to receive the information from the book, and note that the position of the method in the array corresponds 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 method code of the book:
Public class AddBook implements IOperate {@ Override public void work (BookShelf bookShelf) {System.out.println ("Please enter the name of the book:"); Scanner scanner = new Scanner (System.in); String name = scanner.nextLine (); System.out.println ("Please enter the author of the book:"); String author = scanner.nextLine (); System.out.println ("enter the type of the book:") String type = scanner.nextLine (); System.out.println ("Please enter the book price:"); 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 ("New success");}}
Note:
1. Since the reference variable is the output modified address by default, you need to override the toString method in the class Book to print the contents of the book.
two。 Before exiting, the exit method needs to traverse the array and change the objects it points to to null to facilitate recycling. Although the system will also recycle after the end of the program, you should know that there are some programs in large-scale projects that are not easy to stop once they start running.
At this point, the study on "how to write the code for the basic functions of Java book management" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.