3
\$\begingroup\$

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();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            mondayFirstBodyPartFirstExercise.setAdapter(adapter);
            mondayFirstBodyPartExerciseTV.setText("Shoulders");
            break;
        }
        case "chest": {
            List<String> data = db.getChestData();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            mondayFirstBodyPartFirstExercise.setAdapter(adapter);
            mondayFirstBodyPartExerciseTV.setText("Chest");
            break;
        }
        case "back": {
            List<String> data = db.getBackData();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            mondayFirstBodyPartFirstExercise.setAdapter(adapter);
            mondayFirstBodyPartExerciseTV.setText("Back");
            break;
        }
        case "biceps": {
            List<String> data = db.getBicepsData();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            mondayFirstBodyPartFirstExercise.setAdapter(adapter);
            mondayFirstBodyPartExerciseTV.setText("Biceps");
            break;
        }
        case "triceps": {
            List<String> data = db.getTricepsData();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            mondayFirstBodyPartFirstExercise.setAdapter(adapter);
            mondayFirstBodyPartExerciseTV.setText("Triceps");
            break;
        }
        case "forearm": {
            List<String> data = db.getForearmData();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            mondayFirstBodyPartFirstExercise.setAdapter(adapter);
            mondayFirstBodyPartExerciseTV.setText("Forearm");
            break;
        }
        case "stomach": {
            List<String> data = db.getAbsData();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            mondayFirstBodyPartFirstExercise.setAdapter(adapter);
            mondayFirstBodyPartExerciseTV.setText("Stomach");
            break;
        }
        case "legs": {
            List<String> data = db.getLegsData();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            mondayFirstBodyPartFirstExercise.setAdapter(adapter);
            mondayFirstBodyPartExerciseTV.setText("Legs");
            break;
        }
        case "calf": {
            List<String> data = db.getCalfData();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            mondayFirstBodyPartFirstExercise.setAdapter(adapter);
            mondayFirstBodyPartExerciseTV.setText("Calf");
            break;
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – 200_success Nov 10 '18 at 21:27
  • \$\begingroup\$ 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. \$\endgroup\$ – Ankit Soni Nov 10 '18 at 21:29
  • \$\begingroup\$ @Ankit Soni - the List<String> data also gets different values depending on the case. \$\endgroup\$ – 0X0nosugar Nov 10 '18 at 21:35
  • \$\begingroup\$ @0X0nosugar, you are right, my bad. Two parameters then, the db data list and text value. \$\endgroup\$ – Ankit Soni Nov 10 '18 at 21:39
0
\$\begingroup\$

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();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mondayFirstBodyPartFirstExercise.setAdapter(adapter);
    mondayFirstBodyPartExerciseTV.setText(mondayFirstBodyPartFirstExercise.getBodyPartName());
 }
\$\endgroup\$
0
\$\begingroup\$

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

case "shoulders": {
    List<String> data = db.getShouldersData();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mondayFirstBodyPartFirstExercise.setAdapter(adapter);
    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);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mondayFirstBodyPartFirstExercise.setAdapter(adapter);
    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);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    exerciseSpinner.setAdapter(adapter);
    exerciseTextView.setText(getNameOfBodyPart(bodyPartString));
 }

Please note that in order to suggest further improvements, we'd need to see more of your code.

\$\endgroup\$
  • \$\begingroup\$ When adding additional information you should edit your answer instead of adding a comment. I have added that information to your post. Learn more about comments including when to comment and when not to in the Help Center page about Comments. \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Nov 11 '18 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.