Last active
October 27, 2019 00:41
-
-
Save Jadens-arc/52ba2b6393f471edab36f3c51663b0c1 to your computer and use it in GitHub Desktop.
A simple json inventory system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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