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

Creating Library App

Uploaded by

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

Creating Library App

Uploaded by

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

To implement the assignment, we'll create a generic library catalog that can handle various types

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;

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


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

public String getTitle() {


return title;
}

public String getAuthor() {


return author;
}
public String getItemID() {
return 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 class GenericCatalog<T extends LibraryItem> {


private List<T> catalog;

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

public void addItem(T item) {


catalog.add(item);
}

public boolean removeItem(String itemID) {


Optional<T> itemToRemove = catalog.stream()
.filter(item -> item.getItemID().equals(itemID))
.findFirst();
if (itemToRemove.isPresent()) {
catalog.remove(itemToRemove.get());
return true;
}
return false;
}

public T getItem(String itemID) {


return catalog.stream()
.filter(item -> item.getItemID().equals(itemID))
.findFirst()
.orElse(null);
}

public void listItems() {


catalog.forEach(System.out::println);
}
}
3. User Interface
import java.util.Scanner;
public class LibraryApp {
private static GenericCatalog<LibraryItem> catalog = new GenericCatalog<>();

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Library Catalog:");
System.out.println("1. Add Item");
System.out.println("2. Remove Item");
System.out.println("3. View Item");
System.out.println("4. List All Items");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
int option = scanner.nextInt();
scanner.nextLine(); // consume newline

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.");
}
}
}

private static void addItem(Scanner scanner) {


System.out.print("Enter title: ");
String title = scanner.nextLine();
System.out.print("Enter author: ");
String author = scanner.nextLine();
System.out.print("Enter item ID: ");
String itemID = scanner.nextLine();

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


catalog.addItem(newItem);
System.out.println("Item added.");
}

private static void removeItem(Scanner scanner) {


System.out.print("Enter item ID to remove: ");
String itemID = scanner.nextLine();
if (catalog.removeItem(itemID)) {
System.out.println("Item removed.");
} else {
System.out.println("Item not found.");
}
}

private static void viewItem(Scanner scanner) {


System.out.print("Enter item ID to view: ");
String itemID = scanner.nextLine();
LibraryItem item = catalog.getItem(itemID);
if (item != null) {
System.out.println(item);
} else {
System.out.println("Item not found.");
}
}
}
4. Testing
Testing involves adding and removing items, and checking that the catalog correctly handles
various item types. This can be done by running the `LibraryApp` class and following the
command-line prompts to interact with the catalog.
5. Output Screenshot
This implementation demonstrates the use of Java generics to create a flexible and reusable
library catalog, handling various types of library items.

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

You might also like