dimanche 14 juillet 2019

I want to avoid if else in below codes, any suggestion?

To build a dynamic football league table generator. A league table for football. Each team plays a number of matches and the results of each match build the table.

Any suggestion to avoid the if-else statement?

matches.stream().forEach(match -> {
            // Find the record of home team and away team in the league table
            homeTeamRecord = tableEntries.stream().filter(t -> t.getTeamName().equals(match.getHomeTeam())).findFirst().get();
            awayTeamRecord = tableEntries.stream().filter(t -> t.getTeamName().equals(match.getAwayTeam())).findFirst().get();

            /*If home team score > away team score, then
             * home team + 3 points
             * home team's wins + 1
             * away team's lost + 1 
             */
            if (match.getHomeScore() > match.getAwayScore()) {
                homeTeamRecord.setPoints(homeTeamRecord.getPoints() + 3);
                homeTeamRecord.setWon(homeTeamRecord.getWon()       + 1);
                awayTeamRecord.setLost(awayTeamRecord.getLost()     + 1);               
            /*If home team score < away team score, then
             * away team + 3 points
             * away team's wins + 1
             * home team's lost + 1 
             */
            } else if (match.getHomeScore() < match.getAwayScore()) {
                awayTeamRecord.setPoints(awayTeamRecord.getPoints() + 3);
                awayTeamRecord.setWon(awayTeamRecord.getWon()       + 1);
                homeTeamRecord.setLost(homeTeamRecord.getLost()     + 1);           
            /*If home team score equals to away team score, then
             * home team + 1 point
             * away team + 1 point
             * home team's draws + 1
             * away team's draws + 1 
             */
            } else if (match.getHomeScore() == match.getAwayScore()) {
                homeTeamRecord.setPoints(homeTeamRecord.getPoints() + 1);
                awayTeamRecord.setPoints(awayTeamRecord.getPoints() + 1);
                homeTeamRecord.setDrawn(homeTeamRecord.getDrawn()   + 1);
                awayTeamRecord.setDrawn(awayTeamRecord.getDrawn()   + 1);
            }

            // Calculate 'played', 'goals for', 'goals against', 'goal difference' of home team and away team according to their latest match result
            homeTeamRecord.setPlayed(homeTeamRecord.getPlayed() + 1);
            awayTeamRecord.setPlayed(awayTeamRecord.getPlayed() + 1);
            homeTeamRecord.setGoalsFor(homeTeamRecord.getGoalsFor() + match.getHomeScore());
            awayTeamRecord.setGoalsFor(awayTeamRecord.getGoalsFor() + match.getAwayScore());
            homeTeamRecord.setGoalsAgainst(homeTeamRecord.getGoalsAgainst() + match.getAwayScore());
            awayTeamRecord.setGoalsAgainst(awayTeamRecord.getGoalsAgainst() + match.getHomeScore());
            homeTeamRecord.setGoalDifference(homeTeamRecord.getGoalDifference() + match.getHomeScore() - match.getAwayScore());
            awayTeamRecord.setGoalDifference(awayTeamRecord.getGoalDifference() + match.getAwayScore() - match.getHomeScore());

            // Update the league table with the latest team record
            tableEntries.set(tableEntries.indexOf(homeTeamRecord), homeTeamRecord);
            tableEntries.set(tableEntries.indexOf(awayTeamRecord), awayTeamRecord);
        });

Aucun commentaire:

Enregistrer un commentaire