1. Write these classes, as described below:
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
name=Crackers, cost=$4.50, weight=2.25
4. ItemTest class
5. RefrigeratedItem class
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:
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. |