From f0898faf83bfa46e23f18fb4dbd52dfb74b1e37b Mon Sep 17 00:00:00 2001 From: shin-jisong <86762272+shin-jisong@users.noreply.github.com> Date: Mon, 25 Nov 2024 17:26:02 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20QuestionsScoreCalculator=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/QuestionsScoreCalculator.java | 29 +++++ .../domain/QuestionsScoreCalculatorTest.java | 103 ++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 backend/bang-ggood/src/main/java/com/bang_ggood/question/domain/QuestionsScoreCalculator.java create mode 100644 backend/bang-ggood/src/test/java/com/bang_ggood/question/domain/QuestionsScoreCalculatorTest.java diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/question/domain/QuestionsScoreCalculator.java b/backend/bang-ggood/src/main/java/com/bang_ggood/question/domain/QuestionsScoreCalculator.java new file mode 100644 index 000000000..3caf07d38 --- /dev/null +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/question/domain/QuestionsScoreCalculator.java @@ -0,0 +1,29 @@ +package com.bang_ggood.question.domain; + +import java.util.List; + +public class QuestionsScoreCalculator { + + private static final int NO_QUESTIONS_ANSWERED = 0; + private static final int PERCENT = 100; + + private final ChecklistQuestions allAnsweredQuestions; + private final ChecklistQuestions goodAnsweredQuestions; + + public QuestionsScoreCalculator(List questions) { + this.allAnsweredQuestions = new ChecklistQuestions(questions); + this.goodAnsweredQuestions = new ChecklistQuestions(allAnsweredQuestions.filterByAnswer(Answer.GOOD)); + } + + public Integer calculateScore() { + int allAnsweredQuestionCount = allAnsweredQuestions.size(); + int goodAnsweredQuestionCount = goodAnsweredQuestions.size(); + + if (allAnsweredQuestionCount == NO_QUESTIONS_ANSWERED) { + return null; + } + + double score = ((double) goodAnsweredQuestionCount / allAnsweredQuestionCount) * PERCENT; + return (int) Math.round(score); + } +} diff --git a/backend/bang-ggood/src/test/java/com/bang_ggood/question/domain/QuestionsScoreCalculatorTest.java b/backend/bang-ggood/src/test/java/com/bang_ggood/question/domain/QuestionsScoreCalculatorTest.java new file mode 100644 index 000000000..96cfa6d65 --- /dev/null +++ b/backend/bang-ggood/src/test/java/com/bang_ggood/question/domain/QuestionsScoreCalculatorTest.java @@ -0,0 +1,103 @@ +package com.bang_ggood.question.domain; + +import com.bang_ggood.IntegrationTestSupport; +import com.bang_ggood.checklist.ChecklistFixture; +import com.bang_ggood.checklist.domain.Checklist; +import com.bang_ggood.checklist.repository.ChecklistRepository; +import com.bang_ggood.question.ChecklistQuestionFixture; +import com.bang_ggood.question.QuestionFixture; +import com.bang_ggood.room.RoomFixture; +import com.bang_ggood.room.domain.Room; +import com.bang_ggood.room.repository.RoomRepository; +import com.bang_ggood.user.UserFixture; +import com.bang_ggood.user.domain.User; +import com.bang_ggood.user.repository.UserRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class QuestionsScoreCalculatorTest extends IntegrationTestSupport { + + @Autowired + private ChecklistRepository checklistRepository; + + @Autowired + private RoomRepository roomRepository; + + @Autowired + private UserRepository userRepository; + + private Checklist checklist; + + @BeforeEach + void beforeEach() { + User user = userRepository.save(UserFixture.USER1()); + Room room = roomRepository.save(RoomFixture.ROOM_1()); + checklist = checklistRepository.save(ChecklistFixture.CHECKLIST1_USER1(room, user)); + } + + @DisplayName("점수 계산 성공 : 모두 GOOD일 경우") + @Test + void calculateCategoryScore() { + // given + List checklistQuestions = List.of( + ChecklistQuestionFixture.CHECKLIST1_QUESTION2_GOOD(checklist, QuestionFixture.QUESTION1_CATEGORY1) + ); + + // when + QuestionsScoreCalculator calculator = new QuestionsScoreCalculator(checklistQuestions); + + // then + assertThat(calculator.calculateScore()).isEqualTo(100); + } + + @DisplayName("점수 계산 성공 : 일부 답변만 GOOD인 경우") + @Test + void calculateCategoryScore_partialGoodAnswers() { + // given + List checklistQuestions = List.of( + ChecklistQuestionFixture.CHECKLIST1_QUESTION1_BAD(checklist, QuestionFixture.QUESTION1_CATEGORY1), + ChecklistQuestionFixture.CHECKLIST1_QUESTION2_GOOD(checklist, QuestionFixture.QUESTION2_CATEGORY1) + ); + + // when + QuestionsScoreCalculator calculator = new QuestionsScoreCalculator(checklistQuestions); + + // then + assertThat(calculator.calculateScore()).isEqualTo(50); + } + + @DisplayName("점수 계산 성공 : 모든 답변이 BAD인 경우") + @Test + void calculateCategoryScore_allBadAnswers() { + // given + List checklistQuestions = List.of( + ChecklistQuestionFixture.CHECKLIST1_QUESTION1_BAD(checklist, QuestionFixture.QUESTION1_CATEGORY1) + ); + + // when + QuestionsScoreCalculator calculator = new QuestionsScoreCalculator(checklistQuestions); + + // then + assertThat(calculator.calculateScore()).isEqualTo(0); + } + + @DisplayName("점수 계산 성공 : 답변이 없는 경우") + @Test + void calculateCategoryScore_noAnswers() { + // given & when + List checklistQuestions = new ArrayList<>(); + + // when + QuestionsScoreCalculator calculator = new QuestionsScoreCalculator(checklistQuestions); + + // then + assertThat(calculator.calculateScore()).isEqualTo(null); + } + +}