Question: 1. GroceryItem class The GroceryItem class represents one item ordered from a grocery store. The GroceryItem has three instance variables: name (of type String), price
1. GroceryItem class
The GroceryItem class represents one item ordered from a grocery store. The GroceryItem has three instance variables: name (of type String), price (of type double) and quantity (of type int).
Implement the following methods for the GroceryItem class:
- GroceryItem (String name, double price, int qty) - A constructor to initialize all instance variables. A value for each instance variable will be passed as a parameter to the constructor. The parameters must be in the order name, price and quantity.
- Getters for all instance variables.
- Setter methods for each instance variable.
- boolean equals(Object item): a method to test the equality of two items. Two items are considered to be equal if they have the same name. The price and quantity should not be compared. The comparison must be case insensitive.
- String toString(): a method that returns a nicely formatted string description of the item. All instance variables should be displayed, separated by tabs (use "\t") and the entire GroceryItem should be displayed on one line. In addition to name, price and quantity, calculate the total value of the GroceryItem (price * quantity) and add it to the end of the string.
Rice $1.50 3 $4.50
Use the NumberFormat or DecimalFormat class to display prices with exactly two digits following the decimal place.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
