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