From 6cd973a27526a7de3f5b3e3383289cc6b73f8316 Mon Sep 17 00:00:00 2001 From: d0kyoung Date: Fri, 10 May 2024 20:30:46 +0900 Subject: [PATCH 1/5] =?UTF-8?q?=EA=B5=AC=ED=98=84=ED=95=A0=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EB=AA=A9=EB=A1=9D=20=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 docs/README.md diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..9d16f8177 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,33 @@ +# 기능정의 + +## 도메인 + +### 지하철(Staion) + +- [ ] 지하철역 등록 기능 + - [ ] [ERROR] 지하철역은 두글자 이상이여야 한다. + - [ ] [ERROR] 지하철역은 한국어만 가능합니다. + - [ ] [ERROR] 중복된 역 이름은 등록할 수 없습니다. +- [ ] 지하철역 삭제 기능 + - [ ] [ERROR] 존재하는 지하철역만 삭제 가능합니다. + - [ ] [ERROR] 노선에 등록된 역은 삭제가 안됩니다. +- [ ] 지하철역 전체 조회 기능 + +### 노선(Line) + +- [ ] 노선 등록 기능 + - [ ] 등록시 상행 종점과 하행 종점을 입력받는다. + - [ ] [ERROR] 노선의 이름은 두글자 이상이여야 한다. +- [ ] 노선 삭제 기능 + - [ ] [ERROR] 존재하는 노선만 삭제 가능합니다. +- [ ] 노선 목록 조회 기능 +- [ ] 노선에 등록된 역 조회 기능 + - [ ] 노선의 상행 종점부터 하행 종점까지 순서대로 목록을 조회한다. + +### 구간 + +- [ ] 존재하는 노선에 역을 추가한다. + - [ ] [ERROR] 등록된 역만 노선에 추가가 가능하다. +- [ ] 노선에 역을 삭제한다. + - [ ] [ERROR] 노선에 역이 2개 이하면 삭제할 수 없다. + - [ ] [ERROR] 노선에 존재하는 욕만 삭제할 수 있다. \ No newline at end of file From 2197f54c8e637af9ef9baf97eb0418e02f21bf27 Mon Sep 17 00:00:00 2001 From: d0kyoung Date: Fri, 10 May 2024 21:18:25 +0900 Subject: [PATCH 2/5] =?UTF-8?q?=EC=97=AD=20=EA=B4=80=EB=A6=AC,=20=EB=85=B8?= =?UTF-8?q?=EC=84=A0=20=EA=B4=80=EB=A6=AC,=20=EA=B5=AC=EA=B0=84=20?= =?UTF-8?q?=EA=B4=80=EB=A6=AC=20=EC=A7=80=ED=95=98=EC=B2=A0=20=EB=85=B8?= =?UTF-8?q?=EC=84=A0=EB=8F=84=20=EB=93=B1=EB=A1=9D,=EC=A1=B0=ED=9A=8C,=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20=EB=A8=BC=EC=A0=80=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/Application.java | 2 + src/main/java/subway/SubwaySystem.java | 246 ++++++++++++++++++ src/main/java/subway/domain/Line.java | 25 ++ .../java/subway/domain/LineRepository.java | 12 + .../java/subway/domain/StationRepository.java | 12 + 5 files changed, 297 insertions(+) create mode 100644 src/main/java/subway/SubwaySystem.java diff --git a/src/main/java/subway/Application.java b/src/main/java/subway/Application.java index 0bcf786cc..4f0fc24d5 100644 --- a/src/main/java/subway/Application.java +++ b/src/main/java/subway/Application.java @@ -6,5 +6,7 @@ public class Application { public static void main(String[] args) { final Scanner scanner = new Scanner(System.in); // TODO: 프로그램 구현 + SubwaySystem subwaySystem = new SubwaySystem(scanner); + subwaySystem.run(); } } diff --git a/src/main/java/subway/SubwaySystem.java b/src/main/java/subway/SubwaySystem.java new file mode 100644 index 000000000..166137856 --- /dev/null +++ b/src/main/java/subway/SubwaySystem.java @@ -0,0 +1,246 @@ +package subway; + +import java.util.Scanner; +import subway.domain.*; + +public class SubwaySystem { + private final Scanner scanner; + + public SubwaySystem(Scanner scanner) { this.scanner = scanner;} + + public void run() { + boolean keepRunning = true; + while (keepRunning) { + showMainMenu(); + String command = scanner.nextLine().trim(); + switch (command) { + case "1": + manageStations(); + break; + case "2": + manageLines(); + break; + case "3": + manageSections(); + break; + case "4": + printSubwayMap(); + break; + case "Q": + case "q": + System.out.println("[INFO] 프로그램을 종료합니다."); + keepRunning = false; + break; + default: + System.out.println("[ERROR] 선택할 수 없는 기능입니다."); + } + } + } + + private void showMainMenu() { + System.out.println("\n## 메인 화면"); + System.out.println("1. 역 관리"); + System.out.println("2. 노선 관리"); + System.out.println("3. 구간 관리"); + System.out.println("4. 지하철 노선도 출력"); + System.out.println("Q. 종료"); + System.out.print("## 원하는 기능을 선택하세요: "); + } + + private void manageStations() { + while (true) { + System.out.println("\n## 역 관리 화면"); + System.out.println("1. 역 등록"); + System.out.println("2. 역 삭제"); + System.out.println("3. 역 조회"); + System.out.println("B. 돌아가기"); + System.out.print("## 원하는 기능을 선택하세요: "); + String command = scanner.nextLine().trim(); + switch (command) { + case "1": + addStation(); + break; + case "2": + removeStation(); + break; + case "3": + showStations(); + break; + case "B": + case "b": + return; + default: + System.out.println("[ERROR] 선택할 수 없는 기능입니다."); + } + } + } + + private void addStation() { + System.out.print("## 등록할 역 이름을 입력하세요: "); + String stationName = scanner.nextLine().trim(); + if (StationRepository.exists(stationName)) { + System.out.println("[ERROR] 이미 등록된 역 이름입니다."); + } else { + StationRepository.addStation(new Station(stationName)); + System.out.println("[INFO] 지하철 역이 등록되었습니다."); + } + } + + private void removeStation() { + System.out.print("## 삭제할 역 이름을 입력하세요: "); + String stationName = scanner.nextLine().trim(); + if (StationRepository.deleteStation(stationName)) { + System.out.println("[INFO] 지하철 역이 삭제되었습니다."); + } else { + System.out.println("[ERROR] 역이 존재하지 않거나 삭제할 수 없습니다."); + } + } + + private void showStations() { + System.out.println("## 역 목록"); + for (Station station : StationRepository.stations()) { + System.out.println("[INFO] " + station.getName()); + } + } + + + private void manageLines() { + while (true) { + System.out.println("\n## 노선 관리 화면"); + System.out.println("1. 노선 등록"); + System.out.println("2. 노선 삭제"); + System.out.println("3. 노선 조회"); + System.out.println("B. 돌아가기"); + System.out.print("## 원하는 기능을 선택하세요: "); + String command = scanner.nextLine().trim(); + switch (command) { + case "1": + addLine(); + break; + case "2": + removeLine(); + break; + case "3": + showLines(); + break; + case "B": + case "b": + return; + default: + System.out.println("[ERROR] 선택할 수 없는 기능입니다."); + } + } + } + + private void addLine() { + System.out.print("## 등록할 노선 이름을 입력하세요: "); + String lineName = scanner.nextLine().trim(); + if (LineRepository.exists(lineName)) { + System.out.println("[ERROR] 이미 등록된 노선 이름입니다."); + } else { + LineRepository.addLine(new Line(lineName)); + System.out.println("[INFO] 지하철 노선이 등록되었습니다."); + } + } + + private void removeLine() { + System.out.print("## 삭제할 노선 이름을 입력하세요: "); + String lineName = scanner.nextLine().trim(); + if (LineRepository.deleteLineByName(lineName)) { + System.out.println("[INFO] 지하철 노선이 삭제되었습니다."); + } else { + System.out.println("[ERROR] 노선이 존재하지 않거나 삭제할 수 없습니다."); + } + } + + private void showLines() { + System.out.println("## 노선 목록"); + for (Line line : LineRepository.lines()) { + System.out.println("[INFO] " + line.getName()); + } + } + + + private void manageSections() { + while (true) { + System.out.println("\n## 구간 관리 화면"); + System.out.println("1. 구간 등록"); + System.out.println("2. 구간 삭제"); + System.out.println("B. 돌아가기"); + System.out.print("## 원하는 기능을 선택하세요: "); + String command = scanner.nextLine().trim(); + switch (command) { + case "1": + addSection(); + break; + case "2": + removeSection(); + break; + case "B": + case "b": + return; + default: + System.out.println("[ERROR] 선택할 수 없는 기능입니다."); + } + } + } + + private void addSection() { + System.out.print("## 등록할 구간의 노선 이름을 입력하세요: "); + String lineName = scanner.nextLine().trim(); + Line line = LineRepository.getLine(lineName); + if (line == null) { + System.out.println("[ERROR] 노선이 존재하지 않습니다."); + return; + } + + System.out.print("## 등록할 구간의 역 이름을 입력하세요: "); + String stationName = scanner.nextLine().trim(); + Station station = StationRepository.getStation(stationName); + if (station == null) { + System.out.println("[ERROR] 역이 존재하지 않습니다."); + return; + } + + System.out.print("## 구간 순서를 입력하세요: "); + int order = Integer.parseInt(scanner.nextLine().trim()); + if (order < 1 || order > line.getStations().size() + 1) { + System.out.println("[ERROR] 유효하지 않은 구간 순서입니다."); + return; + } + + line.addStationAt(station, order - 1); + System.out.println("[INFO] 구간이 등록되었습니다."); + } + + private void removeSection() { + System.out.print("## 삭제할 구간의 노선 이름을 입력하세요: "); + String lineName = scanner.nextLine().trim(); + Line line = LineRepository.getLine(lineName); + if (line == null) { + System.out.println("[ERROR] 노선이 존재하지 않습니다."); + return; + } + + System.out.print("## 삭제할 구간의 역 이름을 입력하세요: "); + String stationName = scanner.nextLine().trim(); + if (!line.removeStationByName(stationName)) { + System.out.println("[ERROR] 구간 삭제에 실패했습니다."); + } else { + System.out.println("[INFO] 구간이 삭제되었습니다."); + } + } + + + private void printSubwayMap() { + System.out.println("\n## 지하철 노선도"); + for (Line line : LineRepository.lines()) { + System.out.println("[INFO] " + line.getName()); + System.out.println("[INFO] ---"); + for (Station station : line.getStations()) { + System.out.println("[INFO] " + station.getName()); + } + } + } + +} diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index f4d738d5a..8c9da585e 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -1,7 +1,11 @@ package subway.domain; +import java.util.ArrayList; +import java.util.List; + public class Line { private String name; + private List stations = new ArrayList<>(); public Line(String name) { this.name = name; @@ -12,4 +16,25 @@ public String getName() { } // 추가 기능 구현 + public void addStation(Station station) { + stations.add(station); + } + + public void removeStation(Station station) { + stations.remove(station); + } + + public List getStations() { + return stations; + } + + public void addStationAt(Station station, int index) { + if (index >= 0 && index <= stations.size()) { + stations.add(index, station); + } + } + + public boolean removeStationByName(String stationName) { + return stations.removeIf(station -> station.getName().equals(stationName)); + } } diff --git a/src/main/java/subway/domain/LineRepository.java b/src/main/java/subway/domain/LineRepository.java index 49132ddb6..170b37b5f 100644 --- a/src/main/java/subway/domain/LineRepository.java +++ b/src/main/java/subway/domain/LineRepository.java @@ -19,4 +19,16 @@ public static void addLine(Line line) { public static boolean deleteLineByName(String name) { return lines.removeIf(line -> Objects.equals(line.getName(), name)); } + + public static boolean exists(String name) { + return lines.stream() + .anyMatch(line -> Objects.equals(line.getName(), name)); + } + + public static Line getLine(String name) { + return lines.stream() + .filter(line -> line.getName().equals(name)) + .findFirst() + .orElse(null); + } } diff --git a/src/main/java/subway/domain/StationRepository.java b/src/main/java/subway/domain/StationRepository.java index b7245c0f3..cd3974090 100644 --- a/src/main/java/subway/domain/StationRepository.java +++ b/src/main/java/subway/domain/StationRepository.java @@ -19,4 +19,16 @@ public static void addStation(Station station) { public static boolean deleteStation(String name) { return stations.removeIf(station -> Objects.equals(station.getName(), name)); } + + // exists 메소드 추가 + public static boolean exists(String name) { + return stations.stream().anyMatch(station -> Objects.equals(station.getName(), name)); + } + + public static Station getStation(String name) { + return stations.stream() + .filter(station -> station.getName().equals(name)) + .findFirst() + .orElse(null); + } } From a5ac8a00a039264a334e7a8b8d5b64b1a6e4fb0b Mon Sep 17 00:00:00 2001 From: d0kyoung Date: Fri, 10 May 2024 21:30:32 +0900 Subject: [PATCH 3/5] =?UTF-8?q?=EB=A9=94=EC=9D=B8=EB=A9=94=EB=89=B4?= =?UTF-8?q?=EA=B0=80=20=EB=B0=98=EB=B3=B5=EB=90=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/SubwaySystem.java | 16 ++++++++-------- .../java/subway/domain/StationRepository.java | 1 + 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/subway/SubwaySystem.java b/src/main/java/subway/SubwaySystem.java index 166137856..9f662b37b 100644 --- a/src/main/java/subway/SubwaySystem.java +++ b/src/main/java/subway/SubwaySystem.java @@ -59,13 +59,13 @@ private void manageStations() { switch (command) { case "1": addStation(); - break; + return; case "2": removeStation(); - break; + return; case "3": showStations(); - break; + return; case "B": case "b": return; @@ -116,13 +116,13 @@ private void manageLines() { switch (command) { case "1": addLine(); - break; + return; case "2": removeLine(); - break; + return; case "3": showLines(); - break; + return; case "B": case "b": return; @@ -172,10 +172,10 @@ private void manageSections() { switch (command) { case "1": addSection(); - break; + return; case "2": removeSection(); - break; + return; case "B": case "b": return; diff --git a/src/main/java/subway/domain/StationRepository.java b/src/main/java/subway/domain/StationRepository.java index cd3974090..1e22f7ddc 100644 --- a/src/main/java/subway/domain/StationRepository.java +++ b/src/main/java/subway/domain/StationRepository.java @@ -25,6 +25,7 @@ public static boolean exists(String name) { return stations.stream().anyMatch(station -> Objects.equals(station.getName(), name)); } + // getStation 메소드 추가 public static Station getStation(String name) { return stations.stream() .filter(station -> station.getName().equals(name)) From 34ec75ad06f6fe47f3adc5f4f70e7f92a3e75c41 Mon Sep 17 00:00:00 2001 From: d0kyoung Date: Fri, 10 May 2024 21:35:23 +0900 Subject: [PATCH 4/5] =?UTF-8?q?=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=A8=20?= =?UTF-8?q?=EC=8B=9C=EC=9E=91=EC=8B=9C=20=EC=97=AD,=20=EB=85=B8=EC=84=A0?= =?UTF-8?q?=20=EB=93=B1=20=ED=95=84=EC=9A=94=ED=95=9C=20=EC=A0=95=EB=B3=B4?= =?UTF-8?q?=20=EB=AF=B8=EB=A6=AC=20=EC=84=B8=ED=8C=85=20=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/Application.java | 2 ++ src/main/java/subway/Initializer.java | 38 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/main/java/subway/Initializer.java diff --git a/src/main/java/subway/Application.java b/src/main/java/subway/Application.java index 4f0fc24d5..58adb2fd7 100644 --- a/src/main/java/subway/Application.java +++ b/src/main/java/subway/Application.java @@ -6,6 +6,8 @@ public class Application { public static void main(String[] args) { final Scanner scanner = new Scanner(System.in); // TODO: 프로그램 구현 + Initializer.initializeSubwaySystem(); + SubwaySystem subwaySystem = new SubwaySystem(scanner); subwaySystem.run(); } diff --git a/src/main/java/subway/Initializer.java b/src/main/java/subway/Initializer.java new file mode 100644 index 000000000..fc7e78bb0 --- /dev/null +++ b/src/main/java/subway/Initializer.java @@ -0,0 +1,38 @@ +package subway; + +import subway.domain.*; + +public class Initializer { + + public static void initializeSubwaySystem() { + // Register stations + StationRepository.addStation(new Station("교대역")); + StationRepository.addStation(new Station("강남역")); + StationRepository.addStation(new Station("역삼역")); + StationRepository.addStation(new Station("남부터미널역")); + StationRepository.addStation(new Station("양재역")); + StationRepository.addStation(new Station("양재시민의숲역")); + StationRepository.addStation(new Station("매봉역")); + + // Register lines and add stations to them + Line line2 = new Line("2호선"); + line2.addStation(StationRepository.getStation("교대역")); + line2.addStation(StationRepository.getStation("강남역")); + line2.addStation(StationRepository.getStation("역삼역")); + LineRepository.addLine(line2); + + Line line3 = new Line("3호선"); + line3.addStation(StationRepository.getStation("교대역")); + line3.addStation(StationRepository.getStation("남부터미널역")); + line3.addStation(StationRepository.getStation("양재역")); + line3.addStation(StationRepository.getStation("매봉역")); + LineRepository.addLine(line3); + + Line sinbundang = new Line("신분당선"); + sinbundang.addStation(StationRepository.getStation("강남역")); + sinbundang.addStation(StationRepository.getStation("양재역")); + sinbundang.addStation(StationRepository.getStation("양재시민의숲역")); + LineRepository.addLine(sinbundang); + } + +} From 4a5cedd48686836080177918b6822e3832857945 Mon Sep 17 00:00:00 2001 From: d0kyoung Date: Fri, 10 May 2024 21:41:30 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=EB=85=B8=EC=84=A0=20=EB=93=B1=EB=A1=9D?= =?UTF-8?q?=EC=8B=9C=20=EC=83=81=ED=96=89=20=EC=A2=85=EC=A0=90=EC=97=AD,?= =?UTF-8?q?=20=ED=95=98=ED=96=89=20=EC=A2=85=EC=A0=90=EC=97=AD=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/SubwaySystem.java | 27 +++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main/java/subway/SubwaySystem.java b/src/main/java/subway/SubwaySystem.java index 9f662b37b..4e7c3ac22 100644 --- a/src/main/java/subway/SubwaySystem.java +++ b/src/main/java/subway/SubwaySystem.java @@ -137,12 +137,33 @@ private void addLine() { String lineName = scanner.nextLine().trim(); if (LineRepository.exists(lineName)) { System.out.println("[ERROR] 이미 등록된 노선 이름입니다."); - } else { - LineRepository.addLine(new Line(lineName)); - System.out.println("[INFO] 지하철 노선이 등록되었습니다."); + return; + } + + System.out.print("## 등록할 노선의 상행 종점역 이름을 입력하세요: "); + String startStationName = scanner.nextLine().trim(); + Station startStation = StationRepository.getStation(startStationName); + if (startStation == null) { + System.out.println("[ERROR] 상행 종점역이 존재하지 않습니다."); + return; } + + System.out.print("## 등록할 노선의 하행 종점역 이름을 입력하세요: "); + String endStationName = scanner.nextLine().trim(); + Station endStation = StationRepository.getStation(endStationName); + if (endStation == null) { + System.out.println("[ERROR] 하행 종점역이 존재하지 않습니다."); + return; + } + + Line newLine = new Line(lineName); + newLine.addStation(startStation); + newLine.addStation(endStation); + LineRepository.addLine(newLine); + System.out.println("[INFO] 지하철 노선이 등록되었습니다."); } + private void removeLine() { System.out.print("## 삭제할 노선 이름을 입력하세요: "); String lineName = scanner.nextLine().trim();