Creating Library App
Creating Library App
of library items like books, DVDs, and magazines. We'll use Java generics to ensure flexibility
and code reusability.
Here are the steps we'll follow.
1. LibraryItem Class: A generic class with attributes like `title`, `author`, and `itemID`.
2. GenericCatalog Class: A generic catalog that stores items of type `LibraryItem`.
3. Library Operations: Methods to add, remove, and retrieve items in the catalog.
4. User Interface: A simple command-line interface for user interactions.
5. Testing: Comprehensive testing for adding and removing items.
Let's implement the solution in Java.
1. LibraryItem Class
public class LibraryItem {
private String title;
private String author;
private String itemID;
@Override
public String toString() {
return "Title: " + title + ", Author: " + author + ", Item ID: " + itemID;
}
}
2. GenericCatalog Class
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public GenericCatalog() {
this.catalog = new ArrayList<>();
}
switch (option) {
case 1:
addItem(scanner);
break;
case 2:
removeItem(scanner);
break;
case 3:
viewItem(scanner);
break;
case 4:
catalog.listItems();
break;
case 5:
scanner.close();
return;
default:
System.out.println("Invalid option. Please try again.");
}
}
}
References
Divertitto, A. (2022, August 18). Java generics: how to use angled brackets in practice.
CodeGym. https://codegym.cc/groups/posts/generics-in-java
Eck, D. J. (2022). Introduction to programming using java version 9, JavaFX edition. Licensed
under CC 4.0. https://math.hws.edu/javanotes/
Kumar, A. (2023, April 18). Mastering generics in Java: A comprehensive guide for Java
developers. Tech Thoughts Explorer. https://techthoughtsexplorer.hashnode.dev/mastering-
generics-in-java-a-comprehensive-guide-for-java-developers
Bro code. (2020, July 27). Java generics ❓ [Video]. YouTube. https://youtu.be/jUcAyZ5OUm0?
si=s4EtfM0tQMT-4HO1
Coding with John. (2021, December 20). Generics in Java - Full simple tutorial [Video].
YouTube. https://youtu.be/K1iu1kXkVoA?si=MIJp7JpY2GlJw1uW