0% found this document useful (0 votes)
3 views

Lab Manual - Lab 17-Exception Handling

The document outlines a programming lab focused on exception handling in a library system. It includes class definitions for Book and Library, methods for adding, borrowing, and returning books, as well as custom exceptions for handling specific error cases. An example test scenario is provided to demonstrate the functionality and exception handling of the library system.

Uploaded by

aishaabedin0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab Manual - Lab 17-Exception Handling

The document outlines a programming lab focused on exception handling in a library system. It includes class definitions for Book and Library, methods for adding, borrowing, and returning books, as well as custom exceptions for handling specific error cases. An example test scenario is provided to demonstrate the functionality and exception handling of the library system.

Uploaded by

aishaabedin0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

North South University

Department of Electrical and Computer Engineering


CSE 215L: Programming Language II Lab

Lab – 17: Exception Handling

Book

- title: String
- author: String
- price: double
- isBorrowed: boolean

/* constructor */
/* getter-setter */
/* toString */

Library

- books: Book[]
- count: int

+ Library(capacity: int)
+ addBook(book: Book): void
+ borrowBook(title: String): void
+ returnBook(title: String): void
+ showAvailableBooks(): void

Now create BookNotFoundException and BookAlreadyBorrowedException


classes that extend the Exception class.

Methods and Class description:

In the Book class constructor, check if the price is negative. If it is, throw an
IllegalArgumentException with a message like "Price cannot be negative". This
ensures that all Book objects are created with valid, non-negative prices.

addBook(book: Book): void

Attempts to add a new Book object to the library's collection. If the library has available
space, the book is added, and a confirmation message is printed. If the library is full and
an attempt is made to add a book beyond its capacity, an
ArrayIndexOutOfBoundsException is caught, and a message is printed
indicating that no more books can be added.

borrowBook(title: String): void

Attempts to borrow a book by its title. If the book is found and is not already borrowed, it marks
the book as borrowed and prints a success message. If the book is not found, a
BookNotFoundException is thrown. If the book is already borrowed, a
BookAlreadyBorrowedException is thrown.

returnBook(title: String): void

Attempts to return a borrowed book by its title. If the book is found and is
currently borrowed, it marks the book as available and prints a success message. If
the book is not found, a BookNotFoundException is thrown. If the book was
not borrowed, a message is printed indicating the book cannot be returned.

showAvailableBooks(): void

Prints a list of all books in the library that are not currently borrowed. If no books
are available, a message is printed indicating that no books are available.

Example Scenario for Testing:

In your test class, create a Library object with a capacity of 3 and test the
following scenario: (Use Finally to print a message that operation is completed).

1. Create a Book object titled "The Great Gatsby" by "F. Scott Fitzgerald" with a
price of $20.
2. Create a Book object titled "To Kill a Mockingbird" by "Harper Lee" with a price
of $25.
3. Create a Book object titled "1984" by "George Orwell" with a price of $18.
4. Create a Book object titled "Moby Dick" by "Herman Melville" with a price of
-15. This should trigger an exception (IllegalArgumentException)
indicating that the price cannot be negative.
5. Add the first three valid Book objects to the library.
6. Attempt to add a fourth valid book titled "Pride and Prejudice" by "Jane Austen"
with a price of $22. This should trigger an
ArrayIndexOutOfBoundsException as the library is full.
7. Borrow the book "1984".
8. Attempt to borrow the book "1984" again. This should trigger a
BookAlreadyBorrowedException.
9. Attempt to borrow a book titled "The Catcher in the Rye," which is not in the
library. This should trigger a BookNotFoundException.
10. Return the book "1984".
11. Attempt to return the book "The Catcher in the Rye." This should trigger a
BookNotFoundException.
12. Display all available books in the library.
public class Book {
private String title;
private String author;
private double price;
private boolean isBorrowed;

public Book(String title, String author, double price) {


if (price < 0) {
throw new IllegalArgumentException("Price cannot be negative");
}
this.title = title;
this.author = author;
this.price = price;
this.isBorrowed = false;
}

public String getTitle() {


return title;
}

public String getAuthor() {


return author;
}

public double getPrice() {


return price;
}

public boolean isBorrowed() {


return isBorrowed;
}

public void setBorrowed(boolean borrowed) {


isBorrowed = borrowed;
}

@Override
public String toString() {
return title + " by " + author + " ($" + price + ")";
}
}

public class BookNotFoundException extends Exception {


public BookNotFoundException(String message) {
super(message);
}
}

public class BookAlreadyBorrowedException extends Exception {


public BookAlreadyBorrowedException(String message) {
super(message);
}
}

public class Library {


private Book[] books;
private int count;

public Library(int capacity) {


books = new Book[capacity];
count = 0;
}

public void addBook(Book book) {


try {
books[count] = book;
count++;
System.out.println("Book \"" + book.getTitle() + "\" added successfully.");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Cannot add book: Library is full.");
} finally {
System.out.println("addBook operation completed.");
}
}

public void borrowBook(String title) throws BookNotFoundException, BookAlreadyBorrowedException {


for (int i = 0; i < count; i++) {
if (books[i].getTitle().equals(title)) {
if (books[i].isBorrowed()) {
throw new BookAlreadyBorrowedException("Cannot borrow \"" + title + "\": Book is already borrowed.");
}
books[i].setBorrowed(true);
System.out.println("Successfully borrowed \"" + title + "\".");
return;
}
}
throw new BookNotFoundException("Cannot borrow \"" + title + "\": Book not found.");
}

public void returnBook(String title) throws BookNotFoundException {


for (int i = 0; i < count; i++) {
if (books[i].getTitle().equals(title)) {
if (!books[i].isBorrowed()) {
System.out.println("Book \"" + title + "\" was not borrowed.");
return;
}
books[i].setBorrowed(false);
System.out.println("Successfully returned \"" + title + "\".");
return;
}
}
throw new BookNotFoundException("Cannot return \"" + title + "\": Book not found.");
}

public void showAvailableBooks() {


boolean anyAvailable = false;
for (int i = 0; i < count; i++) {
if (!books[i].isBorrowed()) {
System.out.println(books[i]);
anyAvailable = true;
}
}
if (!anyAvailable) {
System.out.println("No books available.");
}
System.out.println("showAvailableBooks operation completed.");
}
}

public class Test {


public static void main(String[] args) {
Library library = new Library(3);

// Create book objects


Book book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald", 20);
Book book2 = new Book("To Kill a Mockingbird", "Harper Lee", 25);
Book book3 = new Book("1984", "George Orwell", 18);

// Attempt to create a book with negative price


try {
Book book4 = new Book("Moby Dick", "Herman Melville", -15);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}

// Add books to the library


library.addBook(book1);
library.addBook(book2);
library.addBook(book3);

// Attempt to add a fourth book to a full library


Book book5 = new Book("Pride and Prejudice", "Jane Austen", 22);
library.addBook(book5);

// Borrow a book
try {
library.borrowBook("1984");
} catch (Exception e) {
System.out.println(e.getMessage());
}

// Attempt to borrow the same book again


try {
library.borrowBook("1984");
} catch (Exception e) {
System.out.println(e.getMessage());
}
// Attempt to borrow a non-existent book
try {
library.borrowBook("The Catcher in the Rye");
} catch (Exception e) {
System.out.println(e.getMessage());
}

// Return a book
try {
library.returnBook("1984");
} catch (Exception e) {
System.out.println(e.getMessage());
}

// Attempt to return a non-existent book


try {
library.returnBook("The Catcher in the Rye");
} catch (Exception e) {
System.out.println(e.getMessage());
}

// Show available books


System.out.println("Available books:");
library.showAvailableBooks();
}
}

You might also like