Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"items": [{"id": 1, "name": "iphone", "price": 120000.0, "quantity": 10}, {"id": 2, "name": "oneplus 9", "price": 90000.0, "quantity": 10}]} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import os | ||
from dataclasses import dataclass | ||
def get_empty_inventory(): | ||
""" | ||
This method returns an empty dictionary containing | ||
inventory data | ||
""" | ||
inventory_dict = dict() | ||
inventory_dict['items'] = list() | ||
return inventory_dict | ||
|
||
def inventory_file_path(): | ||
""" | ||
This method returns standard inventory file path | ||
""" | ||
return 'data/inventory.json' | ||
|
||
def is_inventory_available(): | ||
""" | ||
Returns: | ||
True if the inventory file is found else returns false | ||
""" | ||
return os.path.exists(inventory_file_path()) and os.path.isfile(inventory_file_path()) | ||
|
||
@dataclass | ||
class Inventory: | ||
id: int | ||
name: str | ||
price: float | ||
quantity: int | ||
|
||
def to_dict(self): | ||
""" | ||
This method returns dictionary representation of Inventory object | ||
""" | ||
inv = dict() | ||
inv['id'] = self.id | ||
inv['name'] = self.name | ||
inv['price'] = self.price | ||
inv['quantity'] = self.quantity | ||
return inv | ||
|
||
@staticmethod | ||
def read_inventory_from_input(): | ||
id = int(input('Enter the id: ')) | ||
name = input('Enter the product name: ') | ||
price = float(input('Enter the product price: ')) | ||
quantity = int(input('Enter the quantity: ')) | ||
inventory = Inventory(id,name,price,quantity) | ||
return inventory |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
""" | ||
This module is used to store the inventory data | ||
""" | ||
import inventory | ||
import json | ||
|
||
if __name__ == '__main__': | ||
print("Enter the inventory details") | ||
inventory_dict = inventory.get_empty_inventory() | ||
|
||
while True: | ||
inv = inventory.Inventory.read_inventory_from_input() | ||
inventory_dict['items'].append(inv.to_dict()) | ||
choice = input('Enter n to stop and any other key to continue') | ||
if choice == 'n': | ||
break | ||
|
||
with open(inventory.inventory_file_path(), 'w') as inv_file: | ||
json.dump(inventory_dict, inv_file) | ||
|
||
|
||
|