Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. import os
  2. import pickle
  3.  
  4. class Inventory():
  5. units = ["pounds", "units", "boxes"]
  6.  
  7. def __init__(self):
  8.  
  9. try:
  10. dbFile=open("Inventory.db","rb")
  11. self.invDict=pickle.load(dbFile)
  12. except:
  13. self.invDict = dict()
  14. print("No .db found, creating new database!")
  15. else:
  16. print("Opened Inventory.db!")
  17. for k,v in self.invDict.items():
  18. print("You have {} {} of {}!".format(v.count, v.unit, k))
  19. finally:
  20. input("Press enter to continue...")
  21.  
  22. # self.invSet = set()
  23. # self.invList = list()
  24.  
  25. def remItem(self):
  26. remove=True
  27.  
  28. while remove==True:
  29. os.system("cls")
  30. print("Here is the current inventory, which selection would you like to remove from?")
  31. for k,v in self.invDict.items():
  32. print("{} {} of {}".format(v.count, v.unit, k))
  33. name=input()
  34.  
  35. if name in self.invDict:
  36. print("Out of {} {}, how many {} would you like to remove?".format(self.invDict[name].count, self.invDict[name].unit, self.invDict[name].name))
  37. self.invDict[name].count - float(input())
  38. else:
  39. os.system("cls")
  40. choice = input("That's not currently in your inventory, would you like to add an item instead? (y/n)")
  41. if choice.lower() == "n" or choice.lower() == "N":
  42. remove=True
  43. else:
  44. Inventory.addItem()
  45. finally:
  46. os.system("cls")
  47. print("Now there are only {} {} of {}")
  48.  
  49. choice = input("Would you like to remove another item? (y/n)")
  50. if choice.lower() == "n" or choice.lower() == "no":
  51.  
  52. remove=False
  53. else:
  54. remove=True
  55.  
  56.  
  57. def addItem(self, name=None, count=None, unit=None):
  58.  
  59. add = True
  60.  
  61. while add == True:
  62. os.system("cls")
  63. print("What is the item name?")
  64. name = input()
  65.  
  66. os.system("cls")
  67.  
  68.  
  69. if name in self.invDict:
  70. unit = self.invDict[name].unit
  71. print("You already have {} {} of {}, how many {} would you like to add?".format(self.invDict[name].count, self.invDict[name].unit, self.invDict[name].name, self.invDict[name].unit))
  72. while unit == None:
  73. try:
  74. print("What are the units?")
  75. for i in Inventory.units:
  76. print(i,Inventory.units.index(i))
  77. #inputUnit = int(input())
  78. #unit = Inventory.units[inputUnit]
  79. unit = Inventory.units[int(input())]
  80. except:
  81. os.system("cls")
  82. print("That unit is invalid, please type a number displayed:")
  83. print(i,Inventory.units.index(i))
  84. else:
  85. os.system("cls")
  86. print("You selected", unit+".")
  87.  
  88. print("How many {} of {}?".format(unit, name))
  89. while count == None:
  90. try:
  91. count = float(input())
  92. except:
  93. print("Whoops, that's not a number! Please input a valid amount")
  94. else:
  95. os.system("cls")
  96.  
  97. x = InvItem(name, count, unit)
  98. #print(x,x.name)
  99. #self.invDict[x.name]=x
  100. if x.name in self.invDict:
  101. self.invDict[x.name].count += x.count
  102. else:
  103. self.invDict[x.name] = x
  104. finally:
  105. pickle.dump(self.invDict, open("Inventory.db","wb"))
  106.  
  107. for k,v in self.invDict.items():
  108. print("You have {} {} of {}!".format(v.count, v.unit, k))
  109. #print(self.invDict)
  110. choice = input("Would you like to add another item? (y/n)")
  111. if choice.lower() == "n" or choice.lower() == "no":
  112.  
  113. add = False
  114. else:
  115. name=None
  116. count=None
  117. unit=None
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. class InvItem(Inventory):
  125.  
  126. def __init__(self, name, count, unit):
  127. super().__init__()
  128. self.name = name
  129. self.count = count
  130. self.unit = unit
  131.  
  132. i = Inventory()
  133. i.addItem()
  134.  
  135. input("Press enter to exit")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement