# Arup Guha
# 11/7/2018
# Example of running a simple store, using two dictionaries.
# One links item -> price, the other item -> quantity in stock.

ADD = 1
BUY = 2
PRINT = 3
QUIT = 4

def main():

    # Get initial item prices, set quantities to 0.
    prices = getPrices()
    quantity = {}
    for item in prices:
        quantity[item] = 0

    # Get first menu choice.
    ans = menu()
    totalrevenue = 0

    # Go till quit.
    while ans != QUIT:

        # Add to Stock
        if ans == ADD:

            # Get item and how many.
            item = input("What item are you adding?\n")
            amt = int(input("How many are you adding?\n"))

            # New item, get its price also.
            if item not in prices:
                cost = float(input("How much will you charge for this item?\n"))
                prices[item] = cost
                quantity[item] = amt

            # Otherwise, we are good to go.
            else:
                quantity[item] += amt

        # Buying something
        elif ans == BUY:

            item = input("What do you want to buy?\n")

            # We don't have it.
            if item not in prices:
                print("Sorry we don't have that.")

            else:

                # Get how many they are buying
                print("There are",quantity[item],"of these available to buy.")
                amt = int(input("How many of that item do you want?\n"))

                # Check if amt to buy is valid.
                if 1 <= amt <= quanity[item]:

                    # Update total revenue and stock.
                    totalrevenue += amt*prices[item]
                    quantity[item] -= amt

                # Error message.
                else:
                    print("Sorry not a valid amount to buy.")

        # Just prints the dictionary...
        elif ans == 3:
            for item in prices:
                print(item,prices[item],quantity[item])

        # Get next choice.
        ans = menu()

    # Show final revenue.
    print("The store made",totalrevenue,"money.")
                          
# Prints the menu and returns the user choice.
def menu():

    print("What option do you want?")
    print("1. Add Stock")
    print("2. Buy Items")
    print("3. Print Stock")
    print("4. Quit")
    ans = int(input(""))
    return ans

# Returns a dictionary of initial prices (item->price).
def getPrices():

    n = int(input("How many different items in the store?\n"))
    prices = {}

    # Add each item and price
    for i in range(n):

        item = input("What is the next item?\n")
        cost = float(input("How much will you charge for it?\n"))
        prices[item] = cost

    return prices

main()
