Package cs2110

Class Store

java.lang.Object
cs2110.Store

public class Store extends Object
A mutable data structure that represents the current inventory of a grocery store. Items are stored and accessed by their item codes.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final HashMap<Long,ArrayList<Item>>
    A data structure to keep track of the current inventory of the store, implemented using two data structures (HashMap and ArrayList) that we will study later in the semester.
  • Constructor Summary

    Constructors
    Constructor
    Description
    A basic constructor that initializes the inventory to be empty.
    Store(Scanner inventoryReader)
    A constructor that initializes the inventory of the store based on the lines of a simulation file.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    hasInStock(long itemCode)
    Returns whether there is an item in the store's inventory with the given itemCode.
    void
    shelve(Item item)
    Inserts this item into the store's inventory.
    unshelve(long itemCode)
    Removes and returns an item in the store's inventory with the given itemCode.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • inventory

      private final HashMap<Long,ArrayList<Item>> inventory
      A data structure to keep track of the current inventory of the store, implemented using two data structures (HashMap and ArrayList) that we will study later in the semester. A HashMap stores entries as (key,value) pairs, where the key is used to store and quickly access the entries and the value is the actual data we wish to store. An ArrayList is a Java implementation of a list data type (that is, a collection that stores its data in a linear order) that behaves like a resizable array. Thus, for each item code (key), this inventory map stores a list (value) of all the items with that code currently in stock.
  • Constructor Details

    • Store

      public Store()
      A basic constructor that initializes the inventory to be empty. This will be likely be useful for unit testing.
    • Store

      public Store(Scanner inventoryReader)
      A constructor that initializes the inventory of the store based on the lines of a simulation file. Lines are read one at a time until reading the line "Coupons:" (which signifies the end of the inventory. Each inventory line in this file specifies a quantity of items of a certain type (name, code, and price) to be added to the inventory.
  • Method Details

    • shelve

      public void shelve(Item item)
      Inserts this item into the store's inventory.
    • hasInStock

      public boolean hasInStock(long itemCode)
      Returns whether there is an item in the store's inventory with the given itemCode.
    • unshelve

      public Item unshelve(long itemCode)
      Removes and returns an item in the store's inventory with the given itemCode. There must be such an item in stock, which can be verified by calling hasInStock().