You are to create a car rental service that stores your cars available for rental and customers who rent the cars.
Your first goal is to create a Customer Object Constructor that have the following properties:
- id (string or number)
- name (string)
- carRented (object) (null by default)
Then you should create a Car Object Constructor that have the following properties:
- id (string or number)
- producer (string)
- model (string)
- rentalPricePerDay (number)
- available (boolean) (true by default)
- customer (object) (null by default)
- rentalDuration (number) (in days) (0 by default)
- quotePrice (function)
- reserve (function)
- return (function)
-
The
quotePrice
function takes a variable callrentalDuration
then returns therentalPrice * rentalDuration
-
The
reserve
function takes acustomer
object andrentalDuration
value.- Check if this car is available for rental
- If yes:
- Change
available
tofalse
- Set
this car's customer
tocustomer
- Set
this car's rentalDuration
torentalDuration
return true
- If no:
return false
-
The
return
function take no arguments.- Check if this car is available
- If yes:
return "Sorry, this car have already been returned.";
- If No:
- Set
this car's availible
totrue
- Set
this car's customer
to null - Set
this car's rentalDuration
to null;
After the Car Object, create a Vendor Object Constructor that have the following properties:
- name (string) (provided)
- cars (array) (empty array by default) (provided)
- customers (array) (empty array by default) (provided)
- findCarIndex (function) (provided)
- findCustomerIndex (function) (provided)
- getCar (function) (provided)
- getCustomer (function) (provided)
- addCar (function)
- addCustomer (function)
- removeCar (function)
- removeCustomer (function)
- availableCars (function)
- rentCar (function)
- returnCar (function)
- totalRevenue (function)
-
The
findCarIndex
/findCustomerIndex
function takes ancarID
/customerID
and returns the index or -1. -
The
getCar
/getCustomer
function takes acarID
/customerID
and returns the actual car/customer object or undefined. -
The
addCar
/addCustomer
function takes acarObj
orcustomerObj
.- Check if
car
/customer
exists usinggetCar
/getCustomer
- If
car
/customer
is found console.log("ID already exists")
- Else
- Push the
carObj
/customerObj
intocars
/customers
list console.log("Car/Customer added to warehouse")
- Check if
-
The
removeCar
/removeCustomer
function takes ancarID
/customerID
.- Check if
car
/customer
exists usingfindCarIndex
/findCustomerIndex
- If
carIndex/customerIndex >= 0
- Delete car/customer from the cars/customers list using
array.splice
console.log("Car/Customer deleted");
- Else
console.log("Car/Customer not found")
- Check if
-
The
availableCars
function take no arguments. Use the array.filter to generate an array of cars available for rent. -
The
rentCar
function takescustomerID
andrentalDuration
- Get a list of available cars using the function
availableCars
- If
availableCars
array is empty console.log("All our cars have been rented")
- Else
- Get customer using the function
getCustomer
- If customer is found
1. Set
customer's carRented
to the first available car 2. Call thecar's reserve
function withcustomerObj
andrentalDuration
3.console.log("The car has been reserved");
- Else
1.
console.log("Please provide a valid customerID");
- Get a list of available cars using the function
-
The
returnCar
function takes acustomerID
- Get customer using the function
getCustomer
- if customer is found
- Call
customer's carRented's return
function - Set
customer's carRented
tonull
console.log( "Thank you for using our service");
- else
console.log("Please provide a valid customerID");
- Get customer using the function
-
The
totalRevenue
function takes no arguments. Use the array.reduce to sum up the revenues for each car.