0% found this document useful (0 votes)
8 views8 pages

Simple Library Management Program

Uploaded by

musembirose897
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Simple Library Management Program

Uploaded by

musembirose897
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Program Explanation

This project implements a simple library management program allowing a user to


complete the following tasks:
Add Books - Users can add books or update the number of existing books.
Checkout Books - Users check out a book if there are enough books available.
Return Books - Users return books if the book is in the library system.
Quit - Users can safely quit.
The program uses a HashMap to store books, where the book title is the unique key of
the book. The Book class has the title, author, and available quantity of the book. The system
validates user input, controls for valid data, and provides a success or an error message as
needed.
The assignment illustrates two important principles of object-oriented programming -
encapsulation, and operations that are method based. (Eck, 2022).
Program Code
import [Link];
import [Link];
import [Link];

class Book {
private String title;
private String author;
private int quantity;

public Book(String title, String author, int quantity) {


[Link] = title;
[Link] = author;
[Link] = quantity;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}

public int getQuantity() {


return quantity;
}
public void addQuantity(int amount) {
[Link] += amount;
}
public boolean borrowBook(int amount) {
if (amount <= quantity) {
quantity -= amount;
return true;
}
return false;
}

public void returnBook(int amount) {


[Link] += amount;
}
}

public class LibrarySystem {


private static Map<String, Book> library = new HashMap<>();
private static Scanner scanner = new Scanner([Link]);

public static void main(String[] args) {


int choice;
do {
[Link]("\n===== Library System =====");
[Link]("1. Add Books");
[Link]("2. Borrow Books");
[Link]("3. Return Books");
[Link]("4. Exit");
[Link]("Enter your choice: ");

while (![Link]()) {
[Link]("Invalid input! Please enter a number.");
[Link]();
}
choice = [Link]();
[Link]();

switch (choice) {
case 1:
addBook();
break;
case 2:
borrowBook();
break;
case 3:
returnBook();
break;
case 4:
[Link]("Exiting... Goodbye!");
break;
default:
[Link]("Invalid choice! Please select again.");
}
} while (choice != 4);
}

private static void addBook() {


[Link]("Enter book title: ");
String title = [Link]().trim();
[Link]("Enter book author: ");
String author = [Link]().trim();
[Link]("Enter quantity: ");

while (![Link]()) {
[Link]("Invalid input! Please enter a number.");
[Link]();
}
int quantity = [Link]();
[Link]();

if ([Link](title)) {
[Link](title).addQuantity(quantity);
[Link]("Book quantity updated successfully.");
} else {
[Link](title, new Book(title, author, quantity));
[Link]("Book added successfully.");
}
}

private static void borrowBook() {


[Link]("Enter book title to borrow: ");
String title = [Link]().trim();
if (![Link](title)) {
[Link]("Book not found in library!");
return;
}

[Link]("Enter number of copies to borrow: ");


while (![Link]()) {
[Link]("Invalid input! Please enter a number.");
[Link]();
}
int amount = [Link]();
[Link]();

Book book = [Link](title);


if ([Link](amount)) {
[Link]("Successfully borrowed " + amount + " copy/copies of " + title +
".");
} else {
[Link]("Not enough copies available!");
}
}
private static void returnBook() {
[Link]("Enter book title to return: ");
String title = [Link]().trim();
if (![Link](title)) {
[Link]("This book does not belong to the library system!");
return;
}
[Link]("Enter number of copies to return: ");
while (![Link]()) {
[Link]("Invalid input! Please enter a number.");
[Link]();
}
int amount = [Link]();
[Link]();
Book book = [Link](title);
[Link](amount);
[Link]("Successfully returned " + amount + " copy/copies of " + title + ".");
}
}
Sample Output

References
Eck, D. J. (2022). Introduction to programming using Java, version 9, JavaFX edition. Hobart
and William Smith Colleges. Licensed under CC BY 4.0. [Link]
Liang, Y. D. (2020). Introduction to Java programming and data structures, comprehensive
version (12th ed.). Pearson.

Common questions

Powered by AI

The library management program uses encapsulation by defining a `Book` class with private fields for the book's title, author, and quantity. These fields are not directly accessible from outside the class; instead, they are accessed and modified through public methods such as `getTitle()`, `getAuthor()`, `getQuantity()`, `addQuantity()`, `borrowBook()`, and `returnBook()`. This encapsulation ensures that the book data can only be accessed and modified in controlled ways, maintaining data integrity and hiding implementation details from the user .

The library management system uses a HashMap to store books, with the book title serving as the key and a `Book` object as the value. This allows for constant time complexity O(1) operations for adding, updating, borrowing, and returning books because HashMap provides efficient searches, insertions, and deletions. Using a HashMap helps ensure that the book lookup operations are fast, which is beneficial for swiftly handling library queries .

The program employs several strategies for handling user input errors. It continuously prompts the user until a valid input is received, specifically when expecting an integer for choices or quantities. This is done using a while-loop that checks if the next input is an integer, displaying an error message otherwise. This approach effectively prevents the program from crashing due to invalid inputs and guides users towards correct input formats, improving usability. However, users can still quit prematurely without updating field inputs, indicating a potential area for user experience improvement .

The library management program ensures the integrity of book borrowing and returning operations through several mechanisms. When borrowing books, it checks whether the requested amount is available using the `borrowBook()` method, which returns a boolean. If the amount exceeds the available quantity, it returns false and an error message is displayed. When returning books, the `returnBook()` method is used to increment the quantity, with checks ensuring the book exists in the library system, preventing inconsistencies and unauthorized returns .

Using the book title as the key in the HashMap poses potential limitations such as the inability to handle books with the same title but different editions or authors. This design assumes all titles are unique, which may not always be the case, leading to potential data conflicts or overwriting. An alternative approach could involve using a composite key that includes additional attributes like the author or ISBN, thereby enabling storage of multiple versions of books with the same title .

The lack of a mechanism to store deleted or inactive books means the system does not retain history or logs of books once they are removed. This can affect library operations by creating gaps in inventory records, which might be needed for audits, tracking popular books, or reinstating books accidentally deleted. Implementing a soft delete system (where books are marked inactive but not deleted) could mitigate this by preserving data integrity and allowing for more comprehensive reports and data recovery options .

The library management system contributes to data consistency by ensuring that each operation (adding, borrowing, returning books) is atomic and completed before another begins. By validating each input and systematically checking conditions (e.g., availability before borrowing and authenticating book ownership before returning), it prevents partial updates that could lead to inconsistent states. Sequential reliance on method calls that update the `Book` object's state ensures that the program maintains its internal data integrity .

To enhance scalability, the system could be refactored to use a database system for storing book information instead of a HashMap. This would facilitate handling larger datasets, support transactional operations, and enable more complex queries. Adding support for concurrent access control can prevent data races when multiple users interact simultaneously. Additionally, separating the logic for handling inputs (view) from the book operations (model) within a Model-View-Controller (MVC) pattern could improve maintainability and allow for richer user interfaces, such as a GUI or web-based application .

Method-based operations align with the object-oriented design principle of encapsulation, where implementation details are hidden behind public interfaces (methods). This allows the `LibrarySystem` to interact with `Book` objects through predefined methods like `addQuantity()`, `borrowBook()`, and `returnBook()`. Such design not only maintains encapsulation but also enhances modularity and reusability of the code, allowing for changes in implementation without affecting other parts of the program .

User validation in the library management program improves usability by ensuring that inputs are of an expected format before the system processes them. For example, the program checks if the input is an integer when selecting menu options or specifying the quantity of books, reducing user errors and enhancing interaction reliability. This minimizes user frustration caused by invalid input errors and helps in guiding users step-by-step through system operations .

You might also like