Giter Club home page Giter Club logo

lab-js-problems-objects-and-arrays's Introduction

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.

lab-js-problems-objects-and-arrays's People

Contributors

dshcheung avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.