// Smart Parking Management System - Fixed Version
class Queue {
constructor() { this.items = []; }
enqueue(item) { this.items.push(item); }
dequeue() { return this.items.shift(); }
isEmpty() { return this.items.length === 0; }
}
class Stack {
constructor() { this.items = []; }
push(item) { this.items.push(item); }
pop() { return this.items.pop(); }
isEmpty() { return this.items.length === 0; }
}
class SmartParking {
constructor(totalSlots = 20) {
this.totalSlots = totalSlots;
this.availableSlots = totalSlots;
this.parkedVehicles = new Map(); // Hash Map
this.parkingHistory = [];
this.waitingQueue = new Queue();
this.operationStack = new Stack();
}
// Vehicle Entry
parkVehicle(vehicleNumber, vehicleType = "Car") {
if (this.availableSlots > 0) {
const slot = this.totalSlots - this.availableSlots + 1;
const entryTime = new Date();
this.parkedVehicles.set(vehicleNumber, {
slot: slot,
type: vehicleType,
entryTime: entryTime
});
this.availableSlots--;
this.operationStack.push(`Parked ${vehicleNumber} at slot ${slot}`);
console.log(`Parked ${vehicleNumber} at Slot ${slot}`);
return slot;
} else {
this.waitingQueue.enqueue(vehicleNumber);
console.log(`No slot available. ${vehicleNumber} added to waiting queue.`);
return null;
}
}
// Vehicle Exit
exitVehicle(vehicleNumber) {
if (!this.parkedVehicles.has(vehicleNumber)) {
console.log("Vehicle not found.");
return;
}
const details = this.parkedVehicles.get(vehicleNumber);
const exitTime = new Date();
const durationMs = exitTime - details.entryTime;
const durationMin = Math.ceil(durationMs / 60000);
const fee = durationMin * 10;
this.parkingHistory.push({
vehicleNumber,
slot: details.slot,
type: details.type,
duration: durationMin,
fee: fee,
exitTime: exitTime
});
this.parkedVehicles.delete(vehicleNumber);
this.availableSlots++;
// Serve from queue if waiting
if (!this.waitingQueue.isEmpty()) {
const nextVehicle = this.waitingQueue.dequeue();
this.parkVehicle(nextVehicle);
}
console.log(`Exited ${vehicleNumber}. Fee: Rs. ${fee}`);
return fee;
}
// Vehicle Search
searchVehicle(vehicleNumber) {
if (this.parkedVehicles.has(vehicleNumber)) {
const v = this.parkedVehicles.get(vehicleNumber);
console.log(`Found: Slot ${v.slot}, Type: ${v.type}`);
return v;
}
console.log("Vehicle not found.");
}
// Statistics
showStatistics() {
console.log("\n=== Parking Statistics ===");
console.log("Total Slots:", this.totalSlots);
console.log("Available Slots:", this.availableSlots);
console.log("Occupied:", this.parkedVehicles.size);
console.log("Waiting in Queue:", this.waitingQueue.items.length);
console.log("Total Vehicles Served:", this.parkingHistory.length);
}
// History
showHistory() {
console.log("\n=== Parking History ===");
this.parkingHistory.forEach((record, index) => {
console.log(`${index+1}. ${record.vehicleNumber} | Slot ${record.slot} | Rs. ${record.fee} | ${record.duration} min`);
});
}
// Slot Availability
showAvailability() {
console.log(`\nAvailable Slots: (\( {this.availableSlots}/ \){this.totalSlots})`);
}
}
// ==================== DEMO ====================
const parkingSystem = new SmartParking(15);
console.log("=== Smart Parking System Started ===\n");
parkingSystem.parkVehicle("MH01AB1234", "Car");
parkingSystem.parkVehicle("MH02CD5678", "Bike");
parkingSystem.parkVehicle("MH03EF9012", "Car");
parkingSystem.showAvailability();
parkingSystem.searchVehicle("MH02CD5678");
parkingSystem.exitVehicle("MH01AB1234");
parkingSystem.showStatistics();
parkingSystem.showHistory();
console.log("\n=== System Ready ===");
To embed this project on your website, copy the following code and paste it into your website's HTML: