From 27464345bdadec5e315b1ae51fe581f0d90b533f Mon Sep 17 00:00:00 2001 From: tsulocalize Date: Tue, 10 Sep 2024 10:37:55 +0900 Subject: [PATCH 01/11] =?UTF-8?q?refactor:=20=EB=8F=84=EB=B3=B4=20?= =?UTF-8?q?=EA=B1=B7=EB=8A=94=20=EC=86=8D=EB=8F=84=20=EC=A1=B0=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/bang_ggood/station/service/SubwayStationService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java index 0f10dbba0..6a6b78ff6 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java @@ -13,7 +13,8 @@ public class SubwayStationService { private static final int METER_PER_DEGREE = 111_320; - private static final double AVERAGE_WALKING_SPEED = 1.3 * 60; // meter per minute + // meter per second * minute unit * decreasing speed on open street + private static final double AVERAGE_WALKING_SPEED = 1.3 * 60 * 0.4; private static final List SUBWAY_STATIONS = SubwayReader.readSubwayStationData(); public SubwayStationResponse readNearestStation(double latitude, double longitude) { From 1e66dcd250c62c1384eec5a63ec9972b7fc6dfee Mon Sep 17 00:00:00 2001 From: tsulocalize Date: Tue, 10 Sep 2024 11:42:12 +0900 Subject: [PATCH 02/11] =?UTF-8?q?feat:=20=EA=B0=80=EA=B9=8C=EC=9A=B4=20?= =?UTF-8?q?=EC=A7=80=ED=95=98=EC=B2=A0=20=EC=97=AD=EC=9D=84=202=EA=B0=9C?= =?UTF-8?q?=EA=B9=8C=EC=A7=80=20=EB=B3=B4=EC=97=AC=EC=A3=BC=EB=8A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/exception/ExceptionCode.java | 4 +- .../controller/SubwayStationController.java | 7 +-- .../station/dto/SubwayStationResponse.java | 42 +++++++++++++++++- .../station/service/SubwayStationService.java | 22 +++++++--- .../station/SubwayStationServiceTest.java | 43 +++++++++++++++---- 5 files changed, 99 insertions(+), 19 deletions(-) diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/global/exception/ExceptionCode.java b/backend/bang-ggood/src/main/java/com/bang_ggood/global/exception/ExceptionCode.java index 84e08e336..62fc3f4c0 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/global/exception/ExceptionCode.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/global/exception/ExceptionCode.java @@ -69,7 +69,9 @@ public enum ExceptionCode { ARTICLE_NOT_FOUND(HttpStatus.BAD_REQUEST, "해당 아티클이 존재하지 않습니다."), // Station - STATION_NOT_FOUND(HttpStatus.INTERNAL_SERVER_ERROR, "지하철 역을 찾을 수 없습니다."); + STATION_NOT_FOUND(HttpStatus.INTERNAL_SERVER_ERROR, "지하철 역을 찾을 수 없습니다."), + STATION_NOT_SAME(HttpStatus.INTERNAL_SERVER_ERROR, "지하철 역 검색 중 오류가 발생했습니다.") + ; private final HttpStatus httpStatus; private final String message; diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/station/controller/SubwayStationController.java b/backend/bang-ggood/src/main/java/com/bang_ggood/station/controller/SubwayStationController.java index bb8dc71bb..08da09b23 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/station/controller/SubwayStationController.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/station/controller/SubwayStationController.java @@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import java.util.List; @RestController public class SubwayStationController { @@ -17,10 +18,10 @@ public SubwayStationController(SubwayStationService subwayStationService) { } @GetMapping("/stations/nearest") - public ResponseEntity readNearestStation(@RequestParam("latitude") double latitude, - @RequestParam("longitude") double longitude) { + public ResponseEntity> readNearestStation(@RequestParam("latitude") double latitude, + @RequestParam("longitude") double longitude) { - SubwayStationResponse response = subwayStationService.readNearestStation(latitude, longitude); + List response = subwayStationService.readNearestStation(latitude, longitude); return ResponseEntity.ok(response); } } diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/station/dto/SubwayStationResponse.java b/backend/bang-ggood/src/main/java/com/bang_ggood/station/dto/SubwayStationResponse.java index caa90f182..4a185b7c5 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/station/dto/SubwayStationResponse.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/station/dto/SubwayStationResponse.java @@ -1,10 +1,48 @@ package com.bang_ggood.station.dto; +import com.bang_ggood.global.exception.BangggoodException; +import com.bang_ggood.global.exception.ExceptionCode; import com.bang_ggood.station.domain.SubwayStation; +import java.util.ArrayList; +import java.util.List; -public record SubwayStationResponse(String stationName, String stationLine, Integer walkingTime) { +public class SubwayStationResponse { + + private final String stationName; + private final List stationLine; + private Integer walkingTime; + + public SubwayStationResponse(String stationName, List stationLine, Integer walkingTime) { + this.stationName = stationName; + this.stationLine = stationLine; + this.walkingTime = walkingTime; + } public static SubwayStationResponse of(SubwayStation station, Integer walkingTime) { - return new SubwayStationResponse(station.getName(), station.getLine(), walkingTime); + List stationLine = new ArrayList<>(); + stationLine.add(station.getLine()); + return new SubwayStationResponse(station.getName(), stationLine, walkingTime); + } + + public SubwayStationResponse merge(SubwayStationResponse response) { + if (!stationName.equals(response.stationName)) { + throw new BangggoodException(ExceptionCode.STATION_NOT_SAME); + } + + stationLine.addAll(response.stationLine); + walkingTime = Math.min(walkingTime, response.walkingTime); + return this; + } + + public String getStationName() { + return stationName; + } + + public List getStationLine() { + return stationLine; + } + + public Integer getWalkingTime() { + return walkingTime; } } diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java index 6a6b78ff6..afe177f10 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java @@ -1,13 +1,13 @@ package com.bang_ggood.station.service; -import com.bang_ggood.global.exception.BangggoodException; -import com.bang_ggood.global.exception.ExceptionCode; import com.bang_ggood.station.SubwayReader; import com.bang_ggood.station.domain.SubwayStation; import com.bang_ggood.station.dto.SubwayStationResponse; import org.springframework.stereotype.Service; import java.util.Comparator; import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; @Service public class SubwayStationService { @@ -15,9 +15,11 @@ public class SubwayStationService { private static final int METER_PER_DEGREE = 111_320; // meter per second * minute unit * decreasing speed on open street private static final double AVERAGE_WALKING_SPEED = 1.3 * 60 * 0.4; + private static final int MAX_NESTING_STATION_NUMBER = 4; + private static final int REQUESTED_STATION_NUMBER = 2; private static final List SUBWAY_STATIONS = SubwayReader.readSubwayStationData(); - public SubwayStationResponse readNearestStation(double latitude, double longitude) { + public List readNearestStation(double latitude, double longitude) { return SUBWAY_STATIONS.stream() .map(station -> { double dx = (station.getLatitude() - latitude) * METER_PER_DEGREE; @@ -26,7 +28,17 @@ public SubwayStationResponse readNearestStation(double latitude, double longitud double distance = Math.sqrt(dx * dx + dy * dy); return SubwayStationResponse.of(station, (int) Math.round(distance / AVERAGE_WALKING_SPEED)); }) - .min(Comparator.comparing(SubwayStationResponse::walkingTime)) - .orElseThrow(() -> new BangggoodException(ExceptionCode.STATION_NOT_FOUND)); + .sorted(Comparator.comparing(SubwayStationResponse::getWalkingTime)) + .limit(MAX_NESTING_STATION_NUMBER * REQUESTED_STATION_NUMBER) + .collect(Collectors.groupingBy( + SubwayStationResponse::getStationName, + Collectors.reducing(SubwayStationResponse::merge) + )) + .values() + .stream() + .map(Optional::orElseThrow) + .sorted(Comparator.comparing(SubwayStationResponse::getWalkingTime)) + .limit(REQUESTED_STATION_NUMBER) + .toList(); } } diff --git a/backend/bang-ggood/src/test/java/com/bang_ggood/station/SubwayStationServiceTest.java b/backend/bang-ggood/src/test/java/com/bang_ggood/station/SubwayStationServiceTest.java index dd066cc84..8cd42f666 100644 --- a/backend/bang-ggood/src/test/java/com/bang_ggood/station/SubwayStationServiceTest.java +++ b/backend/bang-ggood/src/test/java/com/bang_ggood/station/SubwayStationServiceTest.java @@ -1,15 +1,18 @@ package com.bang_ggood.station; import com.bang_ggood.IntegrationTestSupport; +import com.bang_ggood.station.dto.SubwayStationResponse; import com.bang_ggood.station.service.SubwayStationService; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import org.springframework.beans.factory.annotation.Autowired; +import java.util.List; import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; public class SubwayStationServiceTest extends IntegrationTestSupport { @@ -19,21 +22,45 @@ public class SubwayStationServiceTest extends IntegrationTestSupport { // check data in "https://apis.map.kakao.com/web/sample/addMapClickEventWithMarker/" private static Stream provideStationData() { return Stream.of( - Arguments.of(37.50495731889611, 126.7550884277559, "상동"), - Arguments.of(37.48352733443973, 126.80085909322227, "소사"), - Arguments.of(37.47909015564278, 126.9517354974442, "서울대입구(관악구청)"), - Arguments.of(37.516248619935034, 127.10303565244502, "잠실(송파구청)") + Arguments.of(37.517406150696104, 127.10333134512422, + new Station("잠실(송파구청)", List.of("2호선", "8호선")), + new Station("잠실나루", List.of("2호선"))), + Arguments.of(37.50808977595056, 127.04649615866747, + new Station("선정릉", List.of("9호선", "분당선")), + new Station("선릉", List.of("2호선", "분당선"))), + Arguments.of(37.538999998345446, 126.97201837726666, + new Station("남영", List.of("1호선")), + new Station("삼각지", List.of("4호선", "6호선"))) ); } - @DisplayName("가까운 지하철 조회 성공") + @DisplayName("가까운 지하철 2개 조회 성공") @ParameterizedTest @MethodSource("provideStationData") - void readNearestStation(double latitude, double longitude, String nearestStationName) { + void readNearestStation(double latitude, double longitude, Station nearestStation, Station nextNearestStation) { // given & when - String stationName = subwayStationService.readNearestStation(latitude, longitude).stationName(); + List responses = subwayStationService.readNearestStation(latitude, longitude); + assertThat(responses).hasSize(2); + SubwayStationResponse nearest = responses.get(0); + SubwayStationResponse nextNearest = responses.get(1); // then - assertThat(stationName).isEqualTo(nearestStationName); + assertAll(() -> { + assertThat(nearest.getStationName()).isEqualTo(nearestStation.name); + assertThat(nearest.getStationLine()).containsAll(nearestStation.lines); + assertThat(nextNearest.getStationName()).isEqualTo(nextNearestStation.name); + assertThat(nextNearest.getStationLine()).containsAll(nextNearestStation.lines); + } + ); + } + + private static class Station { + String name; + List lines; + + public Station(String name, List lines) { + this.name = name; + this.lines = lines; + } } } From 616f925d5bf99046c80e52e42899ec9ee19bce19 Mon Sep 17 00:00:00 2001 From: tsulocalize Date: Thu, 19 Sep 2024 12:56:20 +0900 Subject: [PATCH 03/11] =?UTF-8?q?refactor:=20=EC=9D=B8=EC=9E=90=20?= =?UTF-8?q?=ED=83=80=EC=9E=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../station/controller/SubwayStationController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/station/controller/SubwayStationController.java b/backend/bang-ggood/src/main/java/com/bang_ggood/station/controller/SubwayStationController.java index 08da09b23..9e0178afc 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/station/controller/SubwayStationController.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/station/controller/SubwayStationController.java @@ -18,8 +18,8 @@ public SubwayStationController(SubwayStationService subwayStationService) { } @GetMapping("/stations/nearest") - public ResponseEntity> readNearestStation(@RequestParam("latitude") double latitude, - @RequestParam("longitude") double longitude) { + public ResponseEntity> readNearestStation(@RequestParam("latitude") Double latitude, + @RequestParam("longitude") Double longitude) { List response = subwayStationService.readNearestStation(latitude, longitude); return ResponseEntity.ok(response); From f587db44e8789a7ea9d0528f720f50c49ce3719f Mon Sep 17 00:00:00 2001 From: tsulocalize Date: Thu, 19 Sep 2024 13:05:39 +0900 Subject: [PATCH 04/11] =?UTF-8?q?refactor:=20=EC=BB=A4=EC=8A=A4=ED=85=80?= =?UTF-8?q?=20=EC=98=88=EC=99=B8=EB=A5=BC=20=ED=86=B5=ED=95=9C=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/bang_ggood/global/exception/ExceptionCode.java | 3 +-- .../com/bang_ggood/station/dto/SubwayStationResponse.java | 2 +- .../com/bang_ggood/station/service/SubwayStationService.java | 5 +++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/global/exception/ExceptionCode.java b/backend/bang-ggood/src/main/java/com/bang_ggood/global/exception/ExceptionCode.java index 62fc3f4c0..453259b8d 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/global/exception/ExceptionCode.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/global/exception/ExceptionCode.java @@ -70,8 +70,7 @@ public enum ExceptionCode { // Station STATION_NOT_FOUND(HttpStatus.INTERNAL_SERVER_ERROR, "지하철 역을 찾을 수 없습니다."), - STATION_NOT_SAME(HttpStatus.INTERNAL_SERVER_ERROR, "지하철 역 검색 중 오류가 발생했습니다.") - ; + STATION_NAME_NOT_SAME(HttpStatus.INTERNAL_SERVER_ERROR, "지하철 역을 찾을 수 없습니다."); private final HttpStatus httpStatus; private final String message; diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/station/dto/SubwayStationResponse.java b/backend/bang-ggood/src/main/java/com/bang_ggood/station/dto/SubwayStationResponse.java index 4a185b7c5..634eed2d4 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/station/dto/SubwayStationResponse.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/station/dto/SubwayStationResponse.java @@ -26,7 +26,7 @@ public static SubwayStationResponse of(SubwayStation station, Integer walkingTim public SubwayStationResponse merge(SubwayStationResponse response) { if (!stationName.equals(response.stationName)) { - throw new BangggoodException(ExceptionCode.STATION_NOT_SAME); + throw new BangggoodException(ExceptionCode.STATION_NAME_NOT_SAME); } stationLine.addAll(response.stationLine); diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java index afe177f10..9dcdd82d8 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java @@ -1,12 +1,13 @@ package com.bang_ggood.station.service; +import com.bang_ggood.global.exception.BangggoodException; +import com.bang_ggood.global.exception.ExceptionCode; import com.bang_ggood.station.SubwayReader; import com.bang_ggood.station.domain.SubwayStation; import com.bang_ggood.station.dto.SubwayStationResponse; import org.springframework.stereotype.Service; import java.util.Comparator; import java.util.List; -import java.util.Optional; import java.util.stream.Collectors; @Service @@ -36,7 +37,7 @@ public List readNearestStation(double latitude, double lo )) .values() .stream() - .map(Optional::orElseThrow) + .map(optional -> optional.orElseThrow(() -> new BangggoodException(ExceptionCode.STATION_NOT_FOUND))) .sorted(Comparator.comparing(SubwayStationResponse::getWalkingTime)) .limit(REQUESTED_STATION_NUMBER) .toList(); From b71bcd037da67d2664a821a1cb1eceb6b65a3000 Mon Sep 17 00:00:00 2001 From: tsulocalize Date: Thu, 19 Sep 2024 13:08:34 +0900 Subject: [PATCH 05/11] =?UTF-8?q?style:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=9C=84=EC=B9=98=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../station/SubwayStationServiceTest.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/backend/bang-ggood/src/test/java/com/bang_ggood/station/SubwayStationServiceTest.java b/backend/bang-ggood/src/test/java/com/bang_ggood/station/SubwayStationServiceTest.java index 8cd42f666..2a89ff98c 100644 --- a/backend/bang-ggood/src/test/java/com/bang_ggood/station/SubwayStationServiceTest.java +++ b/backend/bang-ggood/src/test/java/com/bang_ggood/station/SubwayStationServiceTest.java @@ -19,21 +19,6 @@ public class SubwayStationServiceTest extends IntegrationTestSupport { @Autowired SubwayStationService subwayStationService; - // check data in "https://apis.map.kakao.com/web/sample/addMapClickEventWithMarker/" - private static Stream provideStationData() { - return Stream.of( - Arguments.of(37.517406150696104, 127.10333134512422, - new Station("잠실(송파구청)", List.of("2호선", "8호선")), - new Station("잠실나루", List.of("2호선"))), - Arguments.of(37.50808977595056, 127.04649615866747, - new Station("선정릉", List.of("9호선", "분당선")), - new Station("선릉", List.of("2호선", "분당선"))), - Arguments.of(37.538999998345446, 126.97201837726666, - new Station("남영", List.of("1호선")), - new Station("삼각지", List.of("4호선", "6호선"))) - ); - } - @DisplayName("가까운 지하철 2개 조회 성공") @ParameterizedTest @MethodSource("provideStationData") @@ -54,6 +39,21 @@ void readNearestStation(double latitude, double longitude, Station nearestStatio ); } + // check data in "https://apis.map.kakao.com/web/sample/addMapClickEventWithMarker/" + private static Stream provideStationData() { + return Stream.of( + Arguments.of(37.517406150696104, 127.10333134512422, + new Station("잠실(송파구청)", List.of("2호선", "8호선")), + new Station("잠실나루", List.of("2호선"))), + Arguments.of(37.50808977595056, 127.04649615866747, + new Station("선정릉", List.of("9호선", "분당선")), + new Station("선릉", List.of("2호선", "분당선"))), + Arguments.of(37.538999998345446, 126.97201837726666, + new Station("남영", List.of("1호선")), + new Station("삼각지", List.of("4호선", "6호선"))) + ); + } + private static class Station { String name; List lines; From 9ceab176c3b3dbd9a8d02e0ab5d9565269368025 Mon Sep 17 00:00:00 2001 From: tsulocalize Date: Thu, 19 Sep 2024 13:12:37 +0900 Subject: [PATCH 06/11] =?UTF-8?q?refactor:=20=EC=A7=80=ED=95=98=EC=B2=A0?= =?UTF-8?q?=20=EC=97=AD=20=EA=B1=B0=EB=A6=AC=20=EA=B5=AC=ED=95=98=EB=8A=94?= =?UTF-8?q?=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/bang_ggood/station/domain/SubwayStation.java | 9 +++++++++ .../bang_ggood/station/service/SubwayStationService.java | 6 +----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/station/domain/SubwayStation.java b/backend/bang-ggood/src/main/java/com/bang_ggood/station/domain/SubwayStation.java index 7cec623bb..b8aa8db2d 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/station/domain/SubwayStation.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/station/domain/SubwayStation.java @@ -2,6 +2,8 @@ public class SubwayStation { + private static final int METER_PER_DEGREE = 111_320; + private final Integer id; private final String name; private final String line; @@ -16,6 +18,13 @@ public SubwayStation(Integer id, String name, String line, double latitude, doub this.longitude = longitude; } + public double getDistance(double latitude, double longitude) { + double dx = (this.latitude - latitude) * METER_PER_DEGREE; + double dy = + (this.longitude - longitude) * METER_PER_DEGREE * Math.cos(this.latitude); + return Math.sqrt(dx * dx + dy * dy); + } + public Integer getId() { return id; } diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java index 9dcdd82d8..b28f26e53 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java @@ -13,7 +13,6 @@ @Service public class SubwayStationService { - private static final int METER_PER_DEGREE = 111_320; // meter per second * minute unit * decreasing speed on open street private static final double AVERAGE_WALKING_SPEED = 1.3 * 60 * 0.4; private static final int MAX_NESTING_STATION_NUMBER = 4; @@ -23,10 +22,7 @@ public class SubwayStationService { public List readNearestStation(double latitude, double longitude) { return SUBWAY_STATIONS.stream() .map(station -> { - double dx = (station.getLatitude() - latitude) * METER_PER_DEGREE; - double dy = - (station.getLongitude() - longitude) * METER_PER_DEGREE * Math.cos(station.getLatitude()); - double distance = Math.sqrt(dx * dx + dy * dy); + double distance = station.getDistance(latitude, longitude); return SubwayStationResponse.of(station, (int) Math.round(distance / AVERAGE_WALKING_SPEED)); }) .sorted(Comparator.comparing(SubwayStationResponse::getWalkingTime)) From 34b553221ab4340f035fad4339fff85d2c668d4a Mon Sep 17 00:00:00 2001 From: tsulocalize Date: Thu, 19 Sep 2024 13:28:05 +0900 Subject: [PATCH 07/11] =?UTF-8?q?refactor:=20stream=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../station/service/SubwayStationService.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java index b28f26e53..a0855b12c 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java @@ -8,6 +8,8 @@ import org.springframework.stereotype.Service; import java.util.Comparator; import java.util.List; +import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; @Service @@ -20,7 +22,7 @@ public class SubwayStationService { private static final List SUBWAY_STATIONS = SubwayReader.readSubwayStationData(); public List readNearestStation(double latitude, double longitude) { - return SUBWAY_STATIONS.stream() + Map> responseMap = SUBWAY_STATIONS.stream() .map(station -> { double distance = station.getDistance(latitude, longitude); return SubwayStationResponse.of(station, (int) Math.round(distance / AVERAGE_WALKING_SPEED)); @@ -30,9 +32,9 @@ public List readNearestStation(double latitude, double lo .collect(Collectors.groupingBy( SubwayStationResponse::getStationName, Collectors.reducing(SubwayStationResponse::merge) - )) - .values() - .stream() + )); + + return responseMap.values().stream() .map(optional -> optional.orElseThrow(() -> new BangggoodException(ExceptionCode.STATION_NOT_FOUND))) .sorted(Comparator.comparing(SubwayStationResponse::getWalkingTime)) .limit(REQUESTED_STATION_NUMBER) From fc33c052f884a9503c31fd2c4cd62c154391c62b Mon Sep 17 00:00:00 2001 From: tsulocalize Date: Fri, 20 Sep 2024 13:27:56 +0900 Subject: [PATCH 08/11] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=20=EB=AA=85=20=EB=AA=85=ED=99=95=ED=95=98=EA=B2=8C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/bang_ggood/station/domain/SubwayStation.java | 2 +- .../com/bang_ggood/station/service/SubwayStationService.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/station/domain/SubwayStation.java b/backend/bang-ggood/src/main/java/com/bang_ggood/station/domain/SubwayStation.java index b8aa8db2d..b1a2f866a 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/station/domain/SubwayStation.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/station/domain/SubwayStation.java @@ -18,7 +18,7 @@ public SubwayStation(Integer id, String name, String line, double latitude, doub this.longitude = longitude; } - public double getDistance(double latitude, double longitude) { + public double calculateDistance(double latitude, double longitude) { double dx = (this.latitude - latitude) * METER_PER_DEGREE; double dy = (this.longitude - longitude) * METER_PER_DEGREE * Math.cos(this.latitude); diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java index a0855b12c..3d02d511c 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java @@ -24,7 +24,7 @@ public class SubwayStationService { public List readNearestStation(double latitude, double longitude) { Map> responseMap = SUBWAY_STATIONS.stream() .map(station -> { - double distance = station.getDistance(latitude, longitude); + double distance = station.calculateDistance(latitude, longitude); return SubwayStationResponse.of(station, (int) Math.round(distance / AVERAGE_WALKING_SPEED)); }) .sorted(Comparator.comparing(SubwayStationResponse::getWalkingTime)) From b13e9b74ea8de737ac37e40dc06e2de1c86151ce Mon Sep 17 00:00:00 2001 From: tsulocalize Date: Mon, 23 Sep 2024 14:02:23 +0900 Subject: [PATCH 09/11] =?UTF-8?q?refactor:=20walkingTime=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=20=EB=A1=9C=EC=A7=81=20subwayStation=20=EB=82=B4?= =?UTF-8?q?=EB=B6=80=EB=A1=9C=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/bang_ggood/station/domain/SubwayStation.java | 11 +++++++---- .../bang_ggood/station/dto/SubwayStationResponse.java | 4 ++-- .../station/service/SubwayStationService.java | 7 +------ 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/station/domain/SubwayStation.java b/backend/bang-ggood/src/main/java/com/bang_ggood/station/domain/SubwayStation.java index b1a2f866a..94a385d35 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/station/domain/SubwayStation.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/station/domain/SubwayStation.java @@ -3,6 +3,8 @@ public class SubwayStation { private static final int METER_PER_DEGREE = 111_320; + // meter per second * minute unit * decreasing speed on open street + private static final double AVERAGE_WALKING_SPEED = 1.3 * 60 * 0.4; private final Integer id; private final String name; @@ -18,11 +20,12 @@ public SubwayStation(Integer id, String name, String line, double latitude, doub this.longitude = longitude; } - public double calculateDistance(double latitude, double longitude) { + public int calculateWalkingTime(double latitude, double longitude) { double dx = (this.latitude - latitude) * METER_PER_DEGREE; - double dy = - (this.longitude - longitude) * METER_PER_DEGREE * Math.cos(this.latitude); - return Math.sqrt(dx * dx + dy * dy); + double dy = (this.longitude - longitude) * METER_PER_DEGREE * Math.cos(this.latitude); + double distance = Math.sqrt(dx * dx + dy * dy); + + return (int) (Math.round(distance) / AVERAGE_WALKING_SPEED); } public Integer getId() { diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/station/dto/SubwayStationResponse.java b/backend/bang-ggood/src/main/java/com/bang_ggood/station/dto/SubwayStationResponse.java index 634eed2d4..165c3a6e8 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/station/dto/SubwayStationResponse.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/station/dto/SubwayStationResponse.java @@ -18,10 +18,10 @@ public SubwayStationResponse(String stationName, List stationLine, Integ this.walkingTime = walkingTime; } - public static SubwayStationResponse of(SubwayStation station, Integer walkingTime) { + public static SubwayStationResponse of(SubwayStation station, double latitude, double longitude) { List stationLine = new ArrayList<>(); stationLine.add(station.getLine()); - return new SubwayStationResponse(station.getName(), stationLine, walkingTime); + return new SubwayStationResponse(station.getName(), stationLine, station.calculateWalkingTime(latitude, longitude)); } public SubwayStationResponse merge(SubwayStationResponse response) { diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java index 3d02d511c..89489b639 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java @@ -15,18 +15,13 @@ @Service public class SubwayStationService { - // meter per second * minute unit * decreasing speed on open street - private static final double AVERAGE_WALKING_SPEED = 1.3 * 60 * 0.4; private static final int MAX_NESTING_STATION_NUMBER = 4; private static final int REQUESTED_STATION_NUMBER = 2; private static final List SUBWAY_STATIONS = SubwayReader.readSubwayStationData(); public List readNearestStation(double latitude, double longitude) { Map> responseMap = SUBWAY_STATIONS.stream() - .map(station -> { - double distance = station.calculateDistance(latitude, longitude); - return SubwayStationResponse.of(station, (int) Math.round(distance / AVERAGE_WALKING_SPEED)); - }) + .map(station -> SubwayStationResponse.of(station, latitude, longitude)) .sorted(Comparator.comparing(SubwayStationResponse::getWalkingTime)) .limit(MAX_NESTING_STATION_NUMBER * REQUESTED_STATION_NUMBER) .collect(Collectors.groupingBy( From 093573bbc43db164e9f23f633a37f7470ac947fc Mon Sep 17 00:00:00 2001 From: tsulocalize Date: Mon, 23 Sep 2024 14:19:13 +0900 Subject: [PATCH 10/11] =?UTF-8?q?refactor:=20=EC=8A=A4=ED=8A=B8=EB=A6=BC?= =?UTF-8?q?=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../station/service/SubwayStationService.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java index 89489b639..070933f3d 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java @@ -8,8 +8,6 @@ import org.springframework.stereotype.Service; import java.util.Comparator; import java.util.List; -import java.util.Map; -import java.util.Optional; import java.util.stream.Collectors; @Service @@ -20,19 +18,34 @@ public class SubwayStationService { private static final List SUBWAY_STATIONS = SubwayReader.readSubwayStationData(); public List readNearestStation(double latitude, double longitude) { - Map> responseMap = SUBWAY_STATIONS.stream() + List likelyNearStations = findLikelyNearStations(latitude, longitude); + List mergedNearStations = mergeSameStations(likelyNearStations); + + return mergedNearStations.stream() + .sorted(Comparator.comparing(SubwayStationResponse::getWalkingTime)) + .limit(REQUESTED_STATION_NUMBER) + .toList(); + } + + private List findLikelyNearStations(double latitude, double longitude) { + return SUBWAY_STATIONS.stream() .map(station -> SubwayStationResponse.of(station, latitude, longitude)) .sorted(Comparator.comparing(SubwayStationResponse::getWalkingTime)) .limit(MAX_NESTING_STATION_NUMBER * REQUESTED_STATION_NUMBER) + .toList(); + } + + private List mergeSameStations(List stations) { + return stations.stream() .collect(Collectors.groupingBy( SubwayStationResponse::getStationName, Collectors.reducing(SubwayStationResponse::merge) - )); - - return responseMap.values().stream() + )) + .values() + .stream() .map(optional -> optional.orElseThrow(() -> new BangggoodException(ExceptionCode.STATION_NOT_FOUND))) - .sorted(Comparator.comparing(SubwayStationResponse::getWalkingTime)) - .limit(REQUESTED_STATION_NUMBER) .toList(); } + + } From d86ab88bd480a0496b58dead72372f2190599dce Mon Sep 17 00:00:00 2001 From: tsulocalize Date: Mon, 23 Sep 2024 15:23:19 +0900 Subject: [PATCH 11/11] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=20=EB=AA=85=ED=99=95=ED=95=98=EA=B2=8C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/bang_ggood/station/service/SubwayStationService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java index 070933f3d..e80a42da2 100644 --- a/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java +++ b/backend/bang-ggood/src/main/java/com/bang_ggood/station/service/SubwayStationService.java @@ -19,7 +19,7 @@ public class SubwayStationService { public List readNearestStation(double latitude, double longitude) { List likelyNearStations = findLikelyNearStations(latitude, longitude); - List mergedNearStations = mergeSameStations(likelyNearStations); + List mergedNearStations = mergeTransferStations(likelyNearStations); return mergedNearStations.stream() .sorted(Comparator.comparing(SubwayStationResponse::getWalkingTime)) @@ -35,7 +35,7 @@ private List findLikelyNearStations(double latitude, doub .toList(); } - private List mergeSameStations(List stations) { + private List mergeTransferStations(List stations) { return stations.stream() .collect(Collectors.groupingBy( SubwayStationResponse::getStationName,