0% found this document useful (0 votes)
267 views6 pages

Assignment Unit 6 Ex

This document demonstrates a generic library catalog implemented in Java that can store books and DVDs. It utilizes generic classes and methods to create a flexible catalog. The catalog allows users to add, remove, and view library items through a simple command line interface. Example code shows how to create a catalog for books, add and remove books, and display the catalog.

Uploaded by

niotihasan02
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)
267 views6 pages

Assignment Unit 6 Ex

This document demonstrates a generic library catalog implemented in Java that can store books and DVDs. It utilizes generic classes and methods to create a flexible catalog. The catalog allows users to add, remove, and view library items through a simple command line interface. Example code shows how to create a catalog for books, add and remove books, and display the catalog.

Uploaded by

niotihasan02
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
You are on page 1/ 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() {

[Link] = new HashMap<>();

}
// Method to add a new library item

public void addItem(T item) {

[Link]([Link](), item);

[Link]("Item added successfully: " + [Link]());

// Method to remove a library item

public void removeItem(int itemID) {

if ([Link](itemID)) {

T removedItem = [Link](itemID);

[Link]("Item removed successfully: " + [Link]());

} else {

[Link]("Item with ID " + itemID + " not found.");

// Method to retrieve item details

public void getItemDetails(int itemID) {

if ([Link](itemID)) {

T item = [Link](itemID);

[Link]("Item ID: " + [Link]());

[Link]("Title: " + [Link]());


[Link]("Author: " + [Link]());

} else {

[Link]("Item with ID " + itemID + " not found.");

// Method to view the current catalog

public void viewCatalog() {

if ([Link]()) {

[Link]("Catalog is empty.");

} else {

[Link]("Current Catalog:");

for ([Link]<Integer, T> entry : [Link]()) {

[Link]("Item ID: " + [Link]());

[Link]("Title: " + [Link]().getTitle());

[Link]("Author: " + [Link]().getAuthor());

[Link]("--------------------");

```
Example Code Usage:

// Create a library catalog for books

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

// Add some books to the catalog

[Link](new LibraryItem<>("Book1", "Author1", 101));

[Link](new LibraryItem<>("Book2", "Author2", 102));

[Link](new LibraryItem<>("Book3", "Author3", 103));

// Display the current catalog

[Link]();

// Remove an item from the catalog

[Link](102);

// Display the updated catalog

[Link]();

```

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