diff --git a/src/main/java/subway/Application.java b/src/main/java/subway/Application.java index 0bcf786cc..dbfb6ee54 100644 --- a/src/main/java/subway/Application.java +++ b/src/main/java/subway/Application.java @@ -1,10 +1,17 @@ package subway; +import com.sun.tools.javac.Main; +import subway.controller.MainController; +import subway.domain.Station; +import subway.domain.StationRepository; + +import java.util.List; import java.util.Scanner; + public class Application { public static void main(String[] args) { - final Scanner scanner = new Scanner(System.in); - // TODO: 프로그램 구현 + MainController mainController = new MainController(); + mainController.run(); } } diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java new file mode 100644 index 000000000..31bcce571 --- /dev/null +++ b/src/main/java/subway/controller/MainController.java @@ -0,0 +1,53 @@ +package subway.controller; + +import subway.domain.*; +import subway.view.InputView; +import subway.view.OutputView; + +import java.util.List; +import java.util.Map; + +public class MainController { + private final OutputView outputView; + private final InputView inputView; + private final StationService stationService; + private final LineService lineService; + private final SectionService sectionService; + + public MainController() { + outputView = new OutputView(); + inputView = new InputView(); + stationService = new StationService(outputView, inputView); + lineService = new LineService(outputView, inputView); + sectionService = new SectionService(outputView, inputView); + } + + public void run(){ + while (true){ + outputView.mainPage(); + String input = inputView.selectFunction(); + + if(input.equals("1")){ + stationService.manage(); + continue; + } + if(input.equals("2")){ + lineService.manage(); + continue; + } + if(input.equals("3")){ + sectionService.manage(); + continue; + } + if(input.equals("4")){ + lineService.listALl(); + continue; + } + if(input.equals("Q")) return; + + + } + } + + +} diff --git a/src/main/java/subway/domain/LineRepository.java b/src/main/java/subway/domain/LineRepository.java index 49132ddb6..f2f7ed9d7 100644 --- a/src/main/java/subway/domain/LineRepository.java +++ b/src/main/java/subway/domain/LineRepository.java @@ -16,6 +16,11 @@ public static void addLine(Line line) { lines.add(line); } + public static boolean isExist(String name) { + return lines().stream() + .anyMatch(station -> station.getName().equals(name)); + } + public static boolean deleteLineByName(String name) { return lines.removeIf(line -> Objects.equals(line.getName(), name)); } diff --git a/src/main/java/subway/domain/LineService.java b/src/main/java/subway/domain/LineService.java new file mode 100644 index 000000000..43a105c7e --- /dev/null +++ b/src/main/java/subway/domain/LineService.java @@ -0,0 +1,101 @@ +package subway.domain; + +import subway.view.InputView; +import subway.view.OutputView; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class LineService { + private final OutputView outputView; + private final InputView inputView; + + public LineService(OutputView outputView, InputView inputView) { + this.outputView = outputView; + this.inputView = inputView; + } + + public void manage(){ + outputView.lineManagementPage(); + String input = inputView.selectFunction(); + + if(input.equals("1")){ + registerLine(); + return; + } + if(input.equals("2")){ + deleteLine(); + return; + } + if(input.equals("3")){ + listLines(); + return; + } + if(input.equals("B")) return; + } + + private void registerLine(){ + String lineName = inputView.inputRegisterLine(); + String startStationName = inputView.inputStartStation(); + String endStationName = inputView.inputEndStation(); + + if(lineName.length() < 2){ + System.out.println("두 글자 이상"); + return; + } + Line line = new Line(lineName); + + if(Subway.lines.containsKey(line)){ + System.out.println("이미 존재하는 노선"); + } + if(!StationRepository.isExist(startStationName)){ + System.out.println("존재하지 않는 역"); + return; + } + if(!StationRepository.isExist(endStationName)){ + System.out.println("존재하지 않는 역"); + return; + } + + LineRepository.addLine(line); + + List stations = new ArrayList<>(); + stations.add(new Station(startStationName)); + stations.add(new Station(endStationName)); + + Subway.lines.put(line, stations); + outputView.registeredLine(); + } + + public void deleteLine(){ + String stationName = inputView.inputDeleteLine(); + if(!LineRepository.deleteLineByName(stationName)){ + System.out.println("존재하지 않는 노선"); + return; + } + Line line = new Line(stationName); + Subway.lines.remove(line); + outputView.deletedLine(); + } + + public void listLines(){ + LineRepository.lines().stream() + .map(Line::getName) + .forEach(name -> System.out.println("[INFO} " + name)); + } + + public void listALl(){ + for (Map.Entry> entry : Subway.lines.entrySet()) { + Line line = entry.getKey(); + List stations = entry.getValue(); + + System.out.println("[INFO] " + line.getName()); + for (Station station : stations) { + System.out.println("[INFO}" + station.getName()); + } + + System.out.println(); + } + } +} diff --git a/src/main/java/subway/domain/SectionService.java b/src/main/java/subway/domain/SectionService.java new file mode 100644 index 000000000..36f535400 --- /dev/null +++ b/src/main/java/subway/domain/SectionService.java @@ -0,0 +1,72 @@ +package subway.domain; + +import subway.view.InputView; +import subway.view.OutputView; + +import java.util.List; + + +public class SectionService { + private final OutputView outputView; + private final InputView inputView; + + public SectionService(OutputView outputView, InputView inputView) { + this.outputView = outputView; + this.inputView = inputView; + } + + public void manage(){ + outputView.sectionManagementPage(); + String input = inputView.selectFunction(); + + if(input.equals("1")){ + registerSection(); + return; + } + if(input.equals("2")){ + deleteSection(); + } + if(input.equals("B")) return; + } + + private void registerSection(){ + String line = inputView.inputLineToRegisterSection(); + String station = inputView.inputStationToRegisterSection(); + int order = Integer.parseInt(inputView.inputOrder()); + + if(!LineRepository.isExist(line)){ + System.out.println("[ERROR] 존재하지 않는 노선입니다."); + return; + } + if(!StationRepository.isExist(station)){ + System.out.println("[ERROR] 존재하지 않는 역입니다."); + return; + } + + List stations = Subway.lines.get(line); + if(stations.contains(new Station(station))){ + System.out.println("[ERROR] 이미 존재하는 역입니다."); + } + + stations.add(order, new Station(station)); + outputView.registeredSection(); + } + + private void deleteSection(){ + String line = inputView.inputDeleteLineOfSection(); + String station = inputView.inputDeleteStationOfSection(); + + if(!LineRepository.isExist(line)){ + System.out.println("[ERROR] 존재하지 않는 노선입니다."); + return; + } + if(!StationRepository.isExist(station)){ + System.out.println("[ERROR] 존재하지 않는 역입니다."); + return; + } + + List stations = Subway.lines.get(line); + stations.remove(new Station(station)); + outputView.deletedStation(); + } +} diff --git a/src/main/java/subway/domain/StationRepository.java b/src/main/java/subway/domain/StationRepository.java index b7245c0f3..7af98bb27 100644 --- a/src/main/java/subway/domain/StationRepository.java +++ b/src/main/java/subway/domain/StationRepository.java @@ -19,4 +19,9 @@ public static void addStation(Station station) { public static boolean deleteStation(String name) { return stations.removeIf(station -> Objects.equals(station.getName(), name)); } + + public static boolean isExist(String name) { + return stations.stream() + .anyMatch(station -> station.getName().equals(name)); + } } diff --git a/src/main/java/subway/domain/StationService.java b/src/main/java/subway/domain/StationService.java new file mode 100644 index 000000000..e9dc9ec85 --- /dev/null +++ b/src/main/java/subway/domain/StationService.java @@ -0,0 +1,63 @@ +package subway.domain; + +import subway.view.InputView; +import subway.view.OutputView; + +public class StationService { + private final OutputView outputView; + private final InputView inputView; + + public StationService(OutputView outputView, InputView inputView) { + this.outputView = outputView; + this.inputView = inputView; + } + + public void manage(){ + outputView.stationManagementPage(); + String input = inputView.selectFunction(); + + if(input.equals("1")){ + registerStation(); + return; + } + if(input.equals("2")){ + deleteStation(); + return; + } + if(input.equals("3")){ + listStation(); + return; + } + if(input.equals("B")) return; + } + + private void registerStation(){ + String stationName = inputView.inputRegisterStation(); + + if(stationName.length() < 2){ + System.out.println("[ERROR] 두 글자 이상 입력해야합니다."); + return; + } + if(StationRepository.isExist(stationName)){ + System.out.println("[ERROR] 존재하는 역입니다."); + return; + } + + StationRepository.addStation(new Station(stationName)); + outputView.registeredStation(); + } + + private void deleteStation(){ + String stationName = inputView.inputDeleteStation(); + if(!StationRepository.deleteStation(stationName)){ + System.out.println("[ERROR] 역 삭제에 실패했습니다."); + } + outputView.deletedStation(); + } + + private void listStation(){ + StationRepository.stations().stream() + .map(Station::getName) + .forEach(name -> System.out.println("[INFO] " + name)); + } +} diff --git a/src/main/java/subway/domain/Subway.java b/src/main/java/subway/domain/Subway.java new file mode 100644 index 000000000..c68e43525 --- /dev/null +++ b/src/main/java/subway/domain/Subway.java @@ -0,0 +1,10 @@ +package subway.domain; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Subway { + public static Map> lines = new HashMap<>(); + +} diff --git a/src/main/java/subway/view/InputView.java b/src/main/java/subway/view/InputView.java new file mode 100644 index 000000000..c3e25f30a --- /dev/null +++ b/src/main/java/subway/view/InputView.java @@ -0,0 +1,66 @@ +package subway.view; + +import java.util.Scanner; + +public class InputView { + private final Scanner scanner = new Scanner(System.in); + public String selectFunction(){ + System.out.println("## 원하는 기능을 선택하세요"); + return scanner.nextLine(); + } + + public String inputRegisterStation(){ + System.out.println("## 등록할 역 이름을 입력하세요."); + return scanner.nextLine(); + } + + public String inputDeleteStation(){ + System.out.println("## 삭제할 역 이름을 입력하세요."); + return scanner.nextLine(); + } + + public String inputRegisterLine(){ + System.out.println("## 등록할 노선 이름을 입력하세요."); + return scanner.nextLine(); + } + + public String inputStartStation(){ + System.out.println("## 등록할 노선의 상행 종점역 이름을 입력하세요."); + return scanner.nextLine(); + } + + public String inputEndStation(){ + System.out.println("## 등록할 노선의 하행 종점역 이름을 입력하세요."); + return scanner.nextLine(); + } + + public String inputDeleteLine(){ + System.out.println("## 삭제할 노선 이름을 입력하세요."); + return scanner.nextLine(); + } + + public String inputLineToRegisterSection(){ + System.out.println("## 노선을 입력하세요."); + return scanner.nextLine(); + } + + public String inputStationToRegisterSection(){ + System.out.println("## 역이름을 입력하세요."); + return scanner.nextLine(); + } + + public String inputOrder(){ + System.out.println("## 순서를 입력하세요."); + return scanner.nextLine(); + } + + public String inputDeleteLineOfSection(){ + System.out.println("## 삭제할 구간의 노선을 입력하세요."); + return scanner.nextLine(); + } + + public String inputDeleteStationOfSection(){ + System.out.println("## 삭제할 구간의 역을 입력하세요."); + return scanner.nextLine(); + } +} diff --git a/src/main/java/subway/view/OutputView.java b/src/main/java/subway/view/OutputView.java new file mode 100644 index 000000000..771c88d3c --- /dev/null +++ b/src/main/java/subway/view/OutputView.java @@ -0,0 +1,59 @@ +package subway.view; + +public class OutputView { + public void mainPage(){ + System.out.println("## 메인 화면\n" + + "1. 역 관리\n" + + "2. 노선 관리\n" + + "3. 구간 관리\n" + + "4. 지하철 노선도 출력\n" + + "Q. 종료\n"); + } + + public void stationManagementPage(){ + System.out.println("## 역 관리 화면\n" + + "1. 역 등록\n" + + "2. 역 삭제\n" + + "3. 역 조회\n" + + "B. 돌아가기\n"); + } + + public void registeredStation(){ + System.out.println("[INFO] 지하철 역이 등록되었습니다.\n"); + } + + public void deletedStation(){ + System.out.println("[INFO] 지하철 역이 삭제되었습니다.\n"); + } + + public void lineManagementPage(){ + System.out.println("## 노선 관리 화면\n" + + "1. 노선 등록\n" + + "2. 노선 삭제\n" + + "3. 노선 조회\n" + + "B. 돌아가기\n"); + } + + public void registeredLine(){ + System.out.println("[INFO] 지하철 노선이 등록되었습니다.\n"); + } + + public void deletedLine(){ + System.out.println("[INFO] 지하철 노선이 삭제되었습니다.\n"); + } + + public void sectionManagementPage(){ + System.out.println("## 구간 관리 화면\n" + + "1. 구간 등록\n" + + "2. 구간 삭제\n" + + "B. 돌아가기\n"); + } + + public void registeredSection(){ + System.out.println("[INFO] 구간이 등록되었습니다.\n"); + } + + public void deletedSection(){ + System.out.println("[INFO] 구간이 삭제되었습니다.\n"); + } +}