Where is the error in my logic in nested for-loops with ifs?

73 views Asked by At

I have programmed some sort of quiz. First, users can ask questions and answer them. Those are saved in the question/answer-table.

If there are enough entries in this table, a quiz can be started. The quiz goes through the entries in the question/answer-table, picks a question at random and asks the user the question. His answer is saved in a quiz-table.

The entries in the quiz-table take the current user into account, so if there is already an entry for a user (maybe he was asked the question before already), then the answer updates the entry in the quiz-table. If the user has been asked the question before, a new entry in the quiz-table gets created.

Most of this works more or less, but somehow the entries in the quiz-table dont get updated and I dont understand why. So where is the error in my logic here?

//Creates a list.The user answers the question via radiobuttons. 
//All quiz-entries that match the question_ID get put into the list. 
//Bob and Tim could have answere the same question, 
//so the question_ID could be in there several times
List<Quiz> tempQuizList = Quiz.find.where().like("question_ID", clickedRadioAnswer.questionID).findList();

User currentUser = request().name();

if( tempQuizList.size() > 0 ){
    // Go through all entries in the quiz-table
    for (Quiz quizItem : Quiz.find.all()) {
        // If the user from the quiz-table entry is the same as the current user
        if((quizItem.userID).equals(currentUser.email)){
            if(clickedRadioAnswer.answerID.equals(bestAnswer.answerID)){
                // If user has answered correctly, update the entry with the user and an interval to postpone the time when the user has to answer the question again
                Quiz.updateAnswer(clickedRadioAnswer.questionID, currentUser.email, 5000);
            } 
            else{
                Quiz.updateAnswer(clickedRadioAnswer.questionID, currentUser.email, 0);
            }
        }
        if(!(quizItem.userID).equals(currentUser.email)){
            if(clickedRadioAnswer.answerID.equals(bestAnswer.answerID)){
                Quiz.createAnswer(clickedRadioAnswer, currentUser, 5000);
            } 
            else{
                Quiz.createAnswer(clickedRadioAnswer, currentUser, 0);
            }
        }
    }
}
1

There are 1 answers

0
hamena314 On BEST ANSWER

I found a pretty dirty solution:

In my for-loop I ask, if the user from the question in the DB is the same as the one, that is currently logged in. If I add another .like() to my list, my structure becomes way simpler:

List<Quiz> tempQuizList = Quiz.find.where()
.like("question_ID", clickedRadioAnswer.questionID).findList();

becomes:

List<Quiz> tempQuizList = Quiz.find.where()
.like("question_ID", clickedRadioAnswer.questionID)
.like("user_id", currentUser.email).findList();

With this the second part of my big if-block isnt necessary anymore, as the user in the 100% the current user (else the question would not have been put into the list):

if( tempQuizList.size() > 0 ){              
    if(clickedRadioAnswer.answerID.equals(bestAnswer.answerID)){
        Quiz.updateAnswer(clickedRadioAnswer.questionID, currentUser.email, 5000);
    } 
    if(!clickedRadioAnswer.answerID.equals(bestAnswer.answerID)){
        Quiz.updateAnswer(clickedRadioAnswer.questionID, currentUser.email, 0);
    }
}

// Quiz is empty or user has no quizquestion open
if(tempQuizList.size() == 0){
    if(clickedRadioAnswer.answerID.equals(bestAnswer.answerID)){
    Quiz.createAnswer(clickedRadioAnswer, currentUser.email, 5000);
} 
else{
    Quiz.createAnswer(clickedRadioAnswer, currentUser.email, 0);
}

I tried to use the debugger, but found it actually quite hard to see, where the error was. Instead I skyped with a friend and explained to him, what I was doing. He then pointed out, that the for-loop was too much. Together we found the solution with the double-like. It does not look pretty, but get's the job done.