You are shopping for a grocery item. Supermarkets often give prices for a group of items, such
Question:
You are shopping for a grocery item. Supermarkets often give prices for a group of items, such as 5 for $1.25 or 3 for $1, instead of the price for one item. The possibility is if they price apples at 5 for $1.25, you will buy 5 apples instead of 2. But 5 for $1.25 is $0.25 each. So, if you buy 2 apples, they charge you only $0.50.
Write a Java class called Grocery that will handle the total price. Use the following UML diagram.
Class Grocery |
Attributes nameOfItem : String unitCost: double totalCost: double |
Methods Grocery(String item) processItem(int groupCount, double groupPrice, int quantity) : void display() : void |
The names of the variable and methods indicate their functionality.
- Use the constructor of class to initialize the name of item to purchase.
- Use the processItem method to keep track of the total price.
- The total price of the purchase is calculated here.
- Use the display method to print the item name, unit cost and total price to the console.
In a tester class that holds a main method, perform the following tasks
- Create a Scanner object.
- Use the appropriate Scanner method/calls to take name of item.
- Create a Grocery object.
- Use the appropriate Scanner method to take group count and group price following by quantity.
- Call processItem method.
- Call display method.
Your output should be similar to this:
Enter name of item you are purchasing:
cucumbers
Enter price of item as two numbers.
For example, 4 for $2.99 is entered as
4 2.99
Enter price of item as two numbers, now:
6 8.47
Enter number of items purchased:2
You bought: cucumbers
Cost each: $1.41
Total cost: $2.82
Statistical Techniques in Business and Economics
ISBN: 978-0078020520
16th edition
Authors: Douglas Lind, William Marchal