I have an inventory program that works but has one problem. My program lets you add, remove, update, search, and print inventory items, qty, and ID number. Search(option 4) needs to be able to search an inventory item by characters in its name. For example, if Oranges are in inventory, I should be able to type or and it can pull it up and display all the information about it including name, qty, and ID number.
The problem: Currently search only works if you type in the ID number. Question: how do I change over this search function without causing issues for my other program options. I will attach the full program, but the problem is in def search.
Code:
JavaScript
x
174
174
1
import os
2
import json
3
4
class Inventory:
5
def __init__(self):
6
#AT LAUNCH GROUPS AND LOADING FUNCTION
7
self.items = {}
8
self.load()
9
10
def remove(self, ID):
11
#REMOVING ITEMS FOR LISTS AND OUTPUT DOCUMENT
12
del self.items[str(ID)]
13
self.save()
14
15
def add(self, ID, name, qty):
16
#ADDING ITEMS FOR LISTS AND OUTPUT DOCUMENT
17
self.items[str(ID)] = {"name": name, "qty": qty}
18
self.save()
19
20
def update(self, ID, update):
21
#UPDATING ITEMS FOR LISTS AND OUTPUT DOCUMENT
22
self.items[str(ID)]["qty"] += update
23
self.save()
24
25
def search(self, query):
26
for id in self.items:
27
if str(query) == id:
28
return id, self.items[id]['name'], self.items[id]['qty']
29
return None
30
31
def __str__(self):
32
#FORMATTING
33
out = ""
34
for id, d in self.items.items():
35
out += f"ID Number : {id} nItem Name : {d['name']}nQuantity : {d['qty']}n"
36
out += "----------n"
37
return out
38
39
def save(self):
40
#WHERE TO SAVE TO
41
with open('data.txt','w') as outfile:
42
json.dump(self.items, outfile)
43
44
def load(self):
45
#WHERE TO PUT DATA FROM WHEN RELAUNCHING PROGRAM
46
try:
47
with open('data.txt','r') as json_file:
48
self.items = json.load(json_file)
49
except:
50
print("Can't load old inventory, starting fresh")
51
self.items = {}
52
53
54
def menuDisplay():
55
#MENU FOR PROGRAM
56
"""Display the menu"""
57
print('=============================')
58
print('= Inventory Management Menu =')
59
print('=============================')
60
print('(1) Add New Item to Inventory')
61
print('(2) Remove Item from Inventory')
62
print('(3) Update Inventory')
63
print('(4) Search Item in Inventory')
64
print('(5) Print Inventory Report')
65
print('(99) Quit')
66
67
68
def add_one_item(inventory):
69
#ADDING PROMPT AND ERROR CHECKING
70
print('Adding Inventory')
71
print('================')
72
while True:
73
try:
74
new_ID = int(input("Enter an ID number for the item: "))
75
if inventory.search(new_ID):
76
print("ID number is taken, please enter a different ID number")
77
continue
78
new_name = input('Enter the name of the item: ')
79
new_qty = int(input("Enter the quantity of the item: "))
80
inventory.add(new_ID, new_name, new_qty)
81
break
82
except Exception as e:
83
print("Invalid choice! try again! " + str(e))
84
print()
85
86
87
def remove_one_item(inventory):
88
#REMOVING PROMPT AND ERROR CHECKING
89
print('Removing Inventory')
90
print('==================')
91
removing = int(input("Enter the item's ID number to remove from inventory: "))
92
inventory.remove(removing)
93
94
95
def ask_exit_or_continue():
96
#OPTION TO CONTINUE OR QUITE PROGRAM
97
return int(input('Enter 98 to continue or 99 to exit: '))
98
99
100
def update_inventory(inventory):
101
#UPDATING PROMPT AND ERROR CHECKING
102
print('Updating Inventory')
103
print('==================')
104
while True:
105
try:
106
ID = int(input("Enter the item's ID number to update: "))
107
if inventory.search(ID):
108
update = int(input("Enter the updated quantity. Enter 5 for additional or -5 for less: "))
109
inventory.update(ID, update)
110
else:
111
print("ID number is not in the system, please enter a different ID number")
112
continue
113
break
114
except Exception as e:
115
print("Invalid choice! try again! " + str(e))
116
print()
117
118
def search_inventory(inventory):
119
#SEARCHING PROMPT AND ERROR CHECKING
120
print('Searching Inventory')
121
print('===================')
122
search = input("Enter the name of the item: ")
123
result = inventory.search(search)
124
if result is None:
125
print("Item not in inventory")
126
else:
127
ID, name, qty = result
128
print('ID Number: ', ID)
129
print('Item: ', name)
130
print('Quantity: ', qty)
131
print('----------')
132
133
134
def print_inventory(inventory):
135
#PRINT CURRENT LIST OF ITEMS IN INVENTORY
136
print('Current Inventory')
137
print('=================')
138
print(inventory)
139
140
141
def main():
142
#PROGRAM RUNNING COMMAND AND ERROR CHECKING
143
inventory = Inventory()
144
while True:
145
try:
146
menuDisplay()
147
CHOICE = int(input("Enter choice: "))
148
if CHOICE in [1, 2, 3, 4, 5]:
149
if CHOICE == 1:
150
add_one_item(inventory)
151
elif CHOICE == 2:
152
remove_one_item(inventory)
153
elif CHOICE == 3:
154
update_inventory(inventory)
155
elif CHOICE == 4:
156
search_inventory(inventory)
157
elif CHOICE == 5:
158
print_inventory(inventory)
159
exit_choice = ask_exit_or_continue()
160
if exit_choice == 99:
161
exit()
162
elif CHOICE == 99:
163
exit()
164
except Exception as e:
165
print("Invalid choice! try again!"+str(e))
166
print()
167
168
# If the user pick an invalid choice,
169
# the program will come to here and
170
# then loop back.
171
172
173
main()
174
Advertisement
Answer
Add by parameter and send that along with your query.
JavaScript
1
15
15
1
def search(self, query, by):
2
if by == "id":
3
for id in self.items:
4
if str(query) == id:
5
return id, self.items[id]['name'], self.items[id]['qty']
6
elif by == "name":
7
for id in self.items:
8
if str(query) == self.items[id]['name']:
9
return id, self.items[id]['name'], self.items[id]['qty']
10
elif by == "qty":
11
for id in self.items:
12
if str(query) == self.items[id]['qty']:
13
return id, self.items[id]['name'], self.items[id]['qty']
14
return None
15