Supermarket

import csv

Chart = []
Price = []

def see_products():
    with open("products.csv", "r") as file:
        csv_reader = csv.reader(file)
        next(csv_reader)

        for line in csv_reader:
            print(line)

def add_to_chart():
    while True:
        user_product = input("Enter product: ")

        with open("products.csv", "r") as file:
            csv_reader = csv.reader(file)
            product_found = False

            for row in csv_reader:
                if row and len(row) >= 2 and user_product.lower() == row[1].lower():
                    quantity = int(input(f"How many {user_product}s do you want to add? "))
                    total_price = float(row[2].replace('$', '')) * quantity
                    Chart.append((user_product, quantity))
                    Price.append(total_price)
                    print(f"Successfully added {quantity} {user_product}s | Total Price: ${total_price:.2f}")
                    product_found = True
                    break

            if product_found:
                break
            else:
                print("Product not found")

def checkout():
    if not Chart:
        print("Your chart is empty. Add some products before checking out.")
        return
    total_price = sum(Price)

    print("\n===== Checkout =====")
    print("Your Chart:")
    for (product, quantity), price in zip(Chart, Price):
        print(f"{product} x{quantity}: ${price:.2f}")

    print("\nTotal Price: ${:.2f}".format(total_price))
    print("Thank you for shopping at Martin's supermarket!")
    exit()



print("\nWelcome to Mladen's supermarket \n---------------------------------")
print("To see available products write 'p' \n"
      "To add product to chart write 'add' \n"
      "To checkout write 'c'")

while True:
    user_input = input("What would you like to do? (p,add,c): ")

    if user_input.lower() == 'p':
        see_products()
    elif user_input.lower() == 'add':
        add_to_chart()
    elif user_input.lower() == 'c':
        checkout()

    else:
        print("Invalid input. Please try again.")

Last updated