AreWe

Need For Speed III с класове

Jun 13th, 2020 (edited)
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. needForSpeed3 = (input) => {
  2.     function Car(model = '', mileage = 0, fuel = 0) {
  3.         [this.model, this.mileage, this.fuel] = [model, mileage, fuel];
  4.  
  5.         this.drive = (distance = 0, fuel = 0) => {
  6.             if (this.fuel >= fuel) {
  7.                 this.fuel -= fuel;
  8.                 this.mileage += distance;
  9.                 console.log(`${this.model} driven for ${distance} kilometers. ${fuel} liters of fuel consumed.`);
  10.  
  11.                 if (this.mileage >= 100000) {
  12.                     console.log(`Time to sell the ${this.model}!`);
  13.                     return true;
  14.                 }
  15.             } else {
  16.                 console.log('Not enough fuel to make that ride');
  17.             }
  18.  
  19.             return false;
  20.         };
  21.  
  22.         this.refuel = (fuel = 0) => {
  23.             let newFuel = this.fuel + fuel > 75 ? 75 : this.fuel + fuel;
  24.             console.log(`${this.model} refueled with ${newFuel - this.fuel} liters`);
  25.             this.fuel = newFuel;
  26.         };
  27.  
  28.         this.revert = (kilometers = 0) => {
  29.             this.mileage -= kilometers;
  30.             if (this.mileage >= 10000) {
  31.                 console.log(`${this.model} mileage decreased by ${kilometers} kilometers`);
  32.             } else {
  33.                 this.mileage = 10000;
  34.             }
  35.         };
  36.     }
  37.  
  38.     function CarCollection() {
  39.         this.cars = {};
  40.  
  41.         this.addCar = (model = '', mileage = '', fuel = '') => (this.cars[model] = new Car(model, +mileage, +fuel));
  42.         this.printCars = () =>
  43.             Object.entries(this.cars)
  44.                 .sort((a, b) => b[1].mileage - a[1].mileage || a[0].localeCompare(b[0]))
  45.                 .forEach(([model, car]) => {
  46.                     console.log(`${model} -> Mileage: ${car.mileage} kms, Fuel in the tank: ${car.fuel} lt.`);
  47.                 });
  48.  
  49.         this.Drive = (model = '', distance = '', fuel = '') => this.cars[model].drive(+distance, +fuel) ? delete this.cars[model] : null;
  50.         this.Refuel = (model = '', fuel = '') => this.cars[model].refuel(+fuel);
  51.         this.Revert = (model = '', kilometers = '') => this.cars[model].revert(+kilometers);
  52.         this.executeAction = (actionParams) => this[actionParams.shift()](...actionParams);
  53.     }
  54.  
  55.     let cars = new CarCollection();
  56.  
  57.     const count = Number(input.shift());
  58.     for (let i = 0; i < count; i++) {
  59.         cars.addCar(...input.shift().split('|'));
  60.     }
  61.  
  62.     while ((action = input.shift().split(' : '))[0] !== 'Stop') {
  63.         cars.executeAction(action);
  64.     }
  65.  
  66.     cars.printCars();
  67. };
Add Comment
Please, Sign In to add comment