Question: You will write a system to manage a grocery store. The initial store inventory is read from a file. Orders are read from file. The

You will write a system to manage a grocery store. The initial store inventory is read from a file. Orders are read from file. The system stores the inventory, processes the orders, tracks which items are out of stock, and sorts the inventory list using two different algorithms.

Classes:

  • GroceryManager
  • GroceryOrder
  • GroceryItem
  • Dairy subclass of GroceryItem
  • Produce subclass of GroceryItem
  • Meat subclass of GroceryItem
  • GroceryException
  • GroceryDriver provided to you. You do not need to modify it.

The driver runs through the following scenario:

  1. Stock the store by reading inventory from a file
  2. Print inventory
  3. Read the orders
  4. Process orders
  5. Sort by name, print inventory
  6. Sort by price, print inventory
  7. Print restocking list

Concepts used:

  1. Inheritance
  2. Generics
  3. Exception Handling
  4. HashSet
  5. Insertion Sort
  6. Bubble Sort

Classes in Detail

GroceryItem an abstract class that implements Comparable. GroceryItems are things like milk, lettuce, and bread. Each has a price. If they are stocked in the store, they also have a quantity.

Public methods:

  • GroceryItem (String name, int quantity, double price);
  • GroceryItem();
  • void setName(String name);
  • String getName();
  • void setPrice(double price);
  • double getPrice();
  • void setQuantity(int n);
  • int getQuantity();
  • String toString();
  • int compareTo(Object o); // Use name as criterion

Dairy, Produce and Meat

Each is a subclass of GroceryItem. GroceryItems have more specific information associated with them, depending upon their type (subclass). Note that the constructor actually takes an input line that has been read from a file. The line will have data specific to its type, which is why it is sent to the subclass. Remember that the constructor which read the line can parse it and then call the other constructor in the subclass.

Below are the public interfaces

Dairy:

  • Dairy(String name, , int quantity, double price, int refrigerationTemperature); // use super
  • Dairy(String inputLine); // takes file input line, parses and sets data
  • void setRefrigerationTemperature(int temp);
  • int getRefrigerationTemperature();

Produce

  • Produce(String name, , int quantity, double price, boolean isOrganic); // use super
  • Produce (String inputLine); // takes file input line, parses and sets data
  • void isOrganic(boolean organic);
  • boolean getIsOrganic();

Meat

  • Meat(String name, int quantity, double price, boolean isGround); // use super
  • Meat(String inputLine); // takes file input line, parses and sets data
  • void isGround(boolean ground);
  • boolean getIsGround();

GroceryOrder extends ArrayList, limiting T to type GroceryItem

No further methods. Use Java ArrayList

GroceryManager pulls the whole thing together

Contains two private collections:

  • ArrayList< GroceryItem > inventory the stores current inventory
  • HashSet restockingList names of items that need to be reordered/restocked. Entries are unique (HashList does this for you).

Public methods:

  • Void loadInventory(String filename) // reads input and populates inventory
    • Calls appropriate constructor with input line.
    • The input file starts with 3 integers, representing the number of Dairy, Produce, and Meat items to follow. You may assume the data is properly formatted.
  • void processOrder(GroceryOrder order) // Subtracts the items and quantities in the order from the inventory. If the order has more than is in stock, fill it with whatever quantity is available. Be careful not to go below 0. If inventory goes to 0, add to restocking list. If the quantity ordered is greater than the quantity in inventory, throw a GroceryException with message out of xxxx, but keep on going. If an item in the order is not in inventory, also throw a GroceryException but keep going.
  • GroceryItem findItemByName(String name); // Implement any way you want
    • Return null if not found
  • void sortInventoryByName(); // Use BubbleSort, and use compareTo()
  • void sortInventoryByPrice() // Use InsertionSort. Will compare without compareTo()
  • void displayRestockingList() // print the list of items that need to be restocked.
  • void displayInventory() // list inventory with all their class-specific data. Line up columns for readability. Use overloads of toString() from each class. Example:

Name: yogurt Quantity: 0 Price: $7.46 temperature: 34

Name: apples Quantity: 48 Price: $0.82 organic: true

Name: chicken Quantity: 9 Price: $5.29 isGround: false

See separate file: Grocery Manager Sample Output.docx for longer example.

Data Files

groceryInventory.txt

  • The input file starts with 3 integers, representing the number of Dairy, Produce, and Meat items to follow.
  • Each line has the
    • type, name, quantity, price and one type-specific datum
  • You may assume the data is properly formatted.

groceryOrders.txt

  • Each order starts with the word Order on a line
  • Other lines have the type, name, and quantity of an item in the order
    • You may assume the data is properly formatted.

Driver Class:

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /* * Driver for Grocery Manager: Read files, process orders, sort and print results */ public class GroceryDriver { static ArrayList> orders = new ArrayList<>(); public static void main(String[] args) { GroceryManager manager = new GroceryManager(); // stock store manager.loadInventory("groceryInventory.txt"); System.out.println("******** Initial Inventory ********"); manager.displayInventory(); // purchase items System.out.println(" ******** Processing Orders ********"); readOrders(); for (GroceryOrder order : orders) { try { manager.processOrder(order); } catch (GroceryException e) { System.out.println(e.getMessage()); } } manager.displayInventory(); // sort inventory manager.sortInventoryByName(); System.out.println(" ******** Sort by name ********"); manager.displayInventory(); manager.sortInventoryByPrice(); System.out.println(" ******** Sort by price ********"); manager.displayInventory(); System.out.println(" ******** Restocking List ********"); manager.displayReorders(); } public static void readOrders() { Scanner input = null; String line; String[] parts; try { input = new Scanner(new FileInputStream("groceryOrders.txt")); while (input.hasNext()) { GroceryOrder list = new GroceryOrder<>(); input.nextLine();// ORDER line = input.nextLine(); parts = line.split(" "); list.add(new Dairy(parts[1], Integer.parseInt(parts[2]), 0, 0)); line = input.nextLine(); parts = line.split(" "); list.add(new Produce(parts[1], Integer.parseInt(parts[2]), 0, false)); line = input.nextLine(); parts = line.split(" "); list.add(new Meat(parts[1], Integer.parseInt(parts[2]), 0, false)); orders.add(list); } } catch (Exception e) { System.out.println(e); } finally { input.close(); } } }

groceryInventory.txt

12 13 4

Dairy eggs 13 4.96 34

Dairy butter 56 0.77 34

Dairy yogurt 2 13.46 34

Dairy milk 47 16.91 34

Dairy cheddar_cheese 79 10.77 34

Dairy kefir 13 18.96 34

Dairy sour_cream 12 19.76 34

Dairy ice_cream 38 11.21 34

Dairy parmesan 20 5.0 34

Dairy cream 77 6.79 34

Dairy monterey_jack 61 12.54 34

Dairy cream_cheese 0 0.64 34

Produce lettuce 1 0.8 true

Produce apples 52 0.82 true

Produce oranges 65 4.58 true

Produce brussel_sprouts 29 3.66 true

Produce spinach 69 7.56 true

Produce bananas 60 3.06 true

Produce pears 3 9.53 true

Produce cucumbers 37 9.67 true

Produce pomegranite 1 3.43 true

Produce blueberries 40 2.55 true

Produce grapes 28 8.24 true

Produce kale 62 2.04 true

Produce broccoli 53 1.28 true

Meat chicken 59 9.99 true

Meat beef 74 17.55 true

Meat lamb 19 6.54 true

Meat turkey 13 21.04 true

groceryOrders.txt Order Dairy cream_cheese 1 Produce oranges 3 Meat chicken 1 Order Dairy butter 6 Produce lettuce 8 Meat chicken 0 Order Dairy kefir 8 Produce bananas 3 Meat turkey 0 Order Dairy cream 6 Produce broccoli 9 Meat lamb 5 Order Dairy yogurt 8 Produce blueberries 11 Meat turkey 3 Order Dairy sour_cream 4 Produce pomegranite 4 Meat chicken 1 Order Dairy cream_cheese 5 Produce pomegranite 9 Meat beef 2 Order Dairy cream 4 Produce blueberries 5 Meat turkey 1 Order Dairy sour_cream 5 Produce kale 11 Meat lamb 5 Order Dairy ice_cream 5 Produce pomegranite 1 Meat chicken 3 Order Dairy cheddar_cheese 9 Produce cucumbers 9 Meat chicken 2 Order Dairy eggs 9 Produce broccoli 9 Meat lamb 1 Order Dairy eggs 1 Produce apples 0 Meat chicken 2 Order Dairy butter 5 Produce apples 4 Meat beef 3 Order Dairy ice_cream 5 Produce lettuce 2 Meat beef 0 Order Dairy cream 6 Produce lettuce 9 Meat chicken 5 Order Dairy sour_cream 8 Produce pomegranite 7 Meat turkey 1 Order Dairy cream 1 Produce broccoli 4 Meat beef 5 Order Dairy cream_cheese 9 Produce grapes 0 Meat chicken 5 Order Dairy yogurt 8 Produce broccoli 7 Meat chicken 2 Order Dairy monterey_jack 0 Produce brussel_sprouts 0 Meat lamb 5 Order Dairy ice_cream 9 Produce broccoli 9 Meat turkey 1 Order Dairy milk 9 Produce cucumbers 7 Meat beef 5 Order Dairy cream_cheese 9 Produce oranges 5 Meat beef 4 Order Dairy butter 7 Produce oranges 10 Meat beef 0 Order Dairy sour_cream 9 Produce broccoli 5 Meat turkey 0 Order Dairy cream_cheese 3 Produce cucumbers 7 Meat lamb 0 Order Dairy sour_cream 9 Produce lettuce 3 Meat chicken 1 Order Dairy sour_cream 0 Produce lettuce 11 Meat lamb 4 Order Dairy sour_cream 1 Produce grapes 6 Meat chicken 5

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!