Last active
October 20, 2022 10:34
-
-
Save aldrinjenson/186b21657e1da37076e14396e2f57c55 to your computer and use it in GitHub Desktop.
Car Ride booking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CHARGE_PER_KM = 8 | |
const MIN_RATING = 4 | |
let counter = 0 | |
class Driver { | |
constructor({ name = '', ssn = Math.random(), rating = 2, distance = 49 }) { | |
this.name = name | |
this.ssn = ssn | |
this.rating = rating | |
this.distance = distance | |
} | |
getDistance = () => this.distance | |
getSsn = () => this.getSsn | |
getName = () => this.name | |
getRating = () => this.rating | |
} | |
class Car { | |
constructor({ number = '', make = '', color = '', carNo = '', driverSsn = '' }) { | |
this.number = number | |
this.make = make | |
this.color = color | |
this.carNo = carNo | |
this.driverSsn = driverSsn | |
} | |
} | |
class RentRide { | |
constructor({ drivers, cars, distanceToTravel, selectedCar }) { | |
this.drivers = drivers | |
this.cars = cars | |
this.distanceToTravel = distanceToTravel | |
this.cost = distanceToTravel * CHARGE_PER_KM | |
this.selectedCar = selectedCar | |
} | |
sendDriverRequest(chosenDriver) { | |
console.log(`sending request to driver: ${chosenDriver.getName()}`) | |
} | |
getCost() { | |
return this.cost | |
} | |
bookRide() { | |
if (this.selectedCar) { | |
const chosenDriver = this.drivers.find(driver => driver.ssn == this.selectedCar.driverSsn) | |
if (!chosenDriver) { | |
return "Invalid driver chosen!!"; | |
} | |
this.sendDriverRequest(chosenDriver) | |
return this.cost | |
} | |
const filteredDrivers = this.drivers.filter(driver => driver.getRating() >= MIN_RATING) | |
if (!filteredDrivers.length) { | |
return "No driver is present near you!" | |
} | |
const sortedDrivers = filteredDrivers.sort((driverA, driverB) => | |
driverA.getDistance() - driverB.getDistance()) | |
this.sendDriverRequest(sortedDrivers[counter++]) | |
return this.getCost() | |
} | |
} | |
const drivers = [ | |
new Driver({ name: 'John Doe', ssn: '1234', rating: 5, distance: 40 }), | |
new Driver({ name: 'Mary Jane', ssn: '1235', rating: 4, distance: 50 }), | |
new Driver({ name: 'Jack Black', ssn: '1236', rating: 4.4, distance: 20 }), | |
new Driver({ name: 'Molly Joes', ssn: '1237', rating: 3.4, distance: 30 }) | |
] | |
const cars = [ | |
new Car({ driverSsn: '1234', number: 'KL123', make: 'Hyundai', color: 'Blue' }), | |
new Car({ driverSsn: '1235', number: 'JK123', make: 'Toyota', color: 'Blue' }), | |
new Car({ driverSsn: '1236', number: 'ML133', make: 'Renault', color: 'Green' }), | |
new Car({ driverSsn: '1237', number: 'BB234', make: 'Tata', color: 'Gray' }), | |
] | |
console.log(new RentRide({ | |
drivers, cars, distanceToTravel: 50, | |
selectedCar: new Car({ driverSsn: '1235', number: 'JK123', make: 'Toyota', color: 'Blue' }), | |
}).bookRide()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rent a Ride As a customer to Rent a Ride you book a cab.
We charge you as per the distance covered. W
e charge 8rs/km.
The moment you click the button to RIDE, we search for the nearby drivers who will accept your ride. Suppose there are 15 drivers near your location, then we send the request to the first driver who is closest to you, …
There are a few conditions though, based on which we can not send the request to the nearby driver.
In case there is no driver present as per your request for the car, we will ask you to select some other car.
A table was given having drivers, cars model, their ratings, and distance from the customer, and using this data we have to provide the most appropriate car to the customer, with the calculated fare.