-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinventory.py
More file actions
339 lines (311 loc) · 11.8 KB
/
inventory.py
File metadata and controls
339 lines (311 loc) · 11.8 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import re
import shutil
from datetime import datetime
class InventoryManagement:
def __init__(self,filename="inventory.txt"):
self.filename=filename
# -------------------- Backup --------------------
def create_backup(self):
backup_file = self.filename.replace(".txt", "_backup.txt")
shutil.copy(self.filename, backup_file)
print(f" Backup created as: {backup_file}")
# -------------------- ID Generator -------------------
def generate_item_id(self):
"""
Read file reversed and find the last line that starts with 'Item_ID:'.
Return last_id + 1, or 1 if none found / file missing.
"""
try:
with open(self.filename, "r", encoding="utf-8") as f:
lines = f.readlines()
except FileNotFoundError:
return 1
# iterate reversed to find last Item_ID
for line in reversed(lines):
line = line.strip()
if line.startswith("Item_ID:"):
try:
last_id = int(line.split(":", 1)[1].strip())
return last_id + 1
except ValueError:
# malformed line — continue searching earlier lines
continue
return 1
# -------------------- Add Inventory --------------------
def add_inventory(self):
Item_ID=self.generate_item_id()
while True:
Item_Name=input("Enter Item Name: ").strip()
if Item_Name.replace(" ","").isalpha():
break
else:
print("Invalid input!Item_Name should only contain alphabets")
while True:
Category=input("Enter category: ").strip()
if Category.replace(" ","").isalpha():
break
else:
print("Invalid input!Category should only contain alphabets")
while True:
Quantity=input("Enter quantity: ").strip()
if Quantity.isdigit():
break
else:
print("Invalid input!Quantity should only contain numbers")
while True:
Price=input("Enter price: ").strip()
if Price.isdigit():
break
else:
print("Invalid input!Price should only contain numbers")
while True:
Supplier_Name=input("Enter supplier name: ").strip()
if Supplier_Name.replace(" ","").isalpha():
break
else:
print("Invalid input!Supplier_name should only contain alphabets")
Added_Date=datetime.now().strftime("%Y-%m-%d")
try:
with open(self.filename,"a",encoding="utf-8") as f:
f.write(f"Item_ID:{Item_ID}\n")
f.write(f"Item_Name:{Item_Name}\n")
f.write(f"Category:{Category}\n")
f.write(f"Quantity:{Quantity}\n")
f.write(f"Price:{Price}\n")
f.write(f"Supplier_Name:{Supplier_Name}\n")
f.write(f"Added_Date:{Added_Date}\n")
f.write("_____________________________________\n")
except Exception as e:
print(f"Error saving inventory:{e}")
# -------------------- Search Inventory --------------------
def search_inventory(self,field):
try:
with open(self.filename,"r",encoding="utf-8") as f:
lines=f.readlines()
except FileNotFoundError:
print("No file found")
return
block=[]
found=False
value=input(f"Enter the {field.lower()} to search inventory: ").strip()
pattern = rf"^{re.escape(field)}:.*{re.escape(value)}.*$"
for line in lines:
if line.strip()=="_____________________________________":
if any(re.match(pattern, l.strip(), re.IGNORECASE) for l in block):
print("_____Inventory Found_____")
print("\n".join(block))
print("_____________________________________")
found=True
block=[]
else:
block.append(line.strip())
if block:
if any(re.match(pattern, l.strip(), re.IGNORECASE) for l in block):
print("_____Inventory Found_____")
print("\n".join(block))
print("_____________________________________")
found=True
if not found:
print(f"No inventory found with that {field.lower()}")
# -------------------- Delete Inventory --------------------
def delete_inventory(self,field):
try:
with open(self.filename,"r",encoding="utf-8") as f:
lines=f.readlines()
except FileNotFoundError:
print("No file found")
return
block,new_lines=[],[]
value=input(f"Enter the {field.lower()} to delete inventory: ").strip()
pattern = rf"^{re.escape(field)}:.*{re.escape(value)}.*$"
found_any=False
for line in lines:
if line.strip() == "_____________________________________":
if any(re.match(pattern, l.strip(), re.IGNORECASE) for l in block):
print("_____Inventory found_____")
print("".join(block))
confirm=input("Are you sure you want to delete this (yes/no): ").strip().lower()
if confirm == "yes":
self.create_backup()
found_any=True
else:
new_lines.extend(block+["_____________________________________\n"])
else:
new_lines.extend(block+["_____________________________________\n"])
block=[]
else:
block.append(line)
if found_any:
with open(self.filename,"w",encoding="utf-8") as f:
f.writelines(new_lines)
print("Inventory deleted successfully")
else:
print(f"No inventory found with that {field.lower()}")
# -------------------- Update Inventory --------------------
def update_inventory(self,field):
try:
with open(self.filename,"r",encoding="utf-8") as f:
lines=f.readlines()
except FileNotFoundError:
print("No file found")
return
value=input(f"Enter the {field.lower()} to update inventory: ").strip()
pattern = rf"^{re.escape(field)}:.*{re.escape(value)}.*$"
new_lines,block=[],[]
found_any=False
for line in lines:
if line.strip()=="_____________________________________":
if any(re.match(pattern, l.strip(), re.IGNORECASE) for l in block):
print("_____Inventory Found_____")
print("".join(block))
confirm=input("Are you sure you want to update this inventory(yes/no): ").strip().lower()
if confirm == "yes":
self.create_backup()
found_any=True
Item_ID = next((l.split(":", 1)[1].strip() for l in block if l.startswith("Item_ID:")), None)
while True:
Item_Name=input("Enter Item Name: ").strip()
if Item_Name.replace(" ","").isalpha():
break
else:
print("Invalid input!Item_Name should only contain alphabets")
while True:
Category=input("Enter category: ").strip()
if Category.replace(" ","").isalpha():
break
else:
print("Invalid input!Category should only contain alphabets")
while True:
Quantity=input("Enter quantity: ").strip()
if Quantity.isdigit():
break
else:
print("Invalid input!Quantity should only contain numbers")
while True:
Price=input("Enter price: ").strip()
if Price.isdigit():
break
else:
print("Invalid input!Price should only contain numbers")
while True:
Supplier_Name=input("Enter supplier name: ").strip()
if Supplier_Name.replace(" ","").isalpha():
break
else:
print("Invalid input!Supplier_name should only contain alphabets")
Added_Date=datetime.now().strftime("%Y-%m-%d")
new_lines.append(f"Item_ID:{Item_ID}\n")
new_lines.append(f"Item_Name:{Item_Name}\n")
new_lines.append(f"Category:{Category}\n")
new_lines.append(f"Quantity:{Quantity}\n")
new_lines.append(f"Price:{Price}\n")
new_lines.append(f"Supplier_Name:{Supplier_Name}\n")
new_lines.append(f"Added_Date:{Added_Date}\n")
new_lines.append("_____________________________________\n")
else:
new_lines.extend(block+["_____________________________________\n"])
else:
new_lines.extend(block+["_____________________________________\n"])
block=[]
else:
block.append(line)
if found_any:
with open(self.filename,"w",encoding="utf-8") as f:
f.writelines(new_lines)
print("Inventory Updated successfully")
else:
print(f"No inventory found with that {field.lower()}")
# -------------------- View Inventories --------------------
def view_all_inventory_ID(self):
try:
with open(self.filename,"r",encoding="utf-8") as f:
lines=f.readlines()
except FileNotFoundError:
print("No file found")
return
IDS = [line.split(":", 1)[1].strip() for line in lines if line.startswith("Item_ID:")]
if IDS:
print("_____Item ID_____")
for idx,ID in enumerate(IDS,1):
print(f"{idx}.{ID}")
else:
print("No ID found")
def view_all_inventory_names(self):
try:
with open(self.filename,"r",encoding="utf-8") as f:
lines=f.readlines()
except FileNotFoundError:
print("No file found")
return
names = [line.split(":", 1)[1].strip() for line in lines if line.startswith("Item_Name:")]
if names:
print("_____Item Name_____")
for idx,name in enumerate(names,1):
print(f"{idx}.{name}")
else:
print("No Item Name found")
def view_all_inventory(self):
try:
with open(self.filename,"r",encoding="utf-8") as f:
lines=f.readlines()
except FileNotFoundError:
print("No file found")
return
found=False
block=[]
task_num=1
for line in lines:
if line.strip()=="_____________________________________":
if block:
print(f"\nTask{task_num}:")
print("".join(block).strip())
found=True
task_num+=1
block=[]
else:
block.append(line)
if not found:
print("No inventory found")
if __name__ == "__main__":
obj = InventoryManagement()
while True:
print("\n--- Inventory Management ---")
print("1.Add Inventory")
print("2.Search Inventory")
print("3.Delete Inventory")
print("4.Update Inventory")
print("5.View all inventory id")
print("6.View all inventory names")
print("7.View all inventories")
print("8.Exit\n")
choice=input("Enter your choice(1-8): ").strip()
if choice == "1":
obj.add_inventory()
elif choice == "2":
field=input("Search by(Item_ID/Item_Name/Category/Supplier_Name): ").strip()
if field in ["Item_ID","Item_Name","Category","Supplier_Name"]:
obj.search_inventory(field)
else:
print("Invalid choice!Enter choice from(Item_ID/Item_Name/Category/ Supplier_Name).")
elif choice == "3":
field=input("Delete by(Item_ID/Item_Name/Category/Supplier_Name): ").strip()
if field in ["Item_ID","Item_Name","Category", "Supplier_Name"]:
obj.delete_inventory(field)
else:
print("Invalid choice!Enter choice from(Item_ID/Item_Name/Category/ Supplier_Name).")
elif choice == "4":
field=input("Update by(Item_ID/Item_Name/Category/Supplier_Name): ").strip()
if field in ["Item_ID","Item_Name","Category", "Supplier_Name"]:
obj.update_inventory(field)
else:
print("Invalid choice!Enter choice from(Item_ID/Item_Name/Category/ Supplier_Name).")
elif choice == "5":
obj. view_all_inventory_ID()
elif choice == "6":
obj.view_all_inventory_names()
elif choice == "7":
obj.view_all_inventory()
elif choice == "8":
break
else:
print("Invalid input!Enter right choice(1-8)")