Generic Library Catalog
This assignment demonstrates a generic library catalog implemented in Java with example code
and output.
Objectives:
Utilize generic classes and methods for flexibility.
Develop a catalog that can store books and DVDs (expandable to other types).
Implement basic error handling.
Create a simple command-line interface for user interaction.
Generic Catalog Class:
// Generic Library Catalog class
class LibraryCatalog<T extends LibraryItem<?>> {
private Map<Integer, T> catalog;
public LibraryCatalog() {
[Link] = new HashMap<>();
}
// Method to add a new library item
public void addItem(T item) {
[Link]([Link](), item);
[Link]("Item added successfully: " + [Link]());
// Method to remove a library item
public void removeItem(int itemID) {
if ([Link](itemID)) {
T removedItem = [Link](itemID);
[Link]("Item removed successfully: " + [Link]());
} else {
[Link]("Item with ID " + itemID + " not found.");
// Method to retrieve item details
public void getItemDetails(int itemID) {
if ([Link](itemID)) {
T item = [Link](itemID);
[Link]("Item ID: " + [Link]());
[Link]("Title: " + [Link]());
[Link]("Author: " + [Link]());
} else {
[Link]("Item with ID " + itemID + " not found.");
// Method to view the current catalog
public void viewCatalog() {
if ([Link]()) {
[Link]("Catalog is empty.");
} else {
[Link]("Current Catalog:");
for ([Link]<Integer, T> entry : [Link]()) {
[Link]("Item ID: " + [Link]());
[Link]("Title: " + [Link]().getTitle());
[Link]("Author: " + [Link]().getAuthor());
[Link]("--------------------");
```
Example Code Usage:
// Create a library catalog for books
LibraryCatalog<LibraryItem<Book>> bookCatalog = new LibraryCatalog<>();
// Add some books to the catalog
[Link](new LibraryItem<>("Book1", "Author1", 101));
[Link](new LibraryItem<>("Book2", "Author2", 102));
[Link](new LibraryItem<>("Book3", "Author3", 103));
// Display the current catalog
[Link]();
// Remove an item from the catalog
[Link](102);
// Display the updated catalog
[Link]();
```
Output:
```
Item added successfully: Book1
Item added successfully: Book2
Item added successfully: Book3
Current Catalog:
Item ID: 101
Title: Book1
Author: Author1
--------------------
Item ID: 102
Title: Book2
Author: Author2
--------------------
Item ID: 103
Title: Book3
Author: Author3
--------------------
Item removed successfully: Book2
Current Catalog:
Item ID: 101
Title: Book1
Author: Author1
--------------------
Item ID: 103
Title: Book3
Author: Author3
--------------------
```