-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses5.py
More file actions
63 lines (42 loc) · 1.27 KB
/
classes5.py
File metadata and controls
63 lines (42 loc) · 1.27 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
class Flight:
counter = 1
def __init__(self, origin, destination, duration):
# Keep track of id number.
self.id = Flight.counter
Flight.counter += 1
# Keep track of passengers.
self.passengers = []
# Details about flight.
self.origin = origin
self.destination = destination
self.duration = duration
def print_info(self):
print(f"Flight origin: {self.origin}")
print(f"Flight destination: {self.destination}")
print(f"Flight duration: {self.duration}")
print()
print("Passengers:")
for passenger in self.passengers:
print(passenger)
def delay(self, amount):
self.duration += amount
def add_passenger(self, p):
self.passengers.append(p)
p.flight_id = self.id
class Passenger:
def __init__(self, name):
self.name = name
def __repr__(self):
return self.name
def main():
# Create flight.
f1 = Flight(origin="New York", destination="Paris", duration=540)
# Create passengers.
alice = Passenger(name="Alice")
bob = Passenger(name="Bob")
# Add passengers.
f1.add_passenger(alice)
f1.add_passenger(bob)
f1.print_info()
if __name__ == "__main__":
main()