Package cs2110
Class GrocerySimulator
java.lang.Object
cs2110.GrocerySimulator
An interactive simulation of one customer shopping at a grocery store.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanWhether the customer has started checking out.private final CartThe shopping cart for the customer being simulated.private final Coupon[]An array of coupons that are currently valid at the store.private RegisterTransactionThe 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 StoreThe inventory of the grocery store being simulated. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidPrint a message to the console listing available commands and their effects.private voidinvalidCommand(String cmd) Let the user know if the command that they typed is invalid.static voidRuns 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 voidprocessCartAdd(String[] command) Process an "addtocart" command.private voidProcess a "listcart" command by listing the items currently in the cart.private voidprocessCartRemove(String[] command) Process a "removefromcart" command.private voidProcess the "checkout" command by initializing the register transaction with the current cart contents.private voidprocessCommands(Scanner sc, boolean echo) Read commands from a Scanner and execute them.private voidProcess a "listcoupon" command by listing the coupons that have not been scanned.private voidprocessCouponScan(String[] command) Process a "scancoupon" command.private booleanProcess the "pay" command by printing the (sorted) receipt and exiting the simulator.private static StringsortReceiptItems(String unsorted) Rearranges the items on the receipt into alphabetical order.
-
Field Details
-
store
The inventory of the grocery store being simulated. -
cart
The shopping cart for the customer being simulated. Items must be conserved between `store` and `cart`. -
register
The register to use for applying coupons and printing a receipt when the customer checks out. -
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[] scannedAn 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 afterCheckoutWhether 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
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
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
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
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
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
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
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
Let the user know if the command that they typed is invalid.
-