0% found this document useful (0 votes)
4 views3 pages

Programming Assignment Unit 2 Library System Updated

This document outlines a Java program for a library management system that allows users to add, borrow, and return books. It utilizes a HashMap for efficient storage and includes input validation and error handling for a stable user experience. A placeholder for output screenshots is also included.
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)
4 views3 pages

Programming Assignment Unit 2 Library System Updated

This document outlines a Java program for a library management system that allows users to add, borrow, and return books. It utilizes a HashMap for efficient storage and includes input validation and error handling for a stable user experience. A placeholder for output screenshots is also included.
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

Programming Assignment Unit 2 – Library System

This document contains the Java source code, explanation of the implementation, and a

placeholder for the required output screenshots.

Java Source 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 qty) { [Link] += qty; }


public void borrowBook(int qty) { [Link] -= qty; }
public void returnBook(int qty) { [Link] += qty; }
}

public class LibrarySystem {

private static Map<String, Book> library = new HashMap<>();


private static Scanner input = new Scanner([Link]);

public static void addBook() {


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

[Link]("Enter author name: ");


String author = [Link]().trim();

[Link]("Enter quantity: ");


int quantity;
try { quantity = [Link]([Link]()); }
catch (NumberFormatException e) { [Link]("Invalid quantity.");
return; }

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

public static void borrowBook() {


[Link]("Enter book title to borrow: ");
String title = [Link]().trim();

if (![Link](title)) {
[Link]("Book not found.");
return;
}

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


int qty;
try { qty = [Link]([Link]()); }
catch (Exception e) { [Link]("Invalid number."); return; }

Book book = [Link](title);


if (qty > [Link]()) {
[Link]("Not enough copies available.");
} else {
[Link](qty);
[Link]("Successfully borrowed " + qty + " copy/copies.");
}
}

public 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.");
return;
}

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


int qty;
try { qty = [Link]([Link]()); }
catch (Exception e) { [Link]("Invalid number."); return; }

[Link](title).returnBook(qty);
[Link]("Book returned successfully.");
}

public static void main(String[] args) {


while (true) {
[Link]("
===== LIBRARY SYSTEM =====");
[Link]("1. Add Books");
[Link]("2. Borrow Books");
[Link]("3. Return Books");
[Link]("4. Exit");
[Link]("Choose an option: ");

int choice;
try { choice = [Link]([Link]()); }
catch (Exception e) { [Link]("Invalid choice."); continue; }

switch (choice) {
case 1: addBook(); break;
case 2: borrowBook(); break;
case 3: returnBook(); break;
case 4: [Link]("Exiting..."); return;
default: [Link]("Invalid option.");
}
}
}
}

Explanation
This Java program implements a simple library management system with the ability to add,

borrow, and return books. A HashMap is used for efficient book storage and retrieval. Proper

input validation and error handling ensure program stability and user-friendly operation.

Output Screenshot
Insert your IDE output screenshot below after running the program:

[ SCREENSHOT PLACEHOLDER ]

You might also like