Solutions Encaptiolation exercises
Ex 1: Car object
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 46 47 48 49 50 51 52 53 54 55 56 | # car.py (solution)
1. Car object
Create a Car class.
When instanciated the object should be able to take 4 attributes (Make, Model, bhp, mph).
They all 4 should be properties.
class Car:
def __init__(self, *args):
self.make = args[0]
self.model = args[1]
self.bhp = args[2]
self.mph = args[3]
# make
@property
def make(self):
return self.__make
@make.setter
def make(self, make):
self.__make = make
# model
@property
def model(self):
return self.__model
@model.setter
def model(self, model):
self.__model = model
# bhp
@property
def bhp(self):
return self.__bhp
@bhp.setter
def bhp(self, bhp):
self.__bhp = bhp
# mph
@property
def mph(self):
return self.__mph
@mph.setter
def mph(self, mph):
self.__mph = mph
|
Ex 2: Bank
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | """
2. Bank
In the exercise from last monday with the bank, account and customer, change the code to use properties instead of the public variables.
The bank class should futher be change into not taking any accounts as parameters at initialization. The accouts should be added afterwards, eithers as a single account one at a time, or as a collection of accounts (many at the sametime).
Somewhere you should change the code so that a customer only can create one account.
The Customer class should make sure that the customer is over 18 year of age.
"""
class Bank:
def __init__(self):
self.__accounts = [] # When bank is initialized it has the abillity to hold many accounts
@property
def accounts(self):
return self.__accounts
@accounts.setter
def accounts(self, acc):
if type(acc) in [list, tuple, set]:
for i in acc:
self.__has_account(i)
self.__accounts.append(i)
else:
self.__has_account(acc)
self.__accounts.append(acc)
def __has_account(self, acc):
for i in self.__accounts:
if acc.cust.name == i.cust.name:
raise ValueError('Customer aleready has an account!')
return False
class Account:
def __init__(self, no, cust):
self.no = no
self.cust = cust
def __repr__(self):
return str(self.__dict__)
class Customer:
def __init__(self, name, age):
self.name = name
self.age = age
@property
def age(self):
return self.__age
@age.setter
def age(self, age):
if age < 18:
raise ValueError('You must be 18 or above to create an account')
self.__age = age
def __repr__(self):
return str(self.__dict__)
|
Ex 3: Machine -> printer
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | # printer.py (solution)
"""
3. Machine -> printer
Create a Machine class that takes care of powering on and off a the machine.
Create a printer class that is a subclass of the Machine super class.
The printer should be able to print to console.
The printer should have a papertray, which should be in its own class. The papertray class should keep track of the paper, it should have the abillity to use paper and and load new paper in the tray if empty.
"""
class Machine:
""" takes care of turning on and off """
def __init__(self):
self._is_on = False # one _ = protected (to be used only in subclasses)
def power(self):
self._is_on = not self._is_on
class Printer(Machine):
def __init__(self):
Machine.__init__(self)
self.__pt = Papertray()
def print(self, text):
if self.__pt.paper == 0:
print('Papertray is empty')
else:
if self._is_on:
print(text)
self.__pt.paper = self.__pt.paper - 1
else:
print('Printer is off')
@property
def load(self):
return self.__pt.paper
@load.setter
def load(self, no):
self.__pt.paper = no
class Papertray:
def __init__(self):
self.paper = 2
@property
def paper(self):
return self.__paper
@paper.setter
def paper(self, paper):
self.__paper = paper
|