Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿš€ 2๋‹จ๊ณ„ - ์š”๊ธˆ ์กฐํšŒ #482

Open
wants to merge 3 commits into
base: seonje1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/main/java/nextstep/subway/path/FareCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package nextstep.subway.path;

public class FareCalculator {

private static final int BASE_FARE = 1250;
private static final int OVER_50KM_FARE = 2150;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SURCHARGE ๊ฐ€ ๋ณ€๊ฒฝ๋œ๋‹ค๋ฉด OVER_50KM_FARE ๋„ ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•˜๊ฒ ๋„ค์š”.
์ด๋Š” ์œ ์ง€๋ณด์ˆ˜์— ์ทจ์•ฝํ•  ์ˆ˜ ์žˆ์–ด๋ณด์ด์ง€๋งŒ, ๋‹จ์œ„ํ…Œ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์…”์„œ ์ง€๊ธˆ๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

(๊ฐœ์ธ์ ์œผ๋กœ๋Š” ๋‹จ์œ„ํ…Œ์ŠคํŠธ๋ฅผ ๋”์šฑ ๊ผผ๊ผผํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด @ParameterizedTest ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋”์šฑ ์„ธ์„ธํ•˜๊ฒŒ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ์ถ”๊ฐ€ํ–ˆ์„ ๊ฒƒ ๊ฐ™์•„์š”)

private static final int SURCHARGE = 100;

public static int calculate(int distance) {
if (distance < 11) {
return BASE_FARE;
}

if (distance < 51) {
return under50KmOver10KmFareCalculate(distance);
}

return over50KmFareCalculate(distance);
}

private static int under50KmOver10KmFareCalculate(int distance) {
int overDistance = (distance - 10) / 5;
return (BASE_FARE + SURCHARGE) + (SURCHARGE * overDistance);
}

private static int over50KmFareCalculate(int distance) {
int overDistance = (distance - 50) / 8;
return OVER_50KM_FARE + (SURCHARGE * overDistance);
}
}
4 changes: 2 additions & 2 deletions src/main/java/nextstep/subway/path/PathFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public PathResponse getPath(List<Sections> sectionsList, Station sourceStation,
.map(StationResponse::ofEntity)
.collect(Collectors.toList()),
distance,
duration
duration,
FareCalculator.calculate(distance)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ง€ํ•˜์ฒ  ์š”๊ธˆ์€ ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๊ธฐ์ค€์œผ๋กœ ๋ถ€๊ณผํ•˜๋Š” ๊ฒƒ์ด ์ •์ฑ…์ธ๋ฐ์š”.
ํ˜„์žฌ ์ถœ๋ฐœ์—ญ/๋„์ฐฉ์—ญ์ด ๊ฐ™๋”๋ผ๋„ ์กฐํšŒํ•˜๋Š” ๊ฒฝ๋กœ ํƒ€์ž…์— ๋”ฐ๋ผ ์š”๊ธˆ์ด ๋‹ฌ๋ผ์ง€๋Š” ๊ฒƒ ๊ฐ™์•„ ์ˆ˜์ •์ด ํ•„์š” ํ•ด ๋ณด์—ฌ์š”.

CleanShot 2024-03-16 at 12 29 11@2x

);

} catch (RuntimeException e) {
e.printStackTrace();
throw new PathException("์—ฐ๊ฒฐ๋˜์–ด์žˆ์ง€ ์•Š์€ ์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ์˜ ๊ฒฝ๋กœ๋Š” ์กฐํšŒํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/nextstep/subway/path/PathResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ public class PathResponse {

int duration;

public PathResponse(List<StationResponse> stations, int distance, int duration) {
int fare;

public PathResponse(List<StationResponse> stations, int distance, int duration, int fare) {
this.stations = stations;
this.distance = distance;
this.duration = duration;
this.fare = fare;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ˜„์žฌ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•  ๋•Œ ์‘๋‹ต์œผ๋กœ ์š”๊ธˆ์„ ํ•จ๊ป˜ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์žˆ๋„ค์š”.

์š”๊ธˆ์„ ๊ณ„์‚ฐํ•˜๋Š” ์—ญํ• ์€ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” PathFinder ์— ๋‘๊ธฐ์—๋Š” ๊ณผํ•œ ์ฑ…์ž„์„ ๋ถ€๊ณผํ•œ ๊ฒƒ ์œผ๋กœ ๋ณด์—ฌ์ ธ์š”.

๊ฑฐ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ์š”๊ธˆ์„ ๊ณ„์‚ฐํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋ฆฌํŒฉํ† ๋ง ํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”? ๐Ÿ˜Š

}

public List<StationResponse> getStations() {
Expand All @@ -29,4 +32,8 @@ public int getDistance() {
public int getDuration() {
return duration;
}

public int getFare() {
return fare;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void setUp() {
/**
* Given ์ง€ํ•˜์ฒ  ๊ตฌ๊ฐ„์„ ๋“ฑ๋กํ•˜๊ณ 
* When ๊ฒฝ๋กœ๋ฅผ ์กฐํšŒํ•˜๋ฉด
* Then ์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ๊นŒ์ง€์˜ ๊ฒฝ๋กœ์— ์žˆ๋Š” ์—ญ๊ณผ ๊ฑฐ๋ฆฌ๋ฅผ ์กฐํšŒํ•œ๋‹ค
* Then ์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ธธ์ด์˜ ๊ฒฝ๋กœ๋ฅผ ์กฐํšŒํ•œ๋‹ค
*/
@DisplayName("์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ธธ์ด ๊ฒฝ๋กœ ์กฐํšŒ")
@Test
Expand All @@ -77,7 +77,7 @@ void showDistanceShortestPaths() {
/**
* Given ์ง€ํ•˜์ฒ  ๊ตฌ๊ฐ„์„ ๋“ฑ๋กํ•˜๊ณ 
* When ๊ฒฝ๋กœ๋ฅผ ์กฐํšŒํ•˜๋ฉด
* Then ์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ๊นŒ์ง€์˜ ๊ฒฝ๋กœ์— ์žˆ๋Š” ์—ญ๊ณผ ๊ฑฐ๋ฆฌ๋ฅผ ์กฐํšŒํ•œ๋‹ค
* Then ์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ๊นŒ์ง€์˜ ์ตœ๋‹จ ์‹œ๊ฐ„์˜ ๊ฒฝ๋กœ๋ฅผ ์กฐํšŒํ•œ๋‹ค
*/
@DisplayName("์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ๊นŒ์ง€์˜ ์ตœ๋‹จ ์‹œ๊ฐ„ ๊ฒฝ๋กœ ์กฐํšŒ")
@Test
Expand All @@ -95,6 +95,53 @@ void showDurationShortestPaths() {
assertThat(distance).isEqualTo(6);
}

/**
* Given ์ง€ํ•˜์ฒ  ๊ตฌ๊ฐ„์„ ๋“ฑ๋กํ•˜๊ณ 
* When ๊ฒฝ๋กœ๋ฅผ ์กฐํšŒํ•˜๋ฉด
* Then ์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ๊นŒ์ง€์˜ ์š”๊ธˆ์„ ์กฐํšŒํ•œ๋‹ค
*/
@DisplayName("์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ๊นŒ์ง€์˜ ์š”๊ธˆ ์กฐํšŒ")
Comment on lines +98 to +103

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ธ์ˆ˜ํ…Œ์ŠคํŠธ๋ฅผ ์ž˜ ์ž‘์„ฑ ํ•ด ์ฃผ์…จ๋„ค์š” ๐Ÿ‘
ํ์ปด๋ฒ„๋ฅผ ์ด์šฉํ•œ ์ธ์ˆ˜ํ…Œ์ŠคํŠธ๊ฐ€ ์š”๊ตฌ์‚ฌํ•ญ์— ์กด์žฌํ•˜๋Š”๋ฐ์š”!
ํ์ปด๋ฒ„๋ฅผ ์ด์šฉํ•ด์„œ ๋งŒ๋“ค์–ด๋ณด๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”?

CleanShot 2024-03-16 at 12 52 07@2x

@Test
void showPathFare() {
//given
Long ์„ฑ์ˆ˜์—ญid = JsonPathUtil.getId(StationApiRequester.createStationApiCall("์„ฑ์ˆ˜์—ญ"));
SectionCreateRequest ์–‘์žฌ์„ฑ์ˆ˜์—ญ = new SectionCreateRequest(์–‘์žฌ์—ญid, ์„ฑ์ˆ˜์—ญid, 10, 3);
SectionApiRequester.generateSection(์–‘์žฌ์„ฑ์ˆ˜์—ญ, ์‹ ๋ถ„๋‹น์„ id);

//when
ExtractableResponse<Response> response = PathApiRequester.getDistanceShortestPath(๊ต๋Œ€์—ญid, ์„ฑ์ˆ˜์—ญid);

//then
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value());

int fare = response.jsonPath().getInt("fare");
assertThat(fare).isEqualTo(1450);
}

/**
* Given ์ง€ํ•˜์ฒ  ๊ตฌ๊ฐ„์„ ๋“ฑ๋กํ•˜๊ณ 
* When ๊ธธ์ด๊ฐ€ 50 ์ด์ƒ์ธ ๊ฒฝ๋กœ๋ฅผ ์กฐํšŒํ•˜๋ฉด
* Then ์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ๊นŒ์ง€์˜ ์ถ”๊ฐ€๋œ ์š”๊ธˆ์„ ์กฐํšŒํ•œ๋‹ค
*/
@DisplayName("์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ๊นŒ์ง€์˜ ์š”๊ธˆ ์กฐํšŒ")
@Test
void surchargeTest() {
//given
Long ์„ฑ์ˆ˜์—ญid = JsonPathUtil.getId(StationApiRequester.createStationApiCall("์„ฑ์ˆ˜์—ญ"));
SectionCreateRequest ์–‘์žฌ์„ฑ์ˆ˜์—ญ = new SectionCreateRequest(์–‘์žฌ์—ญid, ์„ฑ์ˆ˜์—ญid, 100, 3);
SectionApiRequester.generateSection(์–‘์žฌ์„ฑ์ˆ˜์—ญ, ์‹ ๋ถ„๋‹น์„ id);

//when
ExtractableResponse<Response> response = PathApiRequester.getDistanceShortestPath(๊ต๋Œ€์—ญid, ์„ฑ์ˆ˜์—ญid);

//then
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value());

int fare = response.jsonPath().getInt("fare");
assertThat(fare).isEqualTo(2750);
}


/**
* Given ์ง€ํ•˜์ฒ  ๊ตฌ๊ฐ„์„ ๋“ฑ๋กํ•˜๊ณ 
* When ์ถœ๋ฐœ์—ญ๊ณผ ๋„์ฐฉ์—ญ์„ ๊ฐ™๊ฒŒํ•˜์—ฌ ๊ฒฝ๋กœ๋ฅผ ์กฐํšŒํ•˜๋ฉด
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/nextstep/subway/path/FareCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package nextstep.subway.path;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.*;

class FareCalculatorTest {

@DisplayName("10km ์ดํ•˜ ๊ธฐ๋ณธ ์š”๊ธˆ ๊ณ„์‚ฐ")
@Test
void basicFareCalculate() {
//when
int fare = FareCalculator.calculate(10);

//then
assertThat(fare).isEqualTo(1250);
}

@DisplayName("50km ์ดํ•˜ 10km ์ดˆ๊ณผ ์š”๊ธˆ ๊ณ„์‚ฐ")
@Test
void under50KmOver10KmFareCalculate() {
//when
int fare = FareCalculator.calculate(30);

//then
assertThat(fare).isEqualTo(1750);
}

@DisplayName("50km ์ดˆ๊ณผ ์š”๊ธˆ ๊ณ„์‚ฐ")
@Test
void over50KmFareCalculate() {
//when
int fare = FareCalculator.calculate(105);

//then
assertThat(fare).isEqualTo(2750);
}
}