-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrip_states.py
More file actions
82 lines (59 loc) · 2.34 KB
/
trip_states.py
File metadata and controls
82 lines (59 loc) · 2.34 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
from abc import ABC, abstractmethod
from enums import TripStatus
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from driver import Driver
from trip import Trip
class TripState(ABC):
@abstractmethod
def request(self, trip: 'Trip'):
pass
@abstractmethod
def assign(self, trip: 'Trip', driver: 'Driver'):
pass
@abstractmethod
def start(self, trip: 'Trip'):
pass
@abstractmethod
def end(self, trip: 'Trip'):
pass
class RequestedState(TripState):
def request(self, trip: 'Trip'):
print("Trip is already in requested state.")
def assign(self, trip: 'Trip', driver: 'Driver'):
trip.set_driver(driver)
trip.set_status(TripStatus.ASSIGNED)
trip.set_state(AssignedState())
def start(self, trip: 'Trip'):
print("Cannot start a trip that has not been assigned a driver.")
def end(self, trip: 'Trip'):
print("Cannot end a trip that has not been assigned a driver.")
class AssignedState(TripState):
def request(self, trip: 'Trip'):
print("Trip has already been requested and assigned.")
def assign(self, trip: 'Trip', driver: 'Driver'):
print("Trip is already assigned. To re-assign, cancel first.")
def start(self, trip: 'Trip'):
trip.set_status(TripStatus.IN_PROGRESS)
trip.set_state(InProgressState())
def end(self, trip: 'Trip'):
print("Cannot end a trip that has not started.")
class InProgressState(TripState):
def request(self, trip: 'Trip'):
print("Trip is already in progress.")
def assign(self, trip: 'Trip', driver: 'Driver'):
print("Cannot assign a new driver while trip is in progress.")
def start(self, trip: 'Trip'):
print("Trip is already in progress.")
def end(self, trip: 'Trip'):
trip.set_status(TripStatus.COMPLETED)
trip.set_state(CompletedState())
class CompletedState(TripState):
def request(self, trip: 'Trip'):
print("Cannot request a trip that is already completed.")
def assign(self, trip: 'Trip', driver: 'Driver'):
print("Cannot assign a driver to a completed trip.")
def start(self, trip: 'Trip'):
print("Cannot start a completed trip.")
def end(self, trip: 'Trip'):
print("Trip is already completed.")