Programming Assignment Unit 2
Department of Computer Science, University of the People
CS 1102: Programming 1
Kwok Wing Cheung
September 22, 2023
Here is a java program of a library system to add, borrow and return books. The java
program uses try-catch and nested control structures:
import java.util.Scanner;
class Library {
private String[] titles;
private String[] authors;
private int[] quantities;
private int size; // To keep track of the number of books in the library
public Library() {
this.titles = new String[100]; // Assuming a maximum of 100 books (you can adjust
this size as needed)
this.authors = new String[100];
this.quantities = new int[100];
this.size = 0;
public void addBook(String bookTitle, String bookAuthor, int bookQuantity) {
int index = findBookIndex(bookTitle);
if (index != -1) {
quantities[index] += bookQuantity;
System.out.println(bookQuantity + " copies of '" + bookTitle + "' by " +
bookAuthor + " added. Total: " + quantities[index] + " copies.");
} else {
titles[size] = bookTitle;
authors[size] = bookAuthor;
quantities[size] = bookQuantity;
size++;
System.out.println("'" + bookTitle + "' by " + bookAuthor + " added. Total: " +
bookQuantity + " copies.");
public void borrowBook(String bookTitle, int bookQuantity) {
int index = findBookIndex(bookTitle);
if (index != -1) {
if (quantities[index] >= bookQuantity) {
quantities[index] -= bookQuantity;
System.out.println(bookQuantity + " copies of '" + bookTitle + "' borrowed.
Remaining: " + quantities[index] + " copies.");
} else {
System.out.println("Not enough copies of '" + bookTitle + "' available to
borrow.");
}
} else {
System.out.println("'" + bookTitle + "' not found in the library.");
public void returnBook(String bookTitle, int bookQuantity) {
int index = findBookIndex(bookTitle);
if (index != -1) {
quantities[index] += bookQuantity;
System.out.println(bookQuantity + " copies of '" + bookTitle + "' returned. Total:
" + quantities[index] + " copies.");
} else {
System.out.println("'" + bookTitle + "' not found in the library.");
private int findBookIndex(String bookTitle) {
for (int i = 0; i < size; i++) {
if (titles[i].equalsIgnoreCase(bookTitle)) {
return i;
}
}
return -1;
public static void main(String[] args) {
Library library = new Library();
Scanner scanner = new Scanner(System.in);
try (scanner) {
boolean exit = false;
while (!exit) {
System.out.println("Library Management System");
System.out.println("1. Add a Book");
System.out.println("2. Borrow a Book");
System.out.println("3. Return a Book");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice;
try {
choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
} catch (Exception e) {
System.out.println("Invalid input. Please enter a number.");
scanner.nextLine(); // Consume the invalid input
continue;
switch (choice) {
case 1:
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter author: ");
String author = scanner.nextLine();
System.out.print("Enter quantity: ");
int quantity;
try {
quantity = scanner.nextInt();
} catch (Exception e) {
System.out.println("Invalid input. Please enter a number for quantity.");
scanner.nextLine(); // Consume the invalid input
continue;
library.addBook(title, author, quantity);
break;
case 2:
System.out.print("Enter book title to borrow: ");
String borrowTitle = scanner.nextLine();
System.out.print("Enter the number of copies you want to borrow: ");
int borrowQuantity;
try {
borrowQuantity = scanner.nextInt();
} catch (Exception e) {
System.out.println("Invalid input. Please enter a number for quantity.");
scanner.nextLine(); // Consume the invalid input
continue;
library.borrowBook(borrowTitle, borrowQuantity);
break;
case 3:
System.out.print("Enter book title to return: ");
String returnTitle = scanner.nextLine();
System.out.print("Enter the number of copies you want to return: ");
int returnQuantity;
try {
returnQuantity = scanner.nextInt();
} catch (Exception e) {
System.out.println("Invalid input. Please enter a number for quantity.");
scanner.nextLine(); // Consume the invalid input
continue;
library.returnBook(returnTitle, returnQuantity);
break;
case 4:
System.out.println("Exiting the program.");
exit = true;
break;
default:
System.out.println("Invalid choice. Please select a valid option.");
Here is a sample output for the program above: