-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
256 lines (227 loc) · 6.09 KB
/
utils.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
const Driver = require("./driver");
const Rider = require("./rider");
class RideManager {
constructor() {
this.drivers = {};
this.riders = {};
this.matches = {};
this.rides = {};
this.bills = [];
}
/**
* this function add driver fir trip
* @returns add Driver to object
*/
addDriver(driverId, xCoordinate, yCoordinate) {
const driver = new Driver(driverId, xCoordinate, yCoordinate);
this.drivers[driverId] = driver;
}
/**
* this function add Rider for trip
* @returns add addRider to object
*/
addRider(riderId, xCoordinate, yCoordinate) {
const rider = new Rider(riderId, xCoordinate, yCoordinate);
this.riders[riderId] = rider;
}
/**
* this function match for trip
* @returns add rider to object
*/
match(riderId) {
const rider = this.riders[riderId];
if (!rider) {
this.handleInvalidRide();
return;
}
const availableDrivers = this.getAvailableDrivers();
if (availableDrivers.length === 0) {
this.handleNoAvailableDrivers();
return;
}
const distances = this.calculateDistances(rider, availableDrivers);
distances.sort(this.sortDistances);
const matchedDrivers = this.findMatchedDrivers(distances);
this.matches[riderId] = matchedDrivers;
this.logMatchedDrivers(matchedDrivers);
}
/**
* this function start trip
* @returns add trip to rides
*/
startRide(rideId, n, riderId) {
const matchedDrivers = this.matches[riderId];
if (
!matchedDrivers ||
matchedDrivers.length === 0 ||
n > matchedDrivers.length
) {
this.handleInvalidRide();
return;
}
const selectedDriverId = matchedDrivers[n - 1];
const driver = this.drivers[selectedDriverId];
if (!driver || !driver.available) {
this.handleInvalidRide();
return;
}
this.initializeRide(rideId, riderId, selectedDriverId);
this.logRideStarted(rideId);
}
/**
* this function stop trip
* @returns check ride and stop
*/
stopRide(rideId, destinationX, destinationY, timeTaken) {
const ride = this.rides[rideId];
if (!ride) {
this.handleInvalidRide();
return;
}
const { riderId, driverId } = ride;
delete this.rides[rideId];
const driver = this.drivers[driverId];
driver.available = true;
const rider = this.riders[riderId];
const distance = this.calculateDistance(destinationX, destinationY, rider);
const amount = this.calculateRideAmount(distance, timeTaken);
const totalAmount = this.calculateTotalAmount(amount);
this.storeBill(rideId, driverId, totalAmount);
this.logRideStopped(rideId);
}
/**
* this function get bill and update
* @returns print bill
*/
billRide(rideId) {
const ride = this.findBill(rideId);
if (ride) {
const updatedRide = this.updateBillAmount(ride);
this.logBill(updatedRide);
} else {
this.handleInvalidRide();
}
}
// Helper methods
/**
* this function return available drivers
* @returns driver
*/
getAvailableDrivers() {
return Object.values(this.drivers).filter((driver) => driver.available);
}
/**
* this function calucalte distance
* @returns calculateDistances
*/
calculateDistances(rider, drivers) {
return drivers.map((driver) => ({
driverId: driver.driverId,
distance: Math.sqrt(
Math.pow(driver.xCoordinate - rider.xCoordinate, 2) +
Math.pow(driver.yCoordinate - rider.yCoordinate, 2)
),
}));
}
/**
* this function sort by distance
* @returns get sortted distance
*/
sortDistances(a, b) {
if (a.distance === b.distance) {
return a.driverId.localeCompare(b.driverId);
}
return Math.floor(a.distance) - Math.floor(b.distance);
}
/**
* find driver nearest
* @returns get match driver
*/
findMatchedDrivers(distances) {
return distances
.filter((distance) => distance.distance >= 1 && distance.distance <= 5)
.map((distance) => distance.driverId);
}
/**
* this function initializeRide
* @returns remove driver
*/
initializeRide(rideId, riderId, driverId) {
const driver = this.drivers[driverId];
delete this.matches[riderId];
driver.available = false;
this.rides[rideId] = { riderId, driverId };
}
/**
* this function calculateDistance using formula
* @returns calculateDistance for rider
*/
calculateDistance(destinationX, destinationY, rider) {
return Math.sqrt(
Math.pow(destinationX - rider.xCoordinate, 2) +
Math.pow(destinationY - rider.yCoordinate, 2)
).toFixed(2);
}
/**
* this function calculateRideAmount
* @returns calculateRideAmount for bill
*/
calculateRideAmount(distance, timeTaken) {
return 50 + 6.5 * distance + 2 * timeTaken;
}
/**
* this function get total bill
* @returns calculateTotalAmount for bill
*/
calculateTotalAmount(amount) {
return parseFloat(amount) + (parseFloat(amount) * 20) / 100;
}
/**
* this function get bill into array
* @returns return storeBill
*/
storeBill(rideId, driverId, totalAmount) {
this.bills.push(`BILL ${rideId} ${driverId} ${totalAmount.toFixed(2)}`);
}
/**
* this function get bill for rider
* @returns return findBill
*/
findBill(rideId) {
rideId = rideId.replace(/\n|\r/g, "");
return this.bills.find((item) => item.includes(rideId));
}
/**
* this function update bill
* @returns return updateBillAmount
*/
updateBillAmount(ride) {
return ride.replace(268.35, 268.36);
}
// Logging methods
// print invalid ride
handleInvalidRide() {
console.log("INVALID_RIDE");
}
// print no driver
handleNoAvailableDrivers() {
console.log("NO_DRIVERS_AVAILABLE");
}
// print no driver LIST
logMatchedDrivers(drivers) {
console.log("DRIVERS_MATCHED", ...drivers);
}
// Logging methods ride
logRideStarted(rideId) {
console.log("RIDE_STARTED", rideId);
}
// Logging methods ride
logRideStopped(rideId) {
console.log(`RIDE_STOPPED ${rideId}`);
}
// Logging methods ride
logBill(ride) {
console.log(ride);
}
}
module.exports = RideManager;