-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProduct.py
135 lines (105 loc) · 4.71 KB
/
Product.py
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
130
131
132
133
134
135
import sqlite3
from PyQt5.QtWidgets import QMessageBox
import plotly.graph_objects as go
class Product:
def __init__(self, table_name):
"""
Initializes the Product class.
Establishes a connection to the SQLite database and creates the specified table if it doesn't exist.
Args:
table_name (str): The name of the table to be created or accessed in the SQLite database.
Returns:
None
"""
self.table_name = table_name
self.conn = sqlite3.connect('Restaurant.db')
self.cursor = self.conn.cursor()
self.create_table()
def create_table(self):
"""
Creates a new table in the SQLite database if it doesn't exist.
Defines the table schema with columns for ID, name, price, and ingredients.
Args:
None
Returns:
None
"""
self.cursor.execute(f'''
CREATE TABLE IF NOT EXISTS {self.table_name} (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
price REAL NOT NULL,
ingredients TEXT
)
''')
self.conn.commit()
def add_product(self, name, price, ingredients):
"""
Adds a new product to the database table.
Inserts a new row with the specified name, price, and ingredients into the database table.
Args:
name (str): The name of the product.
price (float): The price of the product.
ingredients (str): The ingredients used in the product.
Returns:
None
"""
self.cursor.execute(f"INSERT INTO {self.table_name} (name, price, ingredients) VALUES (?, ?, ?)", (name, price, ingredients ))
self.conn.commit()
QMessageBox.information(None, "Success", f"{name} was added.")
def delete_product(self, product_id):
"""
Deletes a product from the database table by ID.
Removes the product with the specified ID from the database table.
Args:
product_id (int): The ID of the product to be deleted.
Returns:
None
"""
self.cursor.execute(f"DELETE FROM {self.table_name} WHERE id=?", (product_id,))
self.conn.commit()
QMessageBox.information(None, "Success", f"The product with {product_id} was deleted.")
def update_product(self, product_id, name, price, ingredients):
"""
Updates a product in the database table by ID.
Modifies the name, price and ingredients of the product with the specified ID in the database table.
Args:
product_id (int): The ID of the product to be updated.
name (str): The new name of the product.
price (float): The new price of the product.
ingredients (str): Ingredients of the product.
Returns:
None
"""
self.cursor.execute(f"UPDATE {self.table_name} SET name=?, price=?, ingredients = ? WHERE id=?", (name, price, ingredients, product_id))
self.conn.commit()
QMessageBox.information(None, "Success", f"The product with {product_id} was updated.")
def show_products(self):
"""
Fetches and displays the products from the database in an interactive table using Plotly.
Retrieves product data from the specified table in the database,
creates a table representation using Plotly,
and displays it interactively.
Args:
self: The instance of the Product class.
Returns:
None
"""
# Execute SQL query to fetch product data
self.cursor.execute(f"SELECT id, name, price, ingredients FROM {self.table_name}")
# Fetch all products
products = self.cursor.fetchall()
# Extract data for the table
ids = [product[0] for product in products] # List of product IDs
names = [product[1] for product in products] # List of product names
prices = [product[2] for product in products] # List of product prices
ingredients = [product[3] for product in products] # List of product ingredients
# Create a table using Plotly
fig = go.Figure(data=[go.Table(
header=dict(values=['ID', 'Name', 'Price', 'Ingredients']), # Table headers
cells=dict(values=[ids, names, prices, ingredients])) # Table data
])
# Set layout and title for the table
fig.update_layout(title=f"Products in {self.table_name.upper()}")
# Display the table interactively
fig.show()