0% found this document useful (0 votes)
40 views7 pages

Programming Assignment Unit 6

Uploaded by

cebeni8671
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views7 pages

Programming Assignment Unit 6

Uploaded by

cebeni8671
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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


[Link] = title;
[Link] = author;
[Link] = 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 [Link];
import [Link];
import [Link];

public class GenericCatalog<T extends LibraryItem> {


private List<T> catalog;

public GenericCatalog() {
[Link] = new ArrayList<>();
}

public void addItem(T item) {


[Link](item);
}
public void removeItem(int itemID) {
Optional<T> itemToRemove = [Link]().filter(item -> [Link]() ==
itemID).findFirst();
if ([Link]()) {
[Link]([Link]());
} else {
[Link]("Item with ID " + itemID + " not found.");
}
}

public void displayCatalog() {


for (T item : catalog) {
[Link](item);
}
}
}

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

Code
import [Link];

public class LibraryCatalogApp {


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

while (true) {
[Link]("Library Catalog Management");
[Link]("1. Add Item");
[Link]("2. Remove Item");
[Link]("3. Display Catalog");
[Link]("4. Exit");
[Link]("Choose an option: ");
int choice = [Link]();
[Link](); // consume newline

switch (choice) {
case 1:
[Link]("Enter title: ");
String title = [Link]();
[Link]("Enter author: ");
String author = [Link]();
[Link]("Enter item ID: ");
int itemID = [Link]();
[Link](); // consume newline

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


[Link](newItem);
[Link]("Item added successfully.");
break;
case 2:
[Link]("Enter item ID to remove: ");
int removeID = [Link]();
[Link](); // consume newline
[Link](removeID);
break;
case 3:
[Link]();
break;
case 4:
[Link]("Exiting...");
[Link]();
[Link](0);
break;
default:
[Link]("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
[Link](new LibraryItem("Book Title 1", "Author A", 101));
[Link](new LibraryItem("DVD Title 1", "Director B", 102));
[Link](new LibraryItem("Magazine Title 1", "Editor C", 103));

// Displaying catalog
[Link]("Catalog after adding items:");
[Link]();

// Removing an item
[Link](102);

// Displaying catalog after removal


[Link]("Catalog after removing item with ID 102:");
[Link]();

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


[Link](999);
}
}
Screenshot

You might also like