-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.py
161 lines (138 loc) · 5.43 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
from FStore import FStore
from FStore import Cart
from log import Log
import time
import getpass
import logging
import json
logging.basicConfig(filename="general.log",
format='%(asctime)s %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p')
def getAvilableStock():
stockInfo = open(r"FruitStore\stock.json", "r")
logging.warning("1.0 Stock Downloaded")
Log.registerWarning("Stock downloaded ....... :)")
return json.load(stockInfo)
openStore = FStore(getAvilableStock())
cartInstance = Cart()
def getUserInput(fromWhichMenu):
inputMessage = ''
if fromWhichMenu == "fromMainMenu":
inputMessage = "Please enter your choice : "
elif fromWhichMenu == "fruitMenu":
inputMessage = "Please enter fruit id : "
elif fromWhichMenu == "numbers":
inputMessage = "how many you need? "
elif fromWhichMenu == "addMoreItems":
try:
choice = input("Do you want to add more items to your cart? Y or N ").strip()
if choice == "Y" or choice == "y" or choice == "yes" or choice == "YES":
return True
else:
return False
except ValueError:
print("That's not an int!")
elif fromWhichMenu == "adminStuff":
try:
choice = getpass.getpass("Enter admin password")
if choice == "admin123":
return True
else:
return False
except ValueError:
print("That's not a valid password!")
try:
choice = input(inputMessage).strip()
except ValueError:
print("That's not an int!")
logging.warning("User Action: " + choice)
return choice
def displayMainMenu():
logging.warning("Displayed Main Menu to the user!")
print("""
1. Show available fruits
2. Buy Fruits
3. Show Cart
4. Checkout
5. Exit
6. Display available Stocks (only store admin can access)
""")
def addMoreItems():
if (getUserInput("addMoreItems")):
displayFruitMenu()
choice = getUserInput("fruitMenu")
return choice
else:
print("purchase done")
def displayFruitMenu():
for i in enumerate(openStore.listOfFruits(), start=1):
print(i[0], i[1])
def billFormat(billObj):
for fruitName, price in billObj.items():
print(fruitName + " - " + str(price))
print("Total Bill amount to pay " + str(sum(billObj.values())) + " Rupees \n")
logging.warning("Total Bill amount to pay " + str(sum(billObj.values())) + " Rupees \n")
def checkOutCart():
billMap = {}
cartItems = cartInstance.showCart()
logging.warning("Checking out items from cart - " + str(cartItems) + " in cart")
for fn,count in cartItems.items():
fruitPrice = openStore.getFruitPrice(fn)
billMap[fn] = fruitPrice * count
logging.warning("Preparing bill..")
billFormat(billMap)
def showAvailableFruits():
availableFruits = openStore.listOfFruits()
print("Here's the available fruits, happy purchasing\n")
for id, fruit in availableFruits.items():
print(str(id) + " - " + fruit[0] + "(each " + fruit[0] + " cost " + str(fruit[1]) + " Rupees)")
def buyFruit(fruitId):
if fruitId == '':
return 0
if int(fruitId) in openStore.getFruitsIDs():
fruitCount = int(getUserInput("numbers"))
if fruitCount <= openStore.getAvailableCountForFruit(fruitId):
cartInstance.addToCart(openStore.getFruitName(fruitId), fruitCount)
openStore.updateStock(openStore.getFruitName(fruitId), fruitCount)
print(str(fruitCount) + " " +openStore.getFruitName(fruitId) + " added to your cart \n")
logging.warning(str(fruitCount) + " " +openStore.getFruitName(fruitId) + " added to your cart")
else:
print("The count you entered is either exceeding or we nearing out of stock soon")
else:
print("ID which's entered isn't matching with any fruits which we have!")
if __name__ == "__main__":
while True:
displayMainMenu()
userChoice = getUserInput("fromMainMenu")
if userChoice == '1':
showAvailableFruits()
elif userChoice == '2':
showAvailableFruits()
choice = getUserInput("fruitMenu")
buyFruit(choice)
if(getUserInput("addMoreItems")):
for i in range(len(openStore.giveAvailableFruitsInStock())):
showAvailableFruits()
choice = getUserInput("fruitMenu")
buyFruit(choice)
else:
displayFruitMenu()
elif userChoice == '3':
cartItems = cartInstance.showCart()
print("Currently you have below items in your cart, ")
logging.warning("Showing cart to user, available item/s is/are - " + str(cartItems))
for itemName, itemCount in cartItems.items():
print(itemName + "-" + str(itemCount))
time.sleep(7)
elif userChoice == '4':
checkOutCart()
print("Enjoy Shopping at Ram's Fruit Store!\n")
break
elif userChoice == '5':
break
elif userChoice == '6':
if(getUserInput("adminStuff")):
openStore.displayStock()
break
else:
print("Invalid input. Please enter number between 1-6 ")