-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
309 lines (206 loc) · 10.2 KB
/
main.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
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
#
#
#
# Pharmacy Management System
# @author : SONU
# Language : Python (VERSION 3.7.2)
# Dated : 01-08-2019, 02-08-2019
# Special Thanks to : Michael Dawson, who has unknowingly helped me a lot by writing his book ! :-)
# User Interface : Character User Interface [CUI]
#
#
#
import sys
import random
from os import system #REQUIRED FOR CLEARING THE SCREEN (OS SENSITIVE !!!! : IT'S FOR WINDOWS)
def clear():
_ = system('cls')
def display_menu():
print("""
\n\n
____MENU____
1 - Buy a medicine
2 - Add Stock
3 - Add medicine to an existing stock
4 - Set Up File
5 - Exit
""")
def get_choice():
#GETS THE USER CHOICE
try:
choice = int(input("Enter your choice : "))
return choice
except:
print("Invalid input !! The Program will close now !")
input("Press the ENTER key to continue...")
sys.exit()
def open_file(file_name, mode):
#OPENS A FILE
try:
the_file = open(file_name, mode)
except IOError as e:
print("The Required file is not found in the root directory ! Try choosing the File-Setup.")
input("The Program will close now....Press any key to continue...")
sys.exit()
return the_file
def add_stock():
#ADDS A NEW MEDICINE STOCK
print("ADD A NEW MEDICINE STOCK :-")
main_file = open_file("C:/Users/NSAM/Desktop/Pharmacy_managment_system/data.txt", "r")
pre_data = main_file.readlines()
main_file.close()
m_name = input("Enter the medicine stock name : ")
m_name += "\n"
while m_name in pre_data:
#ALREADY EXISTING MEDICINES IN THE LIST
print("That Stock already exists !")
m_name = input("Enter the medicine stock name : ")
m_name += "\n"
try:
m_stock = int(input("Enter currently available stock : "))
m_price = int(input("Enter the price of each medicine : "))
except:
print("Unexpected input !!")
input("The Program will close now...Press any key to continue...")
sys.exit()
#THE PRICE AND STOCK ARE ACCESSED VIA THE MEDICINE NAME INDEX
#FOR STOCK IT IS 1+ MEDICINE_NAM INDEX, WHILE FOR PRICE IT IS 2+
m_stock = str(m_stock)
m_price = str(m_price)
m_stock += "\n"
m_price += "\n"
#STRING CONVERSION IS IMPORTANT AS writelines() CAN'T WRITE INTEGER TO A TEXT FILE
new_data = [m_name, m_stock, m_price]
main_file = open_file("C:/Users/NSAM/Desktop/Pharmacy_managment_system/data.txt", "a")
main_file.writelines(new_data) #HERE THE ACCESS MODE IS 'a', SO THE FILE IS NOT BEING COMPLETELY ERASED ! DATA IS APPENDED
main_file.close()
print("OPERATION SUCCESSFULL !, stock added !!")
input("Press the ENTER key to continue...")
def add_medicine():
print("ADD MEDICINE:-")
main_file = open_file("C:/Users/NSAM/Desktop/Pharmacy_managment_system/data.txt", "r+")
pre_data = main_file.readlines()
main_file.close()
m_name = input("Enter a stock name to add medicine : ")
m_name += "\n"
while m_name not in pre_data:
print("The entered stock does not exist !, Try Again !")
m_name = input("Enter a stock name to add medicine : ")
m_name += "\n"
try:
print("MEDICINE FOUND SUCCESSFULLY !!")
increment = int(input("Enter the number of medicines to be added :"))
except:
print("Unexpected input !! The Program will close now !")
input("Press the ENTER key to continue...")
sys.exit()
m_index = pre_data.index(m_name)
current = pre_data[m_index+1]
current = current[0:len(current)-1] #OMITTING THE NEWLINE CHARACTER
current = int(current)
#HERE ONLY THE CURRENT STOCK IS NEEDED TO CONVERT TO (int) NOT THE PRICE
#INT CONVERSION IS IMPORTANT TO DO CALCULATIONS
current += increment
current = str(current)
current += "\n" #ADDING THE NEWLINE CHARACTER
#STRING CONVERSION IS IMPORTANT AS writelines() CAN'T WRITE INTEGER TO A TEXT FILE
pre_data[m_index+1] = current #LIST CHANGE ONLY, NOT THE MAIN FILE
main_file = open_file("C:/Users/NSAM/Desktop/Pharmacy_managment_system/data.txt", "w")
#CAUTION ::: THIS FILE IS OPENED FOR THE SECOND TIME, THIS TIME ONLY FOR WRITING THE WHOLE DATA
#CAUTION ::: writelines() erases the whole file, and then writes the content, access mode is set to "w"
#PREVIOUSLY IT WAS OPENED TO CREATE THE pre_data LIST
main_file.writelines(pre_data) #FILE WAS NOT PREVIOUSLY CHANGED, NOW IT CHANGED !!!
main_file.close()
print("OPERATION SUCCESSFULL, medicine added !!")
input("Press the ENTER key to continue...")
def buy():
print("BUY MEDICINE:-")
main_file = open_file("C:/Users/NSAM/Desktop/Pharmacy_managment_system/data.txt", "r+")
pre_data = main_file.readlines()
main_file.close()
customer_name = input("Enter customer name : ")
while not customer_name:
customer_name = input("Please enter customer name : ")
medicine = input("Enter the medicine to be bought :")
while not medicine:
medicine = input("Please enter the medicine to be bought :")
medicine += "\n"
if medicine not in pre_data:
print("That medicine is currently not in stock ! :-( SORRY ")
else:
m_index = pre_data.index(medicine)
m_stock = pre_data[m_index+1]
m_price = pre_data[m_index+2]
m_stock = str(m_stock)
m_price = str(m_price)
m_price = m_price[0:len(m_price)-1] #OMITTING THE NEWLINE CHARACTER
m_stock = m_stock[0:len(m_stock)-1] #OMITTING THE NEWLINE CHARACTER
m_stock = int(m_stock) #INTEGER CONVERSION IS IMPORTANT FOR CALCULATIONS
m_price = int(m_price)
if m_stock <= 0:
print("That medicine stock has no medicine left ! SORRY :-(")
else:
try:
no = int(input("Enter the No. of medicines to be bought : [Ex:- No. of bottles, etc] :"))
except:
print("Unexpected input !! The Program will close now !")
input("Press the ENTER key to continue...")
sys.exit()
total_bill = m_price * no
m_stock = m_stock - no
m_stock = str(m_stock) #STRING CONVERSION IS IMPORTANT AS writelines() CAN'T WRITE INTEGERS TO TEXT FILES
m_price = str(m_price)
m_stock += "\n"
m_price += "\n"
pre_data[m_index+1] = m_stock #CHANGE IN THE LIST CONTENT, NOT THE FILE
pre_data[m_index+2] = m_price
#CAUTION ::: THIS FILE IS OPENED FOR THE SECOND TIME, THIS TIME ONLY FOR WRITING THE WHOLE DATA
#CAUTION ::: writelines() erases the whole file, and then writes the content, access mode is set to "w"
main_file = open_file("C:/Users/NSAM/Desktop/Pharmacy_managment_system/data.txt", "w")
main_file.writelines(pre_data) #FILE CHANGED NOW !!!!
main_file.close()
print("OPERATION SUCCESSULL ! Here's the Bill...\n")
print("Customer name : ",customer_name)
print("Medicine bought : ",medicine)
print("Total price : Rs.",total_bill)
input("Press the ENTER key to continue...")
def setup():
"""SETS UP THE MAIN FILE"""
print("WELCOME TO FILE SETUP....YOU ARE HERE PROBABLY YOU ARE USING THE APP FIRST TIME")
print("A File named 'data.txt' will be created this folder...")
input("Press the ENTER key to create the file :")
file = open_file("C:/Users/NSAM/Desktop/Pharmacy_managment_system/data.txt", "a+")
print("The file has been successfully created !! Please mind that if you modify that file",
"then it will hamper your Pharmacy !! Please Please DON'T OPEN THAT FILE !! ")
print("CONGRATS !!File Setup is Successfull ! Now you can manage your pharmacy via here !!")
def main():
"""THE PROGRAM STARTS HERE"""
display_menu()
choice = get_choice()
while choice not in [1, 2, 3, 4, 5]:
choice = get_choice()
while choice < 5:
if choice == 1:
clear()
buy()
elif choice == 2:
clear()
add_stock()
elif choice == 3:
clear()
add_medicine()
elif choice == 4:
clear()
setup()
clear()
display_menu()
choice = get_choice()
while choice not in [1, 2, 3, 4, 5]:
choice = get_choice()
if choice == 5: #EXIT PROGRAM
clear()
input("Press the ENTER key to exit....")
#PROGRAM STARTS HERE
########
main() #
########