Lab Manual - Lab 17-Exception Handling
Lab Manual - 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
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.
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.
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.
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.
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;
@Override
public String toString() {
return title + " by " + author + " ($" + price + ")";
}
}
// Borrow a book
try {
library.borrowBook("1984");
} catch (Exception e) {
System.out.println(e.getMessage());
}
// Return a book
try {
library.returnBook("1984");
} catch (Exception e) {
System.out.println(e.getMessage());
}