Advertisement
Btwonu

3

Oct 24th, 2020 (edited)
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Parking {
  2.   constructor(capacity) {
  3.     this.capacity = capacity;
  4.     this.vehicles = [];
  5.   }
  6.  
  7.   emptySlots() {
  8.     return this.capacity - this.vehicles.length;
  9.   }
  10.  
  11.   addCar(carModel, carNumber) {
  12.     // if not enough parking space in arr
  13.     if (this.vehicles.length >= this.capacity) {
  14.       throw new Error('Not enough parking space.');
  15.     }
  16.  
  17.     // otherwise: carModel, carNumber, payed: default false
  18.     this.vehicles.push({ carModel, carNumber, payed: false });
  19.  
  20.     return `The ${carModel}, with a registration number ${carNumber}, parked.`;
  21.   }
  22.  
  23.   removeCar(carNumber) {
  24.     // if car is not found: error
  25.     const condition = (carObj) => {
  26.       return carObj.carNumber === carNumber;
  27.     };
  28.  
  29.     // get vehicle
  30.     let currentVehicle = this.vehicles.find(condition);
  31.  
  32.     // get index
  33.     let vehicleIndex = this.vehicles.indexOf(currentVehicle);
  34.  
  35.     if (!currentVehicle) {
  36.       throw new Error("The car, you're looking for, is not found.");
  37.     }
  38.  
  39.     // if car hasn't been payed for
  40.     if (!currentVehicle.payed) {
  41.       throw new Error(
  42.         `${carNumber} needs to pay before leaving the parking lot.`
  43.       );
  44.     }
  45.  
  46.     // otherwise: remove from arr
  47.     this.vehicles.splice(vehicleIndex, 1);
  48.     return `${carNumber} left the parking lot.`;
  49.   }
  50.  
  51.   pay(carNumber) {
  52.     // if car is not found: error
  53.     const condition = (carObj) => {
  54.       return carObj.carNumber === carNumber;
  55.     };
  56.  
  57.     // get vehicle
  58.     let currentVehicle = this.vehicles.find(condition);
  59.  
  60.     if (!currentVehicle) {
  61.       throw new Error(`${carNumber} is not in the parking lot.`);
  62.     }
  63.  
  64.     // if already payed: error
  65.     if (currentVehicle.payed) {
  66.       throw new Error(`${carNumber}'s driver has already payed his ticket.`);
  67.    }
  68.  
  69.    // otherwise: set payed to true
  70.    currentVehicle.payed = true;
  71.    return `${carNumber}'s driver successfully payed for his stay.`;
  72.   }
  73.  
  74.   getStatistics(carNumber) {
  75.     let output = [];
  76.  
  77.     // if param is provided
  78.     // return only the information about the car with the given carNumber:
  79.     // `{carModel} == {carNumber} - {Has payed / Not payed}`
  80.     if (carNumber) {
  81.       const condition = (carObj) => {
  82.         return carObj.carNumber === carNumber;
  83.       };
  84.  
  85.       // get vehicle
  86.       let currentVehicle = this.vehicles.find(condition);
  87.  
  88.       if (currentVehicle.payed) {
  89.         let str = `${currentVehicle.carModel} == ${currentVehicle.carNumber} - Has payed`;
  90.         output.push(str);
  91.       } else {
  92.         let str = `${currentVehicle.carModel} == ${currentVehicle.carNumber} - Not payed`;
  93.         output.push(str);
  94.       }
  95.  
  96.       return output[0]; // MAY BE WRONG
  97.     }
  98.  
  99.     // if no params
  100.     // FULL INFO
  101.     // empty slots in parking lot
  102.     output.push(`The Parking Lot has ${this.emptySlots()} empty spots left.`);
  103.  
  104.     // sort vehicles alphabetically ascending by carModel
  105.     const alphaSort = (objA, objB) => {
  106.       // NOT TESTED
  107.       return objA.carModel.localeCompare(objB.carModel);
  108.     };
  109.  
  110.     this.vehicles.sort(alphaSort);
  111.  
  112.     // f/each vehicle:
  113.     this.vehicles.forEach((vehicle) => {
  114.       if (vehicle.payed) {
  115.         let str = `${vehicle.carModel} == ${vehicle.carNumber} - Has payed`;
  116.         output.push(str);
  117.       } else {
  118.         let str = `${vehicle.carModel} == ${vehicle.carNumber} - Not payed`;
  119.         output.push(str);
  120.       }
  121.     });
  122.  
  123.     return output.join('\n'); // MAY BE WRONG
  124.   }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement