CS 1102-01 - AY2025-T3 Programming Assignment Unit 2
CS 1102-01 - AY2025-T3 Programming Assignment Unit 2
creating a LibrarySystem class with methods to add books, borrow books, return books, and exit the program.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Book {
String title;
String author;
int quantity;
this.title = title;
this.author = author;
this.quantity = quantity;
this.quantity += quantity;
}
public boolean borrowBook(int quantity) {
this.quantity -= quantity;
return true;
} else {
return false;
this.quantity += quantity;
while (true) {
System.out.println("4. Exit");
switch (choice) {
case 1:
addBook();
break;
case 2:
borrowBook();
break;
case 3:
returnBook();
break;
case 4:
return;
default:
System.out.println("Invalid choice! Please enter a number between 1 and 4.");
if (library.containsKey(title)) {
library.get(title).addQuantity(quantity);
} else {
if (!library.containsKey(title)) {
return;
if (library.get(title).borrowBook(quantity)) {
System.out.println("You have borrowed " + quantity + " copies of '" + title + "'.");
} else {
}
private static void returnBook() {
if (!library.containsKey(title)) {
return;
library.get(title).returnBook(quantity);
System.out.println("You have returned " + quantity + " copies of '" + title + "'.");
Book Class: Represents a book with attributes (title, author, quantity) and methods to modify the quantity.
Library System: Stores books using a HashMap<String, Book>, where the key is the book title.
Menu Options:
Add Book: If the book exists, it updates the quantity; otherwise, it adds a new book.
Borrow Book: Checks if the requested number of books is available before borrowing.
Return Book: Ensures the book belongs to the library before accepting a return.
Adding Books:
Borrowing a Book:
Returning a Book:
Borrowing More Than Available:
References: