-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpythonProject.py
117 lines (105 loc) · 3.52 KB
/
pythonProject.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
import os
import json
from datetime import datetime
item = {
"id" : "",
"name" : "",
"quantity" : "",
"date" : ""
}
def checkFileExist(): # Checks if the file exist in the current path through os module
fileControl = os.listdir()
fn = "elements.json"
try:
for file in fileControl:
if file == "elements.json":
return getInput()
else:
yesno = input("File can not found!\nDo you want to create one?(y/n) : ")
cptlz = yesno.capitalize()
if cptlz == "Y":
with open(fn, "x") as file: # "x", creates a file, returns an error if the file exist
file.write('{\n\t"elements" : [\n\t]\n}')
file.close
with open(fn) as jsonFile:
returnFile = json.load(jsonFile)
elmntsOfJson = returnFile["elements"]
loadItemDict = item
loadItemDict["id"] = 0
elmntsOfJson.append(loadItemDict)
jsonDump(returnFile)
else:
print("File couldn't created.")
except:
pass
def getInput(): # Displays options
print("\n====================\n Inventory Options\n====================\n")
options = ("1) Add an Item\n2) Delete an Item\n3) Update Item Quantity\n4) Change Item Name\n5) Exit\n")
print(options)
try:
choice = int(input("Choose an option : "))
if choice > 5 or choice < 1:
print("Please enter valid number!\n")
return getInput()
else:
driveOptions(choice)
except:
print("Please enter valid number!\n")
return getInput()
def driveOptions(choice): # Forwards input to function that user chose
try:
if choice == 1:
addItem()
if choice == 2:
deleteItem()
if choice == 3:
updateQuantity()
if choice == 4:
changeName()
if choice == 5:
print("\nGoodbye for now...\n")
finally:
pass
def addItem():
print("====================\n Adding Item\n====================\n")
fn = "elements.json"
with open(fn) as f:
idAssign = json.load(f)
idIndex = idAssign["elements"][-1]
idNum = idIndex["id"]
idNum += 1
item["id"] = idNum
item["name"] = input("Enter the name of item : ")
item["quantity"] = int(input("Enter the quantity of item : "))
day = datetime.now()
item["date"] = day.strftime("%d-%m-%Y, %X")
with open(fn) as jsonFile:
returnFile = json.load(jsonFile)
elmntsOfJson = returnFile["elements"]
loadItemDict = item
elmntsOfJson.append(loadItemDict)
jsonDump(returnFile)
def jsonDump(data):
fn = "elements.json"
with open(fn, "w") as f:
json.dump(data, f, indent= 4)
f.close
return getInput()
def deleteItem():
print("====================\n Deleting Item\n====================\n")
dlt = int(input(f"Enter item's id number that you want to delete : "))
nullData = []
fn = "elements.json"
with open(fn) as f:
returnFile = json.load(f)
returnList = returnFile["elements"]
returnList[dlt] = nullData
a = returnList.append(returnList)
jsonDump(returnFile)
def updateQuantity(): # Changes the quantity by id of the item
pass
def changeName(): # Changes the name by id of the item
pass
def giveWarning(): # Gives warning when the stock out of run
pass
checkFileExist()