# Displaying 21 spinners to select body parts

I just wanted to ask how can I make my code a bit shorter. As of now, I have way too much code in the class. The program should display about twenty one spinners. I know it's a lot, but in this layout, users have to create their own training plan for 3 training days. In every training day, user has to select up to three body parts (one body part - 3 spinners with exercises, it should look like that)

Monday:

Chest: -first exercise (spinner) -second exercise (spinner) -third exercise (spinner)

Triceps -first exercise (spinner) -second exercise (spinner) -third exercise (spinner) etc..


I've already filled spinners with exercises from database, but I want to make this class shorter because for filling my spinners (just Ctrl+C, Ctrl+V) I've written about 2400 lines of code where about 2000 it's just copied method, with changed adapter etc. Did can i make it shorter? Here's first method

public void setMondayFirstBodyPart_1() {

switch (mondayFirstBodyPartFirstExerciseString) {
case "shoulders": {
List<String> data = db.getShouldersData();
mondayFirstBodyPartExerciseTV.setText("Shoulders");
break;
}
case "chest": {
List<String> data = db.getChestData();
mondayFirstBodyPartExerciseTV.setText("Chest");
break;
}
case "back": {
List<String> data = db.getBackData();
mondayFirstBodyPartExerciseTV.setText("Back");
break;
}
case "biceps": {
List<String> data = db.getBicepsData();
mondayFirstBodyPartExerciseTV.setText("Biceps");
break;
}
case "triceps": {
List<String> data = db.getTricepsData();
mondayFirstBodyPartExerciseTV.setText("Triceps");
break;
}
case "forearm": {
List<String> data = db.getForearmData();
mondayFirstBodyPartExerciseTV.setText("Forearm");
break;
}
case "stomach": {
List<String> data = db.getAbsData();
mondayFirstBodyPartExerciseTV.setText("Stomach");
break;
}
case "legs": {
List<String> data = db.getLegsData();
mondayFirstBodyPartExerciseTV.setText("Legs");
break;
}
case "calf": {
List<String> data = db.getCalfData();
mondayFirstBodyPartExerciseTV.setText("Calf");
break;
}
}
}

• Welcome to Code Review. I suggest that you show more context, so that we can advise you properly. For example, what does the db code look like? See How to Ask. – 200_success Nov 10 '18 at 21:27
• Every case statement is the same except the setText parameter. So just extract the whole duplicated case statement in new method with just one parameter. – Ankit Soni Nov 10 '18 at 21:29
• @Ankit Soni - the List<String> data also gets different values depending on the case. – 0X0nosugar Nov 10 '18 at 21:35
• @0X0nosugar, you are right, my bad. Two parameters then, the db data list and text value. – Ankit Soni Nov 10 '18 at 21:39

I don't know of what type mondayFirstBodyPartFirstExercise currently is but maybe you could use polymorphism and add the behavior there. This would would put database logic into your entities though. (Active Record pattern)

public void setMondayFirstBodyPart_1() {
List<String> data = mondayFirstBodyPartFirstExercise.getData();
mondayFirstBodyPartExerciseTV.setText(mondayFirstBodyPartFirstExercise.getBodyPartName());
}


Let's take a look at one of the case blocks:

case "shoulders": {
List<String> data = db.getShouldersData();
mondayFirstBodyPartExerciseTV.setText("Shoulders");
break;
}


The only lines which depend directly on the selected body part are

List<String> data = db.getShouldersData();


and

mondayFirstBodyPartExerciseTV.setText("Shoulders");


So let's suppose you have a method findDataForBodyPart(String bodyPart) which will make the database call and return the data list (or an empty list if there is no data to be found). In addition to that, you need another method getNameOfBodyPart(String) which will take a String (e.g. "shoulders") and return the desired TextView text (e.g. "Shoulders").

Then you can abbreviate your code as follows:

public void setMondayFirstBodyPart_1() {
List<String> data = findDataForBodyPart(mondayFirstBodyPartFirstExerciseString);
mondayFirstBodyPartExerciseTV.setText(getNameOfBodyPart(mondayFirstBodyPartFirstExerciseString));
}


You can avoid even more repetitions (this time of whole methods) if you pass the value of mondayFirstBodyPartFirstExerciseString as parameter into the method, together with the corresponding Spinner and TextView:

public void setExerciseFor(String bodyPartString, Spinner exerciseSpinner, TextView exerciseTextView) {
List<String> data = findDataForBodyPart(bodyPartString);