Advertisement
viligen

carDealership

Jun 10th, 2022
1,141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class CarDealership {
  2.     constructor(name) {
  3.         this.name = name;
  4.         this.availableCars = [];
  5.         this.soldCars = [];
  6.         this.totalIncome = 0;
  7.     }
  8.     addCar(model, horsepower, price, mileage) {
  9.         if (
  10.             model == '' ||
  11.             horsepower < 0 ||
  12.             !Number.isInteger(horsepower) ||
  13.             price < 0 ||
  14.             mileage < 0
  15.         ) {
  16.             throw new Error('Invalid input!');
  17.         }
  18.         this.availableCars.push({ model, horsepower, price, mileage });
  19.         return `New car added: ${model} - ${horsepower} HP - ${mileage.toFixed(
  20.             2
  21.         )} km - ${price.toFixed(2)}$`;
  22.     }
  23.     sellCar(model, desiredMileage) {
  24.         let searchedCar = this.availableCars.find((o) => o.model == model);
  25.         if (!searchedCar) {
  26.             throw new Error(`${model} was not found!`);
  27.         }
  28.         if (searchedCar.mileage - desiredMileage > 40000) {
  29.             searchedCar.price *= 0.9;
  30.         } else if (
  31.             searchedCar.mileage - desiredMileage <= 40000 &&
  32.             searchedCar.mileage - desiredMileage > 0
  33.         ) {
  34.             searchedCar.price *= 0.95;
  35.         }
  36.         let soldCarIndex = this.availableCars.findIndex(
  37.             (o) => o == searchedCar
  38.         );
  39.         this.availableCars.splice(soldCarIndex, 1);
  40.         this.soldCars.push({
  41.             model: searchedCar.model,
  42.             horsepower: searchedCar.horsepower,
  43.             soldPrice: searchedCar.price,
  44.         });
  45.         this.totalIncome += searchedCar.price;
  46.         return `${model} was sold for ${searchedCar.price.toFixed(2)}$`;
  47.     }
  48.  
  49.     currentCar() {
  50.         if (this.availableCars.length == 0) {
  51.             return 'There are no available cars';
  52.         }
  53.         let result = [];
  54.         result.push('-Available cars:');
  55.         this.availableCars.forEach((o) =>
  56.             result.push(
  57.                 `---${o.model} - ${o.horsepower} HP - ${o.mileage.toFixed(
  58.                     2
  59.                 )} km - ${o.price.toFixed(2)}$`
  60.             )
  61.         );
  62.         return result.join('\n');
  63.     }
  64.  
  65.     salesReport(criteria) {
  66.         let sorted;
  67.         let availableCriteria = ['horsepower', 'model'];
  68.         if (!availableCriteria.includes(criteria)) {
  69.             throw new Error('Invalid criteria!');
  70.         }
  71.         if (criteria == 'model') {
  72.             sorted = this.soldCars.sort((a, b) =>
  73.                 a.model.localeCompare(b.model)
  74.             );
  75.         } else {
  76.             sorted = this.soldCars.sort((a, b) => b.horsepower - a.horsepower);
  77.         }
  78.         let result2 = [];
  79.         result2.push(
  80.             `-${this.name} has a total income of ${this.totalIncome.toFixed(
  81.                 2
  82.             )}$`
  83.         );
  84.         result2.push(`-${sorted.length} cars sold:`);
  85.         sorted.forEach((o) =>
  86.             result2.push(
  87.                 `---${o.model} - ${o.horsepower} HP - ${o.soldPrice.toFixed(
  88.                     2
  89.                 )}$`
  90.             )
  91.         );
  92.         return result2.join('\n');
  93.     }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement