Programming Assignment 2
Programming Assignment 2
import java.util.Scanner;
class Library {
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 = 0;
if (index != -1) {
quantities[index] += bookQuantity;
System.out.println(bookQuantity + " copies of '" + bookTitle + "' by " +
} else {
titles[size] = bookTitle;
authors[size] = bookAuthor;
quantities[size] = bookQuantity;
size++;
if (index != -1) {
quantities[index] -= bookQuantity;
} else {
borrow.");
}
} else {
if (index != -1) {
quantities[index] += bookQuantity;
} else {
if (titles[i].equalsIgnoreCase(bookTitle)) {
return i;
}
}
return -1;
try (scanner) {
while (!exit) {
System.out.println("4. Exit");
int choice;
try {
choice = scanner.nextInt();
} catch (Exception e) {
continue;
switch (choice) {
case 1:
int quantity;
try {
quantity = scanner.nextInt();
} catch (Exception e) {
continue;
break;
case 2:
int borrowQuantity;
try {
borrowQuantity = scanner.nextInt();
} catch (Exception e) {
continue;
library.borrowBook(borrowTitle, borrowQuantity);
break;
case 3:
int returnQuantity;
try {
returnQuantity = scanner.nextInt();
} catch (Exception e) {
continue;
library.returnBook(returnTitle, returnQuantity);
break;
case 4:
exit = true;
break;
default: