1
1
from FStore import FStore
2
+ from FStore import Cart
3
+ import time
4
+ import getpass
2
5
3
- # TODO 1 - get stock from reading a json file.
4
- dict = { 'Apple' : 100 , 'Banana' : 100 , 'Cherry' : 100 }
5
- # Write a function to read stockjson file
6
- # function should return json info which is there in the file.
6
+ import json
7
+ def getAvilableStock ():
8
+ stockInfo = open ( "C: \\ Users \\ sanja \\ Documents \\ GitHub \\ Python-Data-Structure \\ FruitStore \\ stock. json" , "r" )
9
+ return json . load ( stockInfo )
7
10
8
-
9
- # TODO 2 - pass the return of above function, to open your store
10
- openStore = FStore (dict ) # you cannot open a store without stock.
11
+ openStore = FStore (getAvilableStock ())
12
+ cartInstance = Cart ()
11
13
12
14
def getUserInput (fromWhichMenu ):
13
15
@@ -30,77 +32,119 @@ def getUserInput(fromWhichMenu):
30
32
print ("That's not an int!" )
31
33
32
34
return choice
33
- elif fromWhichMenu == "addOrRemove" :
35
+ elif fromWhichMenu == "addMoreItems" :
34
36
try :
35
- choice = input ("Is there anything you want to add or remove ? Y or N " ).strip ()
36
- if choice == "Y" :
37
+ choice = input ("Do you want to add more items to your cart ? Y or N " ).strip ()
38
+ if choice == "Y" or choice == "y" or choice == "yes" or choice == "YES" :
37
39
return True
38
40
else :
39
41
return False
40
42
except ValueError :
41
43
print ("That's not an int!" )
44
+ elif fromWhichMenu == "adminStuff" :
45
+ try :
46
+ choice = getpass .getpass ("Enter admin password" )
47
+ if choice == "admin123" :
48
+ return True
49
+ else :
50
+ return False
51
+ except ValueError :
52
+ print ("That's not a valid password!" )
42
53
43
54
44
55
45
56
def displayMainMenu ():
46
57
print ("""
47
- ====== Fruit Shop =======
48
- 1. Display available Stocks
49
- 2. Buy Fruits
50
- 3. Get Total
58
+ 1. Show available fruits
59
+ 2. Buy Fruits
60
+ 3. Show Cart
51
61
4. Checkout
52
62
5. Exit
63
+ 6. Display available Stocks (only store admin can access)
53
64
""" )
54
65
55
- def displayFruitMenu ():
56
- for i in enumerate (openStore .listOfFruits (), start = 1 ):
57
- print (i [0 ], i [1 ])
58
-
59
- def shoppingCart ():
60
- pass
61
-
62
- def storeOpens ():
63
- # while True:
64
- cart = {}
65
- displayMainMenu ()
66
- choice = getUserInput ("fromMainMenu" )
67
-
68
- if choice == '1' :
69
- openStore .displayStock ()
70
- print ("\n please select from above available stock" , end = "\n " )
71
- elif choice == '2' :
72
- print ("\n What do you want to buy?\n " )
66
+ def addMoreItems ():
67
+ if (getUserInput ("addMoreItems" )):
73
68
displayFruitMenu ()
74
69
choice = getUserInput ("fruitMenu" )
70
+ return choice
71
+ else :
72
+ print ("purchase done" )
73
+
75
74
76
- if choice == '1' :
77
- availableStock = openStore .getStockFromStore ()
78
- count = int (getUserInput ("numbers" ))
79
-
80
- if count <= availableStock ['Apple' ]:
81
- cart ['Apple' ] = count
82
- availableStock ['Apple' ] = availableStock ['Apple' ] - count
83
- print (availableStock )
84
-
85
- print ("Here's your items in cart , " , cart )
86
- # wannaBuyMore = getUserInput("addOrRemove")
87
- # if (wannaBuyMore):
88
- # fruitChoice = getUserInput("fruitMenu")
89
-
90
- # else:
91
- # print("Done")
92
-
93
-
94
- elif choice == '2' :
95
- pass
96
- elif choice == '3' :
97
- pass
75
+ def displayFruitMenu ():
76
+ for i in enumerate (openStore .listOfFruits (), start = 1 ):
77
+ print (i [0 ], i [1 ])
98
78
99
- elif choice == 5 :
100
- pass
79
+ def billFormat (billObj ):
80
+ for fruitName , price in billObj .items ():
81
+ print (fruitName + " - " + str (price ))
82
+
83
+ print ("Total Bill amount to pay " + str (sum (billObj .values ())) + " Rupees \n " )
84
+
85
+
86
+ def checkOutCart ():
87
+ billMap = {}
88
+ cartItems = cartInstance .showCart ()
89
+ for fn ,count in cartItems .items ():
90
+ fruitPrice = openStore .getFruitPrice (fn )
91
+ billMap [fn ] = fruitPrice * count
92
+ billFormat (billMap )
93
+
94
+ def showAvailableFruits ():
95
+ availableFruits = openStore .listOfFruits ()
96
+ print ("Here's the available fruits, happy purchasing\n " )
97
+ for id , fruit in availableFruits .items ():
98
+ print (str (id ) + " - " + fruit [0 ] + "(each " + fruit [0 ] + " cost " + str (fruit [1 ]) + " Rupees)" )
99
+
100
+ def buyFruit (fruitId ):
101
+ if int (fruitId ) in openStore .getFruitsIDs ():
102
+ fruitCount = int (getUserInput ("numbers" ))
103
+ if fruitCount <= openStore .getAvailableCountForFruit (fruitId ):
104
+ cartInstance .addToCart (openStore .getFruitName (fruitId ), fruitCount )
105
+ openStore .updateStock (openStore .getFruitName (fruitId ), fruitCount )
106
+
107
+ print (str (fruitCount ) + " " + openStore .getFruitName (fruitId ) + " added to your cart \n " )
108
+ else :
109
+ print ("The count you entered is either exceeding or we nearing out of stock soon" )
101
110
else :
102
- print ("Invalid input. Please enter number between 1-5 " )
111
+ print ("ID which's entered isn't matching with any fruits which we have! " )
103
112
104
113
105
114
if __name__ == "__main__" :
106
- storeOpens ()
115
+
116
+ while True :
117
+ displayMainMenu ()
118
+ userChoice = getUserInput ("fromMainMenu" )
119
+
120
+ if userChoice == '1' :
121
+ showAvailableFruits ()
122
+ elif userChoice == '2' :
123
+ showAvailableFruits ()
124
+ choice = getUserInput ("fruitMenu" )
125
+ buyFruit (choice )
126
+ if (getUserInput ("addMoreItems" )):
127
+ showAvailableFruits ()
128
+ choice = getUserInput ("fruitMenu" )
129
+ buyFruit (choice )
130
+ # getUserInput("addMoreItems")
131
+ else :
132
+ displayFruitMenu ()
133
+ elif userChoice == '3' :
134
+ cartItems = cartInstance .showCart ()
135
+ print ("Currently you have below items in your cart, " )
136
+ for itemName , itemCount in cartItems .items ():
137
+ print (itemName + "-" + str (itemCount ))
138
+ elif userChoice == '4' :
139
+ checkOutCart ()
140
+
141
+ print ("Enjoy Shopping at Ram's Fruit Store!\n " )
142
+ break
143
+ elif userChoice == '5' :
144
+ break
145
+ elif userChoice == '6' :
146
+ if (getUserInput ("adminStuff" )):
147
+ openStore .displayStock ()
148
+ break
149
+ else :
150
+ print ("Invalid input. Please enter number between 1-6 " )
0 commit comments