File size: 3,299 Bytes
e81d7d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr

# -----------------------
# Data
# -----------------------
INVENTORY = [
    (101, "Hammer", 120),
    (102, "Screwdriver Set", 350),
    (103, "Drill Machine", 2500),
    (104, "Pliers", 180),
    (105, "Wrench", 220),
]

# Index by ID for quick lookup
INV_BY_ID = {item_id: (name, price) for item_id, name, price in INVENTORY}

# -----------------------
# Cart state helpers
# -----------------------
def fmt_cart(cart):
    if not cart:
        return "β€” (empty) β€”"
    lines = [f"{i}. [{item_id}] {name} β€” ${price}" for i, (item_id, name, price) in enumerate(cart)]
    return "\n".join(lines)

def total_cost(cart):
    return sum(price for _, _, price in cart)

# -----------------------
# Actions
# -----------------------
def add_to_cart(item_id, cart):
    cart = cart or []
    msg = ""
    try:
        item_id = int(item_id)
    except (TypeError, ValueError):
        return fmt_cart(cart), "Invalid ID: enter a number.", "", total_cost(cart), cart

    if item_id not in INV_BY_ID:
        return fmt_cart(cart), f"ID {item_id} not found in inventory.", "", total_cost(cart), cart

    name, price = INV_BY_ID[item_id]
    cart.append((item_id, name, price))
    return fmt_cart(cart), f"Added: [{item_id}] {name} (${price})", "", total_cost(cart), cart

def remove_from_cart(index, cart):
    cart = cart or []
    msg_ok, msg_err = "", ""
    try:
        index = int(index)
    except (TypeError, ValueError):
        return fmt_cart(cart), "", "Invalid index: enter a number.", total_cost(cart), cart

    if 0 <= index < len(cart):
        item_id, name, price = cart.pop(index)
        msg_ok = f"Removed: [{item_id}] {name} (${price})"
    else:
        msg_err = f"Index {index} not in cart range (0–{max(len(cart)-1,0)})."
    return fmt_cart(cart), msg_ok, msg_err, total_cost(cart), cart

# -----------------------
# UI
# -----------------------
with gr.Blocks(title="Hardware Store Billing") as demo:
    gr.Markdown("## Hardware Store Billing\nAdd items by **Inventory ID**; remove by **cart index** (0-based).")

    with gr.Row():
        add_id = gr.Number(label="Enter Inventory ID to Add to Cart", value=0, precision=0)
        rem_idx = gr.Number(label="Enter Index ID to Remove from Cart", value=0, precision=0)

    with gr.Row():
        btn_add = gr.Button("Add to Cart", variant="primary")
        btn_rem = gr.Button("Remove from Cart")

    cart_box = gr.Textbox(label="Your Cart", value="β€” (empty) β€”", lines=10)
    add_msg = gr.Textbox(label="Add Item Message", interactive=False)
    rem_msg = gr.Textbox(label="Remove Item Message", interactive=False)
    total = gr.Number(label="Total Cost", value=0, interactive=False, precision=0)

    # Keep cart in state
    cart_state = gr.State([])

    btn_add.click(
        fn=add_to_cart,
        inputs=[add_id, cart_state],
        outputs=[cart_box, add_msg, rem_msg, total, cart_state]
    )

    btn_rem.click(
        fn=remove_from_cart,
        inputs=[rem_idx, cart_state],
        outputs=[cart_box, add_msg, rem_msg, total, cart_state]
    )

    with gr.Accordion("Inventory", open=False):
        gr.Dataframe(
            headers=["ID", "Name", "Price"],
            value=INVENTORY,
            wrap=True
        )

if __name__ == "__main__":
    demo.launch()