From 944551c20ef98ae5d289d6629be208469d8f20a3 Mon Sep 17 00:00:00 2001 From: uo287545 Date: Mon, 11 Mar 2024 23:04:04 +0100 Subject: [PATCH] Fixed the empty answer bug. --- .../java/com/uniovi/services/QuestionService.java | 7 ------- .../uniovi/services/impl/QuestionServiceImpl.java | 15 +++------------ 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/uniovi/services/QuestionService.java b/src/main/java/com/uniovi/services/QuestionService.java index 56b10bbc..c89a3ae9 100644 --- a/src/main/java/com/uniovi/services/QuestionService.java +++ b/src/main/java/com/uniovi/services/QuestionService.java @@ -32,13 +32,6 @@ public interface QuestionService { */ Optional getQuestion(Long id); - /** - * Get a random question - * - * @return The question selected - */ - Optional getRandomQuestion(); - /** * Get a random question from any category * diff --git a/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java b/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java index 61765939..d45c1b5f 100644 --- a/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java +++ b/src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java @@ -41,18 +41,6 @@ public Optional getQuestion(Long id) { return questionRepository.findById(id); } - @Override - public Optional getRandomQuestion() { - List allQuestions = questionRepository.findAll().stream() - .filter(question -> question.getLanguage().equals(LocaleContextHolder.getLocale().getLanguage())).toList(); - int idx = (int) (Math.random() * allQuestions.size()); - Question q = allQuestions.get(idx); - while (q.hasEmptyOptions()){ - return getRandomQuestion(); - } - return Optional.ofNullable(q); - } - @Override public List getRandomQuestions(int num) { List allQuestions = questionRepository.findAll().stream() @@ -60,6 +48,9 @@ public List getRandomQuestions(int num) { List res = new ArrayList<>(); for (int i = 0; i < num; i++) { int idx = (int) (Math.random() * allQuestions.size()); + while (allQuestions.get(idx).hasEmptyOptions()){ + idx = (int) (Math.random() * allQuestions.size()); + } res.add(allQuestions.get(idx)); } return res;