File size: 1,915 Bytes
8700a34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr

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

# Global cart
cart = []

# ---------- Helper Functions ----------
def add_tool(tool_name):
    for item in inventory:
        if item[1] == tool_name:
            cart.append(item)
            break
    return display_cart(), f"βœ… Added {tool_name} to cart."

def remove_tool(tool_name):
    global cart
    for item in cart:
        if item[1] == tool_name:
            cart.remove(item)
            break
    return display_cart(), f"πŸ—‘οΈ Removed {tool_name} from cart."

def display_cart():
    if not cart:
        return "Cart is empty."
    cart_display = "\n".join([f"{t[1]} - ${t[2]}" for t in cart])
    return cart_display

def calculate_total():
    total = sum([t[2] for t in cart])
    return f"πŸ’° Total Bill: ${total}"

# ---------- Interface Components ----------
tool_names = [item[1] for item in inventory]

with gr.Blocks() as demo:
    gr.Markdown("## 🧰 Hardware Store Billing System\nAdd or remove tools and calculate your total.")

    with gr.Row():
        tool_dropdown = gr.Dropdown(tool_names, label="Select Tool to Add", interactive=True)
        add_btn = gr.Button("Add Tool")

    with gr.Row():
        remove_dropdown = gr.Dropdown(tool_names, label="Select Tool to Remove", interactive=True)
        remove_btn = gr.Button("Remove Tool")

    cart_box = gr.Textbox(label="πŸ›’ Cart Contents", lines=6)
    total_btn = gr.Button("Calculate Total")
    result_box = gr.Textbox(label="πŸ’΅ Result", interactive=False)

    add_btn.click(add_tool, inputs=tool_dropdown, outputs=[cart_box, result_box])
    remove_btn.click(remove_tool, inputs=remove_dropdown, outputs=[cart_box, result_box])
    total_btn.click(calculate_total, outputs=result_box)

demo.launch()