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

[지하철 노선도 미션] 김예진 미션 제출합니다. #99

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
266f38d
docs : 기능 명세 작성
jineeee Dec 12, 2020
d85140b
feat : 입력받는 구조 진행, 역 등록 기능
jineeee Dec 12, 2020
8aa766a
feat : 역 등록 시 중복 이름 확인
jineeee Dec 12, 2020
5cd00e6
feat : 노선 등록, 삭제 기능 작업
jineeee Dec 12, 2020
8c2c1d8
feat : 노선 등록, 삭제 예외 처리
jineeee Dec 13, 2020
a4a2b09
docs : 예외 사항 추가
jineeee Dec 13, 2020
ec956b2
feat : 구간 등록 작업
jineeee Dec 15, 2020
f38ee79
fix : stationReposotory 파라미터 수정
jineeee Dec 15, 2020
49c679f
fix : stationReposotory 파라미터 수정에 따른 에러 수정
jineeee Dec 15, 2020
40ec21e
fix : SectionRepository에서 노선도를 출력하도록 수정
jineeee Dec 15, 2020
5f284d6
feat : 구간 삭제 작업
jineeee Dec 15, 2020
52cc92a
feat : 구간 등록 예외 처리 작업
jineeee Dec 15, 2020
48c605b
docs : 에러 상황 추가 및 구체화
jineeee Dec 15, 2020
9822f97
fix : 구간 관리 기능 출력과 기능 분리
jineeee Dec 15, 2020
a402a0d
fix : 역 관리, 노선 관리 기능 분리
jineeee Dec 15, 2020
58f773d
docs : 에러 사항 추가
jineeee Dec 15, 2020
6380741
fix : Constant 클래스 수정
jineeee Dec 15, 2020
d1b3dbe
fix : error 출력 내용 수정
jineeee Dec 15, 2020
2835ce1
fix : 이름 길이 예외 처리 수정
jineeee Dec 15, 2020
c0a3ca2
fix : 역이 2개 이하인 구간은 삭제할 수 없도록 수정
jineeee Dec 15, 2020
e305ecb
fix : line 클래스에서 stations 정보를 관리하도록 수정
jineeee Dec 15, 2020
0919fbf
fix : line 정리
jineeee Dec 15, 2020
f1fd68e
feat : 초기 노선, 역 세팅 작업
jineeee Dec 15, 2020
a9490b3
fix : print 함수 이동
jineeee Dec 15, 2020
2b0fc52
fix : 패키지 구조 변경
jineeee Dec 15, 2020
3cd2815
docs : 클래스 정의 작성
jineeee Dec 15, 2020
99d5706
fix : 객체 접근 제한자 수정
jineeee Dec 15, 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
60 changes: 56 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,58 @@

<br>

## 🕊 기능 정의

- 메인 화면을 출력한다
- 원하는 기능을 입력받아 실행한다.
- [ERROR] 목록에 없는 기능이라면 에러 사항을 출력한다.
- 1. 역 관리
- 역 등록 : 사용자에게 입력받은 역을 등록한다.
- 역 삭제 : 사용자에게 입력받은 역을 삭제한다.
- 역 조회 : 현재 등록된 역의 전체 목록을 출력한다.
- [ERROR] 역 등록 시 역 이름이 2글자 이하이면 에러 사항을 출력한다.
- [ERROR] 역 등록 시 중복된 역 이름이라면 에러 사항을 출력한다.
- [ERROR] 역 삭제 시 삭제해야 하는 역이 존재하지 않는다면 에러 사항을 출력한다.
- 2. 노선 관리
- 노선 등록 : 노선의 이름, 상행 종점역, 하행 종점역을 입력받아 노선을 등록한다.
- 노선 삭제 : 현재 등록된 노선의 전체 목록을 출력한다.
- 노선 조회 : 사용자에게 입력받은 노선을 삭제한다.
- [ERROR] 노선 등록 시 노선 이름이 2글자 이하이면 에러 사항을 출력한다.
- [ERROR] 노선 등록 시 중복된 노선 이름이라면 에러 사항을 출력한다.
- [ERROR] 노선 등록 시 상행 종점역, 하행 종점역으로 입력된 역이 등록되지 않은 역인 경우 에러 사항을 출력한다.
- [ERROR] 노선 삭제 시 삭제해야 하는 노선이 존재하지 않는다면 에러 사항을 출력한다.
- 3. 구간 관리
- 구간 등록 : 노선과 역 이름을 입력받아 구간을 등록한다.
- 구간 삭제 : 노선과 역 이름을 입력받아 구간을 삭제한다.
- [ERROR] 구간 등록 시 중복된 노선과 역 이름이라면 에러 사항을 출력한다.
- [ERROR] 구간 등록 시 현재 노선의 범위 밖이라면 에러 사항을 출력한다.
- [ERROR] 구간 삭제 시 삭제해야 하는 노선과 역 이름이 존재하지 않느다면 에러 사항을 출력한다.
- [ERROR] 구간 삭제 시 삭제해햐 하는 역이 포함된 노선에 역이 2개 이하로 있다면 에러 사항을 출력한다.
- 4. 지하철 노선도 출력
- 전체 노선도 목록을 출력한다.
- Q. 종료

<br>

## ⭐️ 클래스 정의

- *domain*
- Input : 사용자에게 기능을 선택받는 메인 화면을 출력하는 클래스
- Station : 역 이름을 가지는 객체 클래스
- Line : 노선 이름과 노선에 포함된 역 정보를 가지고 관리하는 클래스
- Constant : 프로그램 내에서 사용되는 상수를 가지고 있는 클래스
- *repository*
- StationRepository : 역 등록, 삭제를 수행하는 함수를 가지는 클래스
- LineRepository : 노선 등록, 삭제를 수행하는 함수를 가지는 클래스
- SectionRepository : 구간 등록, 삭제를 수행하는 함수를 가지는 클래스
- *controller*
- StationController : 역 등록, 삭제에 대한 결과를 출력하고, 전체 역 조회를 수행하는 클래스
- LineController : 노선 등록, 삭제에 대한 결과를 출력하고, 전체 노선 조회를 수행하는 클래스
- SectionController : 구간 등록, 삭제에 대한 결과를 출력하고, 전체 지하철 노선과 포함된 역 조회를 수행하는 클래스
- *Application* : Input 클래스의 메소드를 호출해 프로그램을 시작하고 flag로 프로그램 종료를 관리하는 클래스

<br>

## 🚀 기능 요구사항

### 초기 설정
Expand All @@ -17,10 +69,10 @@
- 2호선: 교대역 - 강남역 - 역삼역
- 3호선: 교대역 - 남부터미널역 - 양재역 - 매봉역
- 신분당선: 강남역 - 양재역 - 양재시민의숲역
```
```

<img src="image/domain.png" width="500">

### 지하철 역 관련 기능
- 지하철 역을 등록하고 삭제할 수 있다. (단, 노선에 등록된 역은 삭제할 수 없다)
- 중복된 지하철 역 이름이 등록될 수 없다.
Expand Down Expand Up @@ -410,7 +462,7 @@ public class Application {
- 제공하는 각 클래스의 기본 생성자를 추가할 수 없다.
- 필드(인스턴스 변수)인 name의 접근 제어자 private을 변경할 수 없다.
- 가능하면 setter 메소드(ex. setXXX)를 추가하지 않고 구현한다.

```java
public class Station {
private String name;
Expand All @@ -434,7 +486,7 @@ public class Station {
- 추가로 생성되는 객체에 대해서 XXXRepository 네이밍으로 저장 클래스를 추가할 수 있다.
- 객체들의 상태를 관리하기 위해서 XXXRepository 클래스를 활용해 저장 로직을 구현해야 한다.
- 필요에 따라 자유롭게 수정이 가능하다.

```java
public class StationRepository {
private static final List<Station> stations = new ArrayList<>();
Expand Down
29 changes: 28 additions & 1 deletion src/main/java/subway/Application.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
package subway;

import subway.domain.*;
import subway.repository.LineRepository;
import subway.repository.StationRepository;

import java.util.Arrays;
import java.util.Scanner;

public class Application {
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
// TODO: 프로그램 구현
initialSetting();
Input input = new Input(scanner);
while (!input.quit) {
input.mainInput();
}
}

static void initialSetting() {
StationRepository stationRepository = new StationRepository();
LineRepository lineRepository = new LineRepository();
Station station1 = new Station("교대역");
Station station2 = new Station("강남역");
Station station3 = new Station("역삼역");
Station station4 = new Station("남부터미널역");
Station station5 = new Station("양재역");
Station station6 = new Station("양재시민의숲역");
Station station7 = new Station("매봉역");
stationRepository.addStations(Arrays.asList(station1, station2, station3, station4, station5, station6, station7));
Line line1 = new Line("2호선", station1, station2, station3);
Line line2 = new Line("3호선", station1, station4, station5, station7);
Line line3 = new Line("신분당선", station2, station5, station6);
lineRepository.addLines(Arrays.asList(line1, line2, line3));

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

public class Constant {
public static final int MIN_LENGTH = 2;
public static final String FIRST_COMMAND = "1";
public static final String SECOND_COMMAND = "2";
public static final String THIRD_COMMAND = "3";
public static final String FORTH_COMMAND = "4";
public static final String QUIT_COMMAND = "Q";
public static final String BACK_COMMAND = "B";

public static final String ERROR_PREFIX = "[ERROR]";
public static final String INFO_PREFIX = "[INFO]";

public static final String MAIN_ANNOUNCEMENT = "\n## 메인 화면\n1. 역 관리\n2. 노선 관리\n3. 구간 관리\n4. 지하철 노선도 출력\nQ. 종료\n";
public static final String STATION_ANNOUNCEMENT = "\n## 역 관리 화면\n1. 역 등록\n2. 역 삭제\n3. 역 조회\nB. 돌아가기";
public static final String LINE_ANNOUNCEMENT = "\n## 노선 관리 화면\n1. 노선 등록\n2. 노선 삭제\n3. 노선 조회\nB. 돌아가기";
public static final String SECTION_ANNOUNCEMENT = "\n## 구간 관리 화면\n1. 구간 등록\n2. 구간 삭제\nB. 돌아가기\n";
public static final String INPUT_ANNOUNCEMENT = "## 원하는 기능을 선택하세요.\n";
public static final String INPUT_ERROR_ANNOUNCEMENT = "선택할 수 없는 기능입니다.";
public static final String NO_EXIST_INFO = "등록되지 않은 역 또는 노선입니다.\n";
public static final String LINE_STATION_POSTFIX = "종점역 이름을 입력하세요.";
public static final String DUPLICATE_STATION_NAME = "이미 등록된 역입니다.\n";

public static final String ADD_PREFIX = "\n## 등록할";
public static final String DELETE_PREFIX = "\n## 삭제할";
public static final String NAME_POSTFIX = "이름을 입력해주세요.";
public static final String FAIL = "실패했습니다.";
public static final String NAME_LENGTH_SHORT = "이름은 2글자 이상이어야 합니다.";
}
48 changes: 48 additions & 0 deletions src/main/java/subway/domain/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package subway.domain;

import subway.view.LineController;
import subway.view.SectionController;
import subway.view.StationController;

import java.util.Scanner;

public class Input {
Scanner scanner;
private StationController stationController;
private LineController lineController;
private SectionController sectionController;
public static Boolean quit = false;

public Input(Scanner scanner) {
this.scanner = scanner;
stationController = new StationController(scanner);
lineController = new LineController(scanner);
sectionController = new SectionController(scanner);
}

public void mainInput() {
System.out.print(String.join("\n", Constant.MAIN_ANNOUNCEMENT, Constant.INPUT_ANNOUNCEMENT));
String flag = scanner.next();
if (flag.equals(Constant.FIRST_COMMAND)) {
stationController.printSelection();
return;
}
if (flag.equals(Constant.SECOND_COMMAND)) {
lineController.printSelection();
return;
}
if (flag.equals(Constant.THIRD_COMMAND)) {
sectionController.printSelection();
return;
}
if (flag.equals(Constant.FORTH_COMMAND)) {
sectionController.readSections();
return;
}
if (flag.equals(Constant.QUIT_COMMAND)) {
quit = true;
return;
}
System.err.println(String.join(" ", Constant.ERROR_PREFIX, Constant.INPUT_ERROR_ANNOUNCEMENT));
}
}
23 changes: 22 additions & 1 deletion src/main/java/subway/domain/Line.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
package subway.domain;

import java.util.ArrayList;
import java.util.List;

public class Line {
private String name;
public List<Station> stations = new ArrayList<Station>();

public Line(String name) {
this.name = name;
}

public Line(String name, Station... stations) {
this.name = name;
for(int i=0; i< stations.length; i++){
this.stations.add(stations[i]);
}
}

public String getName() {
return name;
}

// 추가 기능 구현
public boolean existStation(String stationName) {
return stations.stream().anyMatch(s -> s.getName().equals(stationName));
}

public void addStation(String stationName, int order) {
stations.add(order-1, new Station(stationName));
}

public boolean checkStationSize() {
return stations.size() <= Constant.MIN_LENGTH;
}
}
22 changes: 0 additions & 22 deletions src/main/java/subway/domain/LineRepository.java

This file was deleted.

1 change: 0 additions & 1 deletion src/main/java/subway/domain/Station.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ public String getName() {
return name;
}

// 추가 기능 구현
}
22 changes: 0 additions & 22 deletions src/main/java/subway/domain/StationRepository.java

This file was deleted.

46 changes: 46 additions & 0 deletions src/main/java/subway/repository/LineRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package subway.repository;

import subway.domain.Line;
import subway.domain.Station;
import subway.repository.StationRepository;

import java.util.*;

public class LineRepository {
public static final List<Line> lines = new ArrayList<>();
private static StationRepository stationRepository = new StationRepository();

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

public static void addLines(List<Line> lineList){
lineList.forEach(line -> lines.add(line));
}

public static void addLine(String lineName, String upwardName, String downwardName) {
if (stationRepository.checkNameLength(lineName)) {
throw new IllegalStateException();
}
if (checkExistLine(lineName) || !stationRepository.checkExistStation(upwardName)
|| !stationRepository.checkExistStation(downwardName) || upwardName.equals(downwardName)) {
throw new IllegalArgumentException();
}
Line line = new Line(lineName);
line.stations.add(new Station(upwardName));
line.stations.add(new Station(downwardName));
lines.add(line);
}

public static boolean deleteLineByName(String lineName) {
if (!checkExistLine(lineName)) {
throw new IllegalArgumentException();
}
return lines.removeIf(line -> Objects.equals(line.getName(), lineName));
}

public static boolean checkExistLine(String lineName) {
return lines.stream().anyMatch(o -> o.getName().equals(lineName));
}

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

import subway.domain.Line;

import java.util.*;

public class SectionRepository {
private static LineRepository lineRepository = new LineRepository();
private static StationRepository stationRepository = new StationRepository();

public static void addSection(String lineName, String stationName, int order) {
if (!validate(lineName, stationName)) {
throw new IllegalArgumentException();
}
try {
Line targetLine = lineRepository.lines.stream().filter(l -> lineName.equals(l.getName())).findFirst().get();
if (targetLine.existStation(stationName)) {
throw new IllegalStateException();
}
targetLine.addStation(stationName, order);
} catch (IndexOutOfBoundsException e) {
throw new IndexOutOfBoundsException();
}
}

public static boolean deleteSection(String lineName, String stationName) {
if (!validate(lineName, stationName)) {
throw new IllegalArgumentException();
}
Line line = lineRepository.lines.stream().filter(l -> lineName.equals(l.getName())).findFirst().get();
if (line.checkStationSize()) {
throw new IllegalStateException();
}
return line.stations.removeIf(s -> Objects.equals(s.getName(), stationName));
}

static boolean validate(String lineName, String stationName) {
if (!lineRepository.checkExistLine(lineName) || !stationRepository.checkExistStation(stationName)) {
return false;
}
return true;
}

}
Loading