OneCompiler

shoppingCartManagement

159
products = {
    "chicken": {
        "price": 65000,
        "quantity": 100
    },
    "chicken wings": {
        "price": 45000,
        "amount": 200
    },
    "bread": {
        "price": 5000,
        "quantity": 130
    },
    "milk": {
        "price": 14000,
        "quantity": 67
    },
    "cheese": {
        "price": 14000,
        "amount": 67
    },
    "rice": {
        "price": 30000,
        "amount": 92
    },
    "juice": {
        "price": 15000,
        "quantity": 45
    }
}

# each item has name, price, total price, quantity/amount
basket = {}


def print_list(items):
    i = 1
    for name in items:
        print(f"item:{i} \n"
              f"name: {name.upper()} \n"
              f"price: {items[name]['price']}")
        # items[name]["quantity"]
        if items[name].get("quantity", 0):
            print(f"quantity: {items[name]['quantity']} \n")
        else:
            print(f"amount: {items[name]['amount']} \n")
        i += 1


# 1 - provide the list of the items:
# print_list(products)


# add items to a basket
def add_item(item_name, quantity_or_amount, calculate_type):
    # cases:
    # item is already in basket: increase the count in basket
    # item is being for the first time
    # in each case reduce the q/amount from products list
    if item_name in basket:
        basket[item_name][calculate_type] += quantity_or_amount
        basket[item_name]["total"] += quantity_or_amount * basket[item_name]["total"]["price"]
    else:
        price = products[item_name]["price"]
        print("Check:", quantity_or_amount, price)

        basket[item_name] = {
            "price": products[item_name]["price"],
            calculate_type: quantity_or_amount,
            "total": price * quantity_or_amount
        }


def remove_item(item_name, quantity_or_amount, calculate_type):
    if item_name in basket:
        basket[item_name][calculate_type] -= quantity_or_amount
        basket[item_name]["total"] -= basket[item_name][calculate_type] * basket[item_name][calculate_type]
        if basket[item_name][calculate_type] == 0:
            del basket[item_name]
    else:
        print("You don't have this item in your basket. Try again.")


def finish_order(customer_name):
    print(f"Dear {customer_name}, here is your cheque: \n")
    i = 1
    for name in basket:
        print(f"Item:{i} \n"
              f"Name: {name.upper()} \n"
              f"Price: {basket[name]['price']}")
        if basket[name].get("quantity", 0):
            print(f"quantity: {basket[name]['quantity']} \n")
        else:
            print(f"amount: {basket[name]['amount']} \n")
        print(f"Total for {name} is: {basket[name]['total']}")
        i += 1
    total = 0
    for item in basket:
        total += basket[item]["total"]
    print(f"Total is: {total}")


def start_order():
    name = input("Dear customer, enter your name: ")
    option = int(input("Choose an option: \n 1: See the catalog of products available \n 2: Cancel \n"))
    if option == 1:
        print_list(products)
        item_name = input("Type the name of the product to add into a basket \n "
                          "or, type 'quit' to finish the order")
        # amount = input("Enter the quantity/amount you want to purchase: ")
        while item_name != "quit":
            amount = int(input("Enter the quantity/amount you want to purchase: "))
            # item_name, price, quantity_or_amount, calculate_type
            if products[item_name].get("quantity", 0):
                calculate_type = "quantity"
            else:
                calculate_type = "amount"
            add_item(item_name, amount, calculate_type)
            item_name = input("Type the name of the product to add into a basket \n or, type 'quit'")
        finish_order(name)
    else:
        return


start_order()