Method Calculate User Answers

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I am writing spring hibernate app to make online test and this method for calculating user grade I have one to many relations between multi-choice question and answer.



Also, please check this question



@Service
public class StudentServiceImpl implements StudentService

private Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired
QuestionRepository questionRepository;

@Autowired
UserRepository userRepository;

@Autowired
Transformer transformer;

@Override
@Transactional
public void correctAnswer(List<StudentAnswersDTO> questionAnswers)
logger.info("****** start correct answers ********");
Long grade = 0L;
List<QuestionRightAnswerDTO> rightAnswerDTOS = questionRepository.getRightAnswers();
for (StudentAnswersDTO questionAnswer : questionAnswers)
if (questionAnswer.getType().toLowerCase().equals(AppConstants.MULTI_CHOICE))
if (isCorrectAnswer(questionAnswer.getId(), questionAnswer.getAnswerId(), rightAnswerDTOS))
grade++;



User currentStudent = userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
((Student) currentStudent).setGrade(grade);
((Student) currentStudent).setStudentAnswers(transformer.transform(questionAnswers, AnswerSelected.class));

logger.info("****** end correct answers ********");



private boolean isCorrectAnswer(Long id, Long answerId, final List<QuestionRightAnswerDTO> rightAnswerDTOS)
for (QuestionRightAnswerDTO rightAnswer : rightAnswerDTOS)
if (id.equals(rightAnswer.getQuestionId()) && answerId.equals(rightAnswer.getAnswerId()))
return true;

return false;








share|improve this question



























    up vote
    0
    down vote

    favorite












    I am writing spring hibernate app to make online test and this method for calculating user grade I have one to many relations between multi-choice question and answer.



    Also, please check this question



    @Service
    public class StudentServiceImpl implements StudentService

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    QuestionRepository questionRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    Transformer transformer;

    @Override
    @Transactional
    public void correctAnswer(List<StudentAnswersDTO> questionAnswers)
    logger.info("****** start correct answers ********");
    Long grade = 0L;
    List<QuestionRightAnswerDTO> rightAnswerDTOS = questionRepository.getRightAnswers();
    for (StudentAnswersDTO questionAnswer : questionAnswers)
    if (questionAnswer.getType().toLowerCase().equals(AppConstants.MULTI_CHOICE))
    if (isCorrectAnswer(questionAnswer.getId(), questionAnswer.getAnswerId(), rightAnswerDTOS))
    grade++;



    User currentStudent = userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
    ((Student) currentStudent).setGrade(grade);
    ((Student) currentStudent).setStudentAnswers(transformer.transform(questionAnswers, AnswerSelected.class));

    logger.info("****** end correct answers ********");



    private boolean isCorrectAnswer(Long id, Long answerId, final List<QuestionRightAnswerDTO> rightAnswerDTOS)
    for (QuestionRightAnswerDTO rightAnswer : rightAnswerDTOS)
    if (id.equals(rightAnswer.getQuestionId()) && answerId.equals(rightAnswer.getAnswerId()))
    return true;

    return false;








    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am writing spring hibernate app to make online test and this method for calculating user grade I have one to many relations between multi-choice question and answer.



      Also, please check this question



      @Service
      public class StudentServiceImpl implements StudentService

      private Logger logger = LoggerFactory.getLogger(this.getClass());

      @Autowired
      QuestionRepository questionRepository;

      @Autowired
      UserRepository userRepository;

      @Autowired
      Transformer transformer;

      @Override
      @Transactional
      public void correctAnswer(List<StudentAnswersDTO> questionAnswers)
      logger.info("****** start correct answers ********");
      Long grade = 0L;
      List<QuestionRightAnswerDTO> rightAnswerDTOS = questionRepository.getRightAnswers();
      for (StudentAnswersDTO questionAnswer : questionAnswers)
      if (questionAnswer.getType().toLowerCase().equals(AppConstants.MULTI_CHOICE))
      if (isCorrectAnswer(questionAnswer.getId(), questionAnswer.getAnswerId(), rightAnswerDTOS))
      grade++;



      User currentStudent = userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
      ((Student) currentStudent).setGrade(grade);
      ((Student) currentStudent).setStudentAnswers(transformer.transform(questionAnswers, AnswerSelected.class));

      logger.info("****** end correct answers ********");



      private boolean isCorrectAnswer(Long id, Long answerId, final List<QuestionRightAnswerDTO> rightAnswerDTOS)
      for (QuestionRightAnswerDTO rightAnswer : rightAnswerDTOS)
      if (id.equals(rightAnswer.getQuestionId()) && answerId.equals(rightAnswer.getAnswerId()))
      return true;

      return false;








      share|improve this question













      I am writing spring hibernate app to make online test and this method for calculating user grade I have one to many relations between multi-choice question and answer.



      Also, please check this question



      @Service
      public class StudentServiceImpl implements StudentService

      private Logger logger = LoggerFactory.getLogger(this.getClass());

      @Autowired
      QuestionRepository questionRepository;

      @Autowired
      UserRepository userRepository;

      @Autowired
      Transformer transformer;

      @Override
      @Transactional
      public void correctAnswer(List<StudentAnswersDTO> questionAnswers)
      logger.info("****** start correct answers ********");
      Long grade = 0L;
      List<QuestionRightAnswerDTO> rightAnswerDTOS = questionRepository.getRightAnswers();
      for (StudentAnswersDTO questionAnswer : questionAnswers)
      if (questionAnswer.getType().toLowerCase().equals(AppConstants.MULTI_CHOICE))
      if (isCorrectAnswer(questionAnswer.getId(), questionAnswer.getAnswerId(), rightAnswerDTOS))
      grade++;



      User currentStudent = userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
      ((Student) currentStudent).setGrade(grade);
      ((Student) currentStudent).setStudentAnswers(transformer.transform(questionAnswers, AnswerSelected.class));

      logger.info("****** end correct answers ********");



      private boolean isCorrectAnswer(Long id, Long answerId, final List<QuestionRightAnswerDTO> rightAnswerDTOS)
      for (QuestionRightAnswerDTO rightAnswer : rightAnswerDTOS)
      if (id.equals(rightAnswer.getQuestionId()) && answerId.equals(rightAnswer.getAnswerId()))
      return true;

      return false;










      share|improve this question












      share|improve this question




      share|improve this question








      edited Feb 7 at 11:15









      Zoe

      20519




      20519









      asked Feb 7 at 7:58









      bebo00o

      34




      34




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Fist of all I will recommend you to use Constructor based dependency injection. Then I can not see the relation between the Question, QuestionAnswer, and QuestionRightAnswer entities... currently you are selecting all the right answers from the DB which you should NOT do! You have to get all the correct answers for this question only. Use the question.id from QuestionAnswer just to match the QuestionRightAnswer.questionId FK. Also you do take care of the MULTI_CHOICE questions only, but why?



          Consider an implementation such as:



          @Service
          public class StudentServiceImpl implements StudentService

          private Logger logger = LoggerFactory.getLogger(this.getClass());

          QuestionRepository questionRepository;
          UserRepository userRepository;
          Transformer transformer;

          @Autowired
          public NotificationService(QuestionRepository questionRepository,
          UserRepository userRepository,
          Transformer transformer)
          this.questionRepository = questionRepository;
          this.userRepository = userRepository;
          this.transformer = transformer;


          @Override
          @Transactional
          public void correctAnswer(List<StudentAnswersDTO> questionAnswers)
          logger.info("****** start correct answers ********");
          Long grade = questionAnswers
          .stream()
          .filter(questionAnswer -> questionAnswer.getType().toLowerCase().equals(AppConstants.MULTI_CHOICE))
          .filter(questionAnswer -> questionRepository
          .getRightAnswersByQuestionId(questionAnswer.getId())
          .stream()
          .anyMatch(rightAnswer -> questionAnswer.getAnswerId().equals(rightAnswer.getAnswerId())))
          .count();

          User currentStudent = userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
          ((Student) currentStudent).setGrade(grade);
          ((Student) currentStudent).setStudentAnswers(transformer.transform(questionAnswers, AnswerSelected.class));







          share|improve this answer























          • thanks dbl for your comment it really helps me. I found out if I convert list of right answers to map this will enhance the performance
            – bebo00o
            Feb 8 at 5:41


















          up vote
          0
          down vote














           User currentStudent = userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
          ((Student) currentStudent).setGrade(grade);



          Why not just say



           Student currentStudent 
          = (Student) userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
          currentStudent.setGrade(grade);


          Then you don't have to keep repeating the cast.






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186976%2fmethod-calculate-user-answers%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            Fist of all I will recommend you to use Constructor based dependency injection. Then I can not see the relation between the Question, QuestionAnswer, and QuestionRightAnswer entities... currently you are selecting all the right answers from the DB which you should NOT do! You have to get all the correct answers for this question only. Use the question.id from QuestionAnswer just to match the QuestionRightAnswer.questionId FK. Also you do take care of the MULTI_CHOICE questions only, but why?



            Consider an implementation such as:



            @Service
            public class StudentServiceImpl implements StudentService

            private Logger logger = LoggerFactory.getLogger(this.getClass());

            QuestionRepository questionRepository;
            UserRepository userRepository;
            Transformer transformer;

            @Autowired
            public NotificationService(QuestionRepository questionRepository,
            UserRepository userRepository,
            Transformer transformer)
            this.questionRepository = questionRepository;
            this.userRepository = userRepository;
            this.transformer = transformer;


            @Override
            @Transactional
            public void correctAnswer(List<StudentAnswersDTO> questionAnswers)
            logger.info("****** start correct answers ********");
            Long grade = questionAnswers
            .stream()
            .filter(questionAnswer -> questionAnswer.getType().toLowerCase().equals(AppConstants.MULTI_CHOICE))
            .filter(questionAnswer -> questionRepository
            .getRightAnswersByQuestionId(questionAnswer.getId())
            .stream()
            .anyMatch(rightAnswer -> questionAnswer.getAnswerId().equals(rightAnswer.getAnswerId())))
            .count();

            User currentStudent = userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
            ((Student) currentStudent).setGrade(grade);
            ((Student) currentStudent).setStudentAnswers(transformer.transform(questionAnswers, AnswerSelected.class));







            share|improve this answer























            • thanks dbl for your comment it really helps me. I found out if I convert list of right answers to map this will enhance the performance
              – bebo00o
              Feb 8 at 5:41















            up vote
            0
            down vote



            accepted










            Fist of all I will recommend you to use Constructor based dependency injection. Then I can not see the relation between the Question, QuestionAnswer, and QuestionRightAnswer entities... currently you are selecting all the right answers from the DB which you should NOT do! You have to get all the correct answers for this question only. Use the question.id from QuestionAnswer just to match the QuestionRightAnswer.questionId FK. Also you do take care of the MULTI_CHOICE questions only, but why?



            Consider an implementation such as:



            @Service
            public class StudentServiceImpl implements StudentService

            private Logger logger = LoggerFactory.getLogger(this.getClass());

            QuestionRepository questionRepository;
            UserRepository userRepository;
            Transformer transformer;

            @Autowired
            public NotificationService(QuestionRepository questionRepository,
            UserRepository userRepository,
            Transformer transformer)
            this.questionRepository = questionRepository;
            this.userRepository = userRepository;
            this.transformer = transformer;


            @Override
            @Transactional
            public void correctAnswer(List<StudentAnswersDTO> questionAnswers)
            logger.info("****** start correct answers ********");
            Long grade = questionAnswers
            .stream()
            .filter(questionAnswer -> questionAnswer.getType().toLowerCase().equals(AppConstants.MULTI_CHOICE))
            .filter(questionAnswer -> questionRepository
            .getRightAnswersByQuestionId(questionAnswer.getId())
            .stream()
            .anyMatch(rightAnswer -> questionAnswer.getAnswerId().equals(rightAnswer.getAnswerId())))
            .count();

            User currentStudent = userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
            ((Student) currentStudent).setGrade(grade);
            ((Student) currentStudent).setStudentAnswers(transformer.transform(questionAnswers, AnswerSelected.class));







            share|improve this answer























            • thanks dbl for your comment it really helps me. I found out if I convert list of right answers to map this will enhance the performance
              – bebo00o
              Feb 8 at 5:41













            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            Fist of all I will recommend you to use Constructor based dependency injection. Then I can not see the relation between the Question, QuestionAnswer, and QuestionRightAnswer entities... currently you are selecting all the right answers from the DB which you should NOT do! You have to get all the correct answers for this question only. Use the question.id from QuestionAnswer just to match the QuestionRightAnswer.questionId FK. Also you do take care of the MULTI_CHOICE questions only, but why?



            Consider an implementation such as:



            @Service
            public class StudentServiceImpl implements StudentService

            private Logger logger = LoggerFactory.getLogger(this.getClass());

            QuestionRepository questionRepository;
            UserRepository userRepository;
            Transformer transformer;

            @Autowired
            public NotificationService(QuestionRepository questionRepository,
            UserRepository userRepository,
            Transformer transformer)
            this.questionRepository = questionRepository;
            this.userRepository = userRepository;
            this.transformer = transformer;


            @Override
            @Transactional
            public void correctAnswer(List<StudentAnswersDTO> questionAnswers)
            logger.info("****** start correct answers ********");
            Long grade = questionAnswers
            .stream()
            .filter(questionAnswer -> questionAnswer.getType().toLowerCase().equals(AppConstants.MULTI_CHOICE))
            .filter(questionAnswer -> questionRepository
            .getRightAnswersByQuestionId(questionAnswer.getId())
            .stream()
            .anyMatch(rightAnswer -> questionAnswer.getAnswerId().equals(rightAnswer.getAnswerId())))
            .count();

            User currentStudent = userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
            ((Student) currentStudent).setGrade(grade);
            ((Student) currentStudent).setStudentAnswers(transformer.transform(questionAnswers, AnswerSelected.class));







            share|improve this answer















            Fist of all I will recommend you to use Constructor based dependency injection. Then I can not see the relation between the Question, QuestionAnswer, and QuestionRightAnswer entities... currently you are selecting all the right answers from the DB which you should NOT do! You have to get all the correct answers for this question only. Use the question.id from QuestionAnswer just to match the QuestionRightAnswer.questionId FK. Also you do take care of the MULTI_CHOICE questions only, but why?



            Consider an implementation such as:



            @Service
            public class StudentServiceImpl implements StudentService

            private Logger logger = LoggerFactory.getLogger(this.getClass());

            QuestionRepository questionRepository;
            UserRepository userRepository;
            Transformer transformer;

            @Autowired
            public NotificationService(QuestionRepository questionRepository,
            UserRepository userRepository,
            Transformer transformer)
            this.questionRepository = questionRepository;
            this.userRepository = userRepository;
            this.transformer = transformer;


            @Override
            @Transactional
            public void correctAnswer(List<StudentAnswersDTO> questionAnswers)
            logger.info("****** start correct answers ********");
            Long grade = questionAnswers
            .stream()
            .filter(questionAnswer -> questionAnswer.getType().toLowerCase().equals(AppConstants.MULTI_CHOICE))
            .filter(questionAnswer -> questionRepository
            .getRightAnswersByQuestionId(questionAnswer.getId())
            .stream()
            .anyMatch(rightAnswer -> questionAnswer.getAnswerId().equals(rightAnswer.getAnswerId())))
            .count();

            User currentStudent = userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
            ((Student) currentStudent).setGrade(grade);
            ((Student) currentStudent).setStudentAnswers(transformer.transform(questionAnswers, AnswerSelected.class));








            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Feb 7 at 15:45


























            answered Feb 7 at 12:32









            dbl

            1413




            1413











            • thanks dbl for your comment it really helps me. I found out if I convert list of right answers to map this will enhance the performance
              – bebo00o
              Feb 8 at 5:41

















            • thanks dbl for your comment it really helps me. I found out if I convert list of right answers to map this will enhance the performance
              – bebo00o
              Feb 8 at 5:41
















            thanks dbl for your comment it really helps me. I found out if I convert list of right answers to map this will enhance the performance
            – bebo00o
            Feb 8 at 5:41





            thanks dbl for your comment it really helps me. I found out if I convert list of right answers to map this will enhance the performance
            – bebo00o
            Feb 8 at 5:41













            up vote
            0
            down vote














             User currentStudent = userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
            ((Student) currentStudent).setGrade(grade);



            Why not just say



             Student currentStudent 
            = (Student) userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
            currentStudent.setGrade(grade);


            Then you don't have to keep repeating the cast.






            share|improve this answer

























              up vote
              0
              down vote














               User currentStudent = userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
              ((Student) currentStudent).setGrade(grade);



              Why not just say



               Student currentStudent 
              = (Student) userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
              currentStudent.setGrade(grade);


              Then you don't have to keep repeating the cast.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote










                 User currentStudent = userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
                ((Student) currentStudent).setGrade(grade);



                Why not just say



                 Student currentStudent 
                = (Student) userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
                currentStudent.setGrade(grade);


                Then you don't have to keep repeating the cast.






                share|improve this answer














                 User currentStudent = userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
                ((Student) currentStudent).setGrade(grade);



                Why not just say



                 Student currentStudent 
                = (Student) userRepository.findByMobileNumber(SecurityHelper.getCurrentUser());
                currentStudent.setGrade(grade);


                Then you don't have to keep repeating the cast.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Feb 7 at 17:57









                mdfst13

                16.8k42055




                16.8k42055






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186976%2fmethod-calculate-user-answers%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation