Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
53 views

Programming Assignment Unit 6

The document shows a Java implementation of a generic library catalog system. It includes a LibraryItem class to represent items, a generic Catalog class to manage the catalog, and a main app class demonstrating basic usage like adding, removing and displaying items.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Programming Assignment Unit 6

The document shows a Java implementation of a generic library catalog system. It includes a LibraryItem class to represent items, a generic Catalog class to manage the catalog, and a main app class demonstrating basic usage like adding, removing and displaying items.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

import java.util.

HashMap;

import java.util.Map;

// Generic LibraryItem class

class LibraryItem<T> {

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;

// Getters and setters

public String getTitle() {

return title;

public void setTitle(String title) {

this.title = title;

public String getAuthor() {

return author;

public void setAuthor(String author) {

this.author = author;

}
public int getItemID() {

return itemID;

public void setItemID(int itemID) {

this.itemID = itemID;

@Override

public String toString() {

return "Title: " + title + ", Author: " + author + ", Item ID: " + itemID;

// Generic Catalog class

class Catalog<T> {

private Map<Integer, T> items;

public Catalog() {

this.items = new HashMap<>();

public void addItem(int itemID, T item) {

items.put(itemID, item);

public void removeItem(int itemID) {

if (items.containsKey(itemID)) {

items.remove(itemID);

} else {
System.out.println("Item with ID " + itemID + " does not exist in the catalog.");

public void displayCatalog() {

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

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

System.out.println(entry.getValue());

// Main class for user interface

public class LibraryCatalogApp {

public static void main(String[] args) {

// Create Catalog of LibraryItems

Catalog<LibraryItem<?>> catalog = new Catalog<>();

// Add some library items

LibraryItem<Book> book1 = new LibraryItem<>("The Great Gatsby", "F. Scott Fitzgerald", 1);

LibraryItem<DVD> dvd1 = new LibraryItem<>("Inception", "Christopher Nolan", 2);

catalog.addItem(book1.getItemID(), book1);

catalog.addItem(dvd1.getItemID(), dvd1);

// Display catalog

catalog.displayCatalog();

// Remove item

catalog.removeItem(1);
// Display updated catalog

catalog.displayCatalog();

// Sample Book class

class Book {

// Book specific attributes

// Sample DVD class

class DVD {

// DVD specific attributes

Here's a simplified implementation of the requirements in Java:

```java

import java.util.HashMap;

import java.util.Map;

// Generic LibraryItem class

class LibraryItem<T> {

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;

// Getters and setters

public String getTitle() {

return title;

public void setTitle(String title) {

this.title = title;

public String getAuthor() {

return author;

public void setAuthor(String author) {

this.author = author;

public int getItemID() {

return itemID;

public void setItemID(int itemID) {

this.itemID = itemID;

@Override

public String toString() {

return "Title: " + title + ", Author: " + author + ", Item ID: " + itemID;
}

// Generic Catalog class

class Catalog<T> {

private Map<Integer, T> items;

public Catalog() {

this.items = new HashMap<>();

public void addItem(int itemID, T item) {

items.put(itemID, item);

public void removeItem(int itemID) {

if (items.containsKey(itemID)) {

items.remove(itemID);

} else {

System.out.println("Item with ID " + itemID + " does not exist in the catalog.");

public void displayCatalog() {

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

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

System.out.println(entry.getValue());

}
// Main class for user interface

public class LibraryCatalogApp {

public static void main(String[] args) {

// Create Catalog of LibraryItems

Catalog<LibraryItem<?>> catalog = new Catalog<>();

// Add some library items

LibraryItem<Book> book1 = new LibraryItem<>("The Great Gatsby", "F. Scott Fitzgerald", 1);

LibraryItem<DVD> dvd1 = new LibraryItem<>("Inception", "Christopher Nolan", 2);

catalog.addItem(book1.getItemID(), book1);

catalog.addItem(dvd1.getItemID(), dvd1);

// Display catalog

catalog.displayCatalog();

// Remove item

catalog.removeItem(1);

// Display updated catalog

catalog.displayCatalog();

// Sample Book class

class Book {

// Book specific attributes

// Sample DVD class

class DVD {
// DVD specific attributes

This implementation provides a basic system basis for Java library catalogs. It includes a
general `LibraryItem` class, a generic `Catalog` class for handling library items, and a
`LibraryCatalogApp` class with a simple command-line interface. The code also
demonstrates how to add, remove, and display library items, as well as handle scenarios such
as attempting to remove an item that isn't there. Additionally, the approach ensures code
reuse and flexibility by effectively utilizing generics.

You might also like