Goal: to take the user input and output a semi randomized workout.
Issue: I've gotten the rep workout to output, the loops and everything that randomize the actual workouts can't be seen by the action handler. I can get the actual workouts to output in the console line, but not in the GUI.
I've tried just about everything from redoing the code, to moving the code around. I just need the val variable visible to the actionhandler for when the user clicks the randomize button.
I have separated the code out so that it is easier to read. The first part is the GUI and the selection stuff. The second set of code is what does the randomizing in, for example, chestDay. The code also seems to get very touchy when I move the second code snippet around.
I could really use any and all advice, I have a few days remaining to fix this problem.
The variable that I need to be seen in both snippets is the 'val' variable.
package Final;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.text.Text;
public class FinalGUI extends Application {
private static final int range = 6;
// launch the application
public void start(Stage stage) {
try {
// set title for the stage
stage.setTitle("Your Work Out Friend");
// create a VBox
VBox vbox = new VBox();
Text text = new Text();
ComboBox choiceMuscle = new ComboBox();
ComboBox choiceRep = new ComboBox();
// create a label
Label label = new Label("Choose your muscle group and your workout goal.");
// Creates a list that the vbox can use
ObservableList<String> choicesMuscle = FXCollections.observableArrayList(
"Chest day", "Back day", "Leg day");
choiceMuscle.setItems(choicesMuscle);
ObservableList<String> choicesRep = FXCollections.observableArrayList(
"Lose weight", "Gain muscle mass", "Gain Strength");
choiceRep.setItems(choicesRep);
// add label to vbox
vbox.getChildren().add(label);
// add buttons to VBox
vbox.getChildren().addAll(choiceMuscle, choiceRep, text);
Button btnOK = new Button();
btnOK.setText("Give me a workout!");
vbox.getChildren().add(btnOK);
btnOK.setOnAction(a -> {
String cMuscle = choiceMuscle.getSelectionModel().getSelectedItem() + "";
if (cMuscle.equalsIgnoreCase("Chest Day")) {
printArray(chestDay());
}
else if (cMuscle.equalsIgnoreCase("Leg Day")) {
printArray(legDay());
}
else if (cMuscle.equalsIgnoreCase("Back Day")) {
printArray(backDay());
}
//else {
// System.out.println("Invalid choice made. Choose between Chest, Leg or Back days only.");
//}
//});
String cRep = choiceRep.getSelectionModel().getSelectedItem() + "";
String repChoice = "";
//Move code from WorkOutRep here
if (cRep.equalsIgnoreCase("Lose Weight")) {
repChoice = "5 sets of 20 reps, with 1 minute of cardio in between";
}
else if (cRep.equalsIgnoreCase("Gain muscle mass")) {
repChoice = "4 sets of 10 reps, or 5 sets of 8 reps";
}
else if (cRep.equalsIgnoreCase("Gain strength")) {
repChoice = "5 sets of 3 reps, increasing the weight until you can no longer lift the weight.";
}
//else {
// System.out.println("Invalid choice made. Choose between the listed choices only.");
//}
//});
text.setText("Your workout is: " + repChoice + val );
});
// create a scene
Scene scene = new Scene(vbox, 400, 200);
// set the scene
stage.setScene(scene);
stage.show();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static String[] backDay() {
List<String> backList = new ArrayList <>();
backList.add("Deadlift");
backList.add("Pull Up");
backList.add("Back Rows");
backList.add("Bend-Over Barbell Rows");
backList.add("Romanian Deadlift");
backList.add("Front Squat");
return randomizerHelper(backList);
}
public String[] chestDay() {
List <String> chestList = new ArrayList <>();
chestList.add("Bench Press");
chestList.add("Incline Press");
chestList.add("Dip");
chestList.add("Flys");
chestList.add("Reverse Flys");
chestList.add("Supine Press");
return randomizerHelper(chestList);
}
public String[] legDay() {
List<String> legList = new ArrayList <>();
legList.add("Squat");
legList.add("Leg Press");
legList.add("Leg Extension");
legList.add("Dumbbell Step Up");
legList.add("Body Weight Calf Raises");
legList.add("Walking Lunge");
return randomizerHelper(legList);
}
public static String[] randomizerHelper(List<String> arr) {
String[] returnArray = new String[3];
String chosen;
for(int i = 0; i < returnArray.length; i++) {
chosen = arr.get((int) (Math.random() * range));
for(int j = 0; j < returnArray.length; j++) {
if (!(chosen.equalsIgnoreCase(returnArray[j]))) {
returnArray[i]= chosen;
break;
}
}
}
return returnArray;
}
public void printArray(String[] arr) {
for (String val : arr) {
Text text = new Text();
text.setText(val);
}
}
// Main Method
public static void main(String args[]) {
// launch the application
Application.launch(args);
}
}
printArray
method also makes little sense. You just createText
nodes that are thrown away. I recommend to a) learn about fields or b) use return values according to your needs. In any case explain the problem better: "I want to do X; I try this by doing Y" is better than "I want to do Y"...