-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomer.py
218 lines (171 loc) · 6.94 KB
/
customer.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import address
import customer
import dates
import re
import random
import json
class CustomerError(Exception):
pass
class EmailError(CustomerError):
pass
class Customer:
def __init__(self, id: int, name: str, email:str, birthday:str, city, street, house_num, po_num=None):
self.id = id
self.name = name
self.address = address.Address(city, street, house_num, po_num)
self.email = check_customer_email_with_value(email)
self.birthday = check_birthday_with_value(birthday) #just require format dd-mm-yyyy
self.customer_id = random.randint(1, 10000)
self.loaned_books = []
# def __repr__(self):
# print(f"Customer personal id: {self.id}, Customer name: {self.name}, Customer email: {self.email}, Customer birthday: "
# f"{self.birthday}, customer address: {self.address.__repr__()}, customeer loaned books: {self.loaned_books}")
#getters
def get_customer_personal_id(self):
return self.id
def get_customer_name(self):
return self.name
def get_customer_address(self):
return address.str
def get_customer_email(self):
return self.email
def get_customer_birthday(self):
return self.birthday
def get_customer_loaned_books(self):
return self.loaned_books
#setters
def set_customer_name(self, new_name):
self.customer_name = new_name
def set_customer_birthday(self, new_birthday):
self.birthday = new_birthday
def set_customer_customer_id(self, new_id):
self.customer_id = new_id
def set_customer_loans(self, the_list:list):
self.loaned_books = the_list
def add_loaned_book_to_customer(self, new_book_id):
self.loaned_books.append(new_book_id)
def remove_loaned_book_from_customer(self, book_id):
self.loaned_books.pop(book_id)
def set_customer_id(self, id):
self.customer_id = id
def set_customer_email(self, new_email):
regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$' #thanks internet
if (re.search(regex, new_email)):
self.email = new_email
else:
self.email = None
raise EmailError("Invalid Email")
def set_new_customer_address(self, city, street, house_num, po_num=None):
self.address = address.Address(city, street, house_num)
self.address.po_num = po_num
def generate_customer_id(self): #chat
while True:
customer_id = str(random.randint(10000, 999999))
if not self.check_customer_id(customer_id):
return customer_id
def check_customer_id(self, customer_id): #chat
with open('customers.json', 'r') as f:
customers = json.load(f)
for customer in customers:
if customer['customer_id'] == customer_id:
return True
return False
@staticmethod
def get_customer_details_by_name(name): #chat
with open('customers.json', 'r') as f:
customers = json.load(f)
result = []
for customer in customers:
if customer['name'] == name:
result.append(customer)
return result
# @staticmethod
# def get_customer_details_by_id(customer_id): #chat
# with open('customers.json', 'r') as f:
# customers = json.load(f)
# for customer in customers:
# if customer['customer_id'] == customer_id:
# return customer
# return None
def to_dict(self):
return {
"customer_id": self.customer_id,
"id": self.id,
"name": self.name,
"address": self.address.__repr__(),
"email": self.email,
"birthday": self.birthday,
"curr_loaned": self.loaned_books
}
def add_to_customer_file(self):
# Load the existing data as a list of objects
with open('customers.json', 'r') as f:
customers = json.load(f)
# Append the new customer object to the list
customers.append(self.to_dict())
# Convert the values to double quotes
customers_json = str(customers).replace("'", '"')
# Write the entire list of objects to the file as a valid JSON string
with open('customers.json', 'w') as f:
f.write(customers_json)
# def check_email(email):
# regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$' #thanks internet
# return email if re.search(regex, email) else None
def check_customer_email_with_value(email): #for front end
for i in range(2):
if re.match(r"[^@]+@[^@]+\.[^@]+", email):
return email
else:
print("Invalid email address. Please try again.")
print("Failed to provide valid email address after two attempts.")
return None
def check_customer_email(): #for front end
for i in range(2):
email = input("Enter customer email address: ")
if re.match(r"[^@]+@[^@]+\.[^@]+", email):
return email
else:
print("Invalid email address. Please try again.")
print("Failed to provide valid email address after two attempts.")
return None
def check_birthday(): #for frontend
pattern = r'^([0-3][0-9])/([01][0-9])/([0-9]{2})$'
for i in range(2):
birthday = input("Please enter your birthday in the format dd/mm/yy: ")
match = re.match(pattern, birthday)
if match:
return match.group(1) + '/' + match.group(2) + '/' + match.group(3)
print("Invalid input. Please try again.")
print("Max attempts reached. Setting birthday to None.")
return None
def check_birthday_with_value(birthday): #for frontend
pattern = r'^([0-3][0-9])(-|/)([0-1][0-9])(-|/)([0-9]{2-4})$'
for i in range(2):
match = re.match(pattern, birthday)
if match:
return match.group(1) + '/' + match.group(2) + '/' + match.group(3)
print("Invalid input. Please try again.")
print("Max attempts reached. Setting birthday to None.")
return None
def add_loaned_book_to_customer(customer_id, book_id):
with open('customers.json', 'r') as f:
customers = json.load(f)
for customer in customers:
if customer['customer_id'] == customer_id:
customer['curr_loaned'].append(book_id)
break
with open('customers.json', 'w') as f:
json.dump(customers, f)
def remove_loaned_book_from_customer(customer_id, book_id):
with open('customers.json', 'r') as f:
customers = json.load(f)
for customer in customers:
if customer['customer_id'] == customer_id:
customer['curr_loaned'].remove(book_id)
break
with open('customers.json', 'w') as f:
json.dump(customers, f)
# customer = Customer(id=5, name="Pardonski Damzel",email="shawrma56@gmail.com", birthday="12/13/12", city="Kefar Saba",
# street="kebab", house_num="12")
# customer.add_to_customer_file()
# remove_loaned_book_from_customer(8140, 1)