Method Calculate User Answers
Clash 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;
java mysql hibernate
add a comment |Â
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;
java mysql hibernate
add a comment |Â
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;
java mysql hibernate
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;
java mysql hibernate
edited Feb 7 at 11:15
Zoe
20519
20519
asked Feb 7 at 7:58
bebo00o
34
34
add a comment |Â
add a comment |Â
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));
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
add a comment |Â
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.
add a comment |Â
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));
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
add a comment |Â
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));
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
add a comment |Â
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));
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));
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
add a comment |Â
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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered Feb 7 at 17:57
mdfst13
16.8k42055
16.8k42055
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password