Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Parking {
- constructor(capacity) {
- this.capacity = capacity;
- this.vehicles = [];
- }
- emptySlots() {
- return this.capacity - this.vehicles.length;
- }
- addCar(carModel, carNumber) {
- // if not enough parking space in arr
- if (this.vehicles.length >= this.capacity) {
- throw new Error('Not enough parking space.');
- }
- // otherwise: carModel, carNumber, payed: default false
- this.vehicles.push({ carModel, carNumber, payed: false });
- return `The ${carModel}, with a registration number ${carNumber}, parked.`;
- }
- removeCar(carNumber) {
- // if car is not found: error
- const condition = (carObj) => {
- return carObj.carNumber === carNumber;
- };
- // get vehicle
- let currentVehicle = this.vehicles.find(condition);
- // get index
- let vehicleIndex = this.vehicles.indexOf(currentVehicle);
- if (!currentVehicle) {
- throw new Error("The car, you're looking for, is not found.");
- }
- // if car hasn't been payed for
- if (!currentVehicle.payed) {
- throw new Error(
- `${carNumber} needs to pay before leaving the parking lot.`
- );
- }
- // otherwise: remove from arr
- this.vehicles.splice(vehicleIndex, 1);
- return `${carNumber} left the parking lot.`;
- }
- pay(carNumber) {
- // if car is not found: error
- const condition = (carObj) => {
- return carObj.carNumber === carNumber;
- };
- // get vehicle
- let currentVehicle = this.vehicles.find(condition);
- if (!currentVehicle) {
- throw new Error(`${carNumber} is not in the parking lot.`);
- }
- // if already payed: error
- if (currentVehicle.payed) {
- throw new Error(`${carNumber}'s driver has already payed his ticket.`);
- }
- // otherwise: set payed to true
- currentVehicle.payed = true;
- return `${carNumber}'s driver successfully payed for his stay.`;
- }
- getStatistics(carNumber) {
- let output = [];
- // if param is provided
- // return only the information about the car with the given carNumber:
- // `{carModel} == {carNumber} - {Has payed / Not payed}`
- if (carNumber) {
- const condition = (carObj) => {
- return carObj.carNumber === carNumber;
- };
- // get vehicle
- let currentVehicle = this.vehicles.find(condition);
- if (currentVehicle.payed) {
- let str = `${currentVehicle.carModel} == ${currentVehicle.carNumber} - Has payed`;
- output.push(str);
- } else {
- let str = `${currentVehicle.carModel} == ${currentVehicle.carNumber} - Not payed`;
- output.push(str);
- }
- return output[0]; // MAY BE WRONG
- }
- // if no params
- // FULL INFO
- // empty slots in parking lot
- output.push(`The Parking Lot has ${this.emptySlots()} empty spots left.`);
- // sort vehicles alphabetically ascending by carModel
- const alphaSort = (objA, objB) => {
- // NOT TESTED
- return objA.carModel.localeCompare(objB.carModel);
- };
- this.vehicles.sort(alphaSort);
- // f/each vehicle:
- this.vehicles.forEach((vehicle) => {
- if (vehicle.payed) {
- let str = `${vehicle.carModel} == ${vehicle.carNumber} - Has payed`;
- output.push(str);
- } else {
- let str = `${vehicle.carModel} == ${vehicle.carNumber} - Not payed`;
- output.push(str);
- }
- });
- return output.join('\n'); // MAY BE WRONG
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement