-
Notifications
You must be signed in to change notification settings - Fork 203
도전 - 가장 빠른 도착 경로 조회 #230
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
Open
gaegulgaegul
wants to merge
14
commits into
next-step:gaegulgaegul
Choose a base branch
from
gaegulgaegul:challenge
base: gaegulgaegul
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
도전 - 가장 빠른 도착 경로 조회 #230
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
45489c4
test 지하철 노선 시간 정보 인수 테스트 추가
lms-elonsoft 22dbe1d
feat 지하철 노선 시간 정보 관리 기능 추가
lms-elonsoft 9de7f04
test 가장 빠른 도착 경로 조회 인수 테스트 추가
lms-elonsoft a202fd4
test 모든 경로 조회 study
lms-elonsoft 53babb3
fix 지하철 노선 시간 관리 파라미터 데이터 타입 변경
lms-elonsoft e247492
feat 지하철 가장 빠른 도착 경로 조회 기능 추가
lms-elonsoft 00736ec
test 도착시간 관련 단위 테스트 추가
lms-elonsoft 2b73ac2
chore 코드 정리
lms-elonsoft 2fe9fa6
refactor EntitySupplier 적용 리팩터링
lms-elonsoft bad7298
refactor 요금 정책 체인 형식의 클래스 구성으로 변경
lms-elonsoft 107ac85
refactor 일급 컬렉션 적용
lms-elonsoft c1a310c
refactor 메소드 템플릿 패턴 적용
lms-elonsoft 8ae08fe
refactor 최단시간 도착경로 조회 depth 리팩터링
lms-elonsoft fd1b61c
chore 코드 정리
lms-elonsoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package nextstep.subway.domain; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static nextstep.support.entity.Formatters.DATE_TIME_PATH; | ||
|
||
public class ArrivalTime { | ||
private Sections sections; | ||
private Lines lines; | ||
private LocalDateTime dateTime; | ||
private LocalTime startTime; | ||
|
||
private LocalDateTime currentDateTime; | ||
private Line currentLine = null; | ||
|
||
public ArrivalTime(List<Section> sections, List<Line> lines, String time) { | ||
this.sections = new Sections(sections); | ||
this.lines = new Lines(lines); | ||
this.dateTime = LocalDateTime.parse(time, DATE_TIME_PATH); | ||
this.startTime = findLine(sections.get(0)).getStartTime(); | ||
} | ||
|
||
public LocalDateTime value() { | ||
return getTimeOfFastestPath(dateTime); | ||
} | ||
|
||
private LocalDateTime getTimeOfFastestPath(LocalDateTime dateTime) { | ||
this.currentDateTime = dateTime; | ||
|
||
for (Section section : sections.getSections()) { | ||
if (confirmPathCondition(section)) { | ||
return getTimeOfFirstPath(); | ||
} | ||
} | ||
|
||
return currentDateTime; | ||
} | ||
|
||
private boolean confirmPathCondition(Section section) { | ||
Line line = findLine(section); | ||
boolean notSameLine = !line.equals(currentLine); | ||
|
||
setupCurrentDateTime(notSameLine, line, section); | ||
if (notSameLine && goingLastTime(line.getEndTime())) { | ||
return true; | ||
} | ||
setupCurrentLine(notSameLine, line); | ||
return false; | ||
} | ||
|
||
private void setupCurrentDateTime(boolean notSameLine, Line line, Section section) { | ||
if (notSameLine) { | ||
this.currentDateTime = calculateTimeInBeforeStations(line, section); | ||
} | ||
this.currentDateTime = currentDateTime.plusMinutes(section.getDuration()); | ||
} | ||
|
||
private void setupCurrentLine(boolean notSameLine, Line line) { | ||
if (notSameLine) { | ||
this.currentLine = line; | ||
} | ||
} | ||
|
||
private LocalDateTime getTimeOfFirstPath() { | ||
LocalDateTime nextDayAtStartDateTime = changeTime(dateTime.plusDays(1), startTime); | ||
return getTimeOfFastestPath(nextDayAtStartDateTime); | ||
} | ||
|
||
private boolean goingLastTime(LocalTime endTime) { | ||
return currentDateTime.isAfter(changeTime(currentDateTime, endTime)); | ||
} | ||
|
||
private LocalDateTime calculateTimeInBeforeStations(Line currentLine, Section section) { | ||
LocalDateTime targetTime = changeTime(currentDateTime, currentLine.getStartTime()); | ||
LocalDateTime fastestDepartureTime = findFastestDepartureTime(currentDateTime, targetTime, currentLine.getIntervalTime()); | ||
int beforeDuration = getBeforeDuration(currentLine, section); | ||
return fastestDepartureTime.plusMinutes(beforeDuration); | ||
} | ||
|
||
private Line findLine(Section section) { | ||
return lines.indexOf(section); | ||
} | ||
|
||
private LocalDateTime changeTime(LocalDateTime dateTime, LocalTime time) { | ||
return LocalDateTime.of(dateTime.toLocalDate(), time); | ||
} | ||
|
||
private int getBeforeDuration(Line line, Section currentSection) { | ||
List<Section> beforeSections = new ArrayList<>(); | ||
for (Section section : line.getSections()) { | ||
if (section.equals(currentSection)) { | ||
break; | ||
} | ||
beforeSections.add(section); | ||
} | ||
|
||
return new Sections(beforeSections).totalDuration(); | ||
} | ||
|
||
private LocalDateTime findFastestDepartureTime(LocalDateTime referenceTime, LocalDateTime targetTime, int interval) { | ||
if (referenceTime.isAfter(targetTime)) { | ||
return findFastestDepartureTime(referenceTime, targetTime.plusMinutes(interval), interval); | ||
} | ||
return targetTime; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
일급 컬렉션이 구현되어있다면 lines.indexOf(section)도 가능하겠죠
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.
일급 컬렉션으로 구현하니 코드가 간결해지네요 ㅎㅎ