Java Library Management System Code
Java Library Management System Code
The use of private fields in the Book class illustrates the principle of encapsulation, which restricts direct access to the book's data by external classes. This is important as it ensures that the class controls how its data is accessed and modified, protecting the integrity of the object and allowing for controlled modifications only through defined methods like `addQuantity`, `borrowBook`, and `returnBook` .
The Library System manages errors from invalid menu choices by using a do-while loop along with a switch-case statement. When a user inputs an invalid choice, the default case prompts the message 'Invalid choice! Please select again.' The invalid choice doesn't affect the program flow, allowing the user to re-enter their choice .
When there are not enough copies of a book to borrow, the `borrowBook` method in the Book class returns false, which triggers the system to display a message stating 'Not enough copies available!' This informs the user of the issue and prevents them from borrowing more books than are available .
If a user attempts to borrow a book that does not exist in the library, the program displays the message 'Book not found in library!' The program determines this condition by checking if the `library` HashMap does not contain the key corresponding to the requested book title .
The program integrates user feedback through clear and immediate system messages in response to user actions, such as confirming successful operations ('Book added successfully', 'Successfully returned') or indicating errors ('Invalid input! Please enter a number.'). This feedback ensures users know the state of their transactions, enhancing user experience by making the system intuitive, interactive, and easy to navigate without misunderstandings or errors .
The principle of method-based operations is applied through the use of methods in the Book class that control changes to a book's quantity. These include `addQuantity` for adding books, `borrowBook` for borrowing books, which checks availability first, and `returnBook` for returning books. These methods encapsulate the logic for modifying book quantities, ensuring changes are done correctly and consistently .
The Library System program uses a while loop to check if the user input is an integer when a numeric input is expected. If the input is invalid, the program outputs a message indicating 'Invalid input! Please enter a number.' This validation is applied when users need to enter choices or quantities for book operations .
When a book is added to the system, the program first checks if the book already exists in the `library` HashMap. If it does, the `addQuantity` method of the Book class is called to increase its quantity. When a book is returned, the `returnBook` method updates the book's quantity by adding the returned number of copies to the existing quantity. This way, the correct quantity of books is maintained in the system .
Encapsulation in the Book class contributes to system robustness by ensuring that all operations on book data are conducted through controlled methods. This ensures that the data remains consistent and valid throughout the program's execution. By restricting direct access to the book attributes and requiring changes through methods like `addQuantity` and `borrowBook`, it minimizes the risk of accidental data corruption or inconsistent states .
In the Library System program, the HashMap is used to store the collection of books, with the book title as the unique key. HashMap is chosen because it provides efficient storage and access operations, allowing users to quickly find, add, or update books using book titles as keys, which suits the needs of a library management system where quick access to books by title is essential .