File size: 2,881 Bytes
6fb377b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import gradio as gr

# Sample inventory (can be extended later)
inventory = {
    1: {"name": "Apple", "price": 2.0},
    2: {"name": "Banana", "price": 1.0},
    3: {"name": "Orange", "price": 1.5},
    4: {"name": "Mango", "price": 3.0},
    5: {"name": "Grapes", "price": 2.5},
}

# Cart stored as {item_id: quantity}
cart = {}


def get_inventory_display():
    return "\n".join(
        [f"{item_id}: {item['name']}{item['price']})" for item_id, item in inventory.items()]
    )


def view_cart():
    if not cart:
        return "Cart is empty.", "Total: £0.00"

    lines = []
    total = 0

    for item_id, qty in cart.items():
        item = inventory[item_id]
        cost = item["price"] * qty
        total += cost
        lines.append(f"{item['name']} x {qty} = £{cost:.2f}")

    return "\n".join(lines), f"Total: £{total:.2f}"


def add_to_cart(item_id, quantity):
    try:
        item_id = int(item_id)
        quantity = float(quantity)

        if item_id not in inventory:
            return "Invalid product ID.", *view_cart()

        if quantity <= 0:
            return "Quantity must be positive.", *view_cart()

        cart[item_id] = cart.get(item_id, 0) + quantity

        return f"Added {quantity} {inventory[item_id]['name']}(s).", *view_cart()

    except:
        return "Invalid input.", *view_cart()


def remove_from_cart(item_id, quantity):
    try:
        item_id = int(item_id)
        quantity = float(quantity)

        if item_id not in cart:
            return "Item not in cart.", *view_cart()

        if quantity <= 0:
            return "Quantity must be positive.", *view_cart()

        cart[item_id] -= quantity

        if cart[item_id] <= 0:
            del cart[item_id]

        return f"Removed {quantity} item(s).", *view_cart()

    except:
        return "Invalid input.", *view_cart()


with gr.Blocks() as app:
    gr.Markdown("# 🛒 Fruit Seller Shopping Cart")

    gr.Markdown("## 📦 Available Inventory")
    inventory_box = gr.Textbox(value=get_inventory_display(), lines=6, interactive=False)

    with gr.Row():
        item_id_input = gr.Number(label="Product ID")
        quantity_input = gr.Number(label="Quantity")

    with gr.Row():
        add_btn = gr.Button("Add to Cart")
        remove_btn = gr.Button("Remove from Cart")

    status = gr.Textbox(label="Status")
    cart_display = gr.Textbox(label="Cart Details", lines=8)
    total_display = gr.Textbox(label="Total Bill")

    add_btn.click(
        add_to_cart,
        inputs=[item_id_input, quantity_input],
        outputs=[status, cart_display, total_display],
    )

    remove_btn.click(
        remove_from_cart,
        inputs=[item_id_input, quantity_input],
        outputs=[status, cart_display, total_display],
    )

    # Initialize cart view
    app.load(fn=view_cart, outputs=[cart_display, total_display])

app.launch()