-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove factory classes and refactor product creation to use a single …
…factory
- Loading branch information
1 parent
072ca1d
commit 5270e47
Showing
8 changed files
with
51 additions
and
28 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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() |
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,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}") |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .product import Product | ||
from .tv import TV | ||
from .laptop import Laptop |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
mysql-connector-python==8.3.0 | ||
pick==2.2.0 | ||
tabulate==0.9.0 | ||
windows-curses==2.3.2 |