Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Parking {
- constructor(capacity) {
- this.capacity = capacity;
- this.vehicles = [];
- this.currentSpots = 0;
- }
- addCar(carModel, carNumber) {
- if (this.currentSpots + 1 > this.capacity) {
- throw new Error("Not enough parking space.")
- } else {
- let currCar = { carModel: carModel, carNumber: carNumber, payed: false }
- this.vehicles.push(currCar)
- this.currentSpots++;
- }
- return `The ${carModel}, with a registration number ${carNumber}, parked.`;
- }
- removeCar(carNumber) {
- if (this.vehicles.find(x => x.carNumber == carNumber) == undefined) {
- throw new Error("The car, you're looking for, is not found.")
- } else {
- let currCar = this.vehicles.find(x => x.carNumber == carNumber);
- if (currCar.payed == false) {
- throw new Error(`${carNumber} needs to pay before leaving the parking lot.`)
- } else {
- let carrIndex = this.vehicles.findIndex(x => x.carNumber == carNumber);
- this.vehicles = this.vehicles.splice(carrIndex, 1);
- this.currentSpots--;
- return `${carNumber} left the parking lot.`;
- }
- }
- }
- pay(carNumber) {
- if (this.vehicles.find(x => x.carNumber == carNumber) == undefined) {
- throw new Error(`${carNumber} is not in the parking lot.`);
- } else {
- let currCar = this.vehicles.find(x => x.carNumber == carNumber);
- if (currCar.payed == false) {
- currCar.payed = true;
- return `${carNumber}'s driver successfully payed for his stay.`
- } else {
- throw new Error(`${carNumber}'s driver has already payed his ticket.`)
- }
- }
- }
- getStatistics() {
- if (arguments.length > 0) {
- let carNumber = arguments[0];
- let currCarr = this.vehicles.find(x => x.carNumber == carNumber);
- let payedRes;
- if (currCarr.payed) {
- payedRes = 'Has payed';
- } else {
- payedRes = 'Not payed'
- }
- return `${currCarr.carModel} == ${currCarr.carNumber} - ${payedRes}`;
- } else {
- let res = [];
- res.push(`The Parking Lot has ${this.capacity - this.currentSpots} empty spots left.`)
- let sorted = this.vehicles.sort(function (a, b) {
- if (a.carModel > b.carModel) {
- return 1;
- }
- if (b.carModel < b.carModel) {
- return -1;
- }
- });
- sorted.forEach(x => {
- let payedRes;
- if (x.payed) {
- payedRes = 'Has payed';
- } else {
- payedRes = 'Not payed';
- }
- res.push(`${x.carModel} == ${x.carNumber} - ${payedRes}`);
- })
- return res.join('\n');
- }
- }
- }
Add Comment
Please, Sign In to add comment