forked from ArjanCodes/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-creator_after.py
38 lines (25 loc) · 866 Bytes
/
01-creator_after.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from dataclasses import dataclass, field
from datetime import datetime
@dataclass
class ProductDescription:
price: int
description: str
@dataclass
class SaleLineItem:
product: ProductDescription
quantity: int
@dataclass
class Sale:
items: list[SaleLineItem] = field(default_factory=list)
time: datetime = field(default=datetime.now())
def add_line_item(self, product: ProductDescription, quantity: int):
self.items.append(SaleLineItem(product, quantity))
def main() -> None:
headset = ProductDescription(price=5_000, description="Gaming headset")
keyboard = ProductDescription(price=7_500, description="Mechanical gaming keyboard")
sale = Sale()
sale.add_line_item(product=headset, quantity=2)
sale.add_line_item(product=keyboard, quantity=3)
print(sale)
if __name__ == "__main__":
main()