CS1103 Assignment 6
CS1103 Assignment 6
Scenario: Consider you have been asked to develop a generic library catalog in Java that
can store and manage different types of library items.
You are tasked with utilizing generic classes and methods to ensure flexibility and code
reusability.
Requirements:
a. Implement a generic catalog class that can store information about library
items (e.g., books, DVDs, magazines).
b. Ensure that the catalog works seamlessly with different types of items by
using generics.
b. Ensure that the LibraryItem class is compatible with the generic catalog.
3. Library Operations:
a. Develop methods within the generic catalog to add a new library item,
remove an item, and retrieve item details.
4. User Interface:
a. Create a simple command-line interface for users to interact with the library
catalog.
b. Allow users to add a new library item, remove an item, and view the current
catalog.
5. Testing:
a. Implement comprehensive testing for your generic catalog and LibraryItem class.
b. Test scenarios should include adding and removing items, ensuring the catalog works with
various types of library items.
Guidelines
Utilize generic classes and methods to create a flexible and reusable library catalog.
Focus on code modularity and reusability by using generics effectively.
Implement exception handling to manage unexpected scenarios gracefully.
Provide clear and concise comments and documentation for your code.
I created the generic Library as a Java project in eclipse and created a new package
com.GenericLibrary. Then, I created a class named Catalog to hold all the methods for operating
the library. The items.add for adding a new item to the library, the item.remove for removing
item from the library and the getItems for viewing items presently available in the library.
CATALOG CLASS
import java.util.ArrayList;
import java.util.List;
/*This class named catalog,
* hold all methods for the generic library
*/
public class Catalog<T> {
// Generic method for creating an ArrayList.
private List<T> items = new ArrayList<>();
// Generic method for adding an item to ArrayList.
public void addItem(T item) {
items.add(item);
}
// Generic method for removing an item from ArrayList.
public void removeItem(T item) {
items.remove(item);
}
}
}
The librartItem class was created to hold attributes of items in the library.
LIBRARYITEM CLASS
}
}
The LibrarySystem class is the class containing the main and acts as the user interface for
LIBRARYSYSTEM CLASS
import java.util.Scanner;
"****************************************************************");
// choice is refreshed for fresh input at the main Menu
int choice = 0;
// The 4 options available for a user to choose from.
while (choice != 4) {
System.out.println("1. Add Item to Library");
System.out.println("2. Remove Item from Library");
System.out.println("3. View Catalog of Item(s) in Library");
System.out.println("4. Exit Library");
// Instruction for the next step which is to choose an option.
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
// Definition of what each choice made by a user will process.
switch (choice) {
//case 1 is for adding an item to the library catalog
case 1:
System.out.print("Enter item ID: ");// inform user to enter book or
DvD ID
int itemID = scanner.nextInt(); //Scanner to collect book or DvD ID
System.out.print("Enter title: "); // inform user to enter book or DvD
title
String title = scanner.next(); //Scanner to collect book or DvD Title
System.out.print("Enter author: ");// inform user to enter book or DvD
Author
String author = scanner.next(); //Scanner to collect book or DvD
Author
LibraryItem newItem = new LibraryItem(itemID, title, author);
catalog.addItem(newItem); // Adds full item details to catalog
System.out.println(// Output beginning border line.
"****************************************************************");
System.out.println(
"Item added!"); // check to know expected stage of program.
System.out.println(// Output ending border line.
"****************************************************************");
break;
//case 2 is for removing item(s) from the library catalog
case 2:
System.out.print("Enter item ID to remove: ");
int idToRemove = scanner.nextInt();
catalog.getItems().removeIf(item -> item.itemID == idToRemove);
"****************************************************************");
break;
"****************************************************************");
for (LibraryItem item : catalog.getItems()) {
System.out.println("ID: " + item.itemID+",
Title: " + item.title +
", Author: " + item.author );
}
System.out.println( // Output ending border line.
"****************************************************************");
break;
case 4:
System.out.println("Exiting...");
break;
// Default message to handle wrong input option front user.
// Instruct user to enter only expected numbers 1 to 4.
default:
System.out.println(// Output beginning border line.
"***************************************************************");
System.out.println("Invalid choice. Please try again.");
System.out.println("ENTER BETWEEN 1 TO 4.");
System.out.println(// Output ending border line.
"****************************************************************");
}
}
scanner.close();
}
}
At this point I decided to add the attribute “kind” to my item to be able to know what kind of
item it is in the catalog. Then, I tested the program for adding different kinds of item to the
After adding 3 items I deleted the first item, and then viewed the remaining 2 items.
T was unable to solve some problems like the error I encountered in entering the title and author
CATALOG CLASS
import java.util.ArrayList;
import java.util.List;
/*This class named catalog,
* hold all methods for the generic library
*/
public class Catalog<T> {
// Generic method for creating an ArrayList.
private List<T> items = new ArrayList<>();
// Generic method for adding an item to ArrayList.
public void addItem(T item) {
items.add(item);
}
// Generic method for removing an item from ArrayList.
public void removeItem(T item) {
items.remove(item);
}
}
}
LIBRARYITEM CLASS
LIBRARYSYTEM CASS
import java.util.Scanner;;
/* The library class LibrarySystem handles the user interface.
* The command GUI of the library program.
* It contains the main part of the program.
*/
public class LibrarySystem {
public static void main(String[] args) {
Catalog<LibraryItem> catalog = new Catalog<>();
// a scanner is created to wait for user input
Scanner scanner = new Scanner(System.in);
/*This is the default menu of the program to guide the user
* by instruction on what is required to keep the program
* Functional
*/
System.out.println(
"**************Welcome to Alexander ceneric Library!*****************");
System.out.println(
" How Can We Help You? ");
System.out.println(
" Select From menu Options: ");
ystem.out.println(
"******************************************************************
*******");
// choice is refreshed for fresh input at the main Menu
int choice = 0;
// The 4 options available for a user to choose from.
while (choice != 4) {
System.out.println("1. Add Item to Library");
System.out.println("2. Remove Item from Library");
System.out.println("3. View Catalog of Item(s) in Library");
System.out.println("4. Exit Library");
// Instruction for the next step which is to choose an option.
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
// Definition of what each choice made by a user will process.
switch (choice) {
//case 1 is for adding an item to the library catalog
case 1:
System.out.print("Enter item ID: ");// inform user to enter book or
DvD ID
int itemID = scanner.nextInt(); //Scanner to collect book or DvD ID
System.out.print("Enter kind: "); // inform user to enter book or DvD
kind
String kind = scanner.next(); //Scanner to collect book or DvD kind
System.out.print("Enter title: "); // inform user to enter book or DvD
title
String title = scanner.next(); //Scanner to collect book or DvD Title
System.out.print("Enter author: ");// inform user to enter book or DvD
Author
String author = scanner.next();//Scanner to collect book or DvD
Author
LibraryItem newItem = new
LibraryItem(itemID, kind, title, author);
catalog.addItem(newItem); // Adds full item details to catalog
System.out.println(// Output beginning border line.
"****************************************************************");
System.out.println(
"Item added!"); // check to know expected stage of program.
System.out.println(// Output ending border line.
"****************************************************************");
break;
//case 2 is for removing item(s) from the library catalog
case 2:
System.out.print("Enter item ID to remove: ");
int idToRemove = scanner.nextInt();
// key to remove an item is the itemID, it searches for
//a particular ID and removes the item.
catalog.getItems().removeIf(item -> item.itemID == idToRemove);
"****************************************************************");
System.out.println(
"Item Deleted!"); // check to know expected stage of program.
System.out.println(// Output ending border line.
"****************************************************************");
break;
"****************************************************************");
for (LibraryItem item : catalog.getItems()) {
System.out.println("ID: " + item.itemID+", Kind: "+item.kind+","
+" Title: " + item.title + ", Author: " + item.author );
}
System.out.println( // Output ending border line.
"****************************************************************");
break;
case 4:
System.out.println("Exiting...");
break;
// Default message to handle wrong input option front user.
// Instruct user to enter only expected numbers 1 to 4.
default:
System.out.println(// Output beginning border line.
"***************************************************************");
System.out.println("Invalid choice. Please try again.");
System.out.println("ENTER BETWEEN 1 TO 4.");
System.out.println(// Output ending border line.
"****************************************************************");
}
}
scanner.close();
}
}
Reference
Eck, D. J. (2022). Introduction to programming using java version 9, JavaFX edition. Licensed
under CC 4.0. Use the Introduction to Programming Using Java for pdf version of the file.