-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpesa2app.py
More file actions
45 lines (34 loc) · 1.26 KB
/
mpesa2app.py
File metadata and controls
45 lines (34 loc) · 1.26 KB
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
42
43
44
45
from abc import ABC, abstractmethod
# ---------------- ENCAPSULATION ----------------
class MpesaAccount:
def __init__(self, name, balance):
self.name = name
self.__balance = balance # private variable
def deposit(self, amount):
self.__balance += amount
print(f"{amount} deposited. Balance: {self.__balance}")
def withdraw(self, amount):
if amount <= self.__balance:
self.__balance -= amount
print(f"{amount} withdrawn. Balance: {self.__balance}")
return True
return False
def get_balance(self):
return self.__balance
# ---------------- ABSTRACTION ----------------
class MpesaService(ABC):
@abstractmethod
def access_service(self, account, amount):
pass
# ---------------- SEND MONEY ----------------
class SendMoney(MpesaService):
def access_service(self, account, amount):
if account.withdraw(amount):
print(f"Sent {amount} to another us")
# ---------LIPA NA MPESA---------
class LipaNaMpesa(MpesaService):
def access_service(self, account, amount):
if account.withdraw(amount):
print(f"Paid {amount} to merchant using Lipa na MPesa.")
else:
print("Payment failed:insufficent fund")