[JavaSE] Library management system (comprehensive grammar exercises, with thinking process)

》》》My blog homepage
》》》My gitee link
Follow me and make progress together on the road to learning Java! ! !

Everything



Preface

Goal: Consolidate the foundation of JavaSE syntax by implementing a library management system , which does not involve the implementation of interaction between database and web pages. IDE: IntelliJ IDEA Community Edition 2022.3.3

Thoughts on code implementation and implementation steps

No matter what you want to achieve, object-oriented programming has to go through three steps:

  1. find object
  2. Create object
  3. user target audience

Next, these three steps will be analyzed one by one to show the thinking process from 0 to complete realization.

1. Find the object

Analyze requirements: Since we want to implement a library management system, what objects do we need to consider?

  • Book: The book object must have information about the title, author, price, partition, and whether it is borrowed.
  • Bookshelf: a place to store books
  • Ordinary users: users who borrow and return books
  • Admin user: User who manages books
  • Operations for ordinary users (search for books, borrow books, return books, exit the library management system)
  • Operations of administrator users (display books, search for books, add books, delete books, exit the book management system)

2. Create objects

We have analyzed roughly what objects need to be implemented in the library management system.
Next, when creating objects, we need to first define the class< a i=3> and then instantiate the object.

In order to better manage and use classes, we first customize the package:

  • The classes of books and bookshelf are put into the book package
  • Both ordinary users and administrator users are put into the user package
  • The operations of ordinary users and operations of administrator users are put into the operation package.

Everything

Then define the classes in each package:

Book class in book package

needs to define the member variables of the book title, author, price, partition, and information about whether to lend it out, and it needs to be modified with private to increase the encapsulation of the data.
Then quickly generate constructors, getter and setter methods and overridden toString() methods.
Steps to quickly generate:
Right-click the mouse--》Select Generate --》Select Constructor (the same applies to getter and setter and toString methods) --》< /span> All getter and setter methods can be generated directly. Because isBookBorrowed in the Book class defaults to false, which is the state of not being lent out, so there is no need to add the isBookBorrowed parameter when creating the constructor.
Then select according to your needs (hold down the Shift key to select multiple), and click OK.
Everything
Everything

Everything

We need to override the toString() method because when we want to view information about a book, we can directly use System.out.println (the name of the book object to print out the information); that's it. If we don't override the toString() method, it will only print Get a reference to the object.
Everything

I hope that in the end I can display the information that has been lent or not lent, instead of displaying the information of true or false, I can slightly modify the toString() method:
Everything

The class definition of the book is completed, and the next step is

Bookshelf class in book package

We can use an array to simulate a bookshelf. Each element in this array is a Book object.
So we can define a member variable in the bookshelf class Book[] type object array books, books from objects The array is stored sequentially starting from index 0.
We may also need to know how many books have been stored in this object array, so define another member variable int type usedSize
< a i=6>. You also need to know the capacity of this bookshelf. We can set the default capacity of this bookshelf in advance, and this capacity cannot be changed when compiling and running.
Everything

Then we can initialize the member variables of the bookshelf class through the construction method:
Everything
Next, we may need to find a certain book on the bookshelf or put a book in it. The getBook method and the setBook method can be implemented:
Everything
If we add new books, the number of books on the bookshelf will increase, so we need to provide the getUsedSize and setUsedSize methods (which can be quickly generated):
Everything
Because we use the constructor method to initialize the object array, there is no need to set up the entire bookshelf, so we may only need to use the getBooks method:
Everything

At this point we have written all the methods that may be used in the bookshelf class.
So far, the Book class and BookList class in the book package have basically been defined, and the next step is to define them

All operation classes in the operation package

We first list all the previously analyzed operations among ordinary users and administrator users:
The operations of ordinary users: search for books, borrow books, return books, and exit the book management system.
Operations of administrator users: display books, search for books, add books, delete books, and exit the book management system.

So we have to implement 7 operations in total:

  1. Show books
  2. Find books
  3. Add book
  4. Delete book
  5. Borrow books
  6. Return the book
  7. Exit the library management system

Observing the common behaviors of these 7 operations, we can find that:
Except for exiting the management system, each operation needs to be related to Bookshelf< /span> This object interacts with:

  • Displaying books requires obtaining information about all books through the bookshelf object,
  • To add a book, you need to access a certain location of the bookshelf object to add the book,
  • Borrowing books requires changing the information of a certain book in the bookshelf object to "already checked out"...

Therefore, we can define an interface through the common behavior of these operations to abstract the operations:
Everything
Then let each different operation class implement this interface, the specific implementation process Don’t write it yet:
Everything
The definitions of other operation classes are the same.
Everything
Let’s not talk about the specific implementation of each user operation. We have already defined all the classes in the operation package.
The next step is to define it in the user package
Administrator user classes and ordinary user classes

Normal users and administrator users are both users. You can first define a user's abstract class.
Ordinary users and administrator users have different corresponding operations. How to distinguish which operations are performed by ordinary users and which operations are performed by administrator users?

Just like each element in the Book[] type object array can be a different book object, we can also define an object array so that each element in the array stores a different operation object, and then just pass the following By accessing the target, the operation the user wants to perform can be achieved.

Therefore, we can define an object array of type IOperation[ ] in the user abstract class (because each specific operation class implements the IOperation interface, so the types do not conflict). When the administrator user class and the ordinary user class inherit the user abstract class , the subclass user can initialize the object array and assign the subclass user's operations one by one to a certain subscript position of the object array.
Everything
Everything

At this point I noticed a problem:

  • Even if the operation of exiting the management system does not interact with the bookshelf object, we still need to implement the IOperation interface in ExitOperation and override the work method in the IOperation interface.
    Everything

Administrator users and ordinary users require different menu options respectively, so the user abstract class needs to define an abstract menu method, so that subclass users can rewrite different menu methods according to different user situations.
Everything
Whether it is an ordinary user or an administrator user, the user selects the function he wants to operate according to the menu. We need to call the work method in the operation object in the iOperations array of the user object through a specific subscript. .
Because this is a method that can be shared by ordinary users and administrator users, we write it in the User class.
Everything

When the user has finished entering their name and confirmed their identity (ordinary user/administrator user), we need to instantiate an (ordinary user/administrator user) object and can pass in the name parameter, then the User class needs to have a The constructor with the name parameter initializes the name of the object.

Everything
At this point, the definition of the parent class is completed, and it is time to define the administrator user class and ordinary user class.

When constructing a subclass, you need to help the parent class construct it first, and then you can initialize the subclass object, that is, initialize the iOperation object array. In addition, you must implement the menu method:
Everything

The top is for administrator users, and the bottom is for ordinary users.
Everything

At this point, the general framework has been formed. Some classes have not yet been implemented concretely, and some classes still need fine-tuning, which requires continuous adjustment during the use of objects.
Everything

3. Use objects

Now create a separate Main class and write a main method in it so that the entire library management system process starts with user login. User login is implemented by writing a separate login() method in the Main class.
In the login() method, the user is first asked to enter his or her name, and then the user is asked to choose his or her identity. If the user is an administrator, an administrator user object is returned. If the user is an ordinary user, an administrator user object is returned. Returns a normal user object.
Everything

Then the menu of the user class can be called to let the user input which operation to choose. Because we have not yet implemented allowing users to input which operation to choose, we can modify the menu methods of each user class.
Everything
Everything

The return type of the subclass has been modified, and the return type of the menu abstract method in the parent class must also be modified.
Everything

Then we can execute the corresponding operation program based on the user selection returned from the menu method:
Everything
We found that the bookshelf object is required in the doOperation method we previously defined in the User class, then We initialize a bookshelf object directly before the user logs in.
Everything

At this point we can run the program to see the effect:
Everything

If the user wants to operate multiple times, just add a loop.
Everything
Now you only need to complete the functions of each operation to basically implement the library management system. For the specific implementation of the operation method, I only use drawings and text to explain the ideas. You can implement it according to your desired business needs. This is just a reference idea.

  • Borrowing books: Let the user enter the title of the book to be borrowed, then traverse the bookList object array, compare the title of each book with the title entered by the user, if the titles are the same, change the isBookBorrowed of the book to true, that is Can.
  • Returning a book: Similar to borrowing a book, the user is asked to enter the title of the book to be returned, then iterates through the bookList object array, and compares the title of each book with the title entered by the user. If the title is the same, the title of the book is returned. Just change isBookBorrowed to false.
  • Exit the management system: This goes directly to the code:
    Everything
  • Find a book: Let the user enter a book title, then traverse the bookList object array, compare the title of each book with the title entered by the user, if the titles are the same, print out all the information about the book, if not, output could not find it.
  • Display books: directly traverse the bookList object array and print out the information of each book separately.
  • Add new books: There are many things to consider when adding new books. First, let the user enter all the information about the book (title, author, price, genre), and then traverse the bookList object array. It needs to determine whether there is this book on the bookshelf, and then consider whether the bookshelf is full of books or not. Add books under the subscript [number of current books]. Finally, don’t forget:After adding books, add 1 to the number of books.
  • Delete books: Let the user enter the book to be deleted, and then traverse the bookList object array to find whether the book exists. If so, you can delete the book as shown in the following steps:
    Everything
    If you want to delete the book Journey to the West, and the book Journey to the West is traversed through the bookList object array
    Suppose it is found that the reference of subscript 1 points to the book object Journey to the West:
    Everything
    If you want to delete Journey to the West, just overwrite the object references from back to front
    Everything
    Everything

Finally, the position of the last book should be cleared Blank and the number of books should be reduced by 1.
Everything
All reference codes can be found in the full code below.

Complete code

Click here to view the complete code

If you think what I said is wrong or what I did poorly, you can point it out in the comment area. I will humbly accept it and improve the quality of the article. Thank you!


Guess you like

Origin blog.csdn.net/weixin_73276255/article/details/132776283