This quiestions involves GroceryChains which store a list of GroceryStore objects. You are to complete the following tasks. /** * A class for a grocery store. Complete the following tasks: * * - Implement a constructor that takes in initial values for each item type * - Add a method that adds inventory of a given type * - Add a method that returns the inventory of a given type */ public class GroceryStore { // A set of possible items at the grocery store public enum GroceryItem {LETTUCE, TOMATO, CUCUMBER} private int lettuceInventory; private int tomatoInventory; private int cucumberInventory; private String name; // Implement a grocery store constructor which takes in a store name, and initial // amounts for each of the inventory types public GroceryStore(String name, int lettuce, int tomato, int cucumber) { //-----------Start below here. To do: approximate lines of code = 4 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } // Adds the given amount of the given type to the given inventory // You can assume the given amount is always non-negative public void addInventory(GroceryItem type, int amount) { //-----------Start below here. To do: approximate lines of code = 6 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } // Gets the inventory of the given type public int getInventory(GroceryItem type) { //-----------Start below here. To do: approximate lines of code = 7 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } } ______________________________________________________ import java.util.ArrayList; /** * A class for a grocery chain which stores an array list of stores. * * - Add the constructor * - Add a method that adds a grocery store to the chain * - Implement the getTotalInventory method */ public class GroceryChain { private ArrayList stores; private String name; // Add a constructor which takes in a name for the grocery chain and // initializes ArrayList //-----------Start below here. To do: approximate lines of code = 3 //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. // Create a method called addGroceryStore which takes in a GroceryStore // and adds it to the ArrayList //-----------Start below here. To do: approximate lines of code = 2 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. // Calculate the total inventory of the given item over all stores // in the ArrayList public int getTotalInventory(GroceryStore.GroceryItem type) { //-----------Start below here. To do: approximate lines of code = 4 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } } _________________________________________ public class GroceryTester { public static void main(String[] args) { GroceryStore store1 = new GroceryStore("MLG Loblaws", 40, 150, 75); GroceryStore store2 = new GroceryStore("Dundas Square Loblaws", 30, 100, 50); GroceryStore store3 = new GroceryStore("West End Loblaws", 50, 80, 60); // Tests get inventory method System.out.println("The inventory of tomato at MLG Loblaws is " + store1.getInventory(GroceryStore.GroceryItem.TOMATO)); System.out.println("Expected:\nThe inventory of tomato at MLG Loblaws is 150"); System.out.println("The inventory of lettuce at Dundas Square Loblaws is " + store2.getInventory(GroceryStore.GroceryItem.LETTUCE)); System.out.println("Expected:\nThe inventory of lettuce at Dundas Square Loblaws is 30"); System.out.println("The inventory of cucumber at West End Loblaws is " + store3.getInventory(GroceryStore.GroceryItem.CUCUMBER)); System.out.println("Expected:\nThe inventory of cucumber at West End Loblaws is 60"); // Tests the add inventory method store1.addInventory(GroceryStore.GroceryItem.TOMATO, 10); System.out.println("The inventory of tomato at MLG Loblaws is " + store1.getInventory(GroceryStore.GroceryItem.TOMATO)); System.out.println("Expected:\nThe inventory of tomato at MLG Loblaws is 160"); store3.addInventory(GroceryStore.GroceryItem.CUCUMBER, 10); System.out.println("The inventory of cucumber at West End Loblaws is " + store3.getInventory(GroceryStore.GroceryItem.CUCUMBER)); System.out.println("Expected:\nThe inventory of cucumber at West End Loblaws is 70"); // Creates stores GroceryChain loblaws = new GroceryChain("loblaws"); loblaws.addGroceryStore(store1); loblaws.addGroceryStore(store2); loblaws.addGroceryStore(store3); // Tests the getTotalInventory method System.out.println("The total inventory of lettuce is " + loblaws.getTotalInventory(GroceryStore.GroceryItem.LETTUCE)); System.out.println("Expected:\nThe total inventory of lettuce is 120");
This quiestions involves GroceryChains which store a list of GroceryStore objects. You are to complete the following tasks. /** * A class for a grocery store. Complete the following tasks: * * - Implement a constructor that takes in initial values for each item type * - Add a method that adds inventory of a given type * - Add a method that returns the inventory of a given type */ public class GroceryStore { // A set of possible items at the grocery store public enum GroceryItem {LETTUCE, TOMATO, CUCUMBER} private int lettuceInventory; private int tomatoInventory; private int cucumberInventory; private String name; // Implement a grocery store constructor which takes in a store name, and initial // amounts for each of the inventory types public GroceryStore(String name, int lettuce, int tomato, int cucumber) { //-----------Start below here. To do: approximate lines of code = 4 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } // Adds the given amount of the given type to the given inventory // You can assume the given amount is always non-negative public void addInventory(GroceryItem type, int amount) { //-----------Start below here. To do: approximate lines of code = 6 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } // Gets the inventory of the given type public int getInventory(GroceryItem type) { //-----------Start below here. To do: approximate lines of code = 7 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } }
______________________________________________________
import java.util.ArrayList; /** * A class for a grocery chain which stores an array list of stores. * * - Add the constructor * - Add a method that adds a grocery store to the chain * - Implement the getTotalInventory method */ public class GroceryChain { private ArrayList<GroceryStore> stores; private String name; // Add a constructor which takes in a name for the grocery chain and // initializes ArrayList //-----------Start below here. To do: approximate lines of code = 3 //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. // Create a method called addGroceryStore which takes in a GroceryStore // and adds it to the ArrayList //-----------Start below here. To do: approximate lines of code = 2 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. // Calculate the total inventory of the given item over all stores // in the ArrayList public int getTotalInventory(GroceryStore.GroceryItem type) { //-----------Start below here. To do: approximate lines of code = 4 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } }
_________________________________________
public class GroceryTester { public static void main(String[] args) { GroceryStore store1 = new GroceryStore("MLG Loblaws", 40, 150, 75); GroceryStore store2 = new GroceryStore("Dundas Square Loblaws", 30, 100, 50); GroceryStore store3 = new GroceryStore("West End Loblaws", 50, 80, 60); // Tests get inventory method System.out.println("The inventory of tomato at MLG Loblaws is " + store1.getInventory(GroceryStore.GroceryItem.TOMATO)); System.out.println("Expected:\nThe inventory of tomato at MLG Loblaws is 150"); System.out.println("The inventory of lettuce at Dundas Square Loblaws is " + store2.getInventory(GroceryStore.GroceryItem.LETTUCE)); System.out.println("Expected:\nThe inventory of lettuce at Dundas Square Loblaws is 30"); System.out.println("The inventory of cucumber at West End Loblaws is " + store3.getInventory(GroceryStore.GroceryItem.CUCUMBER)); System.out.println("Expected:\nThe inventory of cucumber at West End Loblaws is 60"); // Tests the add inventory method store1.addInventory(GroceryStore.GroceryItem.TOMATO, 10); System.out.println("The inventory of tomato at MLG Loblaws is " + store1.getInventory(GroceryStore.GroceryItem.TOMATO)); System.out.println("Expected:\nThe inventory of tomato at MLG Loblaws is 160"); store3.addInventory(GroceryStore.GroceryItem.CUCUMBER, 10); System.out.println("The inventory of cucumber at West End Loblaws is " + store3.getInventory(GroceryStore.GroceryItem.CUCUMBER)); System.out.println("Expected:\nThe inventory of cucumber at West End Loblaws is 70"); // Creates stores GroceryChain loblaws = new GroceryChain("loblaws"); loblaws.addGroceryStore(store1); loblaws.addGroceryStore(store2); loblaws.addGroceryStore(store3); // Tests the getTotalInventory method System.out.println("The total inventory of lettuce is " + loblaws.getTotalInventory(GroceryStore.GroceryItem.LETTUCE)); System.out.println("Expected:\nThe total inventory of lettuce is 120");

Step by step
Solved in 3 steps









