2024-08-06: Coffee Machine

I can't remember it's Day 13 or 14 or even 15 of #KeepCoding100Days today. But I finally complete the code of Coffer Machine project. 😎

A small Python project for other coder, but a great achievement for me. I will have one day off code to celebrate this. 😇

I will definitely keep updating below code in following a couple days.

MENU = {
    "espresso": {
        "ingredients": {
            "water": 50,
            "milk": 0,
            "coffee": 18,
        },
        "cost": 1.5,
    },
    "latte": {
        "ingredients": {
            "water": 200,
            "milk": 150,
            "coffee": 24,
        },
        "cost": 2.5,
    },
    "cappuccino": {
        "ingredients": {
            "water": 250,
            "milk": 100,
            "coffee": 24,
        },
        "cost": 3.0,
    }
}

resources = {
    "water": 300,
    "milk": 200,
    "coffee": 100,
}


# Above code are initial code. NO CHANGE!

# Print report function
def report():
    print(f"Water: {resources['water']}ml")
    print(f"Milk: {resources['milk']}ml")
    print(f"Coffee: {resources['coffee']}g")
    print(f"Money: ${resources['money']}")


# Check resource sufficient function
def check_resource(coffee_order):
    for item in resource:
        if resource[item] < MENU[coffee_order]['ingredients'][item]:
            print(f"Sorry there is not enough {item}.")
            return False

    return True


# Process Coins function
def insert_coins(coffee_order):
    print("Please insert coins.")
    quarters = int(input("How many quarters?: "))
    dimes = int(input("How many dimes?: "))
    nickles = int(input("How many nickles?: "))
    pennies = int(input("How many pennies?: "))
    coins_sum = quarters * 0.25 + dimes * 0.1 + nickles * 0.05 + pennies * 0.01
    coffee_cost = MENU[coffee_order]['cost']
    if coins_sum < coffee_cost:
        return False, 0
    else:
        return True, coins_sum
    # print(coins_sum, coffee_cost)


# Transaction
def transaction(coin_sum, coffee_order):
    change = coin_sum - MENU[coffee_order]['cost']
    if resource['money'] - change >= 0:
        resource['money'] = resource['money'] + MENU[coffee_order]['cost']
        resource['water'] = resource['water'] - MENU[coffee_order]['ingredients']['water']
        resource['milk'] = resource['milk'] - MENU[coffee_order]['ingredients']['milk']
        resource['coffee'] = resource['coffee'] - MENU[coffee_order]['ingredients']['coffee']
        return True, change, resource
    else:
        return False, change, resource


# create a resource copy in memory
resource = resources

# Add a new money item in resources dictionary
resource['money'] = 0
# print(resources)

# Turn off the machine by entering "off" to the prompt
turn_off_machine = False

while not turn_off_machine:

    # Prompt user by asking "What would you like?"
    coffee_order = input("What would you like? (Espresso $1.5 / Latte $ 2.5 / Cappuccino $3.0): ").lower()
    next_step = True
    if coffee_order == "report":
        report()
        next_step = False
    elif coffee_order == "off":
        turn_off_machine = True
        next_step = False
        print("I am off, see you...")
    else:
        check_resource_result = check_resource(coffee_order)
        # print(check_resource_result)
        if not check_resource_result:
            next_step = False

    if next_step:
        coin = insert_coins(coffee_order)
        coin_okay = coin[0]
        if not coin_okay:
            print("Sorry that's not enough money. Money refunded.")
            next_step = False

    if next_step:
        coin_sum = coin[1]

        # Call function Transaction
        transaction_result = transaction(coin_sum, coffee_order)
        if transaction_result[0]:
            change = round(transaction_result[1], 2)
            resource = transaction_result[2]
            report()
            if change != 0:
                print(f"Here is ${change} in change.")
            print(f"Here is your {coffee_order.capitalize()}. Enjoy!")
        else:
            print("Sorry no enough money for your change. The coins refunded.")