Advertisement
petur_stoqnov

Untitled

Feb 14th, 2021
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Parking {
  2.     constructor(capacity) {
  3.         this.capacity = Number(capacity);
  4.         this.vehicles = [];
  5.     }
  6.  
  7.     addCar(carModel, carNumber) {
  8.         if (this.capacity <= 0) {
  9.             throw new Error('Not enough parking space.')
  10.         }
  11.         this.vehicles.push({carModel, carNumber, payed: false})
  12.         this.capacity--
  13.         return `The ${carModel}, with a registration number ${carNumber}, parked.`
  14.     }
  15.  
  16.     removeCar(carNumber) {
  17.         // found index of the object
  18.         let index = this.vehicles.map(function (car) {return car.carNumber;}).indexOf(carNumber);
  19.         // get the object
  20.         let foundCar = this.vehicles[index]
  21.         if (!foundCar) {
  22.             throw new Error(`The car, you're looking for, is not found.`)
  23.        }
  24.        if (foundCar.payed === false) {
  25.            throw new Error(`${carNumber} needs to pay before leaving the parking lot.`)
  26.        }
  27.        // remove the object from array
  28.        this.vehicles = this.vehicles.splice(index, 1);
  29.        this.capacity++
  30.        return `${carNumber} left the parking lot.`
  31.    }
  32.  
  33.    pay(carNumber) {
  34.        // found index of the object
  35.        let index = this.vehicles.map(function (car) {return car.carNumber;}).indexOf(carNumber);
  36.        // get the object
  37.        let foundCar = this.vehicles[index];
  38.        if(!foundCar){
  39.            throw new Error(`${carNumber} is not in the parking lot.`)
  40.        }
  41.        if(foundCar.payed === true){
  42.            throw new Error(`${carNumber}'s driver has already payed his ticket.`)
  43.         }
  44.         this.vehicles[index].payed = true;
  45.         return`${carNumber}'s driver successfully payed for his stay.`
  46.    }
  47.    getStatistics(carNumber){
  48.        if(carNumber === undefined){
  49.           let result = `The Parking Lot has ${this.capacity} empty spots left.`
  50.            this.vehicles.sort((a,b)=> a.carModel.localeCompare(b.carModel));
  51.            for (let i = 0; i < this.vehicles.length; i++) {
  52.                let currentCar = this.vehicles[i];
  53.                result += `\n${currentCar.carModel} == ${currentCar.carNumber} - ${currentCar.payed === false ? 'Not payed' : 'Has payed'}`
  54.            }
  55.            return result.trim();
  56.        } else {
  57.            // found index of the object
  58.            let index = this.vehicles.map(function (car) {return car.carNumber;}).indexOf(carNumber);
  59.            // get the object
  60.            let foundCar = this.vehicles[index];
  61.            return `${foundCar.carModel} == ${foundCar.carNumber} - ${foundCar.payed === false ? 'Not payed' : 'Has payed'}`
  62.        }
  63.    }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement