In this lab, you will demonstrate your understanding of classes and objects as w
ID: 3733306 • Letter: I
Question
In this lab, you will demonstrate your understanding of classes and objects as well as the use of an ArrayList. There are 2 service classes to be completed, a class that defines a simple object and a class that maintains an ArrayList. A Test Client class is provided. All classes require Javadoc documentation comments at the top to include a description of the program and an @author tag with your name. All public members require complete Javadoc documentation comments.
Click HERE to download the files needed for this Lab.
Service Classes
The CatalogItem class defines a single item in a store’s catalog of items available for purchase. Each item has an integer itemID, which identifies the item, a description, and a price value. All fields should be private, with accessor methods defined. The description and price fields should also define mutator methods. Provide a default constructor which sets the fields to a default value, an overloaded constructor which accepts values for all three fields and a copy constructor which creates a new object which is a copy of the parameter object. A toString method should be defined to return the itemID, description, and price,formatted in currency, as a single descriptive string. An equals method should be included. Two CatalogItem objects are considered equal when their three field values are the same.
The Catalog class will maintain an ArrayList of CatalogItem objects. The Catalog class should be capable of storing up to 100 CatalogItem objects in its list. However, at any given time, there is likely less than 100 CatalogItem objects. The list and all of its object elements should be maintained privately and not exposed to the client except through the methods provided.
The class's default constructor should instantiate an empty list of the maximum number of elements. Method descriptions are below:
The display method should display the list of CatalogItems currently stored in the ArrayList.
The add method adds a CatalogItem object to the ArrayList. There should be two versions of the add method: one should accept one parameter, a CatalogItem object to add to the list. The second add method should accept an itemId, description, and price for a CatalogItem object which will be created and then added to the list. Note: to ensure encapsulation, the CatalogItem object passed as a parameter to the add method must be copied before saving in the ArrayList.
The update method is used to change a CatalogItem object currently stored in the ArrayList. There are two versions of this method. Both versions accept an itemId as the first parameter. The second parameter is either a String value or a double value. The String value will be used to update the CatalogItem object’s descriptionfield while the double value will be used to update the price field.
The priceIncrease method should accept a percentage amount and increase the price field of all CatalogItem objects in the list.
The getCatalogItem method should accept an itemId as a parameter and return the matching CatalogItemfrom the ArrayList. If no matching CatalogItem is found, null should be returned.
The toString method should return the entire list as a String. For easy display, include the newline (' ') character in the String between the CatalogItems.
A static field should be maintained to reflect the current number of CatalogItems held in the ArrayList.
Client application
A Test Client has been created for you. The following technique is suggested for completing this lab. Start small and build on working code!
Create the CatalogItem class. Define all fields as private. Define the public methods. Compile and correct syntax errors.
Create the Catalog class. Define the static field and the private instance fields of the Catalog class. Implement the class constructor and the display method. Compile and correct syntax errors.
Comment out all the code in the Client’s main method other than Test 1. Compile and run. Make corrections as needed to the Catalog and/or CatalogItem class. Once Test 1 completes as expected, move on.
Implement the add methods of the Catalog class. Update the Test Client to use Tests 1 – 4.
Implement the update method of the Catalog class. Update the Test Client to include Test 5.
Implement the priceIncrease method of the Catalog class. Update the Test Client to include Test 6.
Implement the getCatalogItem method of the Catalog class. Update the Test Client to include Test 7.
Implement the toString method of the Catalog class. Update the Test Client to include Test 8.
Be certain your results match the results given. See results.txt file included.
Review your code. Refine the Javadoc documentation in the CatalogItem and Catalog classes.
/** Test Client for testing the Catalog class
* @author Professor Gregory
*/
public class Lab6
{
public static void main(String[] args)
{
// TEST 1
// Create catalog and test the static getCount method
System.out.println("Class has not been created. There are " + Catalog.count + " items.");
Catalog myCatalog = new Catalog();
System.out.println(" Catalog class created. ");
myCatalog.display();
/****** Move this line down to the next Test as you progress in completing the Lab
// TEST 2
// Test the add method (version 1)
CatalogItem item1 = new CatalogItem(123, "Pencils", 1.25);
CatalogItem item2 = new CatalogItem(234, "Crayons", 2.50);
CatalogItem item3 = new CatalogItem(567, "Paper", 5.00);
myCatalog.add(item1);
myCatalog.add(item2);
myCatalog.add(item3);
myCatalog.display();
// TEST 3
// Test the add method (version 2)
myCatalog.add(987,"Coloring Book",9.75);
myCatalog.add(876,"Magic Marker",3.75);
myCatalog.display();
//TEST 4
// Testing encapsulation - item 1 will be changed in client - should not change in Catalog
System.out.print(" Changed LOCAL item " + item1.toString() + " to " );
item1.setDescription("Pencils (10 count)");
System.out.println(item1.toString());
System.out.println("Change should NOT be reflected in the catalog!");
myCatalog.display();
// TEST 5
// Testing update methods
System.out.println(" Changed first item in the catalog");
myCatalog.update(item1.getItemId(),"Pencils (15 count)");
myCatalog.update(item1.getItemId(),1.75);
myCatalog.display();
// TEST 6
// Tesing price increase method
System.out.println(" Increasing prices by 10%");
myCatalog.priceIncrease(.10);
myCatalog.display();
// Price increase should not be reflected in local instance
System.out.println(" LOCAL item2 should contain original price " +
item2.toString());
// Test 7
// Test the getCatalogItem method
System.out.println(" Locating a CatalogItem");
CatalogItem cat = myCatalog.getCatalogItem(123);
if (cat != null)
System.out.println("Found item 123!");
else
System.out.println("Didn't find item 123!");
CatalogItem cat2 = myCatalog.getCatalogItem(123456);
if (cat2 != null)
System.out.println("Found item 123456!");
else
System.out.println("Didn't find item 123456!");
// TEST 8
// Test the toString() method of the Catalog class
System.out.println(" Here is the final list: " + myCatalog.toString());
****** REMOVE THIS LINE for your final test */
}
}
Resukts:
Class has not been created. There are 0 items.
Catalog class created.
The Catalog consists of 0 items.
The Catalog consists of 3 items.
Item: 123, Pencils. Price: $1.25
Item: 234, Crayons. Price: $2.50
Item: 567, Paper. Price: $5.00
The Catalog consists of 5 items.
Item: 123, Pencils. Price: $1.25
Item: 234, Crayons. Price: $2.50
Item: 567, Paper. Price: $5.00
Item: 987, Coloring Book. Price: $9.75
Item: 876, Magic Marker. Price: $3.75
Changed LOCAL item Item: 123, Pencils. Price: $1.25 to Item: 123, Pencils (10 count). Price: $1.25
Change should NOT be reflected in the catalog!
The Catalog consists of 5 items.
Item: 123, Pencils. Price: $1.25
Item: 234, Crayons. Price: $2.50
Item: 567, Paper. Price: $5.00
Item: 987, Coloring Book. Price: $9.75
Item: 876, Magic Marker. Price: $3.75
Changed first item in the catalog
The Catalog consists of 5 items.
Item: 123, Pencils (15 count). Price: $1.75
Item: 234, Crayons. Price: $2.50
Item: 567, Paper. Price: $5.00
Item: 987, Coloring Book. Price: $9.75
Item: 876, Magic Marker. Price: $3.75
Increasing prices by 10%
The Catalog consists of 5 items.
Item: 123, Pencils (15 count). Price: $1.93
Item: 234, Crayons. Price: $2.75
Item: 567, Paper. Price: $5.50
Item: 987, Coloring Book. Price: $10.73
Item: 876, Magic Marker. Price: $4.13
LOCAL item2 should contain original price
Item: 234, Crayons. Price: $2.50
Locating a CatalogItem
Found item 123!
Didn't find item 123456!
Here is the final list:
The Catalog consists of 5 items.
Item: 123, Pencils (15 count). Price: $1.93
Item: 234, Crayons. Price: $2.75
Item: 567, Paper. Price: $5.50
Item: 987, Coloring Book. Price: $10.73
Item: 876, Magic Marker. Price: $4.13
----jGRASP: operation complete.
Explanation / Answer
Explanation:
Please find the below mentioned CatalogItem and Catalog service classes. Also find the output based on the client class provided.
Please let me know if something needs to be changed.
Program:
CatalogItem
public class CatalogItem {
private int itemID;
private String description;
private double priceValue;
public CatalogItem() {
this.itemID = 0;
this.description = "";
this.priceValue = 0;
}
public CatalogItem(int itemID, String description, double priceValue) {
this.itemID = itemID;
this.description = description;
this.priceValue = priceValue;
}
public CatalogItem(CatalogItem catalogItem) {
this.itemID = catalogItem.itemID;
this.description = catalogItem.description;
this.priceValue = catalogItem.priceValue;
}
public int getItemId() {
return itemID;
}
public String getDescription() {
return description;
}
public double getPriceValue() {
return priceValue;
}
public void setDescription(String description) {
this.description = description;
}
public void setPriceValue(double priceValue) {
this.priceValue = priceValue;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CatalogItem other = (CatalogItem) obj;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (itemID != other.itemID)
return false;
if (Double.doubleToLongBits(priceValue) != Double
.doubleToLongBits(other.priceValue))
return false;
return true;
}
@Override
public String toString() {
return "Item: " + itemID + ", " + description + ". Price: $" + String.format("%.2f", priceValue);
}
}
Catalog
import java.util.ArrayList;
public class Catalog {
private ArrayList<CatalogItem> catalogItems;
public static int count;
public Catalog() {
catalogItems = new ArrayList<CatalogItem>();
count = 0;
}
public void add (CatalogItem catalogItem) {
if (count < 100) {
boolean flag = false;
for (int i=0;i<count;i++) {
if (catalogItems.get(i).equals(catalogItem)) {
flag = true;
break;
}
}
if (!flag) {
catalogItems.add(catalogItem);
count++;
} else {
System.out.println("Item already exists in catalog");
}
} else {
System.out.println("No more capacity to add catalog item");
}
}
public void add (int itemID, String description, double priceValue) {
if (count < 100) {
CatalogItem catalogItem = new CatalogItem(itemID, description, priceValue);
boolean flag = false;
for (int i=0;i<count;i++) {
if (catalogItems.get(i).equals(catalogItem)) {
flag = true;
break;
}
}
if (!flag) {
catalogItems.add(catalogItem);
count++;
} else {
System.out.println("Item already exists in catalog");
}
} else {
System.out.println("No more capacity to add catalog item");
}
}
public void update (int itemID, String description) {
for (int i=0;i<count;i++) {
CatalogItem catalogItem = catalogItems.get(i);
if (catalogItem.getItemId() == itemID) {
catalogItem.setDescription(description);
break;
}
}
}
public void update (int itemID, double priceValue) {
for (int i=0;i<count;i++) {
CatalogItem catalogItem = catalogItems.get(i);
if (catalogItem.getItemId() == itemID) {
catalogItem.setPriceValue(priceValue);
break;
}
}
}
public void priceIncrease(double percentageAmount ) {
for (int i=0;i<count;i++) {
CatalogItem catalogItem = catalogItems.get(i);
double newPriceValue = (catalogItem.getPriceValue() * percentageAmount) + catalogItem.getPriceValue();
catalogItem.setPriceValue(newPriceValue);
}
}
public CatalogItem getCatalogItem (int itemID) {
for (int i=0;i<count;i++) {
CatalogItem catalogItem = catalogItems.get(i);
if (catalogItem.getItemId() == itemID) {
return catalogItem;
}
}
return null;
}
public void display() {
if (count > 0) {
System.out.println(" The Catalog consists of "+count+" items.");
for (int i=0;i<count;i++) {
CatalogItem catalogItem = catalogItems.get(i);
System.out.println(catalogItem);
}
} else {
System.out.println(" The Catalog consists of 0 items.");
}
}
@Override
public String toString() {
String output = " The Catalog consists of "+count+" items. ";
for (int i=0;i<count;i++) {
CatalogItem catalogItem = catalogItems.get(i);
output = output + catalogItem + " ";
}
return output;
}
}
Output:
Class has not been created. There are 0 items.
Catalog class created.
The Catalog consists of 0 items.
The Catalog consists of 3 items.
Item: 123, Pencils. Price: $1.25
Item: 234, Crayons. Price: $2.50
Item: 567, Paper. Price: $5.00
The Catalog consists of 5 items.
Item: 123, Pencils. Price: $1.25
Item: 234, Crayons. Price: $2.50
Item: 567, Paper. Price: $5.00
Item: 987, Coloring Book. Price: $9.75
Item: 876, Magic Marker. Price: $3.75
Changed LOCAL item Item: 123, Pencils. Price: $1.25 to Item: 123, Pencils (10 count). Price: $1.25
Change should NOT be reflected in the catalog!
The Catalog consists of 5 items.
Item: 123, Pencils (10 count). Price: $1.25
Item: 234, Crayons. Price: $2.50
Item: 567, Paper. Price: $5.00
Item: 987, Coloring Book. Price: $9.75
Item: 876, Magic Marker. Price: $3.75
Changed first item in the catalog
The Catalog consists of 5 items.
Item: 123, Pencils (15 count). Price: $1.75
Item: 234, Crayons. Price: $2.50
Item: 567, Paper. Price: $5.00
Item: 987, Coloring Book. Price: $9.75
Item: 876, Magic Marker. Price: $3.75
Increasing prices by 10%
The Catalog consists of 5 items.
Item: 123, Pencils (15 count). Price: $1.93
Item: 234, Crayons. Price: $2.75
Item: 567, Paper. Price: $5.50
Item: 987, Coloring Book. Price: $10.73
Item: 876, Magic Marker. Price: $4.13
LOCAL item2 should contain original price
Item: 234, Crayons. Price: $2.75
Locating a CatalogItem
Found item 123!
Didn't find item 123456!
Here is the final list:
The Catalog consists of 5 items.
Item: 123, Pencils (15 count). Price: $1.93
Item: 234, Crayons. Price: $2.75
Item: 567, Paper. Price: $5.50
Item: 987, Coloring Book. Price: $10.73
Item: 876, Magic Marker. Price: $4.13
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.