From 8b3a0a34725f22727ad8a8c715e758a3efc2baae Mon Sep 17 00:00:00 2001 From: Arachneee Date: Thu, 25 Jul 2024 11:41:01 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=B0=B8=EC=97=AC=EC=9E=90=20?= =?UTF-8?q?=EC=A0=95=EC=82=B0=20=ED=98=84=ED=99=A9=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../haengdong/application/ActionService.java | 6 +-- .../domain/action/CurrentMembers.java | 39 ++++++++++++++++--- .../haengdong/domain/action/MemberAction.java | 4 ++ ...BillReports.java => MemberBillReport.java} | 36 +++++++---------- .../domain/action/CurrentMembersTest.java | 28 +++++++++++++ ...rtsTest.java => MemberBillReportTest.java} | 6 +-- 6 files changed, 86 insertions(+), 33 deletions(-) rename server/src/main/java/server/haengdong/domain/action/{MemberBillReports.java => MemberBillReport.java} (66%) rename server/src/test/java/server/haengdong/domain/action/{MemberBillReportsTest.java => MemberBillReportTest.java} (89%) diff --git a/server/src/main/java/server/haengdong/application/ActionService.java b/server/src/main/java/server/haengdong/application/ActionService.java index b28374bb9..0eda074ca 100644 --- a/server/src/main/java/server/haengdong/application/ActionService.java +++ b/server/src/main/java/server/haengdong/application/ActionService.java @@ -9,7 +9,7 @@ import server.haengdong.domain.action.BillActionRepository; import server.haengdong.domain.action.MemberAction; import server.haengdong.domain.action.MemberActionRepository; -import server.haengdong.domain.action.MemberBillReports; +import server.haengdong.domain.action.MemberBillReport; import server.haengdong.domain.event.Event; import server.haengdong.domain.event.EventRepository; import server.haengdong.exception.HaengdongErrorCode; @@ -29,10 +29,10 @@ public List getMemberBillReports(String token) { List billActions = billActionRepository.findByAction_Event(event); List memberActions = memberActionRepository.findAllByEvent(event); - MemberBillReports memberBillReports = MemberBillReports.createByActions(billActions, memberActions); + MemberBillReport memberBillReport = MemberBillReport.createByActions(billActions, memberActions); List memberBillReportResponses = new ArrayList<>(); - memberBillReports.getReports().forEach( + memberBillReport.getReports().forEach( (member, price) -> memberBillReportResponses.add(new MemberBillReportAppResponse(member, price)) ); return memberBillReportResponses; diff --git a/server/src/main/java/server/haengdong/domain/action/CurrentMembers.java b/server/src/main/java/server/haengdong/domain/action/CurrentMembers.java index e6843a404..298a8a965 100644 --- a/server/src/main/java/server/haengdong/domain/action/CurrentMembers.java +++ b/server/src/main/java/server/haengdong/domain/action/CurrentMembers.java @@ -1,19 +1,23 @@ package server.haengdong.domain.action; +import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Set; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -@Getter -@RequiredArgsConstructor(access = AccessLevel.PRIVATE) public class CurrentMembers { private final Set members; + public CurrentMembers() { + this(new HashSet<>()); + } + + private CurrentMembers(Set members) { + this.members = members; + } + public static CurrentMembers of(List memberActions) { List sortedMemberActions = getSortedMemberActions(memberActions); Set members = new HashSet<>(); @@ -34,4 +38,29 @@ private static List getSortedMemberActions(List memb .sorted(Comparator.comparing(MemberAction::getSequence)) .toList(); } + + public CurrentMembers addMemberAction(MemberAction memberAction) { + String memberName = memberAction.getMemberName(); + + Set currentMembers = new HashSet<>(members); + + if (memberAction.isIn()) { + currentMembers.add(memberName); + } else { + currentMembers.remove(memberName); + } + return new CurrentMembers(currentMembers); + } + + public boolean isEmpty() { + return members.isEmpty(); + } + + public int size() { + return members.size(); + } + + public Set getMembers() { + return Collections.unmodifiableSet(members); + } } diff --git a/server/src/main/java/server/haengdong/domain/action/MemberAction.java b/server/src/main/java/server/haengdong/domain/action/MemberAction.java index af340de3f..67387cbde 100644 --- a/server/src/main/java/server/haengdong/domain/action/MemberAction.java +++ b/server/src/main/java/server/haengdong/domain/action/MemberAction.java @@ -43,6 +43,10 @@ public boolean isSameName(String name) { return memberName.equals(name); } + public boolean isIn() { + return status == MemberActionStatus.IN; + } + public boolean isSameStatus(MemberActionStatus memberActionStatus) { return status == memberActionStatus; } diff --git a/server/src/main/java/server/haengdong/domain/action/MemberBillReports.java b/server/src/main/java/server/haengdong/domain/action/MemberBillReport.java similarity index 66% rename from server/src/main/java/server/haengdong/domain/action/MemberBillReports.java rename to server/src/main/java/server/haengdong/domain/action/MemberBillReport.java index aa928d2e4..dbf2b49ac 100644 --- a/server/src/main/java/server/haengdong/domain/action/MemberBillReports.java +++ b/server/src/main/java/server/haengdong/domain/action/MemberBillReport.java @@ -2,33 +2,31 @@ import static java.util.stream.Collectors.toMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.PriorityQueue; -import java.util.Set; import java.util.function.Function; import lombok.Getter; @Getter -public class MemberBillReports { +public class MemberBillReport { private final Map reports; - private MemberBillReports(Map reports) { + private MemberBillReport(Map reports) { this.reports = reports; } - public static MemberBillReports createByActions(List billActions, List memberActions) { + public static MemberBillReport createByActions(List billActions, List memberActions) { PriorityQueue sortedBillActions = new PriorityQueue<>(billActions); PriorityQueue sortedMemberActions = new PriorityQueue<>(memberActions); Map memberBillReports = initReports(memberActions); - Set currentMembers = new HashSet<>(); - + CurrentMembers currentMembers = new CurrentMembers(); while (!sortedBillActions.isEmpty() && !sortedMemberActions.isEmpty()) { if (isMemberActionTurn(sortedMemberActions, sortedBillActions)) { - addMemberAction(sortedMemberActions, currentMembers); + MemberAction memberAction = sortedMemberActions.poll(); + currentMembers = currentMembers.addMemberAction(memberAction); continue; } addBillAction(sortedBillActions, currentMembers, memberBillReports); @@ -38,7 +36,7 @@ public static MemberBillReports createByActions(List billActions, Li addBillAction(sortedBillActions, currentMembers, memberBillReports); } - return new MemberBillReports(memberBillReports); + return new MemberBillReport(memberBillReports); } private static Map initReports(List memberActions) { @@ -58,25 +56,19 @@ private static boolean isMemberActionTurn( return memberAction.getSequence() < billAction.getSequence(); } - private static void addMemberAction(PriorityQueue sortedMemberActions, Set currentMembers) { - MemberAction memberAction = sortedMemberActions.poll(); - String memberName = memberAction.getMemberName(); - if (memberAction.isSameStatus(MemberActionStatus.IN)) { - currentMembers.add(memberName); - return; - } - currentMembers.remove(memberAction.getMemberName()); - } - private static void addBillAction( PriorityQueue sortedBillActions, - Set currentMembers, + CurrentMembers currentMembers, Map memberBillReports ) { BillAction billAction = sortedBillActions.poll(); + if (currentMembers.isEmpty()) { + return; + } + Long pricePerMember = billAction.getPrice() / currentMembers.size(); - for (String currentMember : currentMembers) { - Long price = memberBillReports.getOrDefault(currentMember, 0L) + pricePerMember; + for (String currentMember : currentMembers.getMembers()) { + Long price = memberBillReports.get(currentMember) + pricePerMember; memberBillReports.put(currentMember, price); } } diff --git a/server/src/test/java/server/haengdong/domain/action/CurrentMembersTest.java b/server/src/test/java/server/haengdong/domain/action/CurrentMembersTest.java index 37ddca432..389ca70c1 100644 --- a/server/src/test/java/server/haengdong/domain/action/CurrentMembersTest.java +++ b/server/src/test/java/server/haengdong/domain/action/CurrentMembersTest.java @@ -5,6 +5,7 @@ import static server.haengdong.domain.action.MemberActionStatus.OUT; import java.util.List; +import java.util.Set; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import server.haengdong.domain.event.Event; @@ -27,4 +28,31 @@ void of() { assertThat(currentMembers.getMembers()) .containsExactlyInAnyOrder("망쵸", "웨디"); } + + @DisplayName("인원 변동 액션의 상태가 IN이면 현재 인원에 추가한다.") + @Test + void addMemberAction1() { + CurrentMembers currentMembers = new CurrentMembers(); + Event event = new Event("이벤트", "token"); + MemberAction memberAction = new MemberAction(new Action(event, 1L), "웨디", IN, 1L); + + CurrentMembers addedCurrentMembers = currentMembers.addMemberAction(memberAction); + Set members = addedCurrentMembers.getMembers(); + + assertThat(members).hasSize(1) + .containsExactly("웨디"); + } + + @DisplayName("인원 변동 액션의 상태가 OUT이면 현재 인원에서 제외한다.") + @Test + void addMemberAction2() { + Event event = new Event("이벤트", "token"); + MemberAction memberAction1 = new MemberAction(new Action(event, 1L), "웨디", IN, 1L); + CurrentMembers currentMembers = new CurrentMembers().addMemberAction(memberAction1); + MemberAction memberAction2 = new MemberAction(new Action(event, 1L), "웨디", OUT, 1L); + + CurrentMembers addedCurrentMembers = currentMembers.addMemberAction(memberAction2); + + assertThat(addedCurrentMembers.getMembers()).hasSize(0); + } } diff --git a/server/src/test/java/server/haengdong/domain/action/MemberBillReportsTest.java b/server/src/test/java/server/haengdong/domain/action/MemberBillReportTest.java similarity index 89% rename from server/src/test/java/server/haengdong/domain/action/MemberBillReportsTest.java rename to server/src/test/java/server/haengdong/domain/action/MemberBillReportTest.java index db0e2ff37..9ad27d5bc 100644 --- a/server/src/test/java/server/haengdong/domain/action/MemberBillReportsTest.java +++ b/server/src/test/java/server/haengdong/domain/action/MemberBillReportTest.java @@ -10,7 +10,7 @@ import org.junit.jupiter.api.Test; import server.haengdong.domain.event.Event; -class MemberBillReportsTest { +class MemberBillReportTest { @DisplayName("액션 목록으로 참가자 정산 리포트를 생성한다.") @Test @@ -29,9 +29,9 @@ void createByActions() { new MemberAction(new Action(event, 5L), "감자", OUT, 2L) ); - MemberBillReports memberBillReports = MemberBillReports.createByActions(billActions, memberActions); + MemberBillReport memberBillReport = MemberBillReport.createByActions(billActions, memberActions); - assertThat(memberBillReports.getReports()) + assertThat(memberBillReport.getReports()) .containsAllEntriesOf( Map.of( "감자", 20_000L,