|
3 | 3 | """ |
4 | 4 | import inventory |
5 | 5 | 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 | + |
6 | 12 |
|
7 | 13 | if __name__ == '__main__': |
8 | 14 | print("Enter the inventory details") |
9 | 15 | inventory_dict = inventory.load_inventory_dict() |
10 | 16 |
|
11 | 17 | 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(): |
16 | 54 | break |
| 55 | + |
| 56 | + |
17 | 57 |
|
18 | | - with open(inventory.inventory_file_path(), 'w') as inv_file: |
19 | | - json.dump(inventory_dict,inv_file,indent=4) |
20 | 58 |
|
21 | 59 |
|
22 | 60 |
|
0 commit comments