-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop-exercises-cat.py
More file actions
66 lines (49 loc) · 1.7 KB
/
oop-exercises-cat.py
File metadata and controls
66 lines (49 loc) · 1.7 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Cat:
def __init__(self, name):
self._name = name
self._energy = 50
def show_energy(self):
print(f"{self._name} energy is {self._energy}")
def eat(self):
self._energy = min(100, self._energy + 30)
print(f"{self._name} has eaten. Current energy is {self._energy}")
def play(self):
if self._energy >= 20:
self._energy -= 20
print(f"{self._name} has played. Current energy is {self._energy}")
else:
print(f"{self._name} is too tired to play. Energy is {self._energy}")
def sleep(self):
self._energy = 100
print(f"{self._name} had a nap. Energy is restored to {self._energy}")
class LazyCat(Cat):
def play(self):
if self._energy >= 10:
self._energy -= 10
print(f"{self._name} has played lazily. Current energy is {self._energy}")
else:
print(f"{self._name} is too tired to play. Energy is {self._energy}")
class ActiveCat(Cat):
def play(self):
if self._energy >= 30:
self._energy -= 30
print(f"{self._name} has played Actively. Current energy is {self._energy}")
else:
print(f"{self._name} is too tired to play. Energy is {self._energy}")
class CatManager:
def __init__(self, cats):
self.cats = cats
def play_all(self):
for cat in self.cats:
cat.play()
def eat_all(self):
for cat in self.cats:
cat.eat()
def sleep_all(self):
for cat in self.cats:
cat.sleep()
cats = [Cat("Oscar"), LazyCat("Milo"), ActiveCat("Leo")]
manager = CatManager(cats)
manager.play_all()
manager.eat_all()
manager.sleep_all()