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

Programming Assignment Unit 6

Uploaded by

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

Programming Assignment Unit 6

Uploaded by

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

1.

LibraryItem Class
This class will represent a generic library item with common attributes such as title, author, and
itemID.

Code
public class LibraryItem {
private String title;
private String author;
private int itemID;

public LibraryItem(String title, String author, int itemID) {


this.title = title;
this.author = author;
this.itemID = itemID;
}

public String getTitle() {


return title;
}

public String getAuthor() {


return author;
}

public int getItemID() {


return itemID;
}

@Override
public String toString() {
return "LibraryItem{" +
"title='" + title + '\'' +
", author='" + author + '\'' +
", itemID=" + itemID +
'}';
}
}

2. Generic Catalog Class


This class will store and manage library items using generics.

Code
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class GenericCatalog<T extends LibraryItem> {


private List<T> catalog;

public GenericCatalog() {
this.catalog = new ArrayList<>();
}

public void addItem(T item) {


catalog.add(item);
}
public void removeItem(int itemID) {
Optional<T> itemToRemove = catalog.stream().filter(item -> item.getItemID() ==
itemID).findFirst();
if (itemToRemove.isPresent()) {
catalog.remove(itemToRemove.get());
} else {
System.out.println("Item with ID " + itemID + " not found.");
}
}

public void displayCatalog() {


for (T item : catalog) {
System.out.println(item);
}
}
}

3. User Interface
A simple command-line interface to interact with the catalog.

Code
import java.util.Scanner;

public class LibraryCatalogApp {


public static void main(String[] args) {
GenericCatalog<LibraryItem> catalog = new GenericCatalog<>();
Scanner scanner = new Scanner(System.in);

while (true) {
System.out.println("Library Catalog Management");
System.out.println("1. Add Item");
System.out.println("2. Remove Item");
System.out.println("3. Display Catalog");
System.out.println("4. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // consume newline

switch (choice) {
case 1:
System.out.print("Enter title: ");
String title = scanner.nextLine();
System.out.print("Enter author: ");
String author = scanner.nextLine();
System.out.print("Enter item ID: ");
int itemID = scanner.nextInt();
scanner.nextLine(); // consume newline

LibraryItem newItem = new LibraryItem(title, author, itemID);


catalog.addItem(newItem);
System.out.println("Item added successfully.");
break;
case 2:
System.out.print("Enter item ID to remove: ");
int removeID = scanner.nextInt();
scanner.nextLine(); // consume newline
catalog.removeItem(removeID);
break;
case 3:
catalog.displayCatalog();
break;
case 4:
System.out.println("Exiting...");
scanner.close();
System.exit(0);
break;
default:
System.out.println("Invalid option. Please try again.");
}
}
}
}

4. Testing
To test the functionality, I can run the LibraryCatalogApp class and interact with the catalog
through the command-line interface. Add various items, remove items, and display the catalog to
ensure everything works as expected.

Code
public class LibraryCatalogTest {
public static void main(String[] args) {
GenericCatalog<LibraryItem> catalog = new GenericCatalog<>();

// Adding items
catalog.addItem(new LibraryItem("Book Title 1", "Author A", 101));
catalog.addItem(new LibraryItem("DVD Title 1", "Director B", 102));
catalog.addItem(new LibraryItem("Magazine Title 1", "Editor C", 103));

// Displaying catalog
System.out.println("Catalog after adding items:");
catalog.displayCatalog();

// Removing an item
catalog.removeItem(102);

// Displaying catalog after removal


System.out.println("Catalog after removing item with ID 102:");
catalog.displayCatalog();

// Trying to remove an item that doesn't exist


catalog.removeItem(999);
}
}
Screenshot

You might also like