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

Week1 step3 #724

Open
wants to merge 18 commits into
base: sanghou
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Reduce error logic
Sanghou committed Mar 1, 2023
commit 32d0bde751d2c11fa43024d791b63b55f508d16a
6 changes: 6 additions & 0 deletions src/main/java/subway/sections/Sections.java
Original file line number Diff line number Diff line change
@@ -22,10 +22,16 @@ public Sections() {
}

public Station getUpStation() {
if (sections.isEmpty()) {
return null;
}
return sections.get(0).getUpStation();
}

public Station getDownStation() {
if (sections.isEmpty()) {
return null;
}
return sections.get(sections.size() - 1).getDownStation();
}

10 changes: 3 additions & 7 deletions src/main/java/subway/sections/SectionsValidator.java
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ static void validateRemoveSection(Sections sections, Station station) {
throw new IllegalStateException("노선의 구간은 한 개 이상 남아야 합니다.");
}

if (isStationNotMatchedDownStation(sections, station)) {
if (isStationNotLastDownStation(sections, station)) {
throw new IllegalArgumentException("노선의 중간 구간들은 지울 수 없습니다.");
}
}
@@ -29,14 +29,10 @@ private static boolean isUpStationNotMatchesLastDownStation(Sections sections,Se
}

private static boolean isAlreadyAddedSection(Sections sections, Section section) {
return sections.isStationContained(section.getDownStation()) && !isLastDownStation(sections, section.getDownStation());
return sections.isStationContained(section.getDownStation()) && isStationNotLastDownStation(sections, section.getDownStation());
}

private static boolean isStationNotMatchedDownStation(Sections sections, Station station) {
private static boolean isStationNotLastDownStation(Sections sections, Station station) {
return !Objects.equals(sections.getDownStation().getId(), station.getId());
}

private static boolean isLastDownStation(Sections sections, Station station) {
return Objects.equals(sections.getDownStation().getId(), station.getId());
}
}