forked from metafy-social/python-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
188 lines (140 loc) · 5.12 KB
/
Copy pathmain.py
File metadata and controls
188 lines (140 loc) · 5.12 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
import csv
def check_unique_Product_ID(Product_ID) :
file = open('ProductRecords.csv','r',newline = '')
Reader_obj = csv.DictReader(file)
count = 0
for line in Reader_obj :
if line['Product_ID'] == Product_ID :
count += 1
if count == 0:
return Product_ID
else:
print('The Product_ID already exists..!!')
print()
Product_ID = input('Enter unique Product ID: ')
return check_unique_Product_ID(Product_ID)
file.close()
def Entry():
Data = []
Product_ID = (input('Enter the ID of the Product: '))
Data.append(check_unique_Product_ID(Product_ID))
Product_Name = input('Enter the Name of the Product: ')
Product_Price = int(input('Enter the Price of the Product: '))
Product_Quantity = int(input('Enter the Quantity of the Product: '))
Data.extend([Product_Name, Product_Price, Product_Quantity])
file = open('ProductRecords.csv', 'a',newline = '')
writer_obj = csv.writer(file)
writer_obj.writerow(Data)
file.close()
print('Entered data has been successfully recorded..!!!')
print()
def Extract_Data(Product_ID) :
print()
file = open('ProductRecords.csv','r',newline = '')
Reader_obj = csv.DictReader(file)
for line in Reader_obj:
if line['Product_ID'] == str(Product_ID):
for i in line.keys() :
print(i , ':' , line[i])
file.close()
print()
def Extract_Quantity(Product_ID) :
print()
file = open('ProductRecords.csv','r',newline = '')
Reader_obj = csv.DictReader(file)
for line in Reader_obj:
if line['Product_ID'] == str(Product_ID):
print('Quantity of',
line['Product_Name'] ,
':',
line['Product_Quantity'])
file.close()
print()
def Delete_Record(Product_ID) :
file = open ('ProductRecords.csv','r+',newline = '')
Reader_obj = csv.reader(file)
records = [row for row in Reader_obj][1:]
temp_records = []
for record in records:
if record != []:
if record[0] != Product_ID and record != [] :
temp_records.append(record)
file.close()
file = open ('ProductRecords.csv','w+',newline = '')
writer_obj = csv.writer(file)
writer_obj.writerow(['Product_ID','Product_Name','Product_Price','Product_Quantity'])
writer_obj.writerows(temp_records)
print('Record deleted successfully.')
file.close()
def Update_Price(Product_ID,New_Price) :
file = open('ProductRecords.csv','r')
Reader_obj = csv.DictReader(file, fieldnames = ['Product_ID','Product_Name','Product_Price','Product_Quantity'])
Temp_records = []
for line in Reader_obj :
Temp_records.append(line)
file.close()
for record in Temp_records :
if record['Product_ID'] == Product_ID :
record['Product_Price'] = New_Price
Temp_records = Temp_records[1:]
file = open('ProductRecords.csv','w+',newline = '')
Writer_obj = csv.DictWriter(file, fieldnames = ['Product_ID','Product_Name','Product_Price','Product_Quantity'])
Writer_obj.writeheader()
Writer_obj.writerows(Temp_records)
file.close()
print('Price has been Updated successfully !!!')
def Update_Quantity(Product_ID,New_Quantity) :
file = open('ProductRecords.csv','r')
Reader_obj = csv.DictReader(file, fieldnames = ['Product_ID','Product_Name','Product_Price','Product_Quantity'])
Temp_records = []
for line in Reader_obj :
Temp_records.append(line)
file.close()
for record in Temp_records :
if record['Product_ID'] == Product_ID :
record['Product_Quantity'] = New_Quantity
Temp_records = Temp_records[1:]
file = open('ProductRecords.csv','w+',newline = '')
Writer_obj = csv.DictWriter(file, fieldnames = ['Product_ID','Product_Name','Product_Price','Product_Quantity'])
Writer_obj.writeheader()
Writer_obj.writerows(Temp_records)
file.close()
print('Quantity has been Updated successfully !!!')
while True:
print()
print('Press 1 to Insert Product Details: ')
print('Press 2 to Fetch Product Details using Product_ID: ')
print('Press 3 to Fetch Quantity of the Product using Product_ID: ')
print('Press 4 to Delete Product Details using Product_ID: ')
print('Press 5 to Update Product Price using Product_ID: ')
print('Press 6 to Update Product Quantity using Product_ID: ')
print('Press 0 to Exit: ')
print()
Answer = int(input('Press desired number: '))
if Answer == 1 :
n = int(input('Enter the number of entries to be entered: '))
for i in range(n):
Entry()
elif Answer == 2 :
Product_ID = input('Enter the Product ID: ')
Extract_Data(Product_ID)
elif Answer == 3 :
Product_ID = input('Enter the Product ID: ')
Extract_Quantity(Product_ID)
elif Answer == 4 :
Product_ID = input ('Enter the ID of the product to be deleted : ')
Delete_Record(Product_ID)
elif Answer == 5 :
Product_ID = input('Enter the Product ID: ')
New_Price = int(input('Enter the New Price of the Product: '))
Update_Price(Product_ID,New_Price)
elif Answer == 6 :
Product_ID = input('Enter the Product ID: ')
New_Quantity = int(input('Enter the New Quantity of the Product: '))
Update_Quantity(Product_ID,New_Quantity)
elif Answer == 0 :
print('Thank You !!')
exit()
else:
print('No such option exists..!!')
exit()