Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 stores 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 objects 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 Clients 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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

3rd Edition

978-1119907466

More Books

Students also viewed these Databases questions

Question

Would it be possible for a firm to have a negative cash cycle? How?

Answered: 1 week ago

Question

plan how to achieve impact in practice from your research;

Answered: 1 week ago