-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJS_ ParkingLot.js
97 lines (84 loc) · 2.57 KB
/
JS_ ParkingLot.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
86
87
88
89
90
91
92
93
94
95
96
97
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding("ascii");
let inputString = "";
let currentLine = 0;
process.stdin.on("data", function (chunk) {
inputString += chunk;
});
process.stdin.on("end", function () {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
class ParkingLot {
constructor(size) {
this.size = size;
this.slots = new Array(size).fill(null);
}
park(carId) {
for (let i = 0; i < this.size; i++) {
if (this.slots[i] === null) {
this.slots[i] = carId;
return true; // Successfully parked
}
}
return false; // Parking lot is full
}
remove(carId) {
for (let i = 0; i < this.size; i++) {
if (this.slots[i] === carId) {
this.slots[i] = null;
return true; // Successfully removed
}
}
return false; // Car not found
}
getSlots() {
return this.slots;
}
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const numberOfSlots = parseInt(readLine().trim());
const parkingLotObj = new ParkingLot(numberOfSlots);
ws.write(`Parking Lot created with number of slots as ${numberOfSlots}\n`);
let numberOfOperations = parseInt(readLine().trim());
while (numberOfOperations-- > 0) {
const inputs = readLine().trim().split(' ');
const operation = inputs[0];
const carId = inputs[1];
switch(operation) {
case 'Park':
if (parkingLotObj.park(carId)) {
ws.write(`Parking Started: ${carId}\n`);
} else {
ws.write(`Parking Full: ${carId}\n`);
}
break;
case 'Remove':
if (parkingLotObj.remove(carId)) {
ws.write(`Car id ${carId} removed from parking\n`);
} else {
ws.write(`Car: ${carId} not found\n`);
}
break;
case 'GetSlots':
const status = parkingLotObj.getSlots();
status.forEach((obj, i) => {
if (obj) {
ws.write(`Parked at slot ${i + 1}: ${obj}\n`);
} else {
ws.write(`Slot ${i + 1} is empty\n`);
}
})
break;
default:
break;
}
}
ws.end();
}