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

Java Library Management System Code

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)
28 views8 pages

Java Library Management System Code

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

Unit 2: Programming Assignment

Stella Kavana
CS 1102-01 Programming 1 - AY2026-T1
Ijaz Ud Din (Instructor)
September, 18th ,2025
Basic Library System in Java
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 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 .

You might also like