Skip to content

Commit

Permalink
remove factory classes and refactor product creation to use a single …
Browse files Browse the repository at this point in the history
…factory
  • Loading branch information
Lauritsen1 committed Mar 7, 2024
1 parent 072ca1d commit 5270e47
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 28 deletions.
2 changes: 0 additions & 2 deletions factories/__init__.py

This file was deleted.

6 changes: 0 additions & 6 deletions factories/factory.py

This file was deleted.

6 changes: 0 additions & 6 deletions factories/laptop_factory.py

This file was deleted.

6 changes: 0 additions & 6 deletions factories/tv_factory.py

This file was deleted.

36 changes: 28 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
from db import Connection

from factories.tv_factory import TVFactory
from factories.laptop_factory import LaptopFactory
from pick import pick

from product_factory import ProductFactory

def main():
db = Connection('localhost', 'root', 'password')

tv_factory = TVFactory()
laptop_factory = LaptopFactory()
inventory = ProductFactory()

title = 'Main Menu'
options = ['Create Product', 'View Products', 'Close']
option, index = pick(options, title, indicator='>')

if index == 0:
title = 'Select product to create'
options = list(inventory.product_types.keys())
option, index = pick(options, title, indicator='>')

if option in inventory.product_types.keys():
title = f'Create Product: {option}'
print(title)
name = input('Name: ')
price = input('Price: ')
product = inventory.create_product(option, name, price)
product.display_info()

product1 = tv_factory.create_product('Samsung 65 4k QLED', 7000)
product2 = laptop_factory.create_product('Lenovo Thinkpad', 3000)
elif index == 1:
title = option
options = ['All', 'Search', *inventory.product_types.keys()]
option, index = pick(options, title, indicator='>')

if index == 2:
return

product1.display_info()
product2.display_info()

if __name__ == '__main__':
main()
20 changes: 20 additions & 0 deletions product_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from inspect import getmembers, isclass, isabstract
import products

class ProductFactory:
product_types = {}

def __init__(self):
self.load_products()

def load_products(self) -> None:
members = getmembers(products, lambda m: isclass(m) and not isabstract(m))
for name, _type in members:
if isclass(_type) and issubclass(_type, products.Product):
self.product_types[name] = _type

def create_product(self, product_type, name, price, *args, **kwargs) -> products.Product:
if product_type in self.product_types:
return self.product_types[product_type](name, price, *args, **kwargs)
else:
raise ValueError(f"Invalid product type: {product_type}")
1 change: 1 addition & 0 deletions products/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .product import Product
from .tv import TV
from .laptop import Laptop
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
mysql-connector-python==8.3.0
pick==2.2.0
tabulate==0.9.0
windows-curses==2.3.2

0 comments on commit 5270e47

Please sign in to comment.