In: Computer Science
IN BASIC PYTHON:
Cafeteria Selections Program. This program permits students to choose their lunch purchases from a menu in the cafeteria. It calculates the tax on taxable items (candy, pop) and presents the user with a total. The program accepts a tendered amount and computes and outputs change down to the actual coins and bills involved. An added inventory component could keep a running total of individual items purchased. No graphics needed here.
Mars Bar $1.25
Kit Kat $1.25
Soda Pop $1.50
Water $1.00
Muffins $2,00
Coffee / Tea $1.75
Expresso / Specialty Teas $2.50
Cold Sandwiches $5.00
Pasta $7.50
Gum $.140
PYTHON CODE:
# viewMenu() prints all the items 
def viewMenu(itemPrice):
    for key, value in itemPrice.items():
        print(key + ": $" + str(value));
    print("");
# addItemsToCart() checks if its a valid item and calculates tax
# creates a dictionary which has the items and total value (value * quantity)
def addItemToCart(itemPrice, itemCart):
    cont = "Y";
    while(cont == "Y"):
        # Gets user input for which item to add to the cart
        item = input("Enter the item to add: ");
        try:
            #checks if its a valid entry(if the item is present in the itemPrice dictionary)
            itemValue = itemPrice[item]; 
            # If valid, next line will be executed. 
            if(item == "Mars Bar" or item == "Kit Kat" or item == "Soda Pop"):
                tax = 0.13 * itemValue; # calculates tax for Mars Bar, Kit Kat and Soda Pop
                itemValue = itemValue + tax; # calculates itemValue with tax
            # Gets user input for the quantity
            quantity = int(input("Enter the quantity to add: "));
            try:
                # If the item is already present in the cart, value will be added to
                # the exisiting amount
                if(itemCart[item] > 0):
                    itemCart[item] += itemValue * quantity;
            except KeyError:
                itemCart[item] = itemValue * quantity;
            # Asks user if they would like to add more items
            cont = input("Do you want to continue? (y or n)").upper();            
        # if the item is not present in the itemPrice dictionary, this will be executed
        except KeyError:
            print("Invalid item");
            cont == "Y";
    return itemCart;
# viewItemsInCart() prints all the items in the cart
def viewItemsInCart(itemCart):
    total = 0;
    # If no item is present
    if(not bool(itemCart)):
        print("Cart is empty");
        print("");
    # If the cart is not empty, it prints the values in the cart
    else:
        print("Items in the cart");
        for key, value in itemCart.items():
            print(key + ": $" + str(value));
            total += value;
        print(""); 
    return total;
            
# payBill() displays the total amount and returns the extra amount
def payBill(itemCart):
    totalAmount = viewItemsInCart(itemCart);
    print("Total amount: " + str(totalAmount));
    tenderedAmount = int(input("Pay the amount here: "));
    # Checks if the amount paid by the user is lesser than the amount to be paid
    if(tenderedAmount < totalAmount):
        print("");
        print("Invalid value");
        print("");
        payBill(itemCart);
    # Calculates the amount to be returned
    else:
        cashback = tenderedAmount - totalAmount;
        print("Amount Paid: $" + str(tenderedAmount));
        print("Change Back Amount: $" + str(cashback));
# Dictionary with items and values   
itemPrice = {"Mars Bar": 1.25, "Kit Kat": 1.25, "Soda Pop": 1.50, "Water": 1.00,
             "Muffins": 2.00, "Coffee/Tea": 1.75, "Expresso/Speciality Teas": 2.50,
             "Cold Sandwiches": 5.00, "Pasta": 7.50, "Gum": 0.140};
itemCart = {};
mainMenuInput = "1";
while(mainMenuInput != "4"):
    # Displays Menu where user can select what they would like to do
    print("Welcome to the Cafeteria");
    print("Please select any of the following options");
    print("1. View the menu");
    print("2. Add items to the cart");
    print("3. View items in the cart");
    print("4. Pay the bill and buy");
    print("Enter any other value to quit the program");
    mainMenuInput = input();
    if(mainMenuInput == "1"):
        viewMenu(itemPrice);
    elif (mainMenuInput == "2"):
        itemCart = addItemToCart(itemPrice, itemCart);
    elif (mainMenuInput == "3"):
        viewItemsInCart(itemCart);
    elif (mainMenuInput == "4"):
        payBill(itemCart);
    else:
        break;
OUTPUT:


