Menu

[Solved]Write Class Groceryitemorder Represents Request Purchase Particular Item Given Quantity Ex Q37211599

Write a Class GroceryItemOrder that representsa request to purchase a particular item in a given quantity(example: 6 Bananas).

A GroceryItemOrder classshould store an item name, item quantity and a price per unit.

So you need three data fields:

public String name;

public int quantity;

public double pricePerUnit;

A GroceryItemOrder object should have thefollowing methods:

public GroceryItemOrder(String name, int quantity,double pricePerUnit)

Constructs an item order to purchase the item with the givenname, in the given quantity, which costs the given price perunit

public double getCost()

returns the total cost of this item in its given quantity. Forexample, 6 Bananas that cost 3.24 per unit have a total cost of19.44 (which is outrageous for bananas – but these are specialbananas)

public void setQuanity(int quantity)

sets this grocery item’s quantity to be the given value

public String toString()

returns the string representation of the item.

The String should be formatted to report the quantity, the name,and price per Unit:

6 of Bananas at cost of $3.24 each.

To get the double to be formatted you will need to use Java’sNumberFormat class, (you will need to import java.text.*) and usethe command:

NumberFormat df = NumberFormat.getCurrencyInstance();

//I am just calling it df – you don’t have to

Then to format a double with it:

double number = 32.546723;

System.out.println(df.format(number));//prints$32.55

Next, write a class named GroceryList thatrepresents a list of items to buy from the store. You will need theGroceryItemOrder class and you will create anarray of the type GroceryItemOrder to store thegrocery items and to keep track of its size (the number of items inthe list so far). Assume that a grocery list will never have morethan 10 items (This means you should use aconstant).

               public static final int MAX_ITEMS = 10;

       private GroceryItemOrder[]items;

       private int numItems;

A GroceryList object should have the following methods:

   public GroceryList() {

        items = newGroceryItemOrder[MAX_ITEMS];

        numItems = 0;

   }

This constructs an empty list.

public void printList()

this prints the items in the list to the console

public int getMaxLengthOfList()

returns the maximum length of the grocery list

public int getNumItems()

returns the number of items on the grocery list

public void add(GroceryItemOrder item)

Adds the given item order to the list if the list has fewer than10 items

public double getTotalCost()

returns the total sum cost of all of grocery item orders in thislist.

*************************************************************

I have provided a client class,MakeAGroceryList.java, that will createGroceryLists and will populate them with GroceryItemOrders. Thisclass has the main method. It is this class that you will run. Whenyou run it with your classes written correctly you will get outputthat will be similar to that seen below. It will not be exactbecause the client uses a pseudo-random number for the quantity andcost.

8 of Amazeballs at cost of $15.61 each.

7 of Squeekies at cost of $5.00 each.

2 of Cahoots at cost of $9.51 each.

11 of Hats at cost of $4.06 each.

1 of Yops at cost of $17.07 each.

11 of Oompa Loompas at cost of $29.18 each.

3 of Yurtles at cost of $22.42 each.

8 of BluGold Mittens at cost of $19.13 each.

2 of Bananas at cost of $21.49 each.

2 of Sporks at cost of $28.67 each.

The total of this list is: $882.28

9 of Borfins at cost of $21.65 each.

The total of this smaller list is: $194.82

You will also notice rounding errors! What you display isrounded, so your totals may be off a few cents.

Expert Answer


Answer to Write a Class GroceryItemOrder that represents a request to purchase a particular item in a given quantity (example: 6 B… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *