Abstract methods and Interfaces
Develop a Python program which has three abstract methods and one subclass which allows a user to perform banking operations.
from abc import ABC, abstractmethod
class Account(ABC):
pass
def create_account(self):
pass
def calculate_balance(self):
pass
def pay_in(self):
pass
class Current(Account):
accounts = list()
def create_account(self, name, dob):
self.accounts.append([name, dob, []]) # type, amount
print(self.accounts)
def calculate_balance(self, acc_number):
balance = 0
if self.accounts[acc_number][2]:
for tx in self.accounts[acc_number][2]:
if tx[0] == 1:
balance += tx[1]
else:
balance -= tx[1]
else:
return 0
return balance
def add_transaction(self, acc_number, tx, amount=0):
if self.accounts[acc_number][2]:
self.accounts[acc_number][2].append([tx, amount])
else:
# define a lists of lists
self.accounts[acc_number][2] = [[tx, amount]]
def get_accounts(self):
return self.accounts
obj_Current = Current()
def main():
print("\n ENTER THE REQUIRED OPTION")
option = int(
input(" =========================================== \n \
1: ADD A NEW ACCOUNT \n \
2: ADD TRANSACTION \n \
3: DELETE ACCOUNT \n \
4: EXIT\n \
Option: "))
if option == 1:
# First letter capitalised
surname = input("\n Enter the surname: ").strip().title()
dob = input(" Enter DOB [dd/mm/yyyy]: ") # DOB
if surname and dob:
obj_Current.create_account(surname, dob)
else:
print("\n Please provide the required information")
main()
elif option == 2:
account = int(input("\n Enter account number: "))
accounts = obj_Current.get_accounts()
if len(accounts) > account:
tx = int(
input("\n Enter transaction type:\n \
1: Credit\n \
2: Debit\n\n : "))
amount = float(input(" Enter amount: "))
obj_Current.add_transaction(account, tx, amount)
else:
print("\n Account does not exist!")
main()
elif option == 3:
account = int(input("\n Enter account number: "))
accounts = obj_Current.get_accounts()
if len(accounts) > account:
balance = obj_Current.calculate_balance(account)
print("The current balance is: " + str(balance))
else:
print("\n Account does not exist!")
main()
else:
print("Thank you for using the system!")
exit()
main()
The ABC library provides a way to define abstract interfaces that allows us to use abstract methods that will need to be implemented in a subclass.
Read the article by Knox et al. (2018) and answer the following questions:
1. What is Component-based modelling?
It is an approach to model integration where pluggable model components represent processes
within an integrated model. This model is also called Integrated Environmental Model.
The idea is to integrate models from multiple disciplines to represent complexities in the
environment of a system.
2. Upon what do component-based modelling frameworks depend?
They depend on a consistent structure that is integrated by all the components.
3. Within the context of the work presented in this paper, what is Pynsim?
An open-source Python library to build simulation models for networked systems. This framework
contains abstract classes that can be extended using OOP.
3. How does Pynsim achieve its goal when using object oriented Python
programming?
Pynsim itself uses Object-oriented design, and by providing abstract classes, Pynsim promotes
the use of important OPP concepts, such as inheritance and modularity. This allows multiple
users to ‘plug in’ their Object-oriented code.
Reading notes on the chapter 9 of the book by Philips, D. (2018) Python 3 Object-Oriented programming, 3rd Ed. Packt Publishing
Design patterns.
We discussed the nature of design patterns when discussing the relevance of the architecture
within the parameters that are important for reusability. One of the main concerns to give a
high priority was that the design patterns are situation specific. The design patterns are a
suggested solution to the problems of OOP (Philips, 2018). According to the author, patterns are
a specific set of interactions of objects to resolve a generic problem. The challenge is to
recognise what version of the generic problem is the one we are trying to resolve and adopt the
generic solution to the specific problem.
Iterator design patterns
In Python, known iterators are built considering pre-existing OOP patterns.
Comprehensions
Syntaxes that transform utterable objects into a normal list, set or dictionary.
List comprehensions
These map input values to output values and can apply filters to restrict the output.
The above does not just apply to Lists but also to Sets and Dictionaries.
More advanced features are included in Python. These features make use of the OOP principles to
improve performance.
Generator expressions
Allow the comprehension syntaxes to read one object at a time. This operation saves memory
space.
Generator expressions are also a type of comprehension that compresses the generator's syntax to
one line.
References
Philips, D. (2018) Python 3 Object-Oriented programming, 3rd Ed. Packt Publishing