You are on page 1of 6

Generic Library Catalog Assignment in Java

This program implements a generic library catalog that can store information about different

types of library items like books, DVDs, or magazines.

1. LibraryItem Class:

public class LibraryItem<T> {

private String title;

private String author;

private String itemID;

private T genre; // Generic field for item-specific genre

public LibraryItem(String title, String author, String itemID, T

genre) {

this.title = title;

this.author = author;

this.itemID = itemID;

this.genre = genre;

// Getters and setters omitted for brevity

}
2. Generic Catalog Class:

public class LibraryCatalog<T extends LibraryItem<T>> {

private List<T> items;

public LibraryCatalog() {

items = new ArrayList<>();

public void addItem(T item) {

items.add(item);

public T removeItem(String itemID) throws ItemNotFoundException {

T removedItem = null;

int index = -1;

for (int i = 0; i < items.size(); i++) {

if (items.get(i).getItemID().equals(itemID)) {

removedItem = items.get(i);

index = i;

break;

if (index == -1) {

throw new ItemNotFoundException("Item with ID: " + itemID + "

not found.");
}

items.remove(index);

return removedItem;

public List<T> getItems() {

return items;

3. ItemNotFoundException:

public class ItemNotFoundException extends Exception {

public ItemNotFoundException(String message) {

super(message);

4. User Interface Class (LibraryMenu):

public class LibraryMenu {

private LibraryCatalog<LibraryItem> catalog; // Generic catalog


public LibraryMenu(LibraryCatalog<LibraryItem> catalog) {

this.catalog = catalog;

public void displayMenu() {

Scanner scanner = new Scanner(System.in);

int choice;

do {

System.out.println("\nLibrary Catalog Menu");

System.out.println("1. Add Item");

System.out.println("2. Remove Item");

System.out.println("3. View Catalog");

System.out.println("4. Exit");

System.out.print("Enter your choice: ");

choice = scanner.nextInt();

switch (choice) {

case 1:

addItem();

break;

case 2:

removeItem();

break;

case 3:

viewCatalog();
break;

case 4:

System.out.println("Exiting Library Catalog.");

break;

default:

System.out.println("Invalid Choice!");

} while (choice != 4);

// Implement methods for add, remove, and view functionalities

5. Main Class:

public class Main {

public static void main(String[] args) {

LibraryCatalog<LibraryItem> catalog = new LibraryCatalog<>();

LibraryMenu menu = new LibraryMenu(catalog);

menu.displayMenu();

}
Note: This is a basic example. You can extend the LibraryItem class with specific fields for

different item types (e.g., director for movies) and modify the addItem method in LibraryMenu to

handle user input for these additional fields.

Remember to implement missing methods in LibraryMenu for adding, removing, and

viewing items with proper error handling using the ItemNotFoundException class.

This program demonstrates generic classes and methods for a library catalog with basic

functionalities. Compile and run the code to see the interactive menu and test the functionalities.

You might also like