BYU logo Computer Science

To start this lab, download this zip file.

Project 5 - Shopping Cart

Before doing this project, make sure you have byu-pytest-utils version 0.3.6 or later.

conda activate cs110
pip install -U byu-pytest-utils=0.3.6

Instructions

Write a program named shopping_cart.py that simulates an online marketplace shopping experience.

This program should allow the user to

  • Add an item to the cart
  • View the current cart
  • Checkout
  • Exit the program

The program takes a single argument, which is the inventory JSON file. This file stores the initial inventory of your program, and you save changes to the inventory to this file when the user checks out their cart.

The example below provides details about the specific prompts to use for each action. The .txt files in test_files contain additional examples for how your program is expected to operate.

Add item

When the user chooses to add an item, they then provide a search term and then choose an item from the resulting hits.

Any item whose name or description contains the search term (ignoring case) should be displayed to the user. If the user provides the empty string as the search term (i.e. enters nothing and presses return), then all items should be displayed.

The chosen item is added to the user’s cart. The inventory should be updated to indicate that there is one less of that item available. The cart should be updated to reflect the presence of the added item.

View cart

When the user chooses to view the cart, list the items in the cart and their respective quantities. Finally, print the total cost of the current cart.

To display an item in the cart, use the following format:

{item name} ({item description}) x {item quantity} @ ${item price} = ${total price for these items}

Indent the total with two spaces.

Example

Rice (1 lb. dry jasmine rice) x 2 @ $1.0 = $2.0
Pepper (Orange sweet bell pepper (each)) x 3 @ $1.0 = $3.0
Salsa (16 oz mild tomato salsa) x 1 @ $2.5 = $2.5
  Total: $7.5

Checkout

When the user chooses to checkout, first display the cart following the instructions in View cart.

Then prompt the user with Confirm purchase of ${cart total}? (y/n): .

If the user indicates y or Y, then save the current inventory to the input file.

if the user indicates n or N, return to the main menu as if Checkout had not been selected. The cart contents stay the same as before.

Quit

When the user chooses to exit the program, the program exits without saving the current cart. Changes to the inventory only happen when the user confirms a cart checkout.

Example

% python shopping_cart.py inventory.json

What do you want to do?
(0) Add an item
(1) View cart
(2) Checkout
(3) Quit
Option: 0

Item search term: n

Which item do you want to add to your cart?
(0) Beans (1/2 lb. dry black beans) @ $1.5 (8 in stock)
(1) Rice (1 lb. dry jasmine rice) @ $1.0 (10 in stock)
(2) Tuna (8 oz can of tuna in water) @ $0.75 (11 in stock)
(3) Pepper (Orange sweet bell pepper (each)) @ $1.0 (1 in stock)
(4) Watermelon (Seedless watermelon (each)) @ $5.25 (2 in stock)
Option: 2

What do you want to do?
(0) Add an item
(1) View cart
(2) Checkout
(3) Quit
Option: 0

Item search term: e

Which item do you want to add to your cart?
(0) Beans (1/2 lb. dry black beans) @ $1.5 (8 in stock)
(1) Rice (1 lb. dry jasmine rice) @ $1.0 (10 in stock)
(2) Tomato (Roma tomato (each)) @ $0.8 (5 in stock)
(3) Avocado (Haas avocado (each)) @ $1.0 (2 in stock)
(4) Tuna (8 oz can of tuna in water) @ $0.75 (10 in stock)
(5) Pepper (Orange sweet bell pepper (each)) @ $1.0 (1 in stock)
(6) Watermelon (Seedless watermelon (each)) @ $5.25 (2 in stock)
Option: 6

What do you want to do?
(0) Add an item
(1) View cart
(2) Checkout
(3) Quit
Option: 2

Tuna (8 oz can of tuna in water) x 1 @ $0.75 = $0.75
Watermelon (Seedless watermelon (each)) x 1 @ $5.25 = $5.25
  Total: $6.0

Confirm purchase of $6.0? (y/n): y

What do you want to do?
(0) Add an item
(1) View cart
(2) Checkout
(3) Quit
Option: 3

See the .txt files in test_files for additional examples.

Grading

Each test is worth the number of points indicated above the function definition.

For example:

@max_score(10)
def test_foobar():
    assert True

Would be worth 10 points.

Each test progressively checks more and more elaborate functionality in your program. Focus on implementing the simpler features first.

Above and beyond

If you want an extra challenge, after submitting, make a copy of your program and add the following features:

  • Allow the user to enter '' (i.e. enter nothing and press return) in order to cancel an interaction with a menu and return to the previous menu.
  • Add your own items to the inventory