-
Notifications
You must be signed in to change notification settings - Fork 204
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
base: seonje1
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,11 +37,11 @@ public PathResponse getPath(List<Sections> sectionsList, Station sourceStation, | |
.map(StationResponse::ofEntity) | ||
.collect(Collectors.toList()), | ||
distance, | ||
duration | ||
duration, | ||
FareCalculator.calculate(distance) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
); | ||
|
||
} catch (RuntimeException e) { | ||
e.printStackTrace(); | ||
throw new PathException("์ฐ๊ฒฐ๋์ด์์ง ์์ ์ถ๋ฐ์ญ๊ณผ ๋์ฐฉ์ญ์ ๊ฒฝ๋ก๋ ์กฐํํ ์ ์์ต๋๋ค."); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ํ์ฌ ๊ฒฝ๋ก๋ฅผ ๊ตฌํ ๋ ์๋ต์ผ๋ก ์๊ธ์ ํจ๊ป ๊ณ์ฐํ์ฌ ๋ฐํํ๊ณ ์๋ค์. ์๊ธ์ ๊ณ์ฐํ๋ ์ญํ ์ ๊ฒฝ๋ก๋ฅผ ์ฐพ๋ PathFinder ์ ๋๊ธฐ์๋ ๊ณผํ ์ฑ ์์ ๋ถ๊ณผํ ๊ฒ ์ผ๋ก ๋ณด์ฌ์ ธ์. ๊ฑฐ๋ฆฌ๋ฅผ ์ด์ฉํ์ฌ ์๊ธ์ ๊ณ์ฐํ๋ ํด๋์ค๋ฅผ ๋ง๋ค์ด ๋ฆฌํฉํ ๋ง ํด๋ณด๋ฉด ์ด๋จ๊น์? ๐ |
||
} | ||
|
||
public List<StationResponse> getStations() { | ||
|
@@ -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 |
---|---|---|
|
@@ -56,7 +56,7 @@ void setUp() { | |
/** | ||
* Given ์งํ์ฒ ๊ตฌ๊ฐ์ ๋ฑ๋กํ๊ณ | ||
* When ๊ฒฝ๋ก๋ฅผ ์กฐํํ๋ฉด | ||
* Then ์ถ๋ฐ์ญ๊ณผ ๋์ฐฉ์ญ๊น์ง์ ๊ฒฝ๋ก์ ์๋ ์ญ๊ณผ ๊ฑฐ๋ฆฌ๋ฅผ ์กฐํํ๋ค | ||
* Then ์ถ๋ฐ์ญ๊ณผ ๋์ฐฉ์ญ๊น์ง์ ์ต๋จ ๊ธธ์ด์ ๊ฒฝ๋ก๋ฅผ ์กฐํํ๋ค | ||
*/ | ||
@DisplayName("์ถ๋ฐ์ญ๊ณผ ๋์ฐฉ์ญ๊น์ง์ ์ต๋จ ๊ธธ์ด ๊ฒฝ๋ก ์กฐํ") | ||
@Test | ||
|
@@ -77,7 +77,7 @@ void showDistanceShortestPaths() { | |
/** | ||
* Given ์งํ์ฒ ๊ตฌ๊ฐ์ ๋ฑ๋กํ๊ณ | ||
* When ๊ฒฝ๋ก๋ฅผ ์กฐํํ๋ฉด | ||
* Then ์ถ๋ฐ์ญ๊ณผ ๋์ฐฉ์ญ๊น์ง์ ๊ฒฝ๋ก์ ์๋ ์ญ๊ณผ ๊ฑฐ๋ฆฌ๋ฅผ ์กฐํํ๋ค | ||
* Then ์ถ๋ฐ์ญ๊ณผ ๋์ฐฉ์ญ๊น์ง์ ์ต๋จ ์๊ฐ์ ๊ฒฝ๋ก๋ฅผ ์กฐํํ๋ค | ||
*/ | ||
@DisplayName("์ถ๋ฐ์ญ๊ณผ ๋์ฐฉ์ญ๊น์ง์ ์ต๋จ ์๊ฐ ๊ฒฝ๋ก ์กฐํ") | ||
@Test | ||
|
@@ -95,6 +95,53 @@ void showDurationShortestPaths() { | |
assertThat(distance).isEqualTo(6); | ||
} | ||
|
||
/** | ||
* Given ์งํ์ฒ ๊ตฌ๊ฐ์ ๋ฑ๋กํ๊ณ | ||
* When ๊ฒฝ๋ก๋ฅผ ์กฐํํ๋ฉด | ||
* Then ์ถ๋ฐ์ญ๊ณผ ๋์ฐฉ์ญ๊น์ง์ ์๊ธ์ ์กฐํํ๋ค | ||
*/ | ||
@DisplayName("์ถ๋ฐ์ญ๊ณผ ๋์ฐฉ์ญ๊น์ง์ ์๊ธ ์กฐํ") | ||
Comment on lines
+98
to
+103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
@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 ์ถ๋ฐ์ญ๊ณผ ๋์ฐฉ์ญ์ ๊ฐ๊ฒํ์ฌ ๊ฒฝ๋ก๋ฅผ ์กฐํํ๋ฉด | ||
|
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); | ||
} | ||
} |
There was a problem hiding this comment.
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 ๋ฅผ ์ด์ฉํ์ฌ ๋์ฑ ์ธ์ธํ๊ฒ ํ ์คํธ ์ผ์ด์ค๋ฅผ ์ถ๊ฐํ์ ๊ฒ ๊ฐ์์)