Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9debdbd
docs(README.md): add functions to implementation
Dec 19, 2020
976d4c9
feat(Gap): add Gap class to save distance and time
Dec 19, 2020
982649d
feat(Section): add Section class to save line-stations and gaps
Dec 19, 2020
a5d39e0
refactor(Gap): rearrange arguments of constructor
Dec 19, 2020
d1481a2
feat(SectionRepository): add repository class for Section
Dec 19, 2020
55d1747
feat(Constants): add class for constants
Dec 19, 2020
14da704
refactor(SectionRepositoryTest): fix implementation
Dec 19, 2020
2f73e3f
docs(README.md): add new implementations
Dec 19, 2020
fca4545
fix(StationRepository): add validation for name duplicate
Dec 19, 2020
a795163
test(StationRepository): add unit tests
Dec 19, 2020
3ed48f4
fix(LineRepository): add validation for name duplicate
Dec 19, 2020
a457c9c
test(LineRepository): add unit tests
Dec 19, 2020
c5efcf7
fix(README.md): tick commit implementation
Dec 19, 2020
96e3cae
feat(subway.util): add Initializer
Dec 19, 2020
2a23067
feat(Initializer): add station repository initializer method
Dec 19, 2020
3f42377
feat(Initializer): add methods
Dec 19, 2020
a948fcb
feat(StationRepository): add getStationByName method
Dec 19, 2020
94260a2
feat(LineRepository): add getLineByName method
Dec 19, 2020
395e88b
feat(SectionRepository): add addSectionByName and corresponding methods
Dec 19, 2020
cdc60a8
feat(Initializer) add section repository initializer method
Dec 19, 2020
a1fa3e7
feat(ErrorMessage) add enum for error messages
Dec 19, 2020
e804c59
refactor(subway.domain): add error messages
Dec 19, 2020
405d453
refactor(StationRepository): add validateStationNameExist method
Dec 19, 2020
006deb9
feat(ConsoleOutput): add console output class
Dec 19, 2020
8eb7b37
feat(ConsoleInput): add console input class
Dec 19, 2020
832c5ca
feat(MainMenu): add main menu class
Dec 19, 2020
d7b250f
feat(MainMenuEnum): add main menu enum
Dec 19, 2020
60e2e0b
feat(ConsoleProgram): add console program class
Dec 19, 2020
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@

<br>

## 구현할 기능

- [X] 특정 구간 사이의 차이(시간, 거리)를 저장할 클래스
- [X] 구간 클래스
- [X] 구간 저장소 클래스
- [X] 역 저장소 내에 중복된 이름이 있는지 검증하는 기능
- [X] 노선 저장소 내에 중복된 이름이 있는지 검증하는 기능
- [X] 초기 역/노선/구간 정보를 저장할 상수 클래스
- [X] 초기 역을 설정하는 기능
- [X] 초기 노선을 설정하는 기능
- [X] 초기 구간 정보를 설정하는 기능
- [ ] 출발/도착 역을 입력받는 기능
- [ ] 입력받은 출발/도착 역 이름이 역 저장소에 있는지 검증하는 기능
- [ ] 경로를 탐색하려는 A 역과 B 역이 같은지 검증하는 기능
- [ ] A 역에서 B 역까지의 경로를 최단 거리로 조회하는 기능
- [ ] A 역에서 B 역까지의 경로를 최소 시간으로 조회하는 기능
- [ ] 조회한 경로를 총 거리, 총 소요 시간과 함께 출력하는 기능

## 🚀 기능 요구사항

> 프리코스 3주차 미션에서 사용한 코드를 참고해도 무관하다.
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/subway/domain/Gap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package subway.domain;

public class Gap {

private final int distance;
private final int time;

public Gap(final int distance, final int time) {
this.distance = distance;
this.time = time;
}

public int getTime() {
return time;
}

public int getDistance() {
return distance;
}
}
17 changes: 16 additions & 1 deletion src/main/java/subway/domain/LineRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,30 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import subway.message.ErrorMessage;

public class LineRepository {

private static final List<Line> lines = new ArrayList<>();

public static List<Line> lines() {
return Collections.unmodifiableList(lines);
}

public static void addLine(Line line) {
public static void validateLineNameDuplicate(String lineName) throws IllegalArgumentException {
if (lineNameExists(lineName)) {
throw new IllegalArgumentException(
ErrorMessage.LINE_REPOSITORY_LINE_ALREADY_EXIST.toString()
);
}
}

private static boolean lineNameExists(String name) {
return lines.stream().anyMatch(line -> line.getName().equals(name));
}

public static void addLine(Line line) throws IllegalArgumentException {
validateLineNameDuplicate(line.getName());
lines.add(line);
}

Expand Down
92 changes: 92 additions & 0 deletions src/main/java/subway/domain/Section.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package subway.domain;

import java.util.List;
import subway.message.ErrorMessage;

public class Section {

public static final int MIN_STATIONS_SIZE = 2;
public static final int MIN_GAPS_SIZE = 1;

private final Line line;
private final List<Station> stations;
private final List<Gap> gaps;

public Section(Line line, List<Station> stations, List<Gap> gaps)
throws IllegalArgumentException {
validateStationsSize(stations);
validateGapsSize(gaps);
validateDifferenceBetweenStationsSizeAndGapsSize(stations, gaps);
this.line = line;
this.stations = stations;
this.gaps = gaps;
}

private void validateStationsSize(List<Station> stations) throws IllegalArgumentException {
if (stations.size() < MIN_STATIONS_SIZE) {
throw new IllegalArgumentException(
ErrorMessage.SECTION_STATIONS_TOO_SMALL.toString()
);
}
}

private void validateGapsSize(List<Gap> gaps) throws IllegalArgumentException {
if (gaps.size() < MIN_GAPS_SIZE) {
throw new IllegalArgumentException(
ErrorMessage.SECTION_GAPS_TOO_SMALL.toString()
);
}
}

private void validateDifferenceBetweenStationsSizeAndGapsSize(List<Station> stations,
List<Gap> gaps) throws IllegalArgumentException {
if (stations.size() != gaps.size() + 1) {
throw new IllegalArgumentException(
ErrorMessage.SECTION_GAPS_STATIONS_DIFFERENCE.toString()
);
}
}

private void validateStationIndex(final int index) throws IllegalArgumentException {
if (index < 0 || index >= stations.size()) {
throw new IllegalArgumentException(
ErrorMessage.SECTION_STATION_INDEX_OUT_OF_RANGE.toString()
);
}
}

public Station getStationByIndex(final int index) throws IllegalArgumentException {
validateStationIndex(index);
return stations.get(index);
}

private void validateIndexesDifference(final int indexAhead, final int indexBehind)
throws IllegalArgumentException {
if (Integer.min(indexAhead, indexBehind) + 1 != Integer.max(indexAhead, indexBehind)) {
throw new IllegalArgumentException(
ErrorMessage.SECTION_STATION_INDEXES_DIFFERENCE.toString()
);
}
}

private void validateIndexesRange(final int indexAhead, final int indexBehind)
throws IllegalArgumentException {
if (Integer.min(indexAhead, indexBehind) < 0 ||
Integer.max(indexAhead, indexBehind) >= stations.size()) {
throw new IllegalArgumentException(
ErrorMessage.SECTION_STATION_INDEX_OUT_OF_RANGE.toString()
);
}
}

public Gap getGapByTwoIndexes(final int indexAhead, final int indexBehind)
throws IllegalArgumentException {
validateIndexesDifference(indexAhead, indexBehind);
validateIndexesRange(indexAhead, indexBehind);
return gaps.get(Integer.min(indexAhead, indexBehind));
}

public Line getLine() {
return line;
}
}
54 changes: 54 additions & 0 deletions src/main/java/subway/domain/SectionRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package subway.domain;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import subway.message.ErrorMessage;

public class SectionRepository {

private static final List<Section> sections = new ArrayList<>();

public static List<Section> sections() {
return Collections.unmodifiableList(sections);
}

private static void validateSectionLineNameDuplicate(String lineName)
throws IllegalArgumentException {
if (sections.stream().anyMatch(section -> section.getLine().getName().equals(lineName))) {
throw new IllegalArgumentException(
ErrorMessage.SECTION_REPOSITORY_LINE_ALREADY_EXIST.toString()
);
}
}

public static void addSection(Section section) throws IllegalArgumentException {
validateSectionLineNameDuplicate(section.getLine().getName());
sections.add(section);
}

public static void addSectionByNames(String lineName, String[] stationNames, List<Gap> gaps)
throws IllegalArgumentException {
LineRepository.validateLineNameDuplicate(lineName);
final Line sectionLine = LineRepository.lines().stream()
.filter(line -> line.getName().equals(lineName)).findFirst().get();
final List<Station> stations = new LinkedList<>();
Arrays.stream(stationNames).forEach(stationName -> {
StationRepository.validateStationNameDuplicate(stationName);
stations.add(StationRepository.stations().stream()
.filter(station -> station.getName().equals(stationName)).findFirst().get());
});
addSection(new Section(sectionLine, stations, gaps));
}

public static boolean deleteSectionByLineName(String lineName) {
return sections.removeIf(section -> Objects.equals(section.getLine().getName(), lineName));
}

public static void deleteAll() {
sections.clear();
}
}
27 changes: 26 additions & 1 deletion src/main/java/subway/domain/StationRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,40 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import subway.message.ErrorMessage;

public class StationRepository {

private static final List<Station> stations = new ArrayList<>();

public static List<Station> stations() {
return Collections.unmodifiableList(stations);
}

public static void addStation(Station station) {
private static boolean stationNameExists(String name) {
return stations.stream().anyMatch(station -> station.getName().equals(name));
}

public static void validateStationNameDuplicate(String stationName)
throws IllegalArgumentException {
if (stationNameExists(stationName)) {
throw new IllegalArgumentException(
ErrorMessage.STATION_REPOSITORY_STATION_ALREADY_EXIST.toString()
);
}
}

public static void validateStationNameExist(String stationName)
throws IllegalArgumentException {
if (!stationNameExists(stationName)) {
throw new IllegalArgumentException(
ErrorMessage.STATION_REPOSITORY_STATION_DOES_NOT_EXIST.toString()
);
}
}

public static void addStation(Station station) throws IllegalArgumentException {
validateStationNameDuplicate(station.getName());
stations.add(station);
}

Expand Down
58 changes: 58 additions & 0 deletions src/main/java/subway/message/ErrorMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package subway.message;

import subway.domain.Section;

public enum ErrorMessage {

// 역 관련 에러 메시지
// STATION_INVALID_NAME_LENGTH(String.format("역 이름은 %d자 이상이어야 합니다.", MIN_STATION_NAME_LENGTH)),

// 노선 관련 에러 메시지
// LINE_INVALID_NAME_LENGTH(String.format("노선 이름은 %d자 이상이어야 합니다.", MIN_LINE_NAME_LENGTH)),
LINE_STATION_INDEX_OUT_OF_RANGE("역을 추가하려는 위치가 할당될 수 없는 위치입니다."),
LINE_STATION_DOES_NOT_EXIST("제거하려는 역은 해당 노선에 없는 역입니다."),

// 구간 관련 에러 메시지
SECTION_ITEM_DUPLICATED("구간에는 같은 역이 여러 개 존재할 수 없습니다."),
SECTION_STATIONS_TOO_SMALL(String.format("구간에는 최소 %d개의 역이 존재하여야 합니다.", Section.MIN_STATIONS_SIZE)),
SECTION_GAPS_TOO_SMALL(String.format("구간에는 최소 %d개의 간격이 존재하여야 합니다.", Section.MIN_GAPS_SIZE)),
SECTION_STATION_ALREADY_EXIST("추가하려는 역은 이미 해당 구간에 포함된 역입니다."),
SECTION_GAPS_STATIONS_DIFFERENCE("구간의 크기는 역의 크기보다 1 작아야 합니다."),
SECTION_STATION_INDEX_OUT_OF_RANGE("색인의 범위가 구간 내 역 갯수 범위를 초과했습니다."),
SECTION_STATION_INDEXES_DIFFERENCE("두 색인의 차는 1이어야 합니다."),

// 역 저장소 관련 에러 메시지
STATION_REPOSITORY_STATION_ALREADY_EXIST("이미 등록된 역 이름입니다."),
STATION_REPOSITORY_STATION_DOES_NOT_EXIST("존재하지 않는 역 이름입니다."),
STATION_REPOSITORY_STATION_HAS_PARENT("구간에서 사용중인 역은 삭제할 수 없습니다."),
STATION_REPOSITORY_EMPTY("역 저장소가 비어 있습니다."),

// 노선 저장소 관련 에러 메시지
LINE_REPOSITORY_LINE_ALREADY_EXIST("이미 등록된 노선 이름입니다."),
LINE_REPOSITORY_LINE_DOES_NOT_EXIST("존재하지 않는 노선 이름입니다."),
LINE_REPOSITORY_EMPTY("노선 저장소가 비어 있습니다."),

// 구간 저장소 관련 에러 메시지
SECTION_REPOSITORY_LINE_ALREADY_EXIST("추가하려는 노선은 이미 구간 내에 존재합니다."),

// 메뉴 선택 관련 에러 메시지
MENU_INVALID_SELECTION("선택할 수 없는 기능입니다."),

// 입력 관련 에러 메시지
INPUT_EMPTY_STRING("빈 문장은 입력할 수 없습니다."),
INPUT_INVALID_STRING("올바르지 않은 입력 형식입니다."),

;

private final String message;

ErrorMessage(String message) {
this.message = message;
}

@Override
public String toString() {
return message;
}

}
22 changes: 22 additions & 0 deletions src/main/java/subway/ui/ConsoleInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package subway.ui;

import java.util.Scanner;
import subway.message.ErrorMessage;

public class ConsoleInput {

private ConsoleInput() {
}

public static String scanLine(final Scanner scanner) throws IllegalArgumentException {
final String lineInput = scanner.nextLine();
validateLineInputNotEmpty(lineInput);
return lineInput;
}

private static void validateLineInputNotEmpty(final String lineInput) {
if (lineInput.isEmpty()) {
throw new IllegalArgumentException(ErrorMessage.INPUT_EMPTY_STRING.toString());
}
}
}
38 changes: 38 additions & 0 deletions src/main/java/subway/ui/ConsoleOutput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package subway.ui;

public class ConsoleOutput {
private static final String GENERAL_MESSAGE_HEADER = "## ";
private static final String ERROR_MESSAGE_HEADER = "[ERROR] ";
private static final String INFO_MESSAGE_HEADER = "[INFO] ";

private static final String GENERAL_MESSAGE_MAIN = "메인 화면";
private static final String GENERAL_MESSAGE_PATH = "경로 기준";

private static final String GENERAL_MESSAGE_FUNCTION = "원하는 기능을 선택하세요.";

private ConsoleOutput() {
}

public static void printGeneralMessage(String message) {
System.out.println();
System.out.println(GENERAL_MESSAGE_HEADER + message);
}

public static void printErrorMessage(String errorMessage) {
System.out.println();
System.out.println(ERROR_MESSAGE_HEADER + errorMessage);
}

public static void printInfoMessage(String infoMessage) {
System.out.println(INFO_MESSAGE_HEADER + infoMessage);
}


public static void printMainMenu() {
printGeneralMessage(GENERAL_MESSAGE_MAIN);
for (MainMenuEnum mainMenuEnum : MainMenuEnum.values()) {
System.out.println(mainMenuEnum.getShortcut() + ". " + mainMenuEnum.getName());
}
printGeneralMessage(GENERAL_MESSAGE_FUNCTION);
}
}
Loading