SHOW:
|
|
- or go back to the newest paste.
| 1 | class Parking {
| |
| 2 | constructor(capacity) {
| |
| 3 | this.capacity = capacity; | |
| 4 | this.vehicles = []; | |
| 5 | this.currentSpots = 0; | |
| 6 | } | |
| 7 | addCar(carModel, carNumber) {
| |
| 8 | if (this.currentSpots + 1 > this.capacity) {
| |
| 9 | throw new Error("Not enough parking space.")
| |
| 10 | } else {
| |
| 11 | let currCar = { carModel: carModel, carNumber: carNumber, payed: false }
| |
| 12 | this.vehicles.push(currCar) | |
| 13 | this.currentSpots++; | |
| 14 | } | |
| 15 | return `The ${carModel}, with a registration number ${carNumber}, parked.`;
| |
| 16 | } | |
| 17 | removeCar(carNumber) {
| |
| 18 | if (this.vehicles.find(x => x.carNumber == carNumber) == undefined) {
| |
| 19 | throw new Error("The car, you're looking for, is not found.")
| |
| 20 | } else {
| |
| 21 | let currCar = this.vehicles.find(x => x.carNumber == carNumber); | |
| 22 | if (currCar.payed == false) {
| |
| 23 | throw new Error(`${carNumber} needs to pay before leaving the parking lot.`)
| |
| 24 | } else {
| |
| 25 | let carrIndex = this.vehicles.findIndex(x => x.carNumber == carNumber); | |
| 26 | this.vehicles = this.vehicles.splice(carrIndex, 1); | |
| 27 | this.currentSpots--; | |
| 28 | return `${carNumber} left the parking lot.`;
| |
| 29 | } | |
| 30 | } | |
| 31 | } | |
| 32 | pay(carNumber) {
| |
| 33 | if (this.vehicles.find(x => x.carNumber == carNumber) == undefined) {
| |
| 34 | throw new Error(`${carNumber} is not in the parking lot.`);
| |
| 35 | } else {
| |
| 36 | let currCar = this.vehicles.find(x => x.carNumber == carNumber); | |
| 37 | if (currCar.payed == false) {
| |
| 38 | currCar.payed = true; | |
| 39 | return `${carNumber}'s driver successfully payed for his stay.`
| |
| 40 | } else {
| |
| 41 | throw new Error(`${carNumber}'s driver has already payed his ticket.`)
| |
| 42 | } | |
| 43 | } | |
| 44 | } | |
| 45 | ||
| 46 | getStatistics() {
| |
| 47 | if (arguments.length > 0) {
| |
| 48 | let carNumber = arguments[0]; | |
| 49 | let currCarr = this.vehicles.find(x => x.carNumber == carNumber); | |
| 50 | let payedRes; | |
| 51 | if (currCarr.payed) {
| |
| 52 | payedRes = 'Has payed'; | |
| 53 | } else {
| |
| 54 | payedRes = 'Not payed' | |
| 55 | } | |
| 56 | return `${currCarr.carModel} == ${currCarr.carNumber} - ${payedRes}`;
| |
| 57 | } else {
| |
| 58 | let res = []; | |
| 59 | res.push(`The Parking Lot has ${this.capacity - this.currentSpots} empty spots left.`)
| |
| 60 | let sorted = this.vehicles.sort(function (a, b) {
| |
| 61 | if (a.carModel > b.carModel) {
| |
| 62 | return 1; | |
| 63 | } | |
| 64 | if (b.carModel < b.carModel) {
| |
| 65 | return -1; | |
| 66 | } | |
| 67 | }); | |
| 68 | sorted.forEach(x => {
| |
| 69 | let payedRes; | |
| 70 | if (x.payed) {
| |
| 71 | payedRes = 'Has payed'; | |
| 72 | } else {
| |
| 73 | payedRes = 'Not payed'; | |
| 74 | } | |
| 75 | res.push(`${x.carModel} == ${x.carNumber} - ${payedRes}`);
| |
| 76 | }) | |
| 77 | return res.join('\n');
| |
| 78 | } | |
| 79 | } | |
| 80 | ||
| 81 | } | |
| 82 | ||
| 83 | ||
| 84 | ||
| 85 | ||
| 86 |