Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

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() {

this.catalog = new HashMap<>();

}
// Method to add a new library item

public void addItem(T item) {

catalog.put(item.getItemID(), item);

System.out.println("Item added successfully: " + item.getTitle());

// Method to remove a library item

public void removeItem(int itemID) {

if (catalog.containsKey(itemID)) {

T removedItem = catalog.remove(itemID);

System.out.println("Item removed successfully: " + removedItem.getTitle());

} else {

System.out.println("Item with ID " + itemID + " not found.");

// Method to retrieve item details

public void getItemDetails(int itemID) {

if (catalog.containsKey(itemID)) {

T item = catalog.get(itemID);

System.out.println("Item ID: " + item.getItemID());

System.out.println("Title: " + item.getTitle());


System.out.println("Author: " + item.getAuthor());

} else {

System.out.println("Item with ID " + itemID + " not found.");

// Method to view the current catalog

public void viewCatalog() {

if (catalog.isEmpty()) {

System.out.println("Catalog is empty.");

} else {

System.out.println("Current Catalog:");

for (Map.Entry<Integer, T> entry : catalog.entrySet()) {

System.out.println("Item ID: " + entry.getKey());

System.out.println("Title: " + entry.getValue().getTitle());

System.out.println("Author: " + entry.getValue().getAuthor());

System.out.println("--------------------");

```
Example Code Usage:

// Create a library catalog for books

LibraryCatalog<LibraryItem<Book>> bookCatalog = new LibraryCatalog<>();

// Add some books to the catalog

bookCatalog.addItem(new LibraryItem<>("Book1", "Author1", 101));

bookCatalog.addItem(new LibraryItem<>("Book2", "Author2", 102));

bookCatalog.addItem(new LibraryItem<>("Book3", "Author3", 103));

// Display the current catalog

bookCatalog.viewCatalog();

// Remove an item from the catalog

bookCatalog.removeItem(102);

// Display the updated catalog

bookCatalog.viewCatalog();

```

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

--------------------

```

You might also like