Product Inventory


/ Published in: Python
Save to your folder(s)

My novice attempt at the project listed at: https://github.com/karan/Projects/blob/master/README.md

Feedback is welcome.


Copy this code and paste it in your HTML
  1. # Product Inventory Project
  2. # Create an application which manages an inventory of products.
  3. # Create a product class which has a price, ID, and qty on hand.
  4. # Then create an inventory class which keeps track of various
  5. # products and can sum up the inventory value.
  6.  
  7. class product(object):
  8.  
  9. if __name__ == '__main__':
  10.  
  11. inventory = [[], [], [], []]
  12. types = {'pid' : 0, 'name' : 1, 'price' : 2, 'qty' : 3}
  13.  
  14. def __init__(self):
  15. self.pid = self.inventory[0]
  16. self.name = self.inventory[1]
  17. self.price = self.inventory[2]
  18. self.qty = self.inventory[3]
  19.  
  20. def add(self, pid, name, price, qty):
  21. ''' (int, str, float, int) - > str
  22.  
  23. Adds an item into the inventory containing the following data:
  24. PID, name, price and qty.
  25.  
  26. >>>product.add(74025, 'Bathwater', 62.78, 46)
  27. Product Added
  28. >>>product.add(12674, 'Water', 201.35, 22)
  29. Product Added
  30. >>>inventory.lookup(74025)
  31. Product #: 74025 Name: Bathwater
  32. Price per unit: 62.78 Qty: 46
  33.  
  34. Finished Searching.
  35. >>> inventory.lookup(12674)
  36. Product #: 12674 Name: Water
  37. Price per unit: 201.35 Qty: 22
  38. '''
  39.  
  40. pid = self.pid.append(pid)
  41. name = self.name.append(name)
  42. price = self.price.append(price)
  43. qty = self.qty.append(qty)
  44.  
  45. print('Product Added')
  46.  
  47. def remove(self, pid):
  48. ''' (int) -> str
  49.  
  50. Item is located by user input PID and removed from inventory.
  51.  
  52. >>>product.remove(63456)
  53. Product Removed
  54. >>>product.remove(12674)
  55. Product Removed
  56. >>>inventory.showall()
  57. Product #: 32345 Name: Crazy Texas Robot Millionaire
  58. Price per unit: 40.0 Qty: 54
  59.  
  60. Product #: 41058 Name: Some wires
  61. Price per unit: 40.0 Qty: 2
  62.  
  63. Product #: 71658 Name: Mice
  64. Price per unit: 12.84 Qty: 154
  65.  
  66. Product #: 74025 Name: Bathwater
  67. Price per unit: 62.78 Qty: 46
  68.  
  69. Product #: 81324 Name: Livers
  70. Price per unit: 85.95 Qty: 26
  71.  
  72. Product #: 87421 Name: Poo
  73. Price per unit: 6.99 Qty: 0 - PRODUCT IS OUT OF STOCK
  74.  
  75. Product #: 97256 Name: Shoes with Attitude
  76. Price per unit: 40.0 Qty: 9
  77.  
  78. End of List
  79. '''
  80.  
  81. ind = self.pid.index(pid)
  82. for p in self.inventory:
  83. p.remove(p[ind])
  84.  
  85. print('Product Removed')
  86.  
  87. def changevalue(self, pid, typ, new):
  88. ''' (int, str, str) -> str
  89.  
  90. Changes the value of an item with user specified type
  91. Valid types are 'pid', 'name', 'price' and 'qty'
  92.  
  93. >>> product.changevalue(97256, 'pid', 45206)
  94. Value successfully updated
  95. >>> product.changevalue(81324, 'name', 'tacos')
  96. Value successfully updated
  97. >>> inventory.lookup(45206)
  98. Product #: 45206 Name: Shoes with Attitude
  99. Price per unit: 40.0 Qty: 9
  100.  
  101. Finished Searching.
  102. >>> inventory.lookup(81324)
  103. Product #: 81324 Name: tacos
  104. Price per unit: 85.95 Qty: 26
  105.  
  106. Finished Searching.
  107. '''
  108.  
  109. self.key = self.types.get(typ)
  110. ind = self.pid.index(pid)
  111.  
  112. self.inventory[self.key][ind] = new
  113. print('Value successfully updated')
  114.  
  115. class inventory(object):
  116.  
  117. if __name__ == '__main__':
  118.  
  119. def __init__(self):
  120. self.inventory = product.inventory
  121. self.types = product.types
  122. self.pid = product.pid
  123. self.name = product.name
  124. self.nam = self.types.get('name')
  125. self.key = self.types.get('pid')
  126. self.qty = self.types.get('qty')
  127. self.pri = self.types.get('price')
  128. self.g = list()
  129. self.h = list()
  130.  
  131. def sort_inventory(self):
  132. self.g = []
  133. for p in range(len(self.inventory[0])):
  134. for j in range(len(self.inventory)):
  135. self.h.append(self.inventory[j][p])
  136. self.g.append(self.h)
  137. self.h = []
  138. self.g.sort(key=lambda g: g[0])
  139.  
  140. def showall(self):
  141. ''' (none) -> list
  142.  
  143. Returns a list of PID, NAME, PRICE, QTY per product for each listing
  144. currently in inventory. If QTY is 0, item will be labeled "Out of stock"
  145.  
  146. >>>inventory.showall()
  147. Product #: 32345 Name: Crazy Texas Robot Millionaire
  148. Price per unit: 40.0 Qty: 54
  149.  
  150. Product #: 41058 Name: Some wires
  151. Price per unit: 40.0 Qty: 2
  152.  
  153. Product #: 45206 Name: Shoes with Attitude
  154. Price per unit: 40.0 Qty: 9
  155.  
  156. Product #: 71658 Name: Mice
  157. Price per unit: 12.84 Qty: 154
  158.  
  159. Product #: 74025 Name: Bathwater
  160. Price per unit: 62.78 Qty: 46
  161.  
  162. Product #: 81324 Name: tacos
  163. Price per unit: 85.95 Qty: 26
  164.  
  165. Product #: 87421 Name: Poo
  166. Price per unit: 6.99 Qty: 0 - PRODUCT IS OUT OF STOCK
  167.  
  168. End of List
  169. '''
  170.  
  171. self.sort_inventory()
  172. for x in self.g:
  173. print('Product #: ', end='')
  174. print(x[self.key], end=' ')
  175. print('Name: ', end='')
  176. print(x[self.nam], end='\n')
  177. print('Price per unit: ', end='')
  178. print(x[self.pri], end=' ')
  179. print('Qty: ', end='')
  180. print(x[self.qty], end=' ')
  181. if x[self.qty] == 0:
  182. print('- PRODUCT IS OUT OF STOCK', end='\n \n')
  183. else:
  184. print('', end='\n \n')
  185. print('End of List')
  186.  
  187. def lookup(self, value):
  188. ''' (int) -> list , (float) -> list , (str) -> list
  189.  
  190. Returns a list of all products in inventory which share the same
  191. value input
  192.  
  193. >>> inventory.lookup(40.00)
  194. Product #: 32345 Name: Crazy Texas Robot Millionaire
  195. Price per unit: 40.0 Qty: 54
  196.  
  197. Product #: 41058 Name: Some wires
  198. Price per unit: 40.0 Qty: 2
  199.  
  200. Product #: 97256 Name: Shoes with Attitude
  201. Price per unit: 40.0 Qty: 9
  202.  
  203. Finished Searching
  204. >>> inventory.lookup(0)
  205. Product #: 87421 Name: Poo
  206. Price per unit: 6.99 Qty: 0 - PRODUCT IS OUT OF STOCK
  207.  
  208. Finished Searching.
  209. '''
  210.  
  211. self.sort_inventory()
  212. for x in self.g:
  213. if value in x:
  214. print('Product #: ', end='')
  215. print(x[self.key], end=' ')
  216. print('Name: ', end='')
  217. print(x[self.nam], end='\n')
  218. print('Price per unit: ', end='')
  219. print(x[self.pri], end=' ')
  220. print('Qty: ', end='')
  221. print(x[self.qty], end=' ')
  222. if x[self.qty] == 0:
  223. print('- PRODUCT IS OUT OF STOCK', end='\n \n')
  224. else:
  225. print('', end='\n \n')
  226. print('Finished Searching.')
  227.  
  228. def get_qty(self, pid):
  229. ''' (int) -> float
  230.  
  231. Returns the quantity of product from specified PID
  232.  
  233. >>> inventory.get_qty(45206)
  234. Product #: 45206 Name: Shoes with Attitude
  235. Units in stock: 9
  236. >>> inventory.get_qty(87421)
  237. Product #: 87421 Name: Poo
  238. Units in stock: 0 - PRODUCT IS OUT OF STOCK
  239. '''
  240.  
  241. self.sort_inventory()
  242. ind = self.inventory[self.key].index(pid)
  243.  
  244. print('Product #: ', end='')
  245. print(pid, end=' ')
  246. print('Name: ', end='')
  247. print(self.inventory[self.nam][ind], end='\n')
  248. print('Units in stock:', end=' ')
  249. print(self.inventory[self.qty][ind], end=' ')
  250. if self.inventory[self.qty][ind] == 0:
  251. print('- PRODUCT IS OUT OF STOCK', end='')
  252.  
  253.  
  254. def get_prod_bel_qty(self, qty):
  255. ''' (int) -> list
  256.  
  257. Returns a list of all products in inventory whos quantities are the
  258. same or fall below input qty
  259.  
  260. >>> inventory.get_prod_bel_qty(50)
  261. Product #: 41058 Name: Some wires
  262. Units in stock: 2
  263.  
  264. Product #: 45206 Name: Shoes with Attitude
  265. Units in stock: 9
  266.  
  267. Product #: 74025 Name: Bathwater
  268. Units in stock: 46
  269.  
  270. Product #: 81324 Name: tacos
  271. Units in stock: 26
  272.  
  273. Product #: 87421 Name: Poo
  274. Units in stock: 0 - PRODUCT IS OUT OF STOCK
  275.  
  276. Finished Searching.
  277. '''
  278.  
  279. self.sort_inventory()
  280. for x in self.g:
  281. if qty >= x[self.qty]:
  282. print('Product #: ', end='')
  283. print(x[self.key], end=' ')
  284. print('Name: ', end='')
  285. print(x[self.nam], end='\n')
  286. print('Units in stock:', end=' ')
  287. print(x[self.qty], end=' ')
  288. if x[self.qty] == 0:
  289. print('- PRODUCT IS OUT OF STOCK', end='\n \n')
  290. else:
  291. print('', end='\n \n')
  292. print('Finished Searching.')
  293.  
  294. def sum_inventory_value(self):
  295. ''' (none) -> list
  296.  
  297. Returns a list containing how much money the entire quantities of
  298. each product in inventory are worth. Also returns the sum of the
  299. value of the products in inventory for a total inventory value.
  300.  
  301. >>> inventory.sum_inventory_value()
  302. Product #: 32345 Name: Crazy Texas Robot Millionaire
  303. Price per unit: 40.0 @ 54/unit
  304. Total product value: 2160.0
  305.  
  306. Product #: 41058 Name: Some wires
  307. Price per unit: 40.0 @ 2/unit
  308. Total product value: 80.0
  309.  
  310. Product #: 45206 Name: Shoes with Attitude
  311. Price per unit: 40.0 @ 9/unit
  312. Total product value: 360.0
  313.  
  314. Product #: 71658 Name: Mice
  315. Price per unit: 12.84 @ 154/unit
  316. Total product value: 1977.36
  317.  
  318. Product #: 74025 Name: Bathwater
  319. Price per unit: 62.78 @ 46/unit
  320. Total product value: 2887.88
  321.  
  322. Product #: 81324 Name: tacos
  323. Price per unit: 85.95 @ 26/unit
  324. Total product value: 2234.7
  325.  
  326. Product #: 87421 Name: Poo
  327. Price per unit: 6.99 @ 0/unit
  328. Total product value: 0.0
  329.  
  330. Total value of all units: 9699.94
  331. '''
  332.  
  333. a = 0
  334. self.g = []
  335. self.sort_inventory()
  336. for x in self.g:
  337. i = x[self.qty] * x[self.pri]
  338. a = i + a
  339. print('Product #: ', end='')
  340. print(x[self.key], end=' ')
  341. print('Name: ', end='')
  342. print(x[self.nam], end='\n')
  343. print('Price per unit: ', end='')
  344. print(x[self.pri], end='')
  345. print(' @ ', end='')
  346. print(x[self.qty], end='')
  347. print('/unit', end='\n')
  348. print('Total product value: ', end='')
  349. print(round(i, 2), end='\n \n')
  350. print('Total value of all units: ', end='')
  351. print(round(a, 2), end='')
  352.  
  353. #Sould be easy enough remember for ease of testing.
  354. product = product()
  355. inventory = inventory()
  356.  
  357. #I have included a small inventory for ease of testing
  358. product.add(63456, 'Meow', 60.00, 0)
  359. product.add(32345, 'Crazy Texas Robot Millionaire', 40.00, 54)
  360. product.add(97256, 'Shoes with Attitude', 40.00, 9)
  361. product.add(81324, 'Livers', 85.95, 26)
  362. product.add(87421, 'Poo', 6.99, 0)
  363. product.add(41058, 'Some wires', 40.00, 2)
  364. product.add(71658, 'Mice', 12.84, 154)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.