added abc, interfaces and polymorphism
This commit is contained in:
1 parent
6202f69e1c
commit
35db4c9201
4 files changed
+302
-2
No files matched your search
@@ -0,0 +1,106 @@
|
||||
# Create a payment processing system:
|
||||
|
||||
# Create an abstract PaymentProcessor class with:
|
||||
|
||||
# __init__ takes name (regular, not abstract)
|
||||
# @abstractmethod process_payment(self, amount) — returns a confirmation string
|
||||
# @abstractmethod refund(self, amount) — returns a refund confirmation
|
||||
# @property @abstractmethod transaction_fee — each processor has a different fee
|
||||
# Regular method process_with_fee(self, amount) — calls process_payment with amount minus the fee
|
||||
|
||||
# Create three subclasses:
|
||||
|
||||
# CreditCardProcessor — fee is 0.03 (3%)
|
||||
# PayPalProcessor — fee is 0.05 (5%)
|
||||
# CryptoProcessor — fee is 0.01 (1%)
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
class PaymentProcessor(ABC):
|
||||
|
||||
def __init__(self, name): # please clarify for me again, what does the name do?
|
||||
self.name = name
|
||||
|
||||
@abstractmethod
|
||||
def process_payment(self, amount: float) -> str: # can i set int and float here? how?
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
def refund(self, amount: float) -> str:
|
||||
...
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def transaction_fee(self):
|
||||
...
|
||||
|
||||
def process_with_fee(self, amount):
|
||||
fee = amount * self.transaction_fee
|
||||
final_amount = amount - fee
|
||||
return final_amount
|
||||
|
||||
# creating the subclasses
|
||||
|
||||
# CreditCardProcessor — fee is 0.03 (3%)
|
||||
|
||||
class CreditCardProcessor(PaymentProcessor):
|
||||
|
||||
def refund(self, amount: float) -> str:
|
||||
return f"Your {self.name} refund of: {amount} was processed successfully."
|
||||
|
||||
def process_payment(self, amount: float) -> str: # can i set int and float here? how?
|
||||
return f"Thank you. The {self.name} payment of {amount} was processed successfully."
|
||||
|
||||
@property
|
||||
def transaction_fee(self):
|
||||
return 0.03
|
||||
|
||||
# PayPalProcessor — fee is 0.05 (5%)
|
||||
|
||||
class PayPalProcessor(PaymentProcessor):
|
||||
|
||||
def refund(self, amount: float) -> str:
|
||||
return f"Your {self.name} refund of: {amount} was processed successfully."
|
||||
|
||||
def process_payment(self, amount: float) -> str: # can i set int and float here? how?
|
||||
return f"Thank you. The {self.name} payment of {amount} was processed successfully."
|
||||
|
||||
@property
|
||||
def transaction_fee(self):
|
||||
return 0.05
|
||||
|
||||
# CryptoProcessor — fee is 0.01 (1%)
|
||||
|
||||
class CryptoProcessor(PaymentProcessor):
|
||||
|
||||
def refund(self, amount: float) -> str:
|
||||
return f"Your {self.name} refund of: {amount} was processed successfully."
|
||||
|
||||
def process_payment(self, amount: float) -> str:
|
||||
return f"Thank you. The {self.name} payment of {amount} was processed successfully."
|
||||
|
||||
@property
|
||||
def transaction_fee(self):
|
||||
return 0.01
|
||||
|
||||
|
||||
# --- test
|
||||
|
||||
# Can't create the abstract class
|
||||
#p = PaymentProcessor("test") # TypeError!
|
||||
|
||||
# Subclasses work
|
||||
cc = CreditCardProcessor("Visa")
|
||||
print(cc.process_payment(100)) # "Visa: Processed $100"
|
||||
print(cc.transaction_fee) # 0.03
|
||||
print(cc.process_with_fee(100)) # processes $97.00
|
||||
|
||||
pp = PayPalProcessor("MyPayPal")
|
||||
print(pp.transaction_fee) # 0.05
|
||||
print(pp.process_with_fee(100)) # processes $95.00
|
||||
|
||||
# Missing method = error
|
||||
class BrokenProcessor(PaymentProcessor):
|
||||
pass
|
||||
|
||||
b = BrokenProcessor("oops") # TypeError!
|
||||
Reference in new issue
Block a user