Created
February 22, 2024 00:26
-
-
Save gauravmehla/ae308a1f6dc16578cede9795420e1497 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const app = express(); | |
const port = 3000; | |
let golfCoursesData = { | |
timeslots: { | |
"08:00": { capacity: 4, booked: [], waitingList: [] }, | |
"10:00": { capacity: 4, booked: [], waitingList: [] }, | |
}, | |
}; | |
const addBooking = (user) => { | |
if (!golfCoursesData.timeslots[user.slot]) { | |
console.log("Invalid time slot"); | |
return; | |
} | |
if ( | |
golfCoursesData.timeslots[user.slot].booked.length < | |
golfCoursesData.timeslots[user.slot].capacity | |
) { | |
console.log("Booking successful!"); | |
golfCoursesData.timeslots[user.slot].booked.push(user); | |
} else { | |
console.log("Sorry, the time slot is full. Adding to waitlist."); | |
if (golfCoursesData.timeslots[user.slot].waitingList.length < 2) { | |
console.log("You have been added to the waiting list"); | |
golfCoursesData.timeslots[user.slot].waitingList.push(user); | |
} else { | |
console.log( | |
"Sorry, the waiting list is full. Please try another time slot." | |
); | |
} | |
} | |
}; | |
const cancelBooking = (user) => { | |
if (!golfCoursesData.timeslots[user.slot]) { | |
console.log("Invalid time slot. No booking found for user"); | |
return; | |
} | |
if (golfCoursesData.timeslots[user.slot].booked.length > 0) { | |
const index = golfCoursesData.timeslots[user.slot].booked.findIndex( | |
(booking) => booking.user === user.user | |
); | |
if (index === -1) { | |
console.log("No booking found for user"); | |
return; | |
} | |
golfCoursesData.timeslots[user.slot].booked.splice(index, 1); | |
console.log("Booking cancelled"); | |
// Checking for waiting list | |
if (golfCoursesData.timeslots[user.slot].waitingList.length > 0) { | |
const nextUser = golfCoursesData.timeslots[user.slot].waitingList.shift(); | |
console.log("Next user in waiting list: ", nextUser); | |
addBooking(nextUser); | |
} | |
} | |
}; | |
const viewBookings = () => { | |
for (const [key, value] of Object.entries(golfCoursesData.timeslots)) { | |
console.log( | |
`${key}: ${value.booked.length} booked. waiting list: ${ | |
value.waitingList.length | |
}. Booked users: ${value.booked.map((user) => user.user)}` | |
); | |
} | |
}; | |
// Step 1 | |
addBooking({ slot: "08:00", user: "user1" }); | |
addBooking({ slot: "08:00", user: "user2" }); | |
addBooking({ slot: "08:00", user: "user3" }); | |
addBooking({ slot: "08:00", user: "user4" }); | |
addBooking({ slot: "08:00", user: "user5" }); | |
addBooking({ slot: "08:00", user: "user6" }); | |
addBooking({ slot: "08:00", user: "user7" }); | |
console.log("\n\n\n"); | |
// Step 2 | |
cancelBooking({ slot: "08:00", user: "user4" }); | |
console.log("\n\n\n"); | |
cancelBooking({ slot: "08:00", user: "user5" }); | |
console.log("\n\n\n"); | |
addBooking({ slot: "08:00", user: "user8" }); | |
console.log("\n\n\n"); | |
// Step 3 | |
viewBookings(); | |
app.listen(port, () => { | |
console.log(`Example app listening on port ${port}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment