
Exercise: Days of the week enum
Your task is to create an enum that represents the days of the week and contains a few useful properties and functions. Here are the exact instructions:
- Define an enum class named
DayOfWeekwith the following days:MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY. - Enum elements should be defined in the order they appear in the week.
- Enum should define a primary constructor with two properties:
isWeekend(of typeBoolean) indicating whether the day is a weekend day (Saturday or Sunday) anddayName(of typeString) containing the full name of the day. - Implement a function named
nextDaythat takes aDayOfWeekas input and returns the next day in the sequence. For example, if the input isMONDAY, the function should returnTUESDAY.
The following code should work:
val friday: DayOfWeek = DayOfWeek.FRIDAY println(friday.dayName) // Friday println(friday.isWeekend) // false val saturday: DayOfWeek = friday.nextDay() println(saturday.dayName) // Saturday println(saturday.isWeekend) // true
This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in essentials/enums/DayOfWeek.kt. You can find there example usage.
Once you are done with the exercise, you can check your solution here.
Playground
// TODO fun main() { val friday: DayOfWeek = DayOfWeek.FRIDAY println(friday.dayName) // Friday println(friday.isWeekend) // false val saturday: DayOfWeek = friday.nextDay() println(saturday.dayName) // Saturday println(saturday.isWeekend) // true }