dimanche 4 décembre 2016

Changing else-if code to something cleaner

I have following else-if code and I am really getting annoying with a size of the lines needed for it, so I was thinking if there is some more appropriate way to do it which will be more cleaner and more faster?

if (dialogResult == 0) {
            System.out.println("Yes option to try again!");
            if (level.equals(lp.LEVEL1.getLevelName())) {
                System.out.println(lp.LEVEL1.getLevelName());
                level = lp.LEVEL2.getLevelName(); // set level
                totalBall = lp.LEVEL2.getBallCount();
                numberOfExplodes = lp.LEVEL2.getBallExplosion();

            } else if (level.equals(lp.LEVEL2.getLevelName())) {
                System.out.println(lp.LEVEL2.getLevelName());
                level = lp.LEVEL3.getLevelName(); // set level
                totalBall = lp.LEVEL3.getBallCount();
                numberOfExplodes = lp.LEVEL3.getBallExplosion();

            } else if (level.equals(lp.LEVEL3.getLevelName())) {
                System.out.println(lp.LEVEL3.getLevelName());
                level = lp.LEVEL4.getLevelName(); // set level
                totalBall = lp.LEVEL4.getBallCount();
                numberOfExplodes = lp.LEVEL4.getBallExplosion();

            } else if (level.equals(lp.LEVEL4.getLevelName())) {
                System.out.println(lp.LEVEL4.getLevelName());
                level = lp.LEVEL5.getLevelName(); // set level
                totalBall = lp.LEVEL5.getBallCount();
                numberOfExplodes = lp.LEVEL5.getBallExplosion();

            } else if (level.equals(lp.LEVEL5.getLevelName())) {
                System.out.println(lp.LEVEL5.getLevelName());
                level = lp.LEVEL6.getLevelName(); // set level
                totalBall = lp.LEVEL6.getBallCount();
                numberOfExplodes = lp.LEVEL6.getBallExplosion();

            } else if (level.equals(lp.LEVEL6.getLevelName())) {
                System.out.println(lp.LEVEL6.getLevelName());
                level = lp.LEVEL7.getLevelName(); // set level
                totalBall = lp.LEVEL7.getBallCount();
                numberOfExplodes = lp.LEVEL7.getBallExplosion();

            } else if (level.equals(lp.LEVEL7.getLevelName())) {
                System.out.println(lp.LEVEL7.getLevelName());
                level = lp.LEVEL8.getLevelName(); // set level
                totalBall = lp.LEVEL8.getBallCount();
                numberOfExplodes = lp.LEVEL8.getBallExplosion();

            } else if (level.equals(lp.LEVEL8.getLevelName())) {
                System.out.println(lp.LEVEL8.getLevelName());
                level = lp.LEVEL9.getLevelName(); // set level
                totalBall = lp.LEVEL9.getBallCount();
                numberOfExplodes = lp.LEVEL9.getBallExplosion();

            }
            ...

I was thinking switch could go fine here, but I would like to hear other options as well.

Aucun commentaire:

Enregistrer un commentaire