-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.py
More file actions
127 lines (95 loc) · 2.72 KB
/
classes.py
File metadata and controls
127 lines (95 loc) · 2.72 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
class Person:
def __init__(self,in_name,in_age):
self.name = in_name
self.age = in_age
def get_name(self):
return self.name
def get_age(self):
return self.age
class Customer(Person):
def __init__(self, in_name, in_age):
super().__init__(in_name, in_age)
self.has_ticket = False
self.in_zoo = False
def buy_ticket(self):
self.has_ticket = True
print(self.name + " bought a ticket.")
def enter_zoo(self, zoo):
if self.has_ticket == True:
self.has_ticket = False
zoo.add_customer(self.name)
self.in_zoo = True
else:
print("Purhcase a ticket before entering the zoo.")
def exit_zoo(self, zoo):
if self.in_zoo:
self.in_zoo = False
zoo.remove_customer(self.name)
class Zoo:
def __init__(self,name="Local Zoo"):
self.name = name
self.animals = []
self.customers = []
def add_animal(self, animal):
self.animals.append(animal)
print(f"{self.name} has a(n) {animal}")
def add_animals(self, animals):
self.animals.extend(animals)
print(f"{self.name} has many animals")
def add_customer(self, name):
self.customers.append(name)
print(f"{name} has entered {self.name}")
def remove_customer(self, name):
self.customers.remove(name)
print(f"{name} has left {self.name}")
def visit_animals(self):
for a in self.animals:
print(f"Visiting {a.get_name()}")
a.make_noise()
a.eat_food()
class Animal:
def __init__(self,name):
self.name = name
def get_name(self):
return self.name
def make_noise(self):
print("Every animal makes noise")
def eat_food(self):
print("All creatures need sustenance")
class Fish(Animal):
def __init__(self, name):
super().__init__(name)
def make_noise(self):
print(f"{self.name} says blub, blub, blub")
def eat_food(self):
print(self.name + " eats flakes.")
class Bird(Animal):
def __init__(self, name):
super().__init__(name)
def make_noise(self):
print(self.name + " noises.")
def eat_food(self):
print(self.name + " eats worms.")
class Chimp(Animal):
def __init__(self, name):
super().__init__(name)
def make_noise(self):
print(self.name + " noises.")
def eat_food(self):
print(self.name+"s" + " eating bananas.")
laZoo = Zoo("LA Zoo")
salmon = Fish("Salmon")
robin = Bird("Robin")
bonobo = Chimp("Bonobo")
laZoo.add_animals([salmon, robin, bonobo])
alice = Customer("Alice",25)
bob = Customer("Bob",20)
salmon = Customer("Salmon",1)
charlie = Customer("Charlie",10)
dave = Customer("Dave",30)
for c in [alice, bob, salmon, charlie, dave]:
c.buy_ticket()
c.enter_zoo(laZoo)
laZoo.visit_animals()
for c in [alice, bob, salmon, charlie, dave]:
c.exit_zoo(laZoo)