Btwonu

Need for Speed

Aug 7th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function needForSpeed(arr) {
  2.   class Car {
  3.     constructor(model, mileage, fuel) {
  4.       this.model = model;
  5.       this.mileage = mileage;
  6.       this.fuel = fuel;
  7.     }
  8.  
  9.     drive(params) {
  10.       let [distance, fuelNeeded] = params;
  11.  
  12.       if (this.fuel < fuelNeeded) {
  13.         console.log('Not enough fuel to make that ride');
  14.         return;
  15.       }
  16.  
  17.       this.mileage += distance;
  18.       this.fuel -= fuelNeeded;
  19.  
  20.       console.log(
  21.         `${this.model} driven for ${distance} kilometers. ${fuelNeeded} liters of fuel consumed.`
  22.       );
  23.     }
  24.  
  25.     refuel(params) {
  26.       let [fuelRefill] = params;
  27.       let oldFuel = this.fuel;
  28.  
  29.       this.fuel =
  30.         this.fuel + fuelRefill >= MAX_FUEL ? MAX_FUEL : this.fuel + fuelRefill;
  31.  
  32.       console.log(`${this.model} refueled with ${this.fuel - oldFuel} liters`);
  33.     }
  34.  
  35.     revert(params) {
  36.       let [kilometers] = params;
  37.  
  38.       this.mileage -= kilometers;
  39.  
  40.       if (this.mileage < MIN_MILEAGE) {
  41.         this.mileage = MIN_MILEAGE;
  42.         return;
  43.       }
  44.  
  45.       console.log(
  46.         `${this.model} mileage decreased by ${kilometers} kilometers`
  47.       );
  48.     }
  49.   }
  50.  
  51.   const carCollection = {};
  52.   const MAX_MILEAGE = 100000;
  53.   const MAX_FUEL = 75;
  54.   const MIN_MILEAGE = 10000;
  55.   let carsNumber = Number(arr[0]);
  56.  
  57.   //Get cars
  58.   const carData = getCars(carsNumber);
  59.  
  60.   //Split car data
  61.   carData.forEach((line) => {
  62.     let [car, mileage, fuel] = line.split('|');
  63.     mileage = Number(mileage);
  64.     fuel = Number(fuel);
  65.  
  66.     //Add cars to collection
  67.     carCollection[car] = new Car(car, mileage, fuel);
  68.   });
  69.  
  70.   //Get commands
  71.   let index = carsNumber + 1;
  72.  
  73.   while (arr[index] !== 'Stop') {
  74.     let [command, model, ...args] = arr[index].split(' : ');
  75.     command = command.toLowerCase();
  76.     args = args.map(Number);
  77.  
  78.     //Execute command
  79.     carCollection[model][command](args);
  80.  
  81.     //Remove old cars from collection
  82.     if (carCollection[model].mileage >= MAX_MILEAGE) {
  83.       console.log(`Time to sell the ${model}!`);
  84.  
  85.       delete carCollection[model];
  86.     }
  87.  
  88.     index++;
  89.   }
  90.  
  91.   //Output
  92.   const output = [];
  93.  
  94.   Object.keys(carCollection)
  95.     .sort((a, b) => a.localeCompare(b))
  96.     .sort((a, b) => {
  97.       return carCollection[b].mileage - carCollection[a].mileage;
  98.     })
  99.     .forEach((car) => {
  100.       let msg = `${car} -> Mileage: ${carCollection[car].mileage} kms, Fuel in the tank: ${carCollection[car].fuel} lt.`;
  101.  
  102.       output.push(msg);
  103.     });
  104.  
  105.   console.log(output.join('\n'));
  106.  
  107.   //Declarations
  108.   function getCars(num) {
  109.     const cars = [];
  110.  
  111.     for (let i = 1, l = num + 1; i < l; i++) {
  112.       cars.push(arr[i]);
  113.     }
  114.  
  115.     return cars;
  116.   }
  117. }
  118.  
  119. let result = needForSpeed([
  120.   '4',
  121.   'Lamborghini Veneno|11111|74',
  122.   'Bugatti Veyron|12345|67',
  123.   'Koenigsegg CCXR|67890|12',
  124.   'Aston Martin Valkryie|99900|50',
  125.   'Drive : Koenigsegg CCXR : 382 : 82',
  126.   'Drive : Aston Martin Valkryie : 99 : 23',
  127.   'Drive : Aston Martin Valkryie : 2 : 1',
  128.   'Refuel : Lamborghini Veneno : 40',
  129.   'Revert : Bugatti Veyron : 2000',
  130.   'Stop',
  131. ]);
  132. console.log(result);
  133.  
Add Comment
Please, Sign In to add comment