-
Notifications
You must be signed in to change notification settings - Fork 0
/
warehouse.py
239 lines (189 loc) · 5.72 KB
/
warehouse.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
"""
Program: warehouse control
Author: Ingrid
Functionality:
- Register Items
- id (auto generated): int
- title: str
- category: str
- stock: int
- price: float
"""
# imports
from menu import print_menu, clear, print_item, print_header
from item import Item
import pickle
# global vars
catalog = []
last_id = 0
data_file = 'warehouse.data'
temp_prices =[]
# functions
def serialize_catalog():
global data_file
writer = open(data_file, 'wb') #wb => create/overwrite the file
pickle.dump(catalog, writer)
writer.close()
print(' Data saved! ')
def deserialize_catalog():
global data_file
global last_id
try:
reader = open(data_file, 'rb') #rb => read binary, throw exception if file does not exist
temp_list = pickle.load(reader)
for item in temp_list:
catalog.append(item)
last = catalog[ -1 ]
last_id = last.id +1
how_many = len(catalog)
print(' Deserialized ' + str(how_many) + ' items ')
except:
print("Error, ")
def register_item():
global last_id
try:
print_header("Register new Item")
title = input('Please provide the title: ')
category = input('Please provide category: ')
stock = int(input('Please provide stock: '))
price = float(input('Please provide price: '))
the_item = Item(last_id, title, category, stock, price)
last_id += 1
catalog.append(the_item)
count = len(catalog)
print(" Item saved, you have " + str(count) + " items in your catalog ")
except ValueError:
print("Error, provide valid numbers")
except:
print("Error, please verfiy data")
def display_catalog():
#input('test')
#print_header("Your catalog")
for item in catalog:
print_item(item)
def out_of_stock():
print_header("Items out of stock")
for item in catalog:
if(item.stock == 0):
print_item(item)
def total_stock_value():
print_header("total stock value")
total = 0.0
for item in catalog:
total += item.stock * item.price
print('The total is: $ ' + str(total))
def update_price():
display_catalog()
id = input ('Please choose an id: ')
found = False
for item in catalog:
if(str(item.id) == id):
found = True
price = float(input("Provide new price $"))
item.price = price
if(not found):
print("Error, invalid ID, try again")
def delete_item():
display_catalog()
id = input ('Please choose an item ID to remove : ')
found = False
for item in catalog:
if(str(item.id)== id):
found = True
catalog.remove(item)
print('Item has been deleted')
if (not found):
print(" ERORR, invalid ID, not found.")
def update_stock_value():
display_catalog()
id = input ('Please choose an id:')
found = False
for item in catalog:
if(str(item.id) == id):
found = True
stock_value = float(input("Provide new stock value: "))
item.stock = stock_value
if(not found):
print("Error, invalid ID, try again")
def display_categories():
print_header("Unique categories")
temp = []
for item in catalog:
if(not item.category in temp):
temp.append(item.category)
print(item.category)
def cheapest_product():
print_header("Cheapest Product")
cheapest = catalog[0] # first on the list
for item in catalog: # travel array and compare
if(item.price < cheapest.price):
cheapest = item # keep item as the cheapest
print_item(cheapest)
def sorted_top_prices():
#temp_prices = [] MOVED TO GLOBAL VAR
for item in catalog:
temp_prices.append(item.price) #array with only prices
temp_prices.sort(reverse=True) # sorted prices from large to small
#slice top 3 to only show first 3 prices in list
# print(temp_prices[0:3])
def most_expensive():
print_header("3 Most Expensive Products")
for item in catalog:
if(item.price == temp_prices[0]):
print_item(item)
for item in catalog:
if(item.price == temp_prices[1]):
print_item(item)
for item in catalog:
if(item.price == temp_prices[2]):
print_item(item)
# instructions
deserialize_catalog()
input ('Press Enter to continue...') #putting input bc otherwise it'll go to 'clear'
opc = ''
while(opc != 'x'):
clear()
print_menu()
opc = input('Please select an option: ')
print(opc)
if ( opc == '1'):
register_item()
serialize_catalog()
elif(opc == '2'):
# create a function, call, travel the list and display the title.title
display_catalog()
elif(opc == '3'):
out_of_stock()
# call the fn
# create the fn
# travel the list
# if item.stock is equal to zero
# display_item
elif(opc == '4'):
total_stock_value()
elif(opc == '5'):
update_price()
serialize_catalog() # everytime we modify the data, we are saving the changes
elif(opc== '6'):
delete_item()
serialize_catalog()
elif(opc == '7'):
update_stock_value()
serialize_catalog()
elif(opc == '8'):
display_categories()
elif(opc == '9'):
cheapest_product()
elif(opc == '10'):
sorted_top_prices()
most_expensive()
input('Press Enter to continue...')
#Recommendations for updating price
"""
- show catalog
- ask the user to choose an id
- find the id in the catalog
ask for the new price
set the price
- else, show an error
"""