Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Parking {
- constructor(capacity) {
- this.capacity = Number(capacity);
- this.vehicles = [];
- }
- addCar(carModel, carNumber) {
- if (this.capacity <= 0) {
- throw new Error('Not enough parking space.')
- }
- this.vehicles.push({carModel, carNumber, payed: false})
- this.capacity--
- return `The ${carModel}, with a registration number ${carNumber}, parked.`
- }
- removeCar(carNumber) {
- // found index of the object
- let index = this.vehicles.map(function (car) {return car.carNumber;}).indexOf(carNumber);
- // get the object
- let foundCar = this.vehicles[index]
- if (!foundCar) {
- throw new Error(`The car, you're looking for, is not found.`)
- }
- if (foundCar.payed === false) {
- throw new Error(`${carNumber} needs to pay before leaving the parking lot.`)
- }
- // remove the object from array
- this.vehicles = this.vehicles.splice(index, 1);
- this.capacity++
- return `${carNumber} left the parking lot.`
- }
- pay(carNumber) {
- // found index of the object
- let index = this.vehicles.map(function (car) {return car.carNumber;}).indexOf(carNumber);
- // get the object
- let foundCar = this.vehicles[index];
- if(!foundCar){
- throw new Error(`${carNumber} is not in the parking lot.`)
- }
- if(foundCar.payed === true){
- throw new Error(`${carNumber}'s driver has already payed his ticket.`)
- }
- this.vehicles[index].payed = true;
- return`${carNumber}'s driver successfully payed for his stay.`
- }
- getStatistics(carNumber){
- if(carNumber === undefined){
- let result = `The Parking Lot has ${this.capacity} empty spots left.`
- this.vehicles.sort((a,b)=> a.carModel.localeCompare(b.carModel));
- for (let i = 0; i < this.vehicles.length; i++) {
- let currentCar = this.vehicles[i];
- result += `\n${currentCar.carModel} == ${currentCar.carNumber} - ${currentCar.payed === false ? 'Not payed' : 'Has payed'}`
- }
- return result.trim();
- } else {
- // found index of the object
- let index = this.vehicles.map(function (car) {return car.carNumber;}).indexOf(carNumber);
- // get the object
- let foundCar = this.vehicles[index];
- return `${foundCar.carModel} == ${foundCar.carNumber} - ${foundCar.payed === false ? 'Not payed' : 'Has payed'}`
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement