article banner

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:

val interactor = WorkoutInteractor( manager = WorkoutManager(), nextRepetitionsRepository = FileNextRepetitionsRepository(), ) interactor.start()

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 and secondRound, both of type Int.
  • 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 (or null if not found), and write(repetitions: Repetitions), that writes the given Repetitions to storage.
  • FileNextRepetitionsRepository class, implementing the NextRepetitionsRepository interface. It reads and writes the Repetitions object to a file named next_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 method nextRepetitions(firstRoundDone: Int, secondRoundDone: Int, thirdRoundDone: Int, repetitions: Repetitions): Repetitions, which returns a new Repetitions 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 method start() 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 method getValidIntInput(prompt: String) to get valid integer input from the user. The constructor of this class takes a WorkoutManager and a NextRepetitionsRepository.

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 the WorkoutManager.
  • 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.