HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Java Workout Log

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
workoutjavalog

Problem

I want to get back into Java programming and so I began to teach myself the language by reading books as well as studying for the OCAJP 8 exam which I plan on taking this coming Monday. I feel as though to get a job it would be nice to have this cert as a fresher and it would also be nice to show I have the ability to write effective code. Since I like exercising I though that an app that would allow me to track my progress would be enjoyable and useful to me.

It's a simple app (3 files) that creates a list of exercises for the user to choose from, input the number of sets, reps, and weight used. The final app will be cleaned up and much more elaborate, but I wanted to see if I could get some input on the following before I went any further. Please keep in mind this was written strictly in notepad so that I can focus on the language itself without the help of an IDE at this point in time...

//My first java workout app

```
package WorkoutApp;

import WorkoutApp.FirstExerciseClass;

public class FirstWorkoutApp
{
public static void main(String[] args)
{
FirstExerciseClass f1 = new FirstExerciseClass();
f1.workout();
f1.printResults();
}
}

//My First java workout app

package WorkoutApp;

import java.time.*;
import java.time.format.*;
import java.util.*;
import java.io.File;
import java.io.IOException;
//import java.nio.file.Files;
//import org.apache.commons.io.FileUtils;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class FirstExerciseClass
{

// Creates file object and sets pointer
File filePath = new File("C:\\someFilePath\\Workout Log.txt");

// Gets current day/time information
private LocalDate date = LocalDate.now();
// Set format to month/day/year
private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM dd yyyy");
private String todaysDate = date.format(formatter);

// Dec

Solution

// returns boolean of true if user is planning on doing another exercise otherwise returns false.
public boolean isStillLifting()
{
    boolean continueLifting = false;
    System.out.println("Are you done lifting for the day? (y/n)");  
    char result = keyboard.next().charAt(0);
    return continueLifting = (result=='y' || result == 'Y') ? false : true;
}


I prefer JavaDoc style comments, they get picked up by IDE's easier and allow you to break comments into multi-line quicker.

There's a couple things odd about this function...

First, there's no need to declare variables at the top; you can declare them as you need them.

Second, ternaries that consist of condition ? false : true can be turned into !condition.

So, this function could read like so...

// returns boolean of true if user is planning on doing another exercise otherwise returns false.
public boolean isStillLifting()
{
    System.out.println("Are you done lifting for the day? (y/n)");  
    char result = keyboard.next().charAt(0);
    boolean continueLifting = false;
    return continueLifting = !(result=='y' || result == 'Y');
}


Of course, if you look at it now, it seems rather silly to assign false to continueLifting on one line, and then, on the next, to overwrite it without reading the previous value. I'd combine the two...

// returns boolean of true if user is planning on doing another exercise otherwise returns false.
public boolean isStillLifting()
{
    System.out.println("Are you done lifting for the day? (y/n)");  
    char result = keyboard.next().charAt(0);
    boolean continueLifting = !(result=='y' || result == 'Y');
    return continueLifting; 
}


You could choose this way of writing things or you could just directly return the response.

That said...

public boolean isWorkingOut()
{
    boolean workingOut = false;
    System.out.println("Are you working out today? (y/n)");
    char result = keyboard.next().charAt(0);
    return workingOut = (result=='y' || result == 'Y') ? true : false;
}


It's the same... but inverted?

I think you'd be better off by having a askYesNoQuestion method which returns a boolean and then calling it from these two functions...

public String whatExercise()
{
    System.out.println("Choose an exercise from the list below or press 0 to end your workout.");
    showWorkoutList();
    int exerciseChoice = keyboard.nextInt();
    String exerciseDone = workoutList.get(exerciseChoice-1);
    switch(exerciseChoice)
    {
        case 0:
            System.out.println("We'll start again tomorrow!");
            System.exit(0);
            break;
        case 1:
            System.out.println("Cool, let's do some " + exerciseDone + ".");
            return exerciseDone ;
        case 2:
            System.out.println("Okay aswesome I love " + exerciseDone + "!");
            return exerciseDone;
        case 3:
            System.out.println(exerciseDone + " will get you jacked!");
            return exerciseDone;
        case 4:
            System.out.println(exerciseDone + " sound good!");
            return exerciseDone;
        case 5:
            System.out.println("Let's get some " + exerciseDone + " going!");
            return exerciseDone;
        case 6:
            System.out.println("Can't go wrong with " + exerciseDone + "!");
            return exerciseDone;
        case 7:
            System.out.println("Everyone loves " + exerciseDone + "!");
            return exerciseDone;
        case 8:
            System.out.println("Just a few " + exerciseDone + " will have you swole!");
            return exerciseDone;
        case 9:
            System.out.println("Make sure to have good form on these " + exerciseDone + "!");
            return exerciseDone;
        case 10:
            System.out.println("Get low on these " + exerciseDone + "!");
            return exerciseDone;
        default:
            System.out.println("What was that now?");
            return "";
    }
    return "WHAT?";
}


First thing I spot here is the duplication. Cases 1 through 10 are all "print a message, return exerciseDone".

You could have built a List of Strings as well using format strings. String.format will take a format string, and then any values to put into this string. So String.format("Hello, %s", "Bob") will give "Hello, Bob".

Like that, you could have simplified the creation and printing of the Strings by storing them in a list beforehand. The whole use of the switch would fall away in that case.

The question-answers:



  • Is the code easily readable, ie. if I were on a team would the way I named the variables and methods make sense to others?




It reads easily, but that's by virtue of duplication. Duplication makes it super easy to see what you're doing - because all the code on the screen does the same thing. This isn't really a good point though, because if you make 1 change in one of the values (let's say the user has to enter 9 to

Code Snippets

// returns boolean of true if user is planning on doing another exercise otherwise returns false.
public boolean isStillLifting()
{
    boolean continueLifting = false;
    System.out.println("Are you done lifting for the day? (y/n)");  
    char result = keyboard.next().charAt(0);
    return continueLifting = (result=='y' || result == 'Y') ? false : true;
}
// returns boolean of true if user is planning on doing another exercise otherwise returns false.
public boolean isStillLifting()
{
    System.out.println("Are you done lifting for the day? (y/n)");  
    char result = keyboard.next().charAt(0);
    boolean continueLifting = false;
    return continueLifting = !(result=='y' || result == 'Y');
}
// returns boolean of true if user is planning on doing another exercise otherwise returns false.
public boolean isStillLifting()
{
    System.out.println("Are you done lifting for the day? (y/n)");  
    char result = keyboard.next().charAt(0);
    boolean continueLifting = !(result=='y' || result == 'Y');
    return continueLifting; 
}
public boolean isWorkingOut()
{
    boolean workingOut = false;
    System.out.println("Are you working out today? (y/n)");
    char result = keyboard.next().charAt(0);
    return workingOut = (result=='y' || result == 'Y') ? true : false;
}
public String whatExercise()
{
    System.out.println("Choose an exercise from the list below or press 0 to end your workout.");
    showWorkoutList();
    int exerciseChoice = keyboard.nextInt();
    String exerciseDone = workoutList.get(exerciseChoice-1);
    switch(exerciseChoice)
    {
        case 0:
            System.out.println("We'll start again tomorrow!");
            System.exit(0);
            break;
        case 1:
            System.out.println("Cool, let's do some " + exerciseDone + ".");
            return exerciseDone ;
        case 2:
            System.out.println("Okay aswesome I love " + exerciseDone + "!");
            return exerciseDone;
        case 3:
            System.out.println(exerciseDone + " will get you jacked!");
            return exerciseDone;
        case 4:
            System.out.println(exerciseDone + " sound good!");
            return exerciseDone;
        case 5:
            System.out.println("Let's get some " + exerciseDone + " going!");
            return exerciseDone;
        case 6:
            System.out.println("Can't go wrong with " + exerciseDone + "!");
            return exerciseDone;
        case 7:
            System.out.println("Everyone loves " + exerciseDone + "!");
            return exerciseDone;
        case 8:
            System.out.println("Just a few " + exerciseDone + " will have you swole!");
            return exerciseDone;
        case 9:
            System.out.println("Make sure to have good form on these " + exerciseDone + "!");
            return exerciseDone;
        case 10:
            System.out.println("Get low on these " + exerciseDone + "!");
            return exerciseDone;
        default:
            System.out.println("What was that now?");
            return "";
    }
    return "WHAT?";
}

Context

StackExchange Code Review Q#139214, answer score: 3

Revisions (0)

No revisions yet.