Exercise: Workout manager
In this exercise, you will be creating a console-based push-up workout manager in Kotlin. The goal of this manager is to guide users through a series of push-up sets, recording their performance, and adjusting future workout suggestions based on their results. The program will also store the suggested repetitions for the user's next workout in a file.
This is how you should start the code:
Expected Components:
Repetitions
data class, that stores the number of push-ups to be done in the first and second rounds. It has two properties:firstRound
andsecondRound
, both of typeInt
.NextRepetitionsRepository
interface, defining the contract for reading and writing the next set of repetitions. It has two methods:read()
, that returns the Repetitions from storage (ornull
if not found), andwrite(repetitions: Repetitions)
, that writes the given Repetitions to storage.FileNextRepetitionsRepository
class, implementing theNextRepetitionsRepository
interface. It reads and writes theRepetitions
object to a file namednext_repetitions.txt
. If the file does not exist, it should create it.WorkoutManager
class that handles the logic for determining the next set of repetitions based on the user's performance. It has a methodnextRepetitions(firstRoundDone: Int, secondRoundDone: Int, thirdRoundDone: Int, repetitions: Repetitions): Repetitions
, which returns a newRepetitions
object with adjusted values for the next workout based on the user's performance.WorkoutInteractor
class that serves as the main orchestrator of the application. It interacts with the user, guides them through the workout, gets their input, and provides feedback. It has a methodstart()
that starts the workout routine, guides the user through the push-up sets, records their performance, and provides feedback for the next workout. It also has a helper methodgetValidIntInput(prompt: String)
to get valid integer input from the user. The constructor of this class takes aWorkoutManager
and aNextRepetitionsRepository
.
To read and write file, use File
class. To check that a file exists, use exist
method. To read the content of a file, use readText
method. To write to a file, use writeText
method. To create a new file, use createNewFile
method.
To read user input, use readlnOrNull
function. It returns null
if the input is not a valid integer. To convert a string to an integer, use toIntOrNull
method. To write to the console, use println
function.
The algorithm for updating repetitions should be as follows:
- If the user could not do enough push-ups in the first round, decrease the repetitions for both rounds by 1.
- If the user could not do enough push-ups in the second round, decrease the repetitions for the second round by 1.
- If the user could do more push-ups in the third round than in the first round, increase the repetitions for both rounds by 1.
- If the user could do more push-ups in the third round than in the second round, increase the repetitions for the second round by 1.
- Otherwise, keep the repetitions the same.
Here is what an example conversations with the user might look like:
Hello, I am your push-ups workout assistant!
Now do 5 push-ups
How many push-ups did you do?
5
Now rest for 1 minute
Now do 5 push-ups
How many push-ups did you do?
4
Now rest for 1 minute
Now do as many push-ups as you can!
How many push-ups did you do?
3
Your next repetitions will be: 5 and 4
Hello, I am your push-ups workout assistant!
Now do 5 push-ups
How many push-ups did you do?
5
Now rest for 1 minute
Now do 4 push-ups
How many push-ups did you do?
5
Now rest for 1 minute
Now do as many push-ups as you can!
How many push-ups did you do?
6
Your next repetitions will be: 6 and 5
Hello, I am your push-ups workout assistant!
Now do 6 push-ups
How many push-ups did you do?
-1
Please enter a valid number.
How many push-ups did you do?
0
Now rest for 1 minute
Now do 5 push-ups
How many push-ups did you do?
10
Now rest for 1 minute
Now do as many push-ups as you can!
How many push-ups did you do?
0
Your next repetitions will be: 5 and 4
Instructions:
- Start by defining the
Repetitions
data class with its properties. - Define the
NextRepetitionsRepository
interface with the expected methods. - Implement the
FileNextRepetitionsRepository
class. Ensure you handle potential file-related exceptions. - Implement the
WorkoutManager
class. Here, you'll decide how to adjust the repetitions for the next workout based on the user's performance. - Finally, implement the
WorkoutInteractor
class. This class will interact with the user, guide them through the workout, get their input, and provide feedback based on the logic in theWorkoutManager
. - Once all components are implemented, run the starting code to test your application. Adjust your workout recommendations, perform the push-ups, and see how the program suggests adjustments for your next session!
Once you are done with the exercise, you can check your solution here.