Skip to content

Instantly share code, notes, and snippets.

@Jadens-arc
Last active October 27, 2019 00:41
Show Gist options
  • Save Jadens-arc/52ba2b6393f471edab36f3c51663b0c1 to your computer and use it in GitHub Desktop.
Save Jadens-arc/52ba2b6393f471edab36f3c51663b0c1 to your computer and use it in GitHub Desktop.
A simple json inventory system
import json
class Inventory:
def __init__(self, userFile):
self.inventoryDict = json.loads(open(userFile, 'r').read())
self.inventoryJson = open(userFile, 'w')
def newItem(self, item, amount):
self.inventoryDict[item] = amount
strInv = json.dumps(self.inventoryDict)
self.inventoryJson.write(strInv)
def updateItem(self, item, amount):
self.inventoryDict[item] = (self.inventoryDict[item] + amount)
strInv = json.dumps(self.inventoryDict)
self.inventoryJson.write(strInv)
def removeItem(self, item):
self.inventoryDict.pop(item)
strInv = json.dumps(str(self.inventoryDict))
self.inventoryJson.write(strInv)
myInventory = Inventory('Inventory.json')
myInventory.newItem()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment