File size: 3,242 Bytes
77bfe74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import gradio as gr

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

# Cart data
my_cart = []

# Create lookup dictionaries
inventory_by_id = {item_id: {"name": name, "price": price} for item_id, name, price in inventory}


def get_inventory_choices():
    return [f"{item_id} - {data['name']} (${data['price']})" for item_id, data in inventory_by_id.items()]


def get_cart_choices():
    if not my_cart:
        return []
    return [f"{index}: {item['id']} - {item['name']} (${item['price']})" for index, item in enumerate(my_cart)]


def show_cart():
    if not my_cart:
        return "Cart is empty."
    lines = []
    for i, item in enumerate(my_cart, start=1):
        lines.append(f"{i}. {item['id']} - {item['name']} - ${item['price']}")
    return "\n".join(lines)


def add_tool(selected_item):
    if not selected_item:
        return show_cart(), gr.update(choices=get_cart_choices(), value=None), "Please select an item."

    item_id = int(selected_item.split(" - ")[0])
    item = inventory_by_id[item_id]

    my_cart.append({
        "id": item_id,
        "name": item["name"],
        "price": item["price"]
    })

    return (
        show_cart(),
        gr.update(choices=get_cart_choices(), value=None),
        f"Added {item['name']} to cart."
    )


def remove_tool(selected_cart_item):
    if not selected_cart_item:
        return show_cart(), gr.update(choices=get_cart_choices(), value=None), "Please select a cart item to remove."

    index = int(selected_cart_item.split(":")[0])
    removed_item = my_cart.pop(index)

    return (
        show_cart(),
        gr.update(choices=get_cart_choices(), value=None),
        f"Removed {removed_item['name']} from cart."
    )


def calculate_total():
    total = sum(item["price"] for item in my_cart)
    return f"Total Bill: ${total}"


with gr.Blocks(title="Hardware Store Billing System") as demo:
    gr.Markdown("## Hardware Store Billing System")

    with gr.Row():
        item_dropdown = gr.Dropdown(
            choices=get_inventory_choices(),
            label="Select a tool from inventory"
        )
        cart_dropdown = gr.Dropdown(
            choices=get_cart_choices(),
            label="Select a tool from cart to remove"
        )

    with gr.Row():
        add_button = gr.Button("Add Tool")
        remove_button = gr.Button("Remove Tool")

    cart_display = gr.Textbox(
        label="Cart Contents",
        lines=10,
        interactive=False
    )

    status_output = gr.Textbox(
        label="Status Message",
        interactive=False
    )

    total_button = gr.Button("Calculate Total")
    total_output = gr.Textbox(
        label="Final Bill",
        interactive=False
    )

    add_button.click(
        fn=add_tool,
        inputs=item_dropdown,
        outputs=[cart_display, cart_dropdown, status_output]
    )

    remove_button.click(
        fn=remove_tool,
        inputs=cart_dropdown,
        outputs=[cart_display, cart_dropdown, status_output]
    )

    total_button.click(
        fn=calculate_total,
        inputs=[],
        outputs=total_output
    )

demo.launch()