valiamaximova1

02Parking classes

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