Implement a class named InventoryItem that contains these instance variables: A private String data field named itemName
Question:
Implement a class named InventoryItem that contains these instance variables:
- A private String data field named itemName that gives the name of the item. Its default value is "TBD".
- A private int data field named sku. An SKU (stock keeping unit) is like an ID number for an item. Its default value is zero.
- A private double data field named price that stores the price for the item. The default value is 0.0.
- A private int data field named quantity that tells how many items are in stock. The default value is 0.
Implement the following methods, all of which must be public:
- A no-argument constructor that creates an inventory item with default values.
- A three-argument constructor that creates an inventory item with the specified name, SKU, and price (in that order) with the default quantity.
- A four-argument constructor that creates an inventory item with the specified name, SKU, price, and quantity (in that order).
- The accessor and mutator methods (getters and setters) for all the instance fields.
- A method named getTotalValue() which returns, as a double, the item price times its quantity.
- A method named toString() which returns a string containing the item's name, SKU, price, and quantity, properly labeled. It must not include the total value; that is not an attribute of the object.
- A static method named compare() which takes as its arguments one InventoryItem objects. This method returns:
- -1 if the total value of the first item is less than the total value of the second item
- 0 if the total value of the first item equals the total value of the second item
- 1 if the total value of the first item is greater than the total value of the second item
Note: if the constructors and setters are given a negative price or quantity, they must convert it to a positive number. Hint - use Math.abs().
Draw a UML diagram for this class. You can draw it using pencil and paper and upload a picture of the result. You do not have to use a drawing program unless you want to.
The TestInventory class
This class will contain your main() method and will do the following:
- Create four InventoryItems:
- emptyItem, using the no-argument constructor
- staplers, which has a name of "Stapler, Red", SKU of 91745, and price of $7.89, using the three-argument constructor
- pencils, which has a name "Pencil, #2", SKU of 73105, price of $0.35, and quantity 210
- notebooks, which has a name "Notebook, Spiral", SKU of 68332, price of $2.57, and quantity 38
- Display each of the items (using the toString() method) and its total value.
- Compare pencils to notebooks and prints out which one has greater total value. You must call the compare() method as part of this step . Output must use the inventory item's itemName property.
When You Finish
Please put both classes into one file.Only one of them (TestInventory) will be public. Upload both the Java source code and the UML diagram.
Here is what output from the program might look like. Your output does not have to look exactly like this, but it must reflect the same information.
TBD [SKU 0]: 0 at $0.00 each
Total value: $0.00
Stapler, Red [SKU 91745]: 0 at $7.89 each
Total value: $0.00
Pencil, #2 [SKU 73105]: 210 at $0.35 each
Total value: $73.50
Notebook, Spiral [SKU 68332]: 38 at $2.57 each
Total value: $97.66
Notebook, Spiral has greater value than Pencil, #2
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates