In: Computer Science
Write a class named GroceryList that represents a list of items to buy from the market, and another class named GroceryItemOrder that represents a request to purchase a particular item in a given quantity (example: four boxes of cookies). It also has client class called GroceryClient which creates objects of GroceryList and GroceryItemOrder. (For this assignment, you will have to submit 3 .java files: one for each class and 3 .class files associated with these .java files. So in total you will be submitting 6 files for this problem.) The GroceryList class should use an array field to store the grocery items and to keep track of its size (number of items in the list so far). Assume that a grocery list will have no more than 10 items. A GroceryList object should have the following methods: public GroceryList() Constructs a new empty grocery list. public void add(GroceryItemOrder item) Adds the given item order to this list if the list has fewer than 10 items. public double getTotalCost() Returns the total sum cost of all grocery item orders in this list. The GroceryItemOrder class should store an item quantity and a price per unit. A GroceryItemOrder object should have the following methods: public GroceryItemOrder(String name, int quantity, double pricePerUnit) Constructs an item order to purchase the item with the given name, in the given quantity, which costs the given price per unit. public double getCost() Returns the total cost of this item in its given quantity. For example, four boxes of cookies that cost 2.30 per unit have a total cost of 9.20. public void setQuantity(int quantity) Sets this grocery item’s quantity to be the given value. The GroceryClient class has static main(..) method and initializes objects for GroceryItemOrder class by adding different items along with their unit item price and available quantity and then creates a GroceryList class object and adds the required grocery list using add() method and determines the total cost using getTotalCost() method and displays it on the console.
/** * @fileName GroceryList.java * @author ravi * @since 1/2/17 */ public class GroceryList { GroceryItemOrder[] groceryList; public static int count; /** * This is default constructor */ public GroceryList() { this.groceryList = new GroceryItemOrder[10]; } /** * Adding item to the groceryList * @param item */ public void add(GroceryItemOrder item) { if (count < 10) { groceryList[count++] = item; } } /** * Give total cast for all item for * @return */ public double getTotalCost() { double total = 0; for (GroceryItemOrder item : this.groceryList) { if (item != null) { total += item.getCost(); } } return total; } }
/** * @fileName GroceryItemOrder.java * @author * @since 1/2/17 */ public class GroceryItemOrder { String name; int quantity; double pricePerUnit; /** * This is parametrized constructor this will initialize the instance of GroceryItemOrder class * @param name * @param quantity * @param pricePerUnit */ public GroceryItemOrder(String name, int quantity, double pricePerUnit) { this.name = name; this.quantity = quantity; this.pricePerUnit = pricePerUnit; } /** * This will return the coast fro all quantity for item * @return */ public double getCost() { return this.quantity * this.pricePerUnit; } /** * This will set the quantity of item * @param quantity */ public void setQuantity(int quantity) { this.quantity = quantity; } }
/** * @fileName GroceryClient.java * @author * @since 1/2/17 */ package groceryList; public class GroceryClient { public static void main(String[] args) { GroceryItemOrder item1 = new GroceryItemOrder("cookies",4,2.30); GroceryItemOrder item2 = new GroceryItemOrder("chocolate",15,15.30); GroceryItemOrder item3 = new GroceryItemOrder("icecream",10,40.30); GroceryItemOrder item4 = new GroceryItemOrder("Apple",20,30.30); GroceryItemOrder item5 = new GroceryItemOrder("orange",20,20.30); GroceryItemOrder item6 = new GroceryItemOrder("kivy",4,45.30); GroceryItemOrder item7 = new GroceryItemOrder("banana",50,5.30); GroceryItemOrder item8 = new GroceryItemOrder("salt",4,8.30); GroceryItemOrder item9 = new GroceryItemOrder("lemon",4,3.30); GroceryList groceryList = new GroceryList(); groceryList.add(item1); groceryList.add(item2); groceryList.add(item3); groceryList.add(item4); groceryList.add(item5); groceryList.add(item6); groceryList.add(item7); groceryList.add(item8); groceryList.add(item9); System.out.println("Total Cost :"+groceryList.getTotalCost()); } }
output: