0% found this document useful (0 votes)
48 views13 pages

CS1103 Assignment 6

The document outlines the requirements and implementation details for a generic library catalog in Java, focusing on the use of generics for flexibility and reusability. It describes the creation of a Catalog class to manage library items, a LibraryItem class to define item attributes, and a LibrarySystem class to provide a command-line interface for user interaction. Additionally, it emphasizes the importance of error handling and testing throughout the development process.

Uploaded by

alexcreationz
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)
48 views13 pages

CS1103 Assignment 6

The document outlines the requirements and implementation details for a generic library catalog in Java, focusing on the use of generics for flexibility and reusability. It describes the creation of a Catalog class to manage library items, a LibraryItem class to define item attributes, and a LibrarySystem class to provide a command-line interface for user interaction. Additionally, it emphasizes the importance of error handling and testing throughout the development process.

Uploaded by

alexcreationz
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/ 13

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:

1. Generic Catalog Class:

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.

2. Library Item Class:

a. Create a generic LibraryItem class with attributes such as title, author,


and itemID

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.

b. Implement error handling to manage scenarios such as attempting to remove


a non-existent item.

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

// Generic method for getting and items from


//the catalog and displaying it for user to view.
public List<T> getItems() {
return items;

}
}

The librartItem class was created to hold attributes of items in the library.

LIBRARYITEM CLASS

/*This Class is for defining attributes of item


* to be added to the catalog*/
public class LibraryItem {
/*Declaring of item properties
* Every item in library has an itemID which is an integer data type.
* Every item in library has a title which is a String data type.
* Every item in library has an author which is a String data type.
*/
int itemID;
String title;
String author;

// format for storing library items.


public LibraryItem(int itemID, String title, String author) {
// creating the local parameters to hold data.
this.itemID = itemID;
this.title = title;
this.author = author;

}
}

The LibrarySystem class is the class containing the main and acts as the user interface for

running implementing the operations throiuhgh a simple command line.

LIBRARYSYSTEM CLASS

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

System.out.println(// Output beginning border line.


"****************************************************************");
System.out.println(
"Item Deleted!"); // check to know expected stage of program.
System.out.println(// Output ending border line.

"****************************************************************");
break;

//case 3 is for viewing items in the library catalog


case 3:
System.out.println("Catalog Items:");
System.out.println(// Output beginning border line.

"****************************************************************");
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

library. Below is my screen shoot for adding items to the library.

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

names as the scanner will only accept 1 string at a time.

My complete code at last was as follows

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

// Generic method for getting and items from


//the catalog and displaying it for user to view.
public List<T> getItems() {
return items;

}
}

LIBRARYITEM CLASS

/*This Class is for defining attributes of item


* to be added to the catalog*/
public class LibraryItem {
/*Declaring of item properties
* Every item in library has an itemID which is an integer data type.
* * Every item in library has at kind which is an String data type.
* Every item in library has a title which is a String data type.
* Every item in library has an author which is a String data type.
*/
int itemID;
String kind;
String title;
String author;
// format for storing library items.
public LibraryItem(int itemID, String kind, String title, String author) {
// creating the local parameters to hold data.
this.itemID = itemID;
this.kind = kind;
this.title = title;
this.author = author;
}

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(// Output beginning border line.

"****************************************************************");
System.out.println(
"Item Deleted!"); // check to know expected stage of program.
System.out.println(// Output ending border line.

"****************************************************************");
break;

//case 3 is for viewing items in the library catalog


case 3:
System.out.println("Catalog Items:");
System.out.println(// Output beginning border line.

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

You might also like