-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
189 lines (145 loc) · 5.1 KB
/
Copy pathfunctions.py
File metadata and controls
189 lines (145 loc) · 5.1 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from body import Customer, Product, Order, Admin, products, orders
from storage import save_customers_to_file, load_customers_from_file
from enum import Enum
from gui import load_products, EShopGUI
import json
import sys
customers = load_customers_from_file()
def load_products_from_file(filename="products.json"):
try:
with open(filename, "r") as file:
data = json.load(file)
for item in data:
product = Product(
name=item["name"],
brand=item["brand"],
amount=item["amount"],
price=item["price"]
)
products.append(product)
except FileNotFoundError:
pass
def show_menu():
print(" __________ E-SHOP __________")
print("""
1. LOG IN
2. ADD NEW CUSTOMER
3. LOG IN AS ADMIN
4. EXIT
""")
class MainMenu(Enum):
LOG_IN_CUSTOMER = 1
CREATE_PROFIL = 2
LOG_IN_ADMIN = 3
EXIT = 4
def __str__(self):
return self.name.replace('_', ' ').title()
def user_choice():
show_menu()
while True:
try:
choice = int(input("CHOOSE WHAT YOU WANT TO DO: "))
except ValueError:
print("Please enter a valid number.")
continue
if choice == MainMenu.LOG_IN_CUSTOMER.value:
# logowanie customera
customer = login_customer()
if customer:
# 2️⃣ Wczytaj produkty do GUI i uruchom okno
load_products()
app = EShopGUI(customer) # przekaż zalogowanego klienta
app.mainloop()
continue # wróć do menu po zamknięciu GUI
elif choice == MainMenu.CREATE_PROFIL.value:
# Log in as a new customer
first_name = input("Write your name: ").capitalize().strip()
last_name = input("Write your last name: ").capitalize().strip()
password = input("Write password: ").strip()
new_customer = Customer(first_name, last_name, password)
print(f"|The customer has been successfully created in the system: "
f"{new_customer.first_name} "
f"{new_customer.last_name}, "
f"ID: {new_customer.id} |")
customers.append(new_customer)
save_customers_to_file(customers)
if not products:
print("No products available yet.")
else:
print("AVAILABLE PRODUCTS:")
for i, product in enumerate(products, start=1):
product.display_for_customer(i)
customers.append(new_customer)
elif choice == MainMenu.LOG_IN_ADMIN.value:
# LOGIN as admin to the system
if admin_login():
admin_panel()
elif choice == MainMenu.EXIT.value:
# Quiting programme
print("PROGRAM FINISHED")
sys.exit(0)
def show_all_customers():
print("ALL CUSTOMERS: ")
if not customers:
print("No customers yet")
for customer in customers:
print(f"{customer.id}: {customer.first_name} {customer.last_name}")
print()
def login_customer():
"""
CLI-owe logowanie klienta po jego ID.
Zwraca obiekt Customer lub None.
"""
try:
cid = int(input("Enter your customer ID: "))
except ValueError:
print("Invalid ID. Please enter a number.")
return None
customer = next((c for c in customers if c.id == cid), None)
if not customer:
print("Customer not found.")
return None
print(f"Welcome, {customer.first_name}!")
return customer
def admin_login():
login = input("Username: ")
password = input("Password: ")
if login == "admin" and password == "12345":
print("Logged in successfully.")
return True
else:
print("Incorrect login or password.")
return False
class AdminPanel(Enum):
SHOW_CUSTOMERS = 1
ADD_PRODUCT = 2
SHOW_PRODUCTS = 3
SHOW_SALES = 4
LOGOUT = 5
def __str__(self):
return self.name.replace('_', ' ').title()
def admin_panel():
admin = Admin("admin") # INSTANCJA KLASY
# Here we ask admin what he wants to do as the admin. We use Enum to
while True:
print("\n--- ADMIN PANEL ---")
for option in AdminPanel:
print(f"{option.value}. {option}")
try:
choice = int(input("Choose option: "))
except ValueError:
print("Invalid option. Try again.")
continue
if choice == AdminPanel.SHOW_CUSTOMERS.value:
show_all_customers()
elif choice == AdminPanel.ADD_PRODUCT.value:
admin.add_product()
elif choice == AdminPanel.SHOW_PRODUCTS.value:
admin.show_products()
elif choice == AdminPanel.SHOW_SALES.value:
# Here probably we are going to use some analytics libraries (Pandas, matlplotlib)
pass
elif choice == AdminPanel.LOGOUT.value:
print("Logged out from admin panel." + "\n" * 3)
show_menu()
break