| Current Path : /var/www/html/bibhas.ghoshal/IoT_2019/ |
| Current File : //var/www/html/bibhas.ghoshal/IoT_2019/12BankAcc.py |
#
# The Person class is taken from "Introduction to Computation and
# Programming using Python", by John V Guttag
# Other classes are created by Amey Karkare, but inspired by the
# examples from the same textbook
#
import datetime
class Person(object):
def __init__(self, name):
"""Create a Person"""
self.name = name
try:
lastBlank = name.rindex(' ')
self.lastName = name[lastBlank+1:]
except:
self.lastName = name
self.birthdate = None
def getName(self):
"""Return self's full name"""
return self.name
def getLastName(self):
"""Return self's last name"""
return self.lastName
def setBirthday(self, birthdate):
"""Asume birthdate is of type datetime.date
Sets self's birthday to birthdate"""
self.birthdate = birthdate
def getAge(self):
"""Returns self's current age in days"""
if self.birthdate == None:
raise ValueError ('Birthdate not set for ' + self.name)
return (datetime.date.today() - self.birthdate).days
def __lt__(self, other):
"""Returns True us self's name is lexicographically
less than other's name, and False otherwise"""
if self.lastName == other.lastName:
return self.name < other.name
return self.lastName < other.lastName
def __str__(self):
"""Return self's name"""
return self.name
me = Person('Amey Karkare')
cat = Person('Garfield')
bat = Person('Bruce Wayne')
bat.setBirthday(datetime.date(1939, 5, 27))
cat.setBirthday(datetime.date(1978, 6, 19))
def testPerson():
pList = [me, cat, bat]
for p in pList:
print (p)
print('=== Sorted ===')
pList.sort()
for p in pList:
print (p)
class BankPerson(Person):
# This is a class specific variable, shared by all objects
# of the class
nextId = 0 # identification of BankPerson
def __init__(self, name):
Person.__init__(self, name)
self.id = BankPerson.nextId
BankPerson.nextId += 1;
def getId(self):
return self.id
def __lt__(self, other):
return self.id < other.id
# An alternate would be to have a definition in Customer
# class (return True) and in Employee class (return
# False). But, if we later add any new subclass of BankPerson,
# we must remember to have a definition there too. The
# method below will work even when we add a new subclass.
def isCustomer(self):
return isinstance(self, Customer)
def testBankPerson():
p1 = Person("Amey Karkare")
p2 = BankPerson("P Mukherjee")
p3 = BankPerson("Amey Karkare")
p4 = BankPerson("Vijay M")
print ('p2 is', p2)
print ('Id of p3 is', p3.getId())
print ('Id of p4 is', p4.getId())
print ('p2 < p3 is', p2 < p3)
print ('p4 < p3 is', p4 < p3)
print ('p1 < p3 is', p1 < p3) # < (p1, p3) => p1.__lt__(p3)
print ('p4 < p1 is', p4 < p1) # < (p4, p1) -=> p4.__lt__(p1)
class Customer(BankPerson):
"""A customer is a person holding an account in bank"""
def __init__(self, name):
BankPerson.__init__(self, name)
self.savings = [] # list of savings accounts
self.FDs = [] # list of fixed deposits
def addSavingsAcc(self, acc):
self.savings.append(acc)
def addFD(self, fd):
self.FDs.append(fd)
def removeFD(self, fd):
self.FDs.remove(fd)
# creates a clone of the arg, and returns the clone after
# sorting
#
# Note the naming convention: A class member with a name
# starting with underscore (_) is considered private. This
# is just a convention
def _clone_and_sort(self, acc):
clone = acc[:]
clone.sort()
return clone
# generator version of _clone_and_sort
def _clone_and_sort_g(self, acc):
acc.sort()
for a in acc:
yield a
# The following methods use a clone to avoid unintentional
# modification
def getSavingsAcc(self):
return self._clone_and_sort(self.savings)
def getFDs(self):
return self._clone_and_sort(self.FDs)
# The following methods are generator methods for above
def getSavingsAcc_g(self):
return self._clone_and_sort_g(self.savings)
def getFDs_g(self):
return self._clone_and_sort_g(self.FDs)
def testCustomer():
p1 = Person("Amey Karkare")
p2 = Customer("P Mukherjee")
p3 = Customer("Amey Karkare")
p4 = Customer("Vijay M")
print ('p2 is', p2)
print ('Id of p3 is', p3.getId())
print ('Id of p4 is', p4.getId())
print ('p2 < p3 is', p2 < p3)
print ('p4 < p3 is', p4 < p3)
print ('p1 < p3 is', p1 < p3)
p2.addSavingsAcc(23)
p3.addSavingsAcc(24)
p4.addSavingsAcc(42)
p2.addSavingsAcc(237)
p2.addSavingsAcc(871)
p3.addFD(32)
p2.addFD(25)
p4.addFD(45)
p4.addFD(109)
pList = [p1, p2, p3, p4]
for p in pList:
if (type(p) == Customer):
print ('-----------------------------------------------------')
print ('For customer', p)
print ('\tSavings Accounts :', p.getSavingsAcc())
print ('\tFixed Deposits :', p.getFDs())
class NormalCustomer(Customer):
"""Normal customer does not have anything extra over
Customer. However, creating a separate class allow us to
manage things in case things change in future"""
pass
class PremiumCustomer(Customer):
"""Customer with a dedicated relationship manager"""
def __init__(self, name, relManager):
Customer.__init__(self, name)
self.relManager = relManager
def getRelationshipManager(self):
return self.relManager
class Employee(BankPerson):
"""Should containg Employee specific attributes. Ignoring for the
class purpose"""
pass
def testSubCustomer():
p1 = BankPerson("Amey Karkare")
p2 = PremiumCustomer("P Mukherjee", "Manager 1")
p3 = NormalCustomer("Amey Karkare")
p4 = PremiumCustomer("Vijay M", "Manager 2")
print ('p2 is', p2)
print ('Id of p3 is', p3.getId())
print ('Id of p4 is', p4.getId())
print ('p2 < p3 is', p2 < p3)
print ('p4 < p3 is', p4 < p3)
print ('p1 < p3 is', p1 < p3)
p2.addSavingsAcc(23)
p3.addSavingsAcc(24)
p4.addSavingsAcc(42)
p2.addSavingsAcc(237)
p2.addSavingsAcc(871)
p3.addFD(32)
p2.addFD(25)
p4.addFD(45)
p4.addFD(109)
pList = [p1, p2, p3, p4]
for p in pList:
if (p.isCustomer()):
print ('-----------------------------------------------------')
print ('For customer', p)
print ('\tSavings Accounts :', p.getSavingsAcc())
print ('\tFixed Deposits :', p.getFDs())
if (type(p) == PremiumCustomer):
print ("\tRelation's Manager :", p.getRelationshipManager())
# Generator version testing.
def testSubCustomer_g():
p1 = BankPerson("Amey Karkare")
p2 = PremiumCustomer("P Mukherjee", "Manager 1")
p3 = NormalCustomer("Amey Karkare")
p4 = PremiumCustomer("Vijay M", "Manager 2")
p2.addSavingsAcc(23)
p3.addSavingsAcc(24)
p4.addSavingsAcc(42)
p2.addSavingsAcc(237)
p2.addSavingsAcc(871)
p3.addFD(32)
p2.addFD(25)
p4.addFD(45)
p4.addFD(109)
pList = [p1, p2, p3, p4]
for p in pList:
if (p.isCustomer()):
print ('-----------------------------------------------------')
print ('For customer', p)
print ('\tSavings Accounts :', end=' ')
for s in p.getSavingsAcc_g():
print (s,end=' ')
print()
print ('\tFixed Deposits :', end = ' ')
for s in p.getFDs_g():
print (s,end=' ')
print()
if (type(p) == PremiumCustomer):
print ("\tRelation's Manager :", p.getRelationshipManager())