vendredi 24 avril 2020

Avoid Conditional Statments while constructing an object

I need some suggestion to implement elegant way to avoid if-else. I really cannt avoid the conditional check, I need to do the check and then add certain properties to the object.

@Override
    public Summary addPropertyBasedOnType(AssignmentEvent assignmentEvent, String userId) {

        Summary summary = new Summary();
        summary.setTimestamp((new Timestamp(System.currentTimeMillis())).toString());
        summary.setUserId("");
        summary.setGradingProgress(GradingProgress.Started);
        summary.setComment(COMMENT);

        if (list.contains(assignmentEvent.getItemType())) {
            summary.setGradingProgress(GradingProgress.Incomplete);
            summary.setScoreMaximum(ASSIGNMENT_MAX_SCORE);
            summary.setScoreGiven(ASSIGNMENT_MAX_SCORE);
        } else if (assignmentEvent.getItemType().equalsIgnoreCase("some string")) {
            summary.setGradingProgress(GradingProgress.PendingManual);
        } else if (assignmentEvent.getItemType().equalsIgnoreCase("test string")) {
            if (assignmentEvent.getLatestUserAssignmentData().getScoreSource().getSource() == 0) {
                summary.setGradingProgress(GradingProgress.FullyGraded);
                summary.setScoreGiven(assignmentEvent.getLatestUserAssignmentData().getScore());
                summary.setScoreMaximum(ASSIGNMENT_MAX_SCORE);
            }

            if (assignmentEvent.getLatestUserAssignmentData().getScoreSource().getSource() == 1) {
                summary.setGradingProgress(GradingProgress.Started);
            }
        } else {
            summary.setGradingProgress(GradingProgress.Incomplete);
            summary.setScoreGiven(assignmentEvent.getLatestUserAssignmentData().getScore());
            summary.setScoreMaximum(ASSIGNMENT_MAX_SCORE);
        }
        return summary;
    }

I was thinking Factory Pattern but I think it is an overkill for this.

Aucun commentaire:

Enregistrer un commentaire