Skip to content

Commit ec8fe3d

Browse files
committed
Added updated logic
1 parent a975bf6 commit ec8fe3d

4 files changed

Lines changed: 98 additions & 6 deletions

File tree

July21/EffectivePython/inventory/data/inventory.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111
"name": "oneplus 9",
1212
"price": 90000.0,
1313
"quantity": 15
14+
},
15+
{
16+
"id": 3,
17+
"name": "ipad",
18+
"price": 40000.0,
19+
"quantity": 20
20+
},
21+
{
22+
"id": 4,
23+
"name": "samsung tab",
24+
"price": 25000.0,
25+
"quantity": 15
1426
}
1527
]
1628
}

July21/EffectivePython/inventory/inventory.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ def to_dict(self):
5050
inv['quantity'] = self.quantity
5151
return inv
5252

53+
def __str__(self) -> str:
54+
return f"{self.id}, {self.name}, {self.price}, {self.quantity}"
55+
56+
@staticmethod
57+
def header():
58+
return f"id, name, price, quantity"
59+
60+
61+
5362
@staticmethod
5463
def read_inventory_from_input():
5564
id = int(input('Enter the id: '))

July21/EffectivePython/inventory/main.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,58 @@
33
"""
44
import inventory
55
import json
6+
import menu
7+
8+
def write_to_inventory(inventory_dict):
9+
with open(inventory.inventory_file_path(), 'w') as inv_file:
10+
json.dump(inventory_dict,inv_file,indent=4)
11+
612

713
if __name__ == '__main__':
814
print("Enter the inventory details")
915
inventory_dict = inventory.load_inventory_dict()
1016

1117
while True:
12-
inv = inventory.Inventory.read_inventory_from_input()
13-
inventory_dict['items'].append(inv.to_dict())
14-
choice = input('Enter n to stop and any other key to continue')
15-
if choice == 'n':
18+
choice = menu.main_menu()
19+
20+
if choice == 1:
21+
# Add inventory
22+
inv = inventory.Inventory.read_inventory_from_input()
23+
inventory_dict['items'].append(inv.to_dict())
24+
write_to_inventory(inventory_dict)
25+
elif choice == 2:
26+
# Update the inventory
27+
id = int(input('Enter the Id: '))
28+
count = 0
29+
for item in inventory_dict['items']:
30+
if id == item['id']:
31+
print('product found')
32+
print(item)
33+
price = item['price']
34+
choice = input(f'price: [{price}]')
35+
if choice.strip() != '':
36+
new_price = float(choice.strip())
37+
item['price'] = new_price
38+
quantity = item['quantity']
39+
choice = input(f'quantity: [{quantity}]')
40+
if choice.strip() != '':
41+
new_quantity = int(choice.strip())
42+
item['quantity'] = new_quantity
43+
inventory_dict['items'][count] = item
44+
write_to_inventory(inventory_dict)
45+
break
46+
count = count + 1
47+
48+
else:
49+
# Show the inventory
50+
for item in inventory_dict['items']:
51+
print(str(item))
52+
53+
if not menu.does_user_wants_to_continue():
1654
break
55+
56+
1757

18-
with open(inventory.inventory_file_path(), 'w') as inv_file:
19-
json.dump(inventory_dict,inv_file,indent=4)
2058

2159

2260

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
def main_menu():
3+
"""
4+
This implements the main menu functionality
5+
Returns
6+
1. To Add Inventory Items
7+
2. To Update Inventory
8+
3. To print all the inventory Items
9+
"""
10+
try:
11+
print("Options")
12+
print("1. Add Inventory Items")
13+
print("2. Update Inventory Items")
14+
print("3. Show Inventory Items")
15+
choice = int(input("Enter the choice: "))
16+
if not 1 <= choice <=3:
17+
raise KeyError('Invalid Choice')
18+
return choice
19+
except ValueError as ve:
20+
raise KeyError(ve)
21+
22+
def does_user_wants_to_continue():
23+
"""
24+
This method will ask for the user input to continue or to stop
25+
Returns :
26+
True if user wants to continue
27+
False otherwise
28+
"""
29+
choice = input('Enter n to stop and any other key to continue :')
30+
if choice == 'n':
31+
return False
32+
return True
33+

0 commit comments

Comments
 (0)