{ "cells": [ { "cell_type": "markdown", "id": "a0db856a-2eae-41e5-8288-35b82fd8beaa", "metadata": {}, "source": [ "## Defining Global-level Variables ##" ] }, { "cell_type": "code", "execution_count": 1, "id": "70fe0b5c-917f-4517-a9ed-c62bbb9a41ba", "metadata": {}, "outputs": [], "source": [ "# Since the restaurant name is unlikely to change, this can be a global constant\n", "RESTAURANT_NAME = \"Hungry Hare\"\n", "# Using a nested dictionary for the menu\n", "menu = {\n", " \"sku1\": {\n", " \"name\": \"Hamburger\",\n", " \"price\": 6.51\n", " },\n", " \"sku2\": {\n", " \"name\": \"Cheeseburger\",\n", " \"price\": 7.75\n", " },\n", " \"sku3\": {\n", " \"name\": \"Milkshake\",\n", " \"price\": 5.99\n", " },\n", " \"sku4\": {\n", " \"name\": \"Fries\",\n", " \"price\": 2.39\n", " },\n", " \"sku5\": {\n", " \"name\": \"Sub\",\n", " \"price\": 5.87\n", " },\n", " \"sku6\": {\n", " \"name\": \"Ice Cream\",\n", " \"price\": 1.55\n", " },\n", " \"sku7\": {\n", " \"name\": \"Fountain Drink\",\n", " \"price\": 3.45\n", " },\n", " \"sku8\": {\n", " \"name\": \"Cookie\",\n", " \"price\": 3.15\n", " },\n", " \"sku9\": {\n", " \"name\": \"Brownie\",\n", " \"price\": 2.46\n", " },\n", " \"sku10\": {\n", " \"name\": \"Sauce\",\n", " \"price\": 0.75\n", " }\n", "}\n", "app_actions = {\n", " \"1\": \"Add a new menu item to cart\",\n", " \"2\": \"Remove an item from the cart\",\n", " \"3\": \"Modify a cart item's quantity\",\n", " \"4\": \"View cart\",\n", " \"5\": \"Checkout\",\n", " \"6\": \"Exit\"\n", "}\n", "# We can use a global constant here since the sale tax will remain unchanged\n", "SALES_TAX_RATE = 0.07\n", "cart = {}" ] }, { "cell_type": "markdown", "id": "047470ae-05d9-452f-a7ce-a1955a9170ee", "metadata": {}, "source": [ "## Displaying the Menu ##" ] }, { "cell_type": "code", "execution_count": 2, "id": "7f2434e0-e319-4970-b4cf-59619999be9d", "metadata": {}, "outputs": [], "source": [ "def display_menu():\n", " \"\"\"Displays all menu item SKUs, names, and prices.\"\"\"\n", " # Display a header message\n", " print(\"\\n****Menu****\\n\")\n", " for sku in menu:\n", " # Slice the leading 'sku' string to retrieve the number portion\n", " parsed_sku = sku[3:]\n", " item = menu[sku]['name']\n", " price = menu[sku]['price']\n", " print(\"(\" + parsed_sku + \")\" + \" \" + item + \": $\" + str(price))\n", " print(\"\\n\")" ] }, { "cell_type": "markdown", "id": "9fa4bc3a-f032-4d03-8d81-a02991a37963", "metadata": {}, "source": [ "## Adding Items to the Cart ##" ] }, { "cell_type": "code", "execution_count": 3, "id": "c87b842a-5c05-4913-9bf5-8c648b3c88d5", "metadata": {}, "outputs": [], "source": [ "def add_to_cart(sku, quantity=1):\n", " \"\"\"\n", " Add an item and its quantity to the cart.\n", " \n", " :param string sku: The input SKU number being ordered.\n", " :param int quantity: The input quantity being ordered.\n", " \"\"\"\n", " if sku in menu:\n", " if sku in cart:\n", " cart[sku] += quantity\n", " else:\n", " cart[sku] = quantity\n", " print(\"Added \", quantity, \" of \", menu[sku]['name'], \" to the cart.\")\n", " else:\n", " print(\"I'm sorry. The menu number\", sku, \"that you entered is not on the menu.\")" ] }, { "cell_type": "markdown", "id": "68b7970c-621b-43ad-a8b9-06ce2ff69de4", "metadata": {}, "source": [ "## Removing Items from the Cart ##" ] }, { "cell_type": "code", "execution_count": 4, "id": "80a9c0f1-7cd1-42d8-a96d-6f7ab8eea8fb", "metadata": {}, "outputs": [], "source": [ "def remove_from_cart(sku):\n", " \"\"\"\n", " Remove an item from the cart.\n", " \n", " :param string sku: The input SKU number to remove from the cart.\n", " \"\"\"\n", " if sku in cart:\n", " removed_val = cart.pop(sku)\n", " print(f\"Removed\", removed_val['name'], \"from the cart.\")\n", " else:\n", " print(\"I'm sorry.\", removed_val['name'], \"is not currently in the cart.\")" ] }, { "cell_type": "markdown", "id": "3be70834-2cfa-4a84-bc6b-b61a3cfc33e5", "metadata": {}, "source": [ "## Modifying Items in the Cart ##" ] }, { "cell_type": "code", "execution_count": 5, "id": "17a79269-12d8-473a-867e-bd84c0bede31", "metadata": {}, "outputs": [], "source": [ "def modify_cart(sku, quantity):\n", " \"\"\"\n", " Modify an item's quantity in the cart.\n", " \n", " :param string sku: The input SKU number being modified.\n", " :param int quantity: The input new quantity to use for the SKU.\n", " \"\"\"\n", " if sku in cart:\n", " if quantity > 0:\n", " cart[sku] = quantity\n", " print(\"Modified\", menu[sku]['name'], \"quantity to \", quantity, \" in the cart.\")\n", " else:\n", " # Call the previously defined function to remove a SKU from the cart\n", " remove_from_cart(sku)\n", " else:\n", " print(f\"I'm sorry.\", menu[sku]['name'], \"is not currently in the cart.\")" ] }, { "cell_type": "markdown", "id": "837b65e4-5522-4db6-a96a-9153cf93dae5", "metadata": {}, "source": [ "## Viewing Cart Contents ##" ] }, { "cell_type": "code", "execution_count": 6, "id": "e2e9e7fe-d94c-47f1-b938-08298ac7cb35", "metadata": {}, "outputs": [], "source": [ "def view_cart():\n", " \"\"\"\n", " Display the menu item names and quanitites inside \n", " the cart.\n", " \"\"\"\n", " # Display a header message\n", " print(\"\\n****Cart Contents****\\n\")\n", " subtotal = 0\n", " for sku in cart:\n", " if sku in menu:\n", " quantity = cart[sku]\n", " subtotal += menu[sku][\"price\"] * quantity\n", " print(quantity, \" x \", menu[sku][\"name\"])\n", " tax = subtotal * SALES_TAX_RATE\n", " total = subtotal + tax\n", " print(\"Total: $\", round(total, 2))\n", " print(\"\\n\")" ] }, { "cell_type": "markdown", "id": "be869e62-02bc-4b47-a37d-ea4901ae3601", "metadata": {}, "source": [ "## Checking Out ##" ] }, { "cell_type": "code", "execution_count": 7, "id": "29f3c8f2-3028-4b10-b79b-bd030fd7f542", "metadata": {}, "outputs": [], "source": [ "def checkout():\n", " \"\"\"Display the subtotal information for the user to checkout\"\"\"\n", " # Display a header message\n", " print(\"\\n****Checkout****\\n\")\n", " # Call the previously defined function to view the cart contents\n", " view_cart()\n", " print(\"Thank you for your order! Goodbye!\")\n", " print(\"\\n\")\n" ] }, { "cell_type": "markdown", "id": "5eb38fc9-9b13-4719-9e60-ff814051ede0", "metadata": {}, "source": [ "## Get User Input ##" ] }, { "cell_type": "code", "execution_count": 8, "id": "ce91123f-0081-4ba4-8707-3908275161d6", "metadata": {}, "outputs": [], "source": [ "def get_sku_and_quantity(sku_prompt, quantity_prompt=None):\n", " \"\"\"\n", " Get input from the user.\n", " \n", " :param string sku_prompt: A string representing the prompt to display to the user before they enter the SKU number.\n", " :param string quantity_prompt: A string representing the prompt to display to the user before they enter the quantity.\n", " This defaults to None for cases where quanitity input is not needed.\n", " \n", " :returns: The full sku# value and the quantity (in certain cases)\n", " \"\"\"\n", " # Use the SKU prompt to get input from the user\n", " item_sku = input(sku_prompt)\n", " # String concatenate \"sku\" to the beginning of the entered SKU number\n", " item_sku = \"sku\" + item_sku\n", " # If the quantity prompt is provided, we should get input from the user \n", " if quantity_prompt:\n", " # Use the quantity prompt to get input from the user\n", " quantity = input(quantity_prompt)\n", " # If the user typed a non-digit value, default quantity to 1\n", " if not quantity.isdigit():\n", " quantity = 1\n", " quantity = int(quantity)\n", "\n", " return item_sku, quantity\n", " # Quantity prompt is None meaning we do not need to get input for quantity\n", " else:\n", " return item_sku" ] }, { "cell_type": "markdown", "id": "ca29693e-be08-44ed-8b11-c3a2e3477bf2", "metadata": {}, "source": [ "## Create App Ordering Loop ##" ] }, { "cell_type": "code", "execution_count": 9, "id": "27e2f8af-7689-4be4-b5a1-ecf5cc26e996", "metadata": {}, "outputs": [], "source": [ "def order_loop():\n", " \"\"\"Loop ordering actions until checkout or exit\"\"\"\n", " # Display a welcome message to the user\n", " print(\"Welcome to the \" + RESTAURANT_NAME + \"!\")\n", " # Set the conditional boolean variable that will be used to determine if the while loop\n", " # continues running or whether it should terminate\n", " ordering = True\n", " while ordering:\n", " # Display the app ordering actions\n", " print(\"\\n****Ordering Actions****\\n\")\n", " for number in app_actions:\n", " description = app_actions[number]\n", " print(\"(\" + number + \")\", description)\n", " \n", " response = input(\"Please enter the number of the action you want to take: \")\n", " if response == \"1\":\n", " # User wants to order a menu item. Prompt them for SKU and quantity.\n", " display_menu()\n", " sku_prompt = \"Please enter the SKU number for the menu item you want to order: \"\n", " quantity_prompt = \"Please enter the quantity you want to order [default is 1]: \"\n", " ordered_sku, quantity = get_sku_and_quantity(sku_prompt, quantity_prompt)\n", " add_to_cart(ordered_sku, quantity)\n", " elif response == \"2\":\n", " # User wants to remove an item from the cart. Prompt them for SKU only.\n", " display_menu()\n", " sku_prompt = \"Please enter the SKU number for the menu item you want to remove: \"\n", " item_sku = get_sku_and_quantity(sku_prompt)\n", " remove_from_cart(item_sku)\n", " elif response == \"3\":\n", " # User wants to modify an item quantity in the cart. Prompt them for SKU and quantity.\n", " display_menu()\n", " sku_prompt = \"Please enter the SKU number for the menu item you want to modify: \"\n", " quantity_prompt = \"Please enter the quantity you want to change to [default is 1]: \"\n", " item_sku, quantity = get_sku_and_quantity(sku_prompt, quantity_prompt)\n", " modify_cart(item_sku, quantity)\n", " elif response == \"4\":\n", " # User wants to view the current cart contents. No user input needed.\n", " view_cart()\n", " elif response == \"5\":\n", " # User wants to checkout. No user input needed. Terminate the while loop after displaying.\n", " checkout()\n", " ordering = False\n", " elif response == \"6\":\n", " # User wants to exit before ordering. No user input needed. Terminate the while loop.\n", " print(\"Goodbye!\")\n", " ordering = False\n", " else:\n", " # User has entered an invalid action number. Display a message.\n", " print(\"You have entered an invalid action number. Please try again.\")\n" ] }, { "cell_type": "markdown", "id": "b50a4d50-7d04-4ac8-8227-b5e4287ce4ca", "metadata": {}, "source": [ "## Test Your Ordering App! ##" ] }, { "cell_type": "code", "execution_count": null, "id": "84cf7e6e-1953-431c-b2f8-b0be5cb893b8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Welcome to the Hungry Hare!\n", "\n", "****Ordering Actions****\n", "\n", "(1) Add a new menu item to cart\n", "(2) Remove an item from the cart\n", "(3) Modify a cart item's quantity\n", "(4) View cart\n", "(5) Checkout\n", "(6) Exit\n" ] } ], "source": [ "order_loop()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" } }, "nbformat": 4, "nbformat_minor": 5 }