Created
August 20, 2024 16:52
-
-
Save GAM3RG33K/0ac21856657392231523a4373a7f6844 to your computer and use it in GitHub Desktop.
JalanTech-Alarm-Clock-Assignment
This file contains 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
// @Author - Harshvardhan Joshi ([email protected]) | |
// Objective: | |
// Using object-oriented programming, please implement command line program to implement an | |
// alarm clock | |
// An alarm clock has following features: | |
// 1. It displays the current time | |
// 2. A user can create any number of alarms by specifying the alarm time and day of the week and | |
// time when the alarm should alert | |
// 3. A user can snooze an alarm maximum of 3 times at an interval of 5 minutes each. | |
// 4. A user can delete an alarm | |
const kDefaultSnoozeTime = 5; | |
const kMaxSnoozeCount = 3; | |
const kMinsInHours = 60; | |
const weekdays = [ | |
"Sunday", | |
"Monday", | |
"Tuesday", | |
"Wednesday", | |
"Thursday", | |
"Friday", | |
"Saturday" | |
]; | |
// Base class to show current time | |
class Clock { | |
showTime() { | |
console.log(new Date()); | |
} | |
} | |
// Alarm Data Model to hold & process alarm related data | |
class Alarm { | |
constructor(time, day) { | |
this.timeInEpochMilliseconds = time; | |
this.dayOfTheWeek = day; | |
this.snoozeCount = 0; | |
} | |
isSameAs(hours, minutes, dayOfTheWeek) { | |
var alarmDate = new Date(this.timeInEpochMilliseconds); | |
var alarmHour = alarmDate.getHours(); | |
var alarmMins = alarmDate.getMinutes(); | |
return ( | |
hours === alarmHour && | |
minutes === alarmMins && | |
dayOfTheWeek === this.dayOfTheWeek | |
); | |
} | |
display() { | |
var alarmDate = new Date(this.timeInEpochMilliseconds); | |
var alarmHour = alarmDate.getHours(); | |
var alarmMins = alarmDate.getMinutes(); | |
var alarmDateString = `${alarmDate.getFullYear()}-${ | |
alarmDate.getMonth() + 1 | |
}-${alarmDate.getDate() + 1}`; | |
var alarmTime = `${alarmHour}:${alarmMins}`; | |
var weekDay = this.dayOfTheWeek; | |
console.info(`\n${alarmDateString}(${weekDay}) - ${alarmTime}`); | |
} | |
snooze() { | |
if (this.snoozeCount === kMaxSnoozeCount) { | |
console.log("Cannot snooze further"); | |
} else { | |
this.snoozeCount++; | |
var alarmTime = new Date(this.timeInEpochMilliseconds); | |
var minutes = alarmTime.getMinutes(); | |
var hours = alarmTime.getHours(); | |
minutes += kDefaultSnoozeTime; | |
// Adjust Hours & minutes | |
if (minutes >= kMinsInHours) { | |
hours++; | |
minutes -= kMinsInHours; | |
} | |
// Update alarm time | |
var newTime = new Date(alarmTime); | |
newTime.setMinutes(minutes); | |
newTime.setHours(hours); | |
console.log(`Snoozed, Next time to alarm: ${newTime.toUTCString()}`); | |
this.timeInEpochMilliseconds = newTime.getTime(); | |
} | |
} | |
} | |
// Derived Class from Clock to show time as well as hold Alarms | |
// This class allows user to interact with the alarms, by allowing to add, delete & display them | |
class AlarmClock extends Clock { | |
constructor() { | |
super(); | |
this.alarms = []; | |
} | |
displayAll() { | |
console.info( | |
`\n\n============ ALARMS (${this.alarms.length}) ============\n============ Sorted By Date ============\n\n` | |
); | |
var allAlarms = [...this.alarms]; | |
allAlarms.sort( | |
(a, b) => a.timeInEpochMilliseconds > b.timeInEpochMilliseconds | |
); | |
allAlarms.forEach((alarm) => { | |
alarm.display(); | |
}); | |
console.info("\n\n================================\n\n"); | |
} | |
validateNewAlarmTime(hours, minutes, currentDate) { | |
var currentHour = currentDate.getHours(); | |
var currentMinutes = currentDate.getMinutes(); | |
if (currentHour > hours || currentMinutes > minutes) { | |
return "Can't set alarm in past time"; | |
} | |
return null; | |
} | |
add(hours, minutes, dayOfTheWeek) { | |
var newAlarmDate = new Date(); | |
var currentDay = newAlarmDate.getDay(); | |
var alarmDay = weekdays.indexOf(dayOfTheWeek); | |
var difference = alarmDay - currentDay; | |
// Check if the day is different | |
if (difference !== 0) { | |
if (difference < 0) { | |
difference = (difference + 7) % 7; | |
} | |
} else { | |
// Check if the time is different for same day | |
var error = this.validateNewAlarmTime(hours, minutes, newAlarmDate); | |
if (error) { | |
console.error(error); | |
console.log("Updating the date by a week to create new alarm"); | |
difference = 7; | |
} | |
} | |
newAlarmDate.setDate(newAlarmDate.getDate() + difference); | |
newAlarmDate.setHours(hours); | |
newAlarmDate.setMinutes(minutes); | |
var alarmTimeInEpochMilliseconds = newAlarmDate.getTime(); | |
var newAlarm = new Alarm(alarmTimeInEpochMilliseconds, dayOfTheWeek); | |
this.alarms.push(newAlarm); | |
console.info(`Added new alarm => ${dayOfTheWeek} - ${hours}:${minutes}`); | |
} | |
deleteAlarm(hours, minutes, dayOfTheWeek) { | |
var remainingAlarm = this.alarms.filter((alarm) => { | |
return !alarm.isSameAs(hours, minutes, dayOfTheWeek); | |
}); | |
if (this.alarms.length !== remainingAlarm.length) { | |
console.info(`Alarm Deleted => ${dayOfTheWeek} - ${hours}:${minutes}`); | |
this.alarms = remainingAlarm; | |
} else { | |
console.info("No matching alarm found"); | |
} | |
} | |
} | |
console.log(`\n\n============ DEMO START ============\n\n`); | |
const alarmClock = new AlarmClock(); | |
alarmClock.showTime(); | |
alarmClock.add(6, 45, "Monday"); | |
alarmClock.add(7, 0, "Monday"); | |
alarmClock.add(7, 15, "Monday"); | |
alarmClock.add(7, 30, "Monday"); | |
alarmClock.add(15, 30, "Wednesday"); | |
// Set alarm for Everyday in the week | |
weekdays.forEach((day) => { | |
alarmClock.add(8, 0, day); | |
}); | |
alarmClock.displayAll(); | |
alarmClock.deleteAlarm(8, 0, "Sunday"); | |
alarmClock.deleteAlarm(6, 45, "Monday"); | |
alarmClock.deleteAlarm(7, 0, "Monday"); | |
alarmClock.deleteAlarm(15, 30, "Wednesday"); | |
alarmClock.displayAll(); | |
alarmClock.add(8, 30, "Monday"); | |
alarmClock.displayAll(); | |
console.log(`\n\n============ DEMO END ============\n\n`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment