Wiz1221 / lab-js-problems-objects-and-arrays

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Introduction

You are to create a car rental service that stores your cars available for rental and customers who rent the cars.

Customer Object

Your first goal is to create a Customer Object Constructor that have the following properties:

  1. id (string or number)
  2. name (string)
  3. carRented (object) (null by default)

Car Object

Then you should create a Car Object Constructor that have the following properties:

  1. id (string or number)
  2. producer (string)
  3. model (string)
  4. rentalPricePerDay (number)
  5. available (boolean) (true by default)
  6. customer (object) (null by default)
  7. rentalDuration (number) (in days) (0 by default)
  8. quotePrice (function)
  9. reserve (function)
  10. return (function)
  • The quotePrice function takes a variable call rentalDuration then returns the rentalPrice * rentalDuration

  • The reserve function takes a customer object and rentalDuration value.

    1. Check if this car is available for rental
    2. If yes:
    3. Change available to false
    4. Set this car's customer to customer
    5. Set this car's rentalDuration to rentalDuration
    6. return true
    7. If no:
    8. return false
  • The return function take no arguments.

    1. Check if this car is available
    2. If yes:
    3. return "Sorry, this car have already been returned.";
    4. If No:
    5. Set this car's availible to true
    6. Set this car's customer to null
    7. Set this car's rentalDuration to null;

Vendor Object

After the Car Object, create a Vendor Object Constructor that have the following properties:

  1. name (string) (provided)
  2. cars (array) (empty array by default) (provided)
  3. customers (array) (empty array by default) (provided)
  4. findCarIndex (function) (provided)
  5. findCustomerIndex (function) (provided)
  6. getCar (function) (provided)
  7. getCustomer (function) (provided)
  8. addCar (function)
  9. addCustomer (function)
  10. removeCar (function)
  11. removeCustomer (function)
  12. availableCars (function)
  13. rentCar (function)
  14. returnCar (function)
  15. totalRevenue (function)
  • The findCarIndex/findCustomerIndex function takes an carID/customerID and returns the index or -1.

  • The getCar/getCustomer function takes a carID/customerID and returns the actual car/customer object or undefined.

  • The addCar/addCustomer function takes a carObj or customerObj.

    1. Check if car/customer exists using getCar/getCustomer
    2. If car/customer is found
    3. console.log("ID already exists")
    4. Else
    5. Push the carObj/customerObj into cars/customers list
    6. console.log("Car/Customer added to warehouse")
  • The removeCar/removeCustomer function takes an carID/customerID.

    1. Check if car/customer exists using findCarIndex/findCustomerIndex
    2. If carIndex/customerIndex >= 0
    3. Delete car/customer from the cars/customers list using array.splice
    4. console.log("Car/Customer deleted");
    5. Else
    6. console.log("Car/Customer not found")
  • The availableCars function take no arguments. Use the array.filter to generate an array of cars available for rent.

  • The rentCar function takes customerID and rentalDuration

    1. Get a list of available cars using the function availableCars
    2. If availableCars array is empty
    3. console.log("All our cars have been rented")
    4. Else
    5. Get customer using the function getCustomer
    6. If customer is found 1. Set customer's carRented to the first available car 2. Call the car's reserve function with customerObj and rentalDuration 3. console.log("The car has been reserved");
    7. Else 1. console.log("Please provide a valid customerID");
  • The returnCar function takes a customerID

    1. Get customer using the function getCustomer
    2. if customer is found
    3. Call customer's carRented's return function
    4. Set customer's carRented to null
    5. console.log( "Thank you for using our service");
    6. else
    7. console.log("Please provide a valid customerID");
  • The totalRevenue function takes no arguments. Use the array.reduce to sum up the revenues for each car.

About


Languages

Language:JavaScript 100.0%