Solution for OOP exercises

Bank exercise

 1""" Bank Exercise 
 2
 3Create a 
 4
 5* Bank class
 6* Account Class
 7* Customer class
 8
 9The bank class should be able to hold many account.
10You should be able to add new accounts.
11The Account class should have relevant details.
12The Customer class Should also have relevant details.
13
14Stick to the techniques we have covered so far.
15
16
17"""
18
19class Bank:
20    def __init__(self):
21        self.accounts = []
22
23class Account:
24    def __init__(self, no, cust):
25        self.no = no
26        self.cust = cust
27
28class Customer:
29    def __init__(self, name):
30        self.name = name
31
32"""
33## Overloading
34Add the abillity in your code to overload one or more init methods
35"""
36
37class Customer:
38    def __init__(self, *args):
39        if len(args) == 1:
40            self.name = args[0]
41
42      elif len(args) == 2:
43            self.name = args[0]
44            self.age = args[1]
45      else:
46            self.name = args[0]
47            self.age = args[1]
48            self.gender = [2]
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

Angry bird exercise:

  1# angry_bird.py
  2
  3class Bird:
  4
  5    
  6
  7
  8    def __init__(self, start, dir):
  9        """ Arg: a list of 2 points , a position of the nose """
 10        # position , a list of [x,y] points
 11        self.pos = start
 12        # left : 3; right : 1; up : 4 down : 2 
 13        self.dir = dir
 14
 15        # list of all methods 
 16        self.methods = {'f':self.move_forward,'l':self.turn_left,'r':self.turn_right}
 17
 18    def move_forward(self):
 19        if self.dir == 1:
 20            self.pos[1] = self.pos[1] + 1
 21        elif self.dir == 2:
 22            self.pos[0] = self.pos[0]+1
 23        elif self.dir == 3:
 24            self.pos[1] = self.pos[1]-1
 25        else:
 26            self.pos[0] = self.pos[0]-1
 27
 28
 29    def turn_left(self):
 30        if self.dir == 1:
 31            self.dir = 4
 32        else:
 33            self.dir -= 1
 34
 35    def turn_right(self):
 36        if self.dir == 4:
 37            self.dir = 1
 38        else:
 39            self.dir += 1 
 40    def loose(self):
 41        return 'The bird lost the game!'
 42
 43    def __repr__(self):
 44        return f'{self.__dict__}'
 45
 46class Pig:
 47    def __init__(self, pos):
 48        self.pos = pos
 49        self.is_alive = True
 50
 51    def die(self):
 52        return 'Uff the pig is dead'
 53
 54class Board:    
 55
 56    def __init__(self):
 57        # create a bird at some postion
 58        self.bird = Bird([2, 2],1)
 59        self.c = Bird.__dict__
 60        # create a pig at some position
 61        self.pig = Pig([2,5])
 62
 63    def run(self, cmd):
 64        for i in cmd:
 65            self.bird.methods[i]()
 66
 67    def display(self):
 68        for i in range(1, 11):
 69            for j in range(1, 11):
 70                if (i,j) == tuple(self.bird.pos):
 71                    print('B', end=' ')
 72                elif (i,j) == tuple(self.pig.pos):
 73                    print('P', end=' ')
 74                else:    
 75                    print('*', end=' ')
 76            print()
 77
 78
 79class Workspace:
 80    def __init__(self):
 81        self.moves = None
 82
 83    def display(self):
 84        print('\nwhat steps do you want to perform?')
 85        print('Options: move forward (f), turn left (l), turn right (r)')
 86        print('Type "q" when finnished')
 87
 88    def commands(self):
 89        l = []
 90        q = True
 91        while q:
 92            x = input('Move: ')
 93            if x == 'q':
 94                q = False
 95            else:
 96                l.append(x)
 97
 98        return l
 99
100class Game:
101
102    def main(self):
103        b = Board()
104        b.display()
105
106        w = Workspace()
107        w.display()
108
109        l = w.commands()
110        b.run(l)
111
112        print(self.win(b))
113        print(f'Birds position: {b.bird.pos}')
114        print(f'Pigs position: {b.pig.pos}')
115
116    def win(self, b):
117        if b.bird.pos == b.pig.pos:
118            return b.pig.die()
119        else:
120            return b.bird.loose()
121
122g = Game()
123g.main()
124
125
126
127