Passed
Push — main ( e5e666...89d6da )
by Rahn20
01:13
created

simulation.Simulation.delete_and_remove()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
#!/usr/bin/python3
2
3
"""
4
Mainprogram for scooter simulation
5
"""
6
7
8
from datetime import datetime
9
import time
10
from src.scooter import Scooter
11
from src.api import ApiData
12
from main import Handler
13
14
15
16
class Simulation():
17
    """ Simulation class """
18
19
    # saves scooter id in an array
20
    _scooter_id = []
21
22
    # saves station in an array
23
    _station = []
24
25
    # saves user id in an array
26
    _user_id = []
27
28
    # saves city info in an array
29
    _city_data = []
30
31
32
    def __init__(self):
33
        """ Initialize class """
34
        self.scooter = Scooter()
35
        self.handler = Handler()
36
        self.api = ApiData(1)
37
38
39
    def move(self) -> None:
40
        """ Moves scooters around the cities. Simulation time is 2 minutes. """
41
        end_time = time.time() + 120    # 2 minutes
42
43
        print("\nStep 2 - Moving scooters . . . . . . . .")
44
45
        while time.time() <= end_time:
46
            count = 0
47
48
            while count < len(self._scooter_id):
49
                data = self.api.get_scooter_data(self._scooter_id[count])
50
51
                self.scooter.add_scooter_to_dict(data)
52
                self.scooter.add_city_to_dict(self._city_data[count])
53
54
                count = self.run(count)
55
56
                count += 1
57
                time.sleep(0.001)
58
59
60
    def delete_and_remove(self, count) -> None:
61
        """ Removes a specific element from the list and stop the scooter """
62
        self.end(count)
63
64
        del self._scooter_id[count]
65
        del self._station[count]
66
        del self._user_id[count]
67
        del self._city_data[count]
68
    
69
70
    def run(self, count: int) -> int:
71
        """
72
        Checks if the scooter is inside the city and check battery level, if TRUE
73
        the scooter will be returned otherwise the scooter will continue moving.
74
        """
75
        id = self._scooter_id[count]
76
77
        if self.scooter.check_scooter_in_city() is False:
78
            print("\nScooter {} is outside of the city\n".format(id))
79
            print("Scooter {} will be returned.".format(id))
80
81
            self.delete_and_remove()
82
            count -= 1
83
        elif self.scooter.check_battery():
84
            print("\n\033[1;31m*\033[1;0m Low battery!! the scooter {} needs to be charged.".format(id))
85
            print("Scooter {} will be returned.".format(id))
86
87
            self.delete_and_remove()
88
            count -= 1
89
        else:
90
            self.scooter.change_location()
91
            self.scooter.move_scooter()
92
            self.api.update_rented_scooter()
93
94
        return count
95
96
97
    def end(self, count):
98
        """
99
        Scooter will be checked if it's outside the city/has low battery level or need
100
        meantience. The scooter will be returned and the log/payment/account will be uppdated.
101
        """
102
        self.api.station = self._station[count]
103
        self.handler.api = ApiData(self._user_id[count])
104
105
        self.handler.end_rental()
106
107
108
    def return_scooters(self):
109
        """ All scooters will be returned when simulation time ends. """
110
        count = 0
111
112
        print("\nStep 3 - Returning scooters . . . . . . . .")
113
        print("\nThe simulation has finished, the scooters will be returned.\n")
114
115
        while count < len(self._scooter_id):
116
            data = self.api.get_scooter_data(self._scooter_id[count])
117
118
            self.scooter.add_scooter_to_dict(data)
119
            self.scooter.add_city_to_dict(self._city_data[count])
120
            self.end(count)
121
122
            count += 1
123
            time.sleep(0.001)
124
125
126
    def main(self, total:int):
127
        """ Start simulation program. """
128
        print("\n************ Welcome to Scooter simulation program **************\n")
129
        print("\nThe simulation takes around 3 minutes, do not try to break/stop the program.\n")
130
        print("\nStep 1 - Renting scooters . . . . . . . .")
131
132
        current = datetime.now().time().strftime("%H:%M:%S")
133
        self.handler.start_time = [int(current[:2]), int(current[3:5]), int(current[6:8])]
134
        count = 1
135
136
        # user id 6 means customer id 1, start counting from 6
137
        user = 6 
138
139
        while count <= total:
140
            data = self.api.get_scooter_data(scooter_id = count)
141
142
            if self.scooter.check_scooter_status(data):
143
                # get city
144
                city = self.api.get_city_data()
145
                self._city_data.append(city)
146
                self._scooter_id.append(count)
147
148
                # create log/payment and update customer's account
149
                self._user_id.append(user)
150
                self.api.user_id = str(user)
151
152
                self.api.rent_scooter()
153
                self._station.append(self.api.station)
154
155
                user += 1
156
            else:
157
                print("\n\033[1;31m*\033[1;0m Scooter {} is not available.\n".format(count))
158
159
            count += 1
160
161
        self.move()
162
        self.return_scooters()
163
164
165
166
if __name__ == "__main__":
167
    Simulation().main(1000)