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

[지하철노선관리] 장세환 미션 제출합니다. #9

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,39 @@ public class StationRepository {
- [AngularJS Commit Message Conventions](https://gist.github.com/stephenparish/9941e89d80e2bc58a153) 참고해 commit log를 남긴다.
- [프리코스 과제 제출 문서](https://github.com/woowacourse/woowacourse-docs/tree/master/precourse) 절차를 따라 미션을 제출한다.
- [프리코스 과제 FAQ](https://github.com/woowacourse/woowacourse-docs/tree/master/precourse/faq) 문서를 참고하여 진행할 수 있다.
<br>

## 구현 기능 목록
- 주어진 정보대로 초기설정
- 하드코딩하지 말고 상수로 넣을 것.
- 역, 노선 중복되지 않고 2글자 이상일 경우 추가하는 기능
- 노선의 경우 상, 하행 각 종점 입력받는 기능
- 역, 노선 삭제하는 기능
- 노선에 등록된 역은 삭제 불가
- 구간 추가 기능
- 추가할때 역이름, 순서 입력
- 구간 삭제 기능
- 노선에 2개의 역밖에 남지 않았을 경우 삭제 불가
- 메뉴 기능 구현
- 각 메뉴 클래스마다 사용자 선택시 동작할 기능 구현
- Info UI 구현
- 역, 노선, 지하철 노선도 출력 기능
- 추가 및 삭제 성공시 문구 출력 기능
- Error UI 구현
- 잘못 입력 시 출력 기능
- 존재하지 않는 역, 노선 입력 시 출력 기능
- 이미 존재하는 역, 노선 입력 시 출력 기능
- 컨트롤러 구현하여 Info, Error, Menu 가 바르게 작동하도록 구현
- 메인메뉴 기능 구현
- 역 관리 기능 구현
- 노선 관리 기능 구현
- 구간 관리 기능 구현
- 전체 어플리케이션 작동 기능 구현
- 소문자도 인식할 수 있는 기능 구현
- 노선 정렬기능 구현



<br>

## 📝 License
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/subway/Application.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package subway;

import subway.userinterface.UserController;

import java.util.Scanner;

public class Application {
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
// TODO: 프로그램 구현
UserController.runApp(scanner);
scanner.close();
}
}
4 changes: 3 additions & 1 deletion src/main/java/subway/domain/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public String getName() {
return name;
}

// 추가 기능 구현
public String toString() {
return name;
}
}
68 changes: 57 additions & 11 deletions src/main/java/subway/domain/LineRepository.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,68 @@
package subway.domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.*;

public class LineRepository {
private static final List<Line> lines = new ArrayList<>();
private static final Comparator<Line> comparator = Comparator.comparing(Line::getName);
private static final Map<Line, List<Station>> lines = new TreeMap<>(comparator);
private static final String[] defaultLines = {"2호선", "3호선", "신분당선"};
private static final String[][] defaultStations = {
{"교대역", "강남역", "역삼역"},
{"교대역", "남부터미널역", "양재역", "매봉역"},
{"강남역", "양재역", "양재시민의숲역"}
};

public static List<Line> lines() {
return Collections.unmodifiableList(lines);
static {
for (int i = 0; i < defaultLines.length; i++) {
List<Station> tmpStations = new ArrayList<>();
for (int j = 0; j < defaultStations[i].length; j++) {
tmpStations.add(new Station(defaultStations[i][j]));
}

lines.put(new Line(defaultLines[i]), tmpStations);
}
}

public static Map<Line, List<Station>> lines() {
return Collections.unmodifiableMap(lines);
}

public static void addLine(Line line, Station startStation, Station endStation) {
lines.put(line, new ArrayList<>(Arrays.asList(startStation, endStation)));
}

public static void addStationInLineByName(String line, String station, int seq) {
lines.get(findLineByName(line)).add(seq - 1,
StationRepository.findStationByName(station));
}

public static void deleteLineByName(String name) {
lines.keySet().removeIf(line -> Objects.equals(line.getName(), name));
}

public static void deleteStationInLineByName(String line, String station) {
lines.get(findLineByName(line)).removeIf(st -> st.getName().equals(station));
}

public static Line findLineByName(String name) {
return lines.keySet().stream().filter(x -> x.getName().equals(name))
.findFirst().get();
}

public static boolean hasStationInLine(String name) {
for (Line line : lines.keySet()) {
if (hasStationInCertainLine(line, name)) {
return true;
}
}
return false;
}

public static void addLine(Line line) {
lines.add(line);
public static boolean hasStationInCertainLine(Line line, String stationName) {
return lines.get(line).stream().anyMatch(station -> station.getName().equals(stationName));
}

public static boolean deleteLineByName(String name) {
return lines.removeIf(line -> Objects.equals(line.getName(), name));
public static boolean isLinePresent(String name) {
return lines.keySet().stream().anyMatch(line -> line.getName().equals(name));
}
}
45 changes: 45 additions & 0 deletions src/main/java/subway/domain/MenuRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package subway.domain;

import subway.domain.menu.intervalmenu.DeleteIntervalMenu;
import subway.domain.menu.intervalmenu.ExitFromIntervalMenu;
import subway.domain.menu.intervalmenu.IntervalManageMenu;
import subway.domain.menu.intervalmenu.RegisterIntervalMenu;
import subway.domain.menu.linemenu.*;
import subway.domain.menu.mainmenu.*;
import subway.domain.menu.stationmenu.*;

import java.util.*;

public class MenuRepository {
public static final List<String> mainMenuButtons = new ArrayList<>(Arrays.asList
(StationMenu.MENU_BUTTON, LineMenu.MENU_BUTTON, IntervalMenu.MENU_BUTTON, PrintIntervalsMenu.MENU_BUTTON, ExitMenu.MENU_BUTTON));
public static final List<String> stationMenuButtons = new ArrayList<>(Arrays.asList
(RegisterStationMenu.MENU_BUTTON, DeleteStationMenu.MENU_BUTTON, PrintStationsMenu.MENU_BUTTON, ExitFromStationMenu.MENU_BUTTON));
public static final List<String> lineMenuButtons = new ArrayList<>(Arrays.asList
(RegisterLineMenu.MENU_BUTTON, DeleteLineMenu.MENU_BUTTON, PrintLinesMenu.MENU_BUTTON, ExitFromLineMenu.MENU_BUTTON));
public static final List<String> intervalMenuButtons = new ArrayList<>(Arrays.asList
(RegisterIntervalMenu.MENU_BUTTON, DeleteIntervalMenu.MENU_BUTTON, ExitFromIntervalMenu.MENU_BUTTON));

public static final Map<String, Menu> mainMenu = new HashMap<>();
public static final Map<String, StationManageMenu> stationMenu = new HashMap<>();
public static final Map<String, LineManageMenu> lineMenu = new HashMap<>();
public static final Map<String, IntervalManageMenu> intervalMenu = new HashMap<>();

static {
mainMenu.put(StationMenu.MENU_BUTTON, new StationMenu());
mainMenu.put(LineMenu.MENU_BUTTON, new LineMenu());
mainMenu.put(IntervalMenu.MENU_BUTTON, new IntervalMenu());
mainMenu.put(PrintIntervalsMenu.MENU_BUTTON, new PrintIntervalsMenu());

stationMenu.put(RegisterStationMenu.MENU_BUTTON, new RegisterStationMenu());
stationMenu.put(DeleteStationMenu.MENU_BUTTON, new DeleteStationMenu());
stationMenu.put(PrintStationsMenu.MENU_BUTTON, new PrintStationsMenu());

lineMenu.put(RegisterLineMenu.MENU_BUTTON, new RegisterLineMenu());
lineMenu.put(DeleteLineMenu.MENU_BUTTON, new DeleteLineMenu());
lineMenu.put(PrintLinesMenu.MENU_BUTTON, new PrintLinesMenu());

intervalMenu.put(RegisterIntervalMenu.MENU_BUTTON, new RegisterIntervalMenu());
intervalMenu.put(DeleteIntervalMenu.MENU_BUTTON, new DeleteIntervalMenu());
}
}
4 changes: 3 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,7 @@ public String getName() {
return name;
}

// 추가 기능 구현
public String toString() {
return name;
}
}
20 changes: 18 additions & 2 deletions src/main/java/subway/domain/StationRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

public class StationRepository {
private static final List<Station> stations = new ArrayList<>();
private static final String[] defaultStations = {"교대역", "강남역", "역삼역", "남부터미널역", "양재역", "양재시민의숲역", "매봉역"};

static {
for (String s : defaultStations) {
stations.add(new Station(s));
}
}

public static List<Station> stations() {
return Collections.unmodifiableList(stations);
Expand All @@ -16,7 +23,16 @@ public static void addStation(Station station) {
stations.add(station);
}

public static boolean deleteStation(String name) {
return stations.removeIf(station -> Objects.equals(station.getName(), name));
public static void deleteStationByName(String name) {
stations.removeIf(station -> Objects.equals(station.getName(), name));
}

public static Station findStationByName(String name) {
return stations.stream().filter(station -> station.getName().equals(name))
.findFirst().get();
}

public static boolean isStationPresent(String name) {
return stations.stream().anyMatch(station -> station.getName().equals(name));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package subway.domain.menu.intervalmenu;

import subway.domain.LineRepository;
import subway.userinterface.MenuInterface;
import subway.userinterface.Error;
import subway.userinterface.Info;

import java.util.Scanner;

public class DeleteIntervalMenu implements IntervalManageMenu {
public static final String MENU_BUTTON = "2";

@Override
public void run(Scanner scanner) {
checkLineName(scanner);
}

private void checkLineName(Scanner scanner) {
MenuInterface.printDeleteInterval_Line();
String lineNameInput = scanner.next();
if (Error.printNotExistLineError(lineNameInput) || Error.printNotEnoughStationInLineError(lineNameInput)) {
return;
}

checkStationName(scanner, lineNameInput);
}

private void checkStationName(Scanner scanner, String lineNameInput) {
MenuInterface.printDeleteInterval_Station();
String stationNameInput = scanner.next();
if (Error.printStationNotExistInCertainLineError(lineNameInput, stationNameInput)) {
return;
}

runDeleteInterval(lineNameInput, stationNameInput);
}

private void runDeleteInterval(String lineNameInput, String stationNameInput) {
LineRepository.deleteStationInLineByName(lineNameInput, stationNameInput);
Info.printIntervalDeleted(lineNameInput, stationNameInput);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package subway.domain.menu.intervalmenu;

public class ExitFromIntervalMenu {
public static final String MENU_BUTTON = "B";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package subway.domain.menu.intervalmenu;

import java.util.Scanner;

public interface IntervalManageMenu {
void run(Scanner scanner);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package subway.domain.menu.intervalmenu;

import subway.domain.LineRepository;
import subway.userinterface.MenuInterface;
import subway.userinterface.Error;
import subway.userinterface.Info;

import java.util.Scanner;

public class RegisterIntervalMenu implements IntervalManageMenu {
public static final String MENU_BUTTON = "1";

@Override
public void run(Scanner scanner) {
checkLineName(scanner);
}

private void checkLineName(Scanner scanner) {
MenuInterface.printAddInterval_Line();
String lineNameInput = scanner.next();
if (Error.printNotExistLineError(lineNameInput)) {
return;
}

checkStationName(scanner, lineNameInput);
}

private void checkStationName(Scanner scanner, String lineNameInput) {
MenuInterface.printAddInterval_Station();
String stationNameInput = scanner.next();
if (Error.printNotExistStationError(stationNameInput) ||
Error.printStationAlreadyExistInCertainLineError(lineNameInput, stationNameInput)) {
return;
}

checkSequence(scanner, lineNameInput, stationNameInput);
}

private void checkSequence(Scanner scanner, String lineNameInput, String stationNameInput) {
MenuInterface.printAddInterval_Sequence();
int sequenceInput;
try {
sequenceInput = scanner.nextInt();
} catch (Exception e) {
Error.printNotIntInput();
String trashCan = scanner.next();
return;
}
if (Error.printIsWrongSequenceInput(lineNameInput, sequenceInput)) {
return;
}

runRegisterInterval(lineNameInput, stationNameInput, sequenceInput);
}

private void runRegisterInterval(String lineNameInput, String stationNameInput, int sequenceInput) {
LineRepository.addStationInLineByName(lineNameInput, stationNameInput, sequenceInput);
Info.printIntervalRegistered(lineNameInput, stationNameInput);
}
}
24 changes: 24 additions & 0 deletions src/main/java/subway/domain/menu/linemenu/DeleteLineMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package subway.domain.menu.linemenu;

import subway.domain.LineRepository;
import subway.userinterface.MenuInterface;
import subway.userinterface.Error;
import subway.userinterface.Info;

import java.util.Scanner;

public class DeleteLineMenu implements LineManageMenu {
public static final String MENU_BUTTON = "2";

@Override
public void run(Scanner scanner) {
MenuInterface.printDeleteLine();

String lineNameInput = scanner.next();
if (Error.printNotExistLineError(lineNameInput)) {
return;
}
LineRepository.deleteLineByName(lineNameInput);
Info.printLineDeleted(lineNameInput);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package subway.domain.menu.linemenu;

public class ExitFromLineMenu {
public static final String MENU_BUTTON = "B";
}
7 changes: 7 additions & 0 deletions src/main/java/subway/domain/menu/linemenu/LineManageMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package subway.domain.menu.linemenu;

import java.util.Scanner;

public interface LineManageMenu {
void run(Scanner scanner);
}
Loading