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