-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstrategy.py
41 lines (28 loc) · 983 Bytes
/
strategy.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
39
40
41
from abc import abstractmethod
class ShoppingCart:
def __init__(self, list_items):
self.list_items = list_items
def pay(self, payment_strategy):
payment_strategy.pay()
# Strategy Pattern
class PaymentStrategy:
@abstractmethod
def pay(self):
pass
class CreditCart(PaymentStrategy):
def __init__(self, name, card_number, cvv, exp_date):
self.name = name
self.card_number = card_number
self.cvv = cvv
self.exp_date = exp_date
def pay(self):
print(f"paying with credit card nº: {self.card_number}")
class PayPal(PaymentStrategy):
def __init__(self, email, password):
self.email = email
self.password = password
def pay(self):
print(f"paying with paypal from user: {self.email}")
cart = ShoppingCart(["item 1", "item 2", "item 3"])
cart.pay(CreditCart("Arnaldo", "1111.2222.3333.4444", "000", "2100/01/01"))
cart.pay(PayPal("[email protected]", "1234"))