1. Write these classes, as described below:

  • Item
  • ItemTest
  • RefrigeratedItem
  • Warehouse
  • WarehouseTest

Class diagram: see image.

2. Overview - As the class diagram on the right shows, a Warehouse contains an array of Items (and RefrigeratedItems).

3. Item class

  • An Item has a name and a weight which are supplied in the constructor and also have getters.
  • The cost method returns the cost of the item which is 2 times the weight.
  • Item overrides toString to return a string with a format exactly like the example shown below (2 decimal places, $ on cost, etc):
name=Crackers, cost=$4.50, weight=2.25

4. ItemTest class

  • testItemCost - tests the cost method.
  • testItemToString - test the toString method

5. RefrigeratedItem class

  • A RefrigeratedItem also has a temperature which is supplied in the constructor and has a getter.
  • A RefrigeratedItem has an additional constructor that converts an Item into a RefrigeratedItem. Thus, the constructor accepts an Item and a temperature and uses the name and weight of the Item to initialize this RefrigeratedItem. Hint: this constructor should call the first constructor with a statement like: super(item.getName(), item.getWeight(), temp).
  • The cost of a RefrigeratedItem is 2 times the weight plus: (100-temp)*0.1. For example: if weight=3.0 and temp=10.0, then the cost that is returned is: 15.0 (=2*3.0 + (100-10.0)*0.1).
  • RefrigeratedItem overrides toString to return a string with a format exactly like the example shown below (2 decimal places, $ on cost, etc):
name=Ice Cream, cost=$10.00, weight=1.00, temp=20.00 degrees

6. Return to the ItemTest class. We will use this class to also test the RefrigeratedItem class:

  • testRefrigeratedItem - test the first constructor.
  • testRefrigeratedItem2 - test the second constructor.
  • testRefrigeratedItemCost - test the cost method.
  • testRefrigeratedItemToString - test the toString method.

7. Warehouse class

Field Description
items A protected array of size 10 of Item objects
numItems The number of items in the array.

Method Description
Warehouse() A no-arg constructor that does nothing.
addItem(item: Item) Adds item to the items in the next available position, provided a position is available
getItem(i: int): Item) Returns the Item in items at index i, or null if the index is invalid
getItem(name:String): Item Returns the Item in items with name exactly the same as the argument, or null if it does not exist. Hint: loop over the items and look for a name that matches.
getNumItems():int Returns the number of items in items
getAverageTemp():double Returns the average temperature of the RefrigeratedItems
getRefrigeratedItems(): RefrigeratedItem[] Returns an array of the refrigerated items in items. Hint: you will need to count the number of refrigerated items first, in order to create the array to return (see class notes)
getTotalCost():double Returns the sum of the cost of all the items in items
getTotalCostRefrigerated():double Returns the sum of the cost of all the RefrigeratedItems in items
removeItem(i:int):Item Removes and returns the Item in items at index i, or null if the index is invalid.
removeItem(name:String):Item Removes and returns the Item in items whose name exactly matches the argument, or null if the item was not found.
toString():String Returns a string with all the items in the ArrayList. It should look exactly as shown below. Hint: simply loop over items and call toString on each one:

name=Crackers, cost=$4.50, weight=2.25 name=Apples, cost=$7.00, weight=3.50
name=Ice Cream, cost=$10.00, weight=1.00, temp=20.00 degrees name=Soda, cost=$3.00, weight=1.50
name=Frozen Veggies, cost=$16.00, weight=3.00, temp=0.05 degrees
...

8. WarehouseTest - Write the following test methods described below. Each test method should have a comment explaining what you are testing. Each test method should be stand-alone. Each test method should display nicely formatted output to the console that shows that the method works. main should simply call each of the test methods one after the other. Do not expect full credit for this unless you have written thorough, organized, self-documenting test code that produces meaningful output.

Method Description
testAddItem Add one item and check the number of items to verify
testAdditem_Multiple Add three items and check the number of items to verify. At least one should be a refrigerated item
testGetItem_WithIndex Add three items and retrieve the one at position 1
testGetItem_WithName Add three items and try to find one with a name that exists
testGetAverageTemp Add 5 items, 3 of which are refrigerated. Verify the average temperature
testGetRefrigeratedItems Add 5 items, 3 of which are refrigerated. Verify that the 3 are returned in array
testGetTotalCost Add 5 items, 3 of which is refrigerated. Verify the total cost.
testGetTotalCostRefrigerated Add 5 items, 3 of which are refrigerated. Verify the total cost of the 3 refrigerated items
testRemoveItem_WithIndex Add 5 items, 3 of which are refrigerated. Remove the one at position 2 and verify: the item is returned, and the number of items is decremented
testRemoveItem_WithName Add 5 items, 3 of which are refrigerated. Remove one with a name that exists and verify: the item is returned, and the number of items is decremented
testToString Add 5 items, 3 of which are refrigerated. Verify that the result is correct.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.