# Arup Guha
# 6/22/2022
# Grocery Store program

# List of indexes into stock
SELLPRICE = 0
WHOLESALE = 1
QUANTITY = 2

# Constants for menu choices
STOCK = 1
SELL = 2
PRINT = 3
QUIT = 4

def main():
    

    # Initially empty, start with $10,000
    stock = {}
    cash = 10000

    # Get the user's first choice.
    choice = menu()

    # Keep going until the user quits.
    while choice != QUIT:

        # Add items to stock.
        if choice == STOCK:
            cash = stockStore(stock, cash)

        # Sell stuff.
        elif choice == SELL:
            cash += sell(stock)

        # Print store status.
        elif choice == PRINT:
            printStock(stock, cash)

        # Get next choice.
        choice = menu()

# Prints menu choices.
def printChoices():
    print("Please choose one of the following options")
    print("1. Add stock to the store.")
    print("2. Sell items from the store.")
    print("3. Print the store inventory and cash on hand.")
    print("4. Quit")
    
def menu():

    # Get the user's answer.
    printChoices()
    ans = int(input())

    # Repeat until they enter a valid choice.
    while ans < 1 or ans > 4:

        print("Sorry that is not a valid choice.")
        printChoices()
        ans = int(input())

    # The user's choice.
    return ans

# Stocks the store (items stored in stock) and takes in the amount of money
# the store has to purchase items from suppliers.
def stockStore(stock, cash):

    ans = "yes"

    # Stop when they don't want to buy any more.
    while ans == "yes":

        item = input("What item do you want to buy?\n")

        # This item is not in stock.
        if not item in stock:
            wholesale = float(input("What is its wholesale price?\n"))
            sellprice = float(input("What price are you selling it at?\n"))
            stock[item] = [sellprice, wholesale, 0]

        quantity = int(input("How much of this item do you want?\n"))

        # Want to buy too many items.
        if quantity*stock[item][WHOLESALE] > cash:

            # Error message.
            print("Sorry, you don't have enough money.")
            quantity = cash//stock[item][WHOLESALE]

            print("The most you can afford is",quantity,"of ",item)

        # Update your cash.
        cash -= quantity*stock[item][WHOLESALE]

        # Add item to store.
        stock[item][QUANTITY] += quantity

        # See if they want more stuff.
        print("You now have",cash,"dollars left.")
        ans = input("Do you want to purchase more stock?(yes/no)\n")

    # Return amount of cash left.
    return cash

def sell(stock):

    ans = "yes"

    # Stores what we've gotten from the buyer in money.
    amountCollected = 0

    # Keep going until they don't want to buy any more.
    while ans == "yes":

        # Get which item they want.
        item = input("What item do you want to buy?\n")

        # Error case.
        if not item in stock:
            print("Sorry, we don't have that item. Please try again.")
            continue

        # Ask how much they want to buy.
        print("We have", stock[item][QUANTITY],"of",item,"in stock.")
        buy = int(input("How many do you want to buy?\n"))

        # Update amount of money we have.
        amountCollected += buy*stock[item][SELLPRICE]

        # Update quantity in store.
        stock[item][QUANTITY] -= buy

        # See if they want to buy more.
        ans = input("Would you like to buy anohter item?(yes/no)\n")

    # Return what we've collected.
    return amountCollected
                  
def printStock(stock, cash):

    # Prints cash first.
    print("You have",cash,"dollars on hand.")

    # Chart Header
    print("Item\tQuantity\tWholesale\tSell Price")
    
    # Go through each item.
    for item in stock:
        print(item,"\t",stock[item][QUANTITY],"\t",stock[item][WHOLESALE],"\t", stock[item][SELLPRICE])

# Start it.
main()
