-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (80 loc) · 2.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
let parking_lot = (function () {
let parkingSize = 0,
parkingArray = [],
emptySlots = [];
const createParkinglotSize = (n) => {
parkingSize = n;
console.warn("Created parking lot with " + n + " slots");
};
const calculateCharge = (parkedTime) => {
let totalCharges = 0;
if (parkedTime < 3) {
totalCharges = 10;
} else {
totalCharges = 10 + (parkedTime - 2) * 10;
}
return totalCharges;
};
const parkVehicle = (vehicleNumber) => {
if (parkingArray.includes(vehicleNumber)) {
console.log("Sorry, Duplicate Entry");
} else {
if (emptySlots.length) {
const slotNumber = emptySlots[0];
parkingArray[slotNumber - 1] = vehicleNumber;
emptySlots.shift();
console.log("Allocated slot number: " + slotNumber);
} else {
if (parkingArray.length < parkingSize) {
parkingArray.push(vehicleNumber);
console.log("Allocated slot number: " + parkingArray.length);
} else {
console.log("Sorry, parking lot is full");
}
}
}
};
const leaveVehicle = (vehicleNumber, parkedTime) => {
if (parkingArray.includes(vehicleNumber)) {
let arrayindex = parkingArray.indexOf(vehicleNumber);
emptySlots.push(arrayindex + 1);
emptySlots.sort();
parkingArray[arrayindex] = null;
console.log(
"Registration number " +
vehicleNumber +
" with Slot Number " +
(arrayindex + 1) +
" is free with Charge " +
calculateCharge(parkedTime)
);
} else {
console.log("Registration number " + vehicleNumber + " not found");
}
};
const parkingStatus = () => {
console.log("Slot No. Registration No.");
parkingArray.forEach((item, index) => {
if (item) console.log(index + 1 + " " + item);
});
};
return { createParkinglotSize, parkVehicle, leaveVehicle, parkingStatus };
})();
parking_lot.createParkinglotSize(6);
parking_lot.parkVehicle("KA-01-HH-1234");
parking_lot.parkVehicle("KA-01-HH-9999");
parking_lot.parkVehicle("KA-01-BB-0001");
parking_lot.parkVehicle("KA-01-HH-7777");
parking_lot.parkVehicle("KA-01-HH-2701");
parking_lot.parkVehicle("KA-01-HH-3141");
parking_lot.leaveVehicle("KA-01-HH-3141", 4);
parking_lot.parkingStatus();
parking_lot.parkVehicle("KA-01-P-333");
parking_lot.parkVehicle("DL-12-AA-9999");
parking_lot.leaveVehicle("KA-01-HH-1234", 4);
parking_lot.leaveVehicle("KA-01-BB-0001", 6);
parking_lot.leaveVehicle("DL-12-AA-9999", 2);
parking_lot.parkVehicle("KA-09-HH-0987");
parking_lot.parkVehicle("CA-09-IO-1111");
parking_lot.parkVehicle("KA-09-HH-0123");
parking_lot.parkingStatus();