Package cs2110

Class GrocerySimulator

java.lang.Object
cs2110.GrocerySimulator

public class GrocerySimulator extends Object
An interactive simulation of one customer shopping at a grocery store.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private boolean
    Whether the customer has started checking out.
    private final Cart
    The shopping cart for the customer being simulated.
    private final Coupon[]
    An array of coupons that are currently valid at the store.
    The register to use for applying coupons and printing a receipt when the customer checks out.
    private final boolean[]
    An array that tracks which coupons have been scanned (either successfully or unsuccessfully) during the simulation.
    private final Store
    The inventory of the grocery store being simulated.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor for the grocery simulator.
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    Print a message to the console listing available commands and their effects.
    private void
    Let the user know if the command that they typed is invalid.
    static void
    main(String[] args)
    Runs the grocery simulator program, either by accepting commands one at a time through the console or by batch processing a text file line-by-line that is passed in as a program argument.
    private void
    Process an "addtocart" command.
    private void
    Process a "listcart" command by listing the items currently in the cart.
    private void
    Process a "removefromcart" command.
    private void
    Process the "checkout" command by initializing the register transaction with the current cart contents.
    private void
    processCommands(Scanner sc, boolean echo)
    Read commands from a Scanner and execute them.
    private void
    Process a "listcoupon" command by listing the coupons that have not been scanned.
    private void
    Process a "scancoupon" command.
    private boolean
    Process the "pay" command by printing the (sorted) receipt and exiting the simulator.
    private static String
    Rearranges the items on the receipt into alphabetical order.

    Methods inherited from class java.lang.Object

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

    • store

      private final Store store
      The inventory of the grocery store being simulated.
    • cart

      private final Cart cart
      The shopping cart for the customer being simulated. Items must be conserved between `store` and `cart`.
    • register

      private RegisterTransaction register
      The register to use for applying coupons and printing a receipt when the customer checks out.
    • coupons

      private final Coupon[] coupons
      An array of coupons that are currently valid at the store. Each of these coupons has a unique `id()`, and no entries of this array are null.
    • scanned

      private final boolean[] scanned
      An array that tracks which coupons have been scanned (either successfully or unsuccessfully) during the simulation. The indices of `applied` and `coupons` correspond, so `applied[i]` is true if and only if `coupons[i]` was scanned.
    • afterCheckout

      private boolean afterCheckout
      Whether the customer has started checking out. This is the "state" of the simulator's finite state machine, which determines which commands are currently valid.
  • Constructor Details

    • GrocerySimulator

      public GrocerySimulator(String name) throws FileNotFoundException
      Constructor for the grocery simulator. Takes as input a String `name` and parses the file "`name`.sim" to initialize the store inventory and list of coupons. Throws an exception if the simulation file cannot be found.
      Throws:
      FileNotFoundException
  • Method Details

    • main

      public static void main(String[] args)
      Runs the grocery simulator program, either by accepting commands one at a time through the console or by batch processing a text file line-by-line that is passed in as a program argument.
    • processCommands

      private void processCommands(Scanner sc, boolean echo)
      Read commands from a Scanner and execute them. Return when the "exit" command is read or when simulation is completed by the "pay" command. If `echo` is true, print the read command after the prompt.
    • processCouponList

      private void processCouponList()
      Process a "listcoupon" command by listing the coupons that have not been scanned.
    • processCartList

      private void processCartList()
      Process a "listcart" command by listing the items currently in the cart.
    • processCartAdd

      private void processCartAdd(String[] command)
      Process an "addtocart" command. First, checks whether the command was issued at an appropriate time (between entering the store and checking out) and prints an error message if not. Then, checks to ensure that the command has exactly one additional argument (the item code) and prints an error message if not. Then, attempts to add an item to the cart with the specified item code with three possible results. 1. If the cart is already full, print "Cart is already full. You cannot add another item." 2. If the cart is not full but the store does not have any items with this code in stock, print "This item has no units in stock." 3. If the cart is not full and the store has an item with this code in stock, unshelve and add it to the cart and print "item_name added to cart." where item_name is the String obtained from the `name()` method of the added item.
    • processCartRemove

      private void processCartRemove(String[] command)
      Process a "removefromcart" command. First, checks whether the command was issued at an appropriate time (between entering the store and checking out) and prints an error message if not. Then, checks to ensure that the command has exactly one additional argument (the item code) and prints an error message if not. Then, attempts to remove an item from the cart with the specified item code with two possible results. 1. If the cart did not contain such an item, print "There was no item with this code in your cart." 2. If the cart contained such an item, remove and reshelve it and print "item_name removed from cart." where item_name is the String obtained from the `name()` method of the removed item.
    • processCheckout

      private void processCheckout()
      Process the "checkout" command by initializing the register transaction with the current cart contents.
    • processCouponScan

      private void processCouponScan(String[] command)
      Process a "scancoupon" command. First, checks whether the user has checked out with the "checkout" command and prints an error message if not. Then, checks to ensure that the command has exactly one additional argument (the coupon id) and prints an error message if not. Then, attempts to locate and scan the coupon, with three possible results. 1. If there is no coupon with the given id, print "A coupon with this id could not be found." 2. If there is a coupon with the given id, but it cannot be applied, print "This coupon could not be applied." 3. If there is a coupon with the given id and it can be applied, apply it and print "Coupon applied!".
    • processPay

      private boolean processPay()
      Process the "pay" command by printing the (sorted) receipt and exiting the simulator.
    • sortReceiptItems

      private static String sortReceiptItems(String unsorted)
      Rearranges the items on the receipt into alphabetical order. This produces a canonical receipt for each transaction that can be verified during testing.
    • getCommandHelp

      private void getCommandHelp()
      Print a message to the console listing available commands and their effects.
    • invalidCommand

      private void invalidCommand(String cmd)
      Let the user know if the command that they typed is invalid.