CSE 111: Programming with Functions

W05 Project: Grocery Store

Purpose

Prove that you can write a Python program that reads CSV files and creates, populates, and uses a dictionary.

Project

Background

Your uncle has a grocery store, he has just started to use an online service that enables his customers to order groceries online. After a customer completes an order, the online service sends a CSV file that contains the customer’s requests to the grocery store. Your uncle has asked you to write a program that reads the CSV file and prints to the terminal window a receipt that lists the purchased items and shows the subtotal, the sales tax amount, and the total.

User Requirements

The program must read two csv files, the customer's order and and a product catalog. Each item in the customer's order will be looked up in the product catalog to get get the current price. And order will be displayed in the terminal that shows the customer's order details. Use the following details to create the program.

  1. Read the products inventory from the file products.csv.
  2. Read the customer's order from the file request.csv
  3. For each item in the order lookup the product in the catalog, use the catalog information to calculate and display the order.
  4. Display the order receipt.
    1. Print a store name (you choose the name) at the top of the receipt.
    2. Print the list of ordered items. Include the item name, quantity ordered and price per item.
    3. Sum and print the number of ordered items.
    4. Sum and print the subtotal due.
    5. Compute and print the sales tax amount. Use 6% as the sales tax rate.
    6. Compute and print the total amount due.
    7. Print a thank you message.
    8. Get the current date and time from your computer’s operating system and print the current date and time.
    9. Include a try block and except blocks to handle FileNotFoundError, PermissionError, and KeyError.

Design

The bulk of the work for this program is performed in the main function. Make sure you include error handling as described in the user requirements.

Function Specifications
Function Name Parameters
Return Type
Description
read_dictionary Parameters
filename,
key_column_index
Return Tpe
Dictionary
This function reads the product data from the csv file passed to the function in the filename parameter. The dictionary key is contained in the csv data column indicated by the key_column_index parameter, the value of each dictionary item is the list derived from the values in the row of the csv file. Function returns a dictionary of products.
main Reads the receipt.csv file, processes the file and displays the receipt according to the user requirements.

Helpful information.

Milestone

Start your project by writing and testing the read_dictionary function.

  1. Create a folder for this week's project, name it whatever you want.
  2. Open the folder you just created in VSCode.
  3. Download both of these files: products.csv and request.csv and save them in your project's folder.
  4. Open the products.csv file in VS Code and examine it. Notice that each row in this file contains three values separated by commas: a product number, a product name, and a retail price. Also, notice that each product number in the products.csv file is unique. This means that your program can read the products.csv file into a dictionary and use the product numbers as keys in the dictionary.
  5. Create a file named receipt.py in your project folder.
  6. Code the read_dictionary function. It should open a CSV file for reading and use a csv.reader to read each row and populate a compound dictionary with the contents of the products.csv file.
  7. Create the main function and add the following features.
    1. Call the read_dictionary function, store the returned dictionary in the variable products_dict.
    2. Display the dictionary.
    3. Open the request.csv file for reading.
    4. Skip the first line of the request.csv file because the first line contains column headings.
    5. Uses a loop to read and processes each row from the request.csv file. Within the body of the loop, your program must do the following for each row:
      1. Use the requested product number to find the corresponding item in the products_dict.
      2. Print the product name, requested quantity, and product price.
  8. At the bottom of your receipt.py file, add a call to the main function. Be certain to protect the call to main with an if statement as taught in the preparation content for week 3.

Testing Procedure

Verify that your program works correctly by following each step in this testing procedure:

  1. Download the test_products.py file and save it in the same folder where you saved your receipt.py program. Run the test_products.py file and ensure that the test_read_dictionary function passes. If it doesn’t pass, there is a mistake in your read_dictionary function. Read the output from pytest, fix the mistake, and run the test_products.py file again until the test function passes.
    > python test_products.py
    =================== test session starts ====================
    platform win32--Python 3.8.6, pytest-6.1.2, py-1.9.0, pluggy
    rootdir: C:\Users\cse111\week05
    collected 1 item
    test_products.py::test_read_dictionary PASSED         [100%]
    ==================== 1 passed in 0.12s =====================
  2. Run your program and verify that it prints the products dictionary and requested items as shown in the sample output below.
    > python receipt.py
    All Products
    {'D150': ['D150', '1 gallon milk', '2.85'], 'D083': ['D083',
    '1 cup yogurt', '0.75'], 'D215': ['D215', '1 lb cheddar
    cheese', '3.35'], 'P019': ['P019', 'iceberg lettuce',
    '1.15'], 'P020': ['P020', 'green leaf lettuce', '1.79'],
    'P021': ['P021', 'butterhead lettuce', '1.83'], 'P025':
    ['P025', '8 oz arugula', '2.19'], 'P143': ['P143', '1 lb
    baby carrots', '1.39'], 'W231': ['W231', '32 oz granola',
    '3.21'], 'W112': ['W112', 'wheat bread', '2.55'], 'C013':
    ['C013', 'twix candy bar', '0.85'], 'H001': ['H001', '8
    rolls toilet tissue', '6.45'], 'H014': ['H014', 'facial
    tissue', '2.49'], 'H020': ['H020', 'aluminum foil', '2.39'],
    'H021': ['H021', '12 oz dish soap', '3.19'], 'H025':
    ['H025', 'toilet cleaner', '4.50']}
    Requested Items
    wheat bread: 2 @ 2.55
    1 cup yogurt: 4 @ 0.75
    32 oz granola: 1 @ 3.21
    twix candy bar: 2 @ 0.85
    1 cup yogurt: 3 @ 0.75

Milestone Submission

On or before the due date, return to Canvas and report your progress on this milestone.

Project Completion

Finish your project by completing main function. Ensure your code accomplishes ALL the requirements specified above.

  1. Remove the line that prints the product dictionary
  2. Add the remaining code necessary to fulfill the user requirement #4.

Helpful Information

Testing

Verify that your program works correctly by following each step in this testing procedure:

  1. Run your program and verify that it prints a receipt formatted similarly to the one shown below. Your program must print the current date and time with exactly the same formatting as shown below. Also, verify that your program computes the number of items, subtotal, sales tax, and total as shown below.
    t; python receipt.py
    kom Emporium
    eat bread: 2 @ 2.55
    cup yogurt: 4 @ 0.75
     oz granola: 1 @ 3.21
    ix candy bar: 2 @ 0.85
    cup yogurt: 3 @ 0.75
    mber of Items: 12
    btotal: 15.26
    les Tax: 0.92
    tal: 16.18
    ank you for shopping at the Inkom Emporium.
    d Nov  4 05:10:30 2020
  2. Verify that the except block to handle KeyError that you added to your program works correctly by doing the following:

    1. Temporarily add the following line to the end of your request.csv file and then save the file.
      R002,5
    2. Run your program again and verify that it prints an error message like the one shown below.
      > python receipt.py
      Error: unknown product ID in the request.csv file
      'R002'
      Hint: if you wrote an except block in your program to handle KeyError and added "R002,5" to the request.csv file and saved the request.csv file and ran your program but your program isn’t raising a KeyError, then look in your program to see if you wrote an if statement before the statement that finds a value in the products dictionary. Look for code similar to this:
      if prod_num in products_dict:
          prod_info_list = products_dict[prod_num]
          ⋮
      
      If your program contains an if statement similar to the one above, then the if statement is probably preventing your program from raising a KeyError. Delete the if statement and unindent the lines of code inside the if statement and test your program again.
  3. Verify that the except block to handle FileNotFoundError that you added to your program works correctly by doing the following:
    1. Temporarily delete or rename the products.csv file.
    2. Run your program again and verify that it prints an error message like the one shown below.
      > python receipt.py
      Error: missing file
      [Errno 2] No such file or directory: 'products.csv'
Exceeding the Requirements

If your program fulfills the requirements for this assignment as described above, your program will earn 93% of the possible points. In order to earn the remaining 7% of points, you will need to add one or more features to your program so that it exceeds the requirements. Use your creativity to add features. Add a comment to the top of your code that explains your enhancement(s).

Document your enhancements:

If you choose to "exceed requirements" place a comment at the top of your code file that describes what you did to enhance your program.

Project Submission

Return to Canvas and upload your receipt.py file for feedback.

Don't get a zero!

If an error prevents your program from running to completion the grader will score your assignment with a 0 and request that you fix and resubmit your program. In other words, rather than submitting a program that doesn't work, it's best to ask for help to understand how to fix the problem before submitting your program.

Useful Links:

  1. Return to: Week Overview | Course Home