Skip to content

Commit

Permalink
Added inventory application
Browse files Browse the repository at this point in the history
  • Loading branch information
qtsathish committed Jul 3, 2021
1 parent 19d40a3 commit 96608a8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions July21/EffectivePython/inventory/data/inventory.json
@@ -0,0 +1 @@
{"items": [{"id": 1, "name": "iphone", "price": 120000.0, "quantity": 10}, {"id": 2, "name": "oneplus 9", "price": 90000.0, "quantity": 10}]}
50 changes: 50 additions & 0 deletions July21/EffectivePython/inventory/inventory.py
@@ -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
22 changes: 22 additions & 0 deletions July21/EffectivePython/inventory/main.py
@@ -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)



0 comments on commit 96608a8

Please sign in to comment.