From 62f62ecf64f1631d022f75ca100a602c946784ef Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 6 May 2024 20:06:39 +0900 Subject: [PATCH 001/104] =?UTF-8?q?feat:=20=EC=97=AD=EA=B3=BC=20=EB=85=B8?= =?UTF-8?q?=EC=84=A0=20=EA=B4=80=EB=A6=AC=EB=A5=BC=20=EC=9C=84=ED=95=9C=20?= =?UTF-8?q?=EC=9D=B8=ED=84=B0=ED=8E=98=EC=9D=B4=EC=8A=A4=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80(=EB=93=B1=EB=A1=9D,=20=EC=82=AD=EC=A0=9C,=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C,=20=EB=8F=8C=EC=95=84=EA=B0=80=EA=B8=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/service/Managerbale.java | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/main/java/subway/service/Managerbale.java diff --git a/src/main/java/subway/service/Managerbale.java b/src/main/java/subway/service/Managerbale.java new file mode 100644 index 000000000..1597a8ac7 --- /dev/null +++ b/src/main/java/subway/service/Managerbale.java @@ -0,0 +1,8 @@ +package subway.service; + +public interface Managerbale { + public abstract void register(); + public abstract void delete(); + public abstract void read(); + public abstract void goBack(); +} From 54d53883e3a3ff55241534efb54ff31e10aaa5cf Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 6 May 2024 20:17:44 +0900 Subject: [PATCH 002/104] =?UTF-8?q?doc:=20=EC=84=9C=EB=B9=84=EC=8A=A4=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EA=B8=B0=ED=8B=80=20=EB=A7=88=EB=A0=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/subway/service/LineManagerImpl.java | 23 ++++++++++++++++++ .../subway/service/SectionManagerImpl.java | 23 ++++++++++++++++++ .../subway/service/StationManagerImpl.java | 24 +++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 src/main/java/subway/service/LineManagerImpl.java create mode 100644 src/main/java/subway/service/SectionManagerImpl.java create mode 100644 src/main/java/subway/service/StationManagerImpl.java diff --git a/src/main/java/subway/service/LineManagerImpl.java b/src/main/java/subway/service/LineManagerImpl.java new file mode 100644 index 000000000..aa81bbe5b --- /dev/null +++ b/src/main/java/subway/service/LineManagerImpl.java @@ -0,0 +1,23 @@ +package subway.service; + +public class LineManagerImpl implements Managerbale{ + @Override + public void register() { + + } + + @Override + public void delete() { + + } + + @Override + public void read() { + + } + + @Override + public void goBack() { + + } +} diff --git a/src/main/java/subway/service/SectionManagerImpl.java b/src/main/java/subway/service/SectionManagerImpl.java new file mode 100644 index 000000000..c9e1d1231 --- /dev/null +++ b/src/main/java/subway/service/SectionManagerImpl.java @@ -0,0 +1,23 @@ +package subway.service; + +public class SectionManagerImpl implements Managerbale{ + @Override + public void register() { + + } + + @Override + public void delete() { + + } + + @Override + public void read() { + + } + + @Override + public void goBack() { + + } +} diff --git a/src/main/java/subway/service/StationManagerImpl.java b/src/main/java/subway/service/StationManagerImpl.java new file mode 100644 index 000000000..0013a4f58 --- /dev/null +++ b/src/main/java/subway/service/StationManagerImpl.java @@ -0,0 +1,24 @@ +package subway.service; + +public class StationManagerImpl implements Managerbale{ + + @Override + public void register() { + + } + + @Override + public void delete() { + + } + + @Override + public void read() { + + } + + @Override + public void goBack() { + + } +} From fa1e0219e07760f06945d52884ca75bf21935fb5 Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 6 May 2024 20:34:04 +0900 Subject: [PATCH 003/104] doc : add Exceptions --- src/main/java/subway/util/SubwayConstraintHandler.java | 4 ++++ src/main/java/subway/util/SubwayException.java | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 src/main/java/subway/util/SubwayConstraintHandler.java create mode 100644 src/main/java/subway/util/SubwayException.java diff --git a/src/main/java/subway/util/SubwayConstraintHandler.java b/src/main/java/subway/util/SubwayConstraintHandler.java new file mode 100644 index 000000000..be85e252c --- /dev/null +++ b/src/main/java/subway/util/SubwayConstraintHandler.java @@ -0,0 +1,4 @@ +package subway.util; + +public class SubwayConstraintHandler { +} diff --git a/src/main/java/subway/util/SubwayException.java b/src/main/java/subway/util/SubwayException.java new file mode 100644 index 000000000..2bd21543c --- /dev/null +++ b/src/main/java/subway/util/SubwayException.java @@ -0,0 +1,4 @@ +package subway.util; + +public class SubwayException { +} From d65b0a49b6d0bb66b0a585c0aadbcf67663b731f Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 6 May 2024 20:39:34 +0900 Subject: [PATCH 004/104] doc : add mainController --- src/main/java/subway/controller/MainController.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/main/java/subway/controller/MainController.java diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java new file mode 100644 index 000000000..659399ac8 --- /dev/null +++ b/src/main/java/subway/controller/MainController.java @@ -0,0 +1,4 @@ +package subway.controller; + +public class MainController { +} From a1ead405b3e6af41c8399ee1d8fdfc58d6ffe91f Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 6 May 2024 21:38:20 +0900 Subject: [PATCH 005/104] =?UTF-8?q?doc:=20=EA=B8=B0=EB=8A=A5=20=EC=A0=95?= =?UTF-8?q?=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 70 +++++++++++++++++++ src/main/java/subway/Application.java | 4 +- .../subway/controller/MainController.java | 1 + .../java/subway/service/LineManagerImpl.java | 3 +- src/main/java/subway/service/Managerbale.java | 2 +- .../subway/service/SectionManagerImpl.java | 2 +- .../subway/service/StationManagerImpl.java | 2 +- 7 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 docs/README.md diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..ea873601d --- /dev/null +++ b/docs/README.md @@ -0,0 +1,70 @@ +# 기능정의 +### 사전등록정보 +- 교대역, 강남역, 역삼역, 남부터미널역, 양재역, 양재시민의 숲역, 매봉역 + - 하행 - 상행 +- 2호선: 교대역 - 강남역 - 역삼역 +- 3호선: 교대역 - 남부터미널역 - 양재역 - 매봉역 +- 신분당선: 강남역 - 양재역 - 양재시민의숲역 + +## Managerable +- 공통 기능 추출 후 추상화 + - 역 관련 기능/지하철 노선 등록/노선 구간 추가 기능 service 코드가 확장 +- 등록/삭제/조회/(메인화면으로)돌아가기 +```aidl +public abstract class Managerable{} +``` +> interface 로 초기 기획하다가 추상 클래스로 변경함. +> **되돌아가기**을 공통적으로 갖고 있으며 기능 수행 후 **출력하는 내용**이 거의 비슷하니 확장하는 방향으로 변경. +> 맞는 선택인지 구현하면서 계속 고민해볼 것 + +### StationManagerImpl +> 역 관련 기능 +- 지하철 역 등록 + - 등록 이후 노선에 등록하지 않기 + - 노선에 등록되어 있으면 중복 등록 불가 ```(util.2)``` +- 지하철 역 삭제 + - 노선에 등록된 역은 삭제할 수 없다. ```(util.2)``` +- 지하철 역의 목록 조회 + - 지하철 역 가나다순 나열하기 + - 역 이름 앞에 **'[INFO]'** 붙이기 ```(view )``` + - 한줄씩 출력 + +### LineManagerImpl +> 지하철 노선 등록 +- 노선 등록 + 1. 노선 이름 입력 + 2. 상행 종점 이름 입력 + 3. 하행 종점 이름 입력 + 4. **'[INFO] 지하철 노선이 등록되었습니다.'** ```(view)``` +- 지하철 노선 삭제 +- 지하철 노선 목록 조회 + - **'[INFO] n 호선/신분당선'** 나열 ```(view)``` + +### SectionManagerImpl +> 노선에 구간 추가 +- 구간 등록 + 1. 등록한 노선 입력 ```(util.2 : true-continue, false - retry)``` + 2. 등록할 역 이름 입력 + 3. 노선에서 차지할 순서 입력 + 4. **'[INFO] 지하철 구간이 등록되었습니다.'** ```(view)``` + +### MapServcie +> 지하철 노선에 등록된 역 조회 +- 노선의 상행 종점부터 하행 종점까지 연결된 순으로 역 목록 조회 + + + +### util +> 에러나 제약 사항 체크 + +위반 시 **[ERROR]** 를 반드시 앞에 붙인다. + +### Exception +1. 제공되는 서비스 외 문자 입력 및 선택 시 발생 +2. 노선에 등록되어있는지 확인(조회 시) + +### SubwayConstraintHandler +1. 지하철 역/노선 이름은 2글자 이상인지 확인 +- 아니면 다시 입력 받도록 +2. 노선에 등록되어있는지 확인(수정 및 업데이트 시) +- 있으면 true 없으면 false \ No newline at end of file diff --git a/src/main/java/subway/Application.java b/src/main/java/subway/Application.java index 0bcf786cc..41325a810 100644 --- a/src/main/java/subway/Application.java +++ b/src/main/java/subway/Application.java @@ -1,10 +1,12 @@ package subway; +import subway.controller.MainController; + import java.util.Scanner; public class Application { public static void main(String[] args) { - final Scanner scanner = new Scanner(System.in); // TODO: 프로그램 구현 + MainController main = new MainController(); } } diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 659399ac8..bf5b7e7f4 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -1,4 +1,5 @@ package subway.controller; public class MainController { + } diff --git a/src/main/java/subway/service/LineManagerImpl.java b/src/main/java/subway/service/LineManagerImpl.java index aa81bbe5b..c5177efeb 100644 --- a/src/main/java/subway/service/LineManagerImpl.java +++ b/src/main/java/subway/service/LineManagerImpl.java @@ -1,9 +1,10 @@ package subway.service; -public class LineManagerImpl implements Managerbale{ +public class LineManagerImpl extends Managerbale{ @Override public void register() { + } @Override diff --git a/src/main/java/subway/service/Managerbale.java b/src/main/java/subway/service/Managerbale.java index 1597a8ac7..057ada1a5 100644 --- a/src/main/java/subway/service/Managerbale.java +++ b/src/main/java/subway/service/Managerbale.java @@ -1,6 +1,6 @@ package subway.service; -public interface Managerbale { +public abstract class Managerbale { public abstract void register(); public abstract void delete(); public abstract void read(); diff --git a/src/main/java/subway/service/SectionManagerImpl.java b/src/main/java/subway/service/SectionManagerImpl.java index c9e1d1231..7f9a8134a 100644 --- a/src/main/java/subway/service/SectionManagerImpl.java +++ b/src/main/java/subway/service/SectionManagerImpl.java @@ -1,6 +1,6 @@ package subway.service; -public class SectionManagerImpl implements Managerbale{ +public class SectionManagerImpl extends Managerbale{ @Override public void register() { diff --git a/src/main/java/subway/service/StationManagerImpl.java b/src/main/java/subway/service/StationManagerImpl.java index 0013a4f58..914f943f5 100644 --- a/src/main/java/subway/service/StationManagerImpl.java +++ b/src/main/java/subway/service/StationManagerImpl.java @@ -1,6 +1,6 @@ package subway.service; -public class StationManagerImpl implements Managerbale{ +public class StationManagerImpl extends Managerbale{ @Override public void register() { From 6ca34c6437235d378861afc3500a78ff2f5d6306 Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 6 May 2024 21:50:42 +0900 Subject: [PATCH 006/104] =?UTF-8?q?doc:=20=EA=B8=B0=EB=8A=A5=20=EC=A0=95?= =?UTF-8?q?=EC=9D=98=20=EB=B3=B4=EA=B0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/docs/README.md b/docs/README.md index ea873601d..d251b1b2b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,4 +1,6 @@ # 기능정의 +> **지하철 역과 노선을 관리하는 지하철 노선도 기능을 구현한 코드** + ### 사전등록정보 - 교대역, 강남역, 역삼역, 남부터미널역, 양재역, 양재시민의 숲역, 매봉역 - 하행 - 상행 @@ -6,7 +8,12 @@ - 3호선: 교대역 - 남부터미널역 - 양재역 - 매봉역 - 신분당선: 강남역 - 양재역 - 양재시민의숲역 -## Managerable +# Controller +- 사용자 입력과 Service 코드 매칭 +- 유효한 입력인지 확인 ```(Exception 1)``` + +# Service +### Managerable - 공통 기능 추출 후 추상화 - 역 관련 기능/지하철 노선 등록/노선 구간 추가 기능 service 코드가 확장 - 등록/삭제/조회/(메인화면으로)돌아가기 @@ -21,14 +28,17 @@ public abstract class Managerable{} > 역 관련 기능 - 지하철 역 등록 - 등록 이후 노선에 등록하지 않기 - - 노선에 등록되어 있으면 중복 등록 불가 ```(util.2)``` + - 노선에 등록되어 있으면 중복 등록 불가 ```(SubwayConstraintHandler.2)``` - 지하철 역 삭제 - - 노선에 등록된 역은 삭제할 수 없다. ```(util.2)``` + - 노선에 등록된 역은 삭제할 수 없다. ```(SubwayConstraintHandler.2)``` - 지하철 역의 목록 조회 - 지하철 역 가나다순 나열하기 - 역 이름 앞에 **'[INFO]'** 붙이기 ```(view )``` - 한줄씩 출력 +- View 코드에서 출력 포맷 활용하여 구현하고 호출할 때 매개변수로 구문이 다르게 나가도록 + - 출력 포맷 적극 활용할 것 + ### LineManagerImpl > 지하철 노선 등록 - 노선 등록 @@ -43,7 +53,7 @@ public abstract class Managerable{} ### SectionManagerImpl > 노선에 구간 추가 - 구간 등록 - 1. 등록한 노선 입력 ```(util.2 : true-continue, false - retry)``` + 1. 등록한 노선 입력 ```(SubwayConstraintHandler.2 : true-continue, false - retry)``` 2. 등록할 역 이름 입력 3. 노선에서 차지할 순서 입력 4. **'[INFO] 지하철 구간이 등록되었습니다.'** ```(view)``` @@ -54,17 +64,19 @@ public abstract class Managerable{} -### util +# util > 에러나 제약 사항 체크 위반 시 **[ERROR]** 를 반드시 앞에 붙인다. ### Exception +Controller 에서 주로 사용되는 예외 1. 제공되는 서비스 외 문자 입력 및 선택 시 발생 -2. 노선에 등록되어있는지 확인(조회 시) ### SubwayConstraintHandler +Service 에서 주로 사용되는 예외 1. 지하철 역/노선 이름은 2글자 이상인지 확인 -- 아니면 다시 입력 받도록 + - 아니면 다시 입력 받도록 2. 노선에 등록되어있는지 확인(수정 및 업데이트 시) -- 있으면 true 없으면 false \ No newline at end of file + - 있으면 true 없으면 false +3. 노선에 등록되어있는지 확인(조회 시) From 0e05d2effb1d37b68abcb6e8a8f94a3cf66abc44 Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 6 May 2024 22:11:19 +0900 Subject: [PATCH 007/104] =?UTF-8?q?doc:=20=EC=A7=88=EB=AC=B8=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=ED=95=9C=20=EB=B7=B0=20=EC=84=B8=ED=8C=85=20setting?= =?UTF-8?q?=20view=20related=20with=20Question?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/view/Ask.java | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/main/java/subway/view/Ask.java diff --git a/src/main/java/subway/view/Ask.java b/src/main/java/subway/view/Ask.java new file mode 100644 index 000000000..2a585add0 --- /dev/null +++ b/src/main/java/subway/view/Ask.java @@ -0,0 +1,35 @@ +package subway.view; + +import java.io.StringWriter; + +public class Ask { + private StringWriter sb = new StringWriter(); + + public void Main(){ + sb.append("## 메인 화면\n"); + sb.append("1. 역 관리\n"); + sb.append("2. 노선 관리\n"); + sb.append("3. 구간 관리\n"); + sb.append("4. 지하철 노선도 출력\n"); + sb.append("Q. 종료\n"); + sb.flush(); + } + + public void WhatToManage(String target){ + sb.append("## " + target + " 관리 화면\n"); + sb.append("1. "+target + "등록\n"); + sb.append("2. "+target + "삭제\n"); + sb.append("3. "+target + "조회\n"); + sb.append("B. 돌아가기\n"); + } + + public void Function(){ + sb.append("## 원하는 기능을 선택하세요\n"); + sb.flush(); + } + + public void Name(String work, String name){ // 등록 or 삭제 + sb.append("## "+work+"할 " + name + " 이름을 입력하세요\n"); + sb.flush(); + } +} From 1e0abc3ca9e2498bed5199aefdff8906a9449159 Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 6 May 2024 22:12:29 +0900 Subject: [PATCH 008/104] =?UTF-8?q?doc:=20=EC=A7=88=EB=AC=B8=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=ED=95=9C=20=EB=B7=B0=20=EC=84=B8=ED=8C=85=20setting?= =?UTF-8?q?=20view=20related=20with=20Question?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/view/Ask.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/subway/view/Ask.java b/src/main/java/subway/view/Ask.java index 2a585add0..d3ab5f41e 100644 --- a/src/main/java/subway/view/Ask.java +++ b/src/main/java/subway/view/Ask.java @@ -21,6 +21,7 @@ public void WhatToManage(String target){ sb.append("2. "+target + "삭제\n"); sb.append("3. "+target + "조회\n"); sb.append("B. 돌아가기\n"); + sb.flush(); } public void Function(){ From 1183107b0ce7139d8c4e27be65727308e9435b62 Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 6 May 2024 22:24:00 +0900 Subject: [PATCH 009/104] =?UTF-8?q?doc:=20=EC=98=A4=ED=83=80=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- .../subway/service/{LineManagerImpl.java => LineManager.java} | 0 .../service/{SectionManagerImpl.java => SectionManager.java} | 0 .../service/{StationManagerImpl.java => StationManager.java} | 0 4 files changed, 1 insertion(+), 1 deletion(-) rename src/main/java/subway/service/{LineManagerImpl.java => LineManager.java} (100%) rename src/main/java/subway/service/{SectionManagerImpl.java => SectionManager.java} (100%) rename src/main/java/subway/service/{StationManagerImpl.java => StationManager.java} (100%) diff --git a/docs/README.md b/docs/README.md index d251b1b2b..8d3072577 100644 --- a/docs/README.md +++ b/docs/README.md @@ -69,7 +69,7 @@ public abstract class Managerable{} 위반 시 **[ERROR]** 를 반드시 앞에 붙인다. -### Exception +### SubwayException Controller 에서 주로 사용되는 예외 1. 제공되는 서비스 외 문자 입력 및 선택 시 발생 diff --git a/src/main/java/subway/service/LineManagerImpl.java b/src/main/java/subway/service/LineManager.java similarity index 100% rename from src/main/java/subway/service/LineManagerImpl.java rename to src/main/java/subway/service/LineManager.java diff --git a/src/main/java/subway/service/SectionManagerImpl.java b/src/main/java/subway/service/SectionManager.java similarity index 100% rename from src/main/java/subway/service/SectionManagerImpl.java rename to src/main/java/subway/service/SectionManager.java diff --git a/src/main/java/subway/service/StationManagerImpl.java b/src/main/java/subway/service/StationManager.java similarity index 100% rename from src/main/java/subway/service/StationManagerImpl.java rename to src/main/java/subway/service/StationManager.java From 05abd7572119079f23431df1b9ce31209d8eb9cd Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 6 May 2024 22:24:50 +0900 Subject: [PATCH 010/104] =?UTF-8?q?doc:=20=EC=9D=B4=EB=A6=84=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20fix=20these=20file=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/service/LineManager.java | 2 +- src/main/java/subway/service/SectionManager.java | 2 +- src/main/java/subway/service/StationManager.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index c5177efeb..8eb3fd618 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -1,6 +1,6 @@ package subway.service; -public class LineManagerImpl extends Managerbale{ +public class LineManager extends Managerbale{ @Override public void register() { diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 7f9a8134a..ceebd82ba 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -1,6 +1,6 @@ package subway.service; -public class SectionManagerImpl extends Managerbale{ +public class SectionManager extends Managerbale{ @Override public void register() { diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index 914f943f5..eddf47fec 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -1,6 +1,6 @@ package subway.service; -public class StationManagerImpl extends Managerbale{ +public class StationManager extends Managerbale{ @Override public void register() { From 696159d8e808e3a1bc2884181d8fff7d097f17f6 Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 6 May 2024 22:43:42 +0900 Subject: [PATCH 011/104] =?UTF-8?q?doc:=20=EC=84=9C=EB=B9=84=EC=8A=A4=20?= =?UTF-8?q?=EC=B4=88=EA=B8=B0=20=ED=9D=90=EB=A6=84=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/LineController.java | 6 +++++ .../subway/controller/MainController.java | 26 +++++++++++++++++++ .../subway/controller/SectionController.java | 5 ++++ .../subway/controller/StationController.java | 4 +++ 4 files changed, 41 insertions(+) create mode 100644 src/main/java/subway/controller/LineController.java create mode 100644 src/main/java/subway/controller/SectionController.java create mode 100644 src/main/java/subway/controller/StationController.java diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java new file mode 100644 index 000000000..13f0ef61d --- /dev/null +++ b/src/main/java/subway/controller/LineController.java @@ -0,0 +1,6 @@ +package subway.controller; + +public class LineController { + public LineController(){} + +} diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index bf5b7e7f4..4a59f09ba 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -1,5 +1,31 @@ package subway.controller; +import subway.view.Ask; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + public class MainController { + Ask ask = new Ask(); + public MainController(){ + ask.Main(); + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + try { + String command = br.readLine(); + if(command.equals("Q")) { + System.out.println(" 안녕히 가세요. "); + return; + } + int validCommand = Integer.parseInt(command); + CoreController(validCommand); + }catch (Exception e){ + e.printStackTrace(); + } + } + public void CoreController(int command){ + if(command == 1) new StationController(); + if(command == 2) new LineController(); + if(command == 3) new SectionController(); + } } diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java new file mode 100644 index 000000000..d2e6a6a0b --- /dev/null +++ b/src/main/java/subway/controller/SectionController.java @@ -0,0 +1,5 @@ +package subway.controller; + +public class SectionController { + public SectionController(){} +} diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java new file mode 100644 index 000000000..a59abf01c --- /dev/null +++ b/src/main/java/subway/controller/StationController.java @@ -0,0 +1,4 @@ +package subway.controller; + +public class StationController { +} From fb196804314c928cdadf34f2f172cc8e17dad391 Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 6 May 2024 22:55:49 +0900 Subject: [PATCH 012/104] =?UTF-8?q?refactor:=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC,=20headController=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/MainController.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 4a59f09ba..62ab7f7b1 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -9,20 +9,25 @@ public class MainController { Ask ask = new Ask(); public MainController(){ ask.Main(); - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - try { - String command = br.readLine(); - if(command.equals("Q")) { - System.out.println(" 안녕히 가세요. "); - return; - } - int validCommand = Integer.parseInt(command); - CoreController(validCommand); + try{ + headController(); }catch (Exception e){ e.printStackTrace(); } } + public void headController() throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String command = br.readLine(); + if(command.equals("Q")) { + System.out.println(" 안녕히 가세요. "); + return; + } + int validCommand = Integer.parseInt(command); + CoreController(validCommand); + headController(); + } + public void CoreController(int command){ if(command == 1) new StationController(); if(command == 2) new LineController(); From 70df824cdc3bc937898cde2bc4a12c06f432577b Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 6 May 2024 23:00:20 +0900 Subject: [PATCH 013/104] =?UTF-8?q?refactor:=20=EA=B0=80=EB=8F=85=EC=84=B1?= =?UTF-8?q?=EC=9D=84=20=EC=9C=84=ED=95=B4=20=ED=8F=B4=EB=8D=94=20=EB=B0=8F?= =?UTF-8?q?=20=EB=B3=80=EC=88=98=20=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 5 +++-- .../subway/{util => config}/SubwayConstraintHandler.java | 2 +- src/main/java/subway/{util => config}/SubwayException.java | 2 +- src/main/java/subway/controller/MainController.java | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-) rename src/main/java/subway/{util => config}/SubwayConstraintHandler.java (64%) rename src/main/java/subway/{util => config}/SubwayException.java (59%) diff --git a/docs/README.md b/docs/README.md index 8d3072577..22c96d5d8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -64,8 +64,9 @@ public abstract class Managerable{} -# util -> 에러나 제약 사항 체크 +# config +> 1. 에러나 제약 사항 체크 +> 2. 상수값 모음 위반 시 **[ERROR]** 를 반드시 앞에 붙인다. diff --git a/src/main/java/subway/util/SubwayConstraintHandler.java b/src/main/java/subway/config/SubwayConstraintHandler.java similarity index 64% rename from src/main/java/subway/util/SubwayConstraintHandler.java rename to src/main/java/subway/config/SubwayConstraintHandler.java index be85e252c..e2927ba5a 100644 --- a/src/main/java/subway/util/SubwayConstraintHandler.java +++ b/src/main/java/subway/config/SubwayConstraintHandler.java @@ -1,4 +1,4 @@ -package subway.util; +package subway.config; public class SubwayConstraintHandler { } diff --git a/src/main/java/subway/util/SubwayException.java b/src/main/java/subway/config/SubwayException.java similarity index 59% rename from src/main/java/subway/util/SubwayException.java rename to src/main/java/subway/config/SubwayException.java index 2bd21543c..cabec8c48 100644 --- a/src/main/java/subway/util/SubwayException.java +++ b/src/main/java/subway/config/SubwayException.java @@ -1,4 +1,4 @@ -package subway.util; +package subway.config; public class SubwayException { } diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 62ab7f7b1..6d5637d74 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -6,9 +6,9 @@ import java.io.InputStreamReader; public class MainController { - Ask ask = new Ask(); + Ask askView = new Ask(); public MainController(){ - ask.Main(); + askView.Main(); try{ headController(); }catch (Exception e){ From ba446953f5009d6408107f3ead93e34f6ce44bc7 Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 6 May 2024 23:38:40 +0900 Subject: [PATCH 014/104] =?UTF-8?q?refactor:=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EB=94=94=EB=A0=89=ED=86=A0=EB=A6=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/config/{ => handler}/SubwayConstraintHandler.java | 2 +- src/main/java/subway/config/{ => handler}/SubwayException.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/main/java/subway/config/{ => handler}/SubwayConstraintHandler.java (57%) rename src/main/java/subway/config/{ => handler}/SubwayException.java (52%) diff --git a/src/main/java/subway/config/SubwayConstraintHandler.java b/src/main/java/subway/config/handler/SubwayConstraintHandler.java similarity index 57% rename from src/main/java/subway/config/SubwayConstraintHandler.java rename to src/main/java/subway/config/handler/SubwayConstraintHandler.java index e2927ba5a..b986bef7d 100644 --- a/src/main/java/subway/config/SubwayConstraintHandler.java +++ b/src/main/java/subway/config/handler/SubwayConstraintHandler.java @@ -1,4 +1,4 @@ -package subway.config; +package subway.config.handler; public class SubwayConstraintHandler { } diff --git a/src/main/java/subway/config/SubwayException.java b/src/main/java/subway/config/handler/SubwayException.java similarity index 52% rename from src/main/java/subway/config/SubwayException.java rename to src/main/java/subway/config/handler/SubwayException.java index cabec8c48..3f7d6e368 100644 --- a/src/main/java/subway/config/SubwayException.java +++ b/src/main/java/subway/config/handler/SubwayException.java @@ -1,4 +1,4 @@ -package subway.config; +package subway.config.handler; public class SubwayException { } From 2059d6b24e84a9fb9564cce1de0c70183b62e702 Mon Sep 17 00:00:00 2001 From: aristo Date: Tue, 7 May 2024 14:36:03 +0900 Subject: [PATCH 015/104] =?UTF-8?q?feat:=20StationController=20=ED=86=A0?= =?UTF-8?q?=EB=8C=80=20=EA=B0=96=EC=B6=94=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/MainController.java | 7 ++-- .../subway/controller/StationController.java | 33 +++++++++++++++++++ .../subway/view/{Ask.java => AskView.java} | 2 +- 3 files changed, 38 insertions(+), 4 deletions(-) rename src/main/java/subway/view/{Ask.java => AskView.java} (97%) diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 6d5637d74..ef18b6b87 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -1,14 +1,15 @@ package subway.controller; -import subway.view.Ask; +import subway.view.AskView; import java.io.BufferedReader; import java.io.InputStreamReader; public class MainController { - Ask askView = new Ask(); + AskView ask = new AskView(); + StationController stationController = new StationController(); public MainController(){ - askView.Main(); + ask.Main(); try{ headController(); }catch (Exception e){ diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index a59abf01c..0a7c7ecdb 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -1,4 +1,37 @@ package subway.controller; +import subway.view.AskView; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + public class StationController { + AskView ask = new AskView(); + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + public void askQuestion(){ + ask.Function(); + try{ + String command = br.readLine(); + // command 내용 확인하고 맞는 처리와 연결 + }catch (Exception e){ + e.printStackTrace(); + } + } + + public int registerControll() throws Exception{ + ask.Name("등록", "역"); + String station = br.readLine(); + register(station); + // "지하철 역이 등록되었습니다." 출력 + return Integer.parseInt(br.readLine()); + } + + public void register(String station){ + // db 접근해서 데이터 삽입하는 서비스와 연결 + } + + public void requestStations() throws Exception{ + // 역 목록을 출력하는 서비스 코 + } } diff --git a/src/main/java/subway/view/Ask.java b/src/main/java/subway/view/AskView.java similarity index 97% rename from src/main/java/subway/view/Ask.java rename to src/main/java/subway/view/AskView.java index d3ab5f41e..1bb8c12a7 100644 --- a/src/main/java/subway/view/Ask.java +++ b/src/main/java/subway/view/AskView.java @@ -2,7 +2,7 @@ import java.io.StringWriter; -public class Ask { +public class AskView { private StringWriter sb = new StringWriter(); public void Main(){ From b913481cf51889a5d88a89abccb0e94bc2dfeb0c Mon Sep 17 00:00:00 2001 From: aristo Date: Tue, 7 May 2024 14:52:23 +0900 Subject: [PATCH 016/104] =?UTF-8?q?feat:=20Contoller=20=EA=B3=B5=ED=86=B5?= =?UTF-8?q?=20=EB=82=B4=EC=9A=A9=20=EC=B6=94=EC=83=81=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/LineController.java | 17 ++++++++- .../subway/controller/MainController.java | 3 +- .../subway/controller/ManageController.java | 33 +++++++++++++++++ .../subway/controller/StationController.java | 37 +++++++++++-------- 4 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 src/main/java/subway/controller/ManageController.java diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 13f0ef61d..19b09c8bd 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -1,6 +1,21 @@ package subway.controller; +import subway.view.AskView; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + public class LineController { + AskView ask = new AskView(); + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public LineController(){} - + public void work(){ + ask.Function(); + try{ + String command = br.readLine(); + // command 내용 확인하고 맞는 처리와 연결 + }catch (Exception e){ + e.printStackTrace(); + } + } } diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index ef18b6b87..312465e16 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -29,8 +29,9 @@ public void headController() throws Exception{ headController(); } + // 생성자와 함께 출력과 서비스를 제공하려 했는데 무슨 일 할 때마다 클래스 생성하는 것은 비효율적인 것 같다. 메소드로 바꾸자. public void CoreController(int command){ - if(command == 1) new StationController(); + if(command == 1) stationController.work(); if(command == 2) new LineController(); if(command == 3) new SectionController(); } diff --git a/src/main/java/subway/controller/ManageController.java b/src/main/java/subway/controller/ManageController.java new file mode 100644 index 000000000..c9992752e --- /dev/null +++ b/src/main/java/subway/controller/ManageController.java @@ -0,0 +1,33 @@ +package subway.controller; + +import subway.view.AskView; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +public abstract class ManageController { + AskView ask = new AskView(); + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + public void work(){ + ask.Function(); + // 관련 컨트롤러 호출 부분 + } + + public void registerControll() { + // 등록하기 + } + + public void register(String station){ + // db 접근해서 데이터 삽입하는 서비스와 연결 + } + + public void delete(String station){ + // db 접근해서 데이터 삭제하는 서비스와 연결 + } + + // 되돌아가기 + public void goBack(){ + return; + } +} diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 0a7c7ecdb..b8ef3e44d 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -1,15 +1,8 @@ package subway.controller; -import subway.view.AskView; - -import java.io.BufferedReader; -import java.io.InputStreamReader; - -public class StationController { - AskView ask = new AskView(); - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - - public void askQuestion(){ +public class StationController extends ManageController{ + @Override + public void work(){ ask.Function(); try{ String command = br.readLine(); @@ -19,19 +12,31 @@ public void askQuestion(){ } } - public int registerControll() throws Exception{ + public void registerControll(){ ask.Name("등록", "역"); - String station = br.readLine(); - register(station); - // "지하철 역이 등록되었습니다." 출력 - return Integer.parseInt(br.readLine()); + try{ + String station = br.readLine(); + register(station); + // "지하철 역이 등록되었습니다." 출력 + }catch (Exception e){ + e.printStackTrace(); + } } public void register(String station){ // db 접근해서 데이터 삽입하는 서비스와 연결 } + public void delete(String station){ + // db 접근해서 데이터 삭제하는 서비스와 연결 + } + public void requestStations() throws Exception{ - // 역 목록을 출력하는 서비스 코 + // 역 목록을 출력하는 서비스 코드 + } + + // 되돌아가 + public void goBack(){ + return; } } From 7bdec4cd538d8986722a461fb8265d81ea29ad97 Mon Sep 17 00:00:00 2001 From: aristo Date: Tue, 7 May 2024 14:59:34 +0900 Subject: [PATCH 017/104] =?UTF-8?q?feat:=20controller=20=EB=93=A4=EC=97=90?= =?UTF-8?q?=EA=B2=8C=20=EC=B6=94=EC=83=81=ED=99=94=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/LineController.java | 25 +++++++++++++------ .../subway/controller/ManageController.java | 9 +++---- .../subway/controller/SectionController.java | 22 +++++++++++++++- .../subway/controller/StationController.java | 8 +++--- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 19b09c8bd..255af4744 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -1,14 +1,8 @@ package subway.controller; -import subway.view.AskView; - -import java.io.BufferedReader; -import java.io.InputStreamReader; - -public class LineController { - AskView ask = new AskView(); - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); +public class LineController extends ManageController{ public LineController(){} + @Override public void work(){ ask.Function(); try{ @@ -18,4 +12,19 @@ public void work(){ e.printStackTrace(); } } + + @Override + public void registerControll() { + + } + + @Override + public void register(String station) { + + } + + @Override + public void delete(String station) { + + } } diff --git a/src/main/java/subway/controller/ManageController.java b/src/main/java/subway/controller/ManageController.java index c9992752e..3fc66642d 100644 --- a/src/main/java/subway/controller/ManageController.java +++ b/src/main/java/subway/controller/ManageController.java @@ -14,17 +14,14 @@ public void work(){ // 관련 컨트롤러 호출 부분 } - public void registerControll() { + public abstract void registerControll() ; // 등록하기 - } - public void register(String station){ + public abstract void register(String station); // db 접근해서 데이터 삽입하는 서비스와 연결 - } - public void delete(String station){ + public abstract void delete(String station); // db 접근해서 데이터 삭제하는 서비스와 연결 - } // 되돌아가기 public void goBack(){ diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index d2e6a6a0b..5e9e81b24 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -1,5 +1,25 @@ package subway.controller; -public class SectionController { +public class SectionController extends ManageController{ public SectionController(){} + + @Override + public void work() { + + } + + @Override + public void registerControll() { + + } + + @Override + public void register(String station) { + + } + + @Override + public void delete(String station) { + + } } diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index b8ef3e44d..27cfe0a1d 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -12,6 +12,7 @@ public void work(){ } } + @Override public void registerControll(){ ask.Name("등록", "역"); try{ @@ -23,10 +24,12 @@ public void registerControll(){ } } + @Override public void register(String station){ // db 접근해서 데이터 삽입하는 서비스와 연결 } + @Override public void delete(String station){ // db 접근해서 데이터 삭제하는 서비스와 연결 } @@ -34,9 +37,4 @@ public void delete(String station){ public void requestStations() throws Exception{ // 역 목록을 출력하는 서비스 코드 } - - // 되돌아가 - public void goBack(){ - return; - } } From 17c896da3d0777a20fa53d27ac3c6deab3191925 Mon Sep 17 00:00:00 2001 From: aristo Date: Tue, 7 May 2024 15:07:11 +0900 Subject: [PATCH 018/104] =?UTF-8?q?refactor:=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/controller/LineController.java | 8 ++++---- src/main/java/subway/controller/MainController.java | 10 ++++++++-- src/main/java/subway/controller/SectionController.java | 10 +++++++--- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 255af4744..21676d936 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -1,7 +1,6 @@ package subway.controller; public class LineController extends ManageController{ - public LineController(){} @Override public void work(){ ask.Function(); @@ -15,16 +14,17 @@ public void work(){ @Override public void registerControll() { - + // 노선 등록하기 + //상행성 종점과 하행성 종점 입력 } @Override public void register(String station) { - + // 새로운 노선 db 와 연동하여 생성하기 } @Override public void delete(String station) { - + // 노선 db 에서 삭제 } } diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 312465e16..c2d76012c 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -8,6 +8,9 @@ public class MainController { AskView ask = new AskView(); StationController stationController = new StationController(); + LineController lineController = new LineController(); + SectionController sectionController = new SectionController(); + public MainController(){ ask.Main(); try{ @@ -24,15 +27,18 @@ public void headController() throws Exception{ System.out.println(" 안녕히 가세요. "); return; } + + // command 가 유효한 명령이 아닐 경우 코드 추가 int validCommand = Integer.parseInt(command); CoreController(validCommand); + ask.Main(); headController(); } // 생성자와 함께 출력과 서비스를 제공하려 했는데 무슨 일 할 때마다 클래스 생성하는 것은 비효율적인 것 같다. 메소드로 바꾸자. public void CoreController(int command){ if(command == 1) stationController.work(); - if(command == 2) new LineController(); - if(command == 3) new SectionController(); + if(command == 2) lineController.work(); + if(command == 3) sectionController.work(); } } diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 5e9e81b24..7fcec3a65 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -1,11 +1,15 @@ package subway.controller; public class SectionController extends ManageController{ - public SectionController(){} - @Override public void work() { - + ask.Function(); + try{ + String command = br.readLine(); + // command 내용 확인하고 맞는 처리와 연결 + }catch (Exception e){ + e.printStackTrace(); + } } @Override From 1eb50908e386906c8e7e49ac73a3b3d406c34631 Mon Sep 17 00:00:00 2001 From: aristo Date: Tue, 7 May 2024 16:06:26 +0900 Subject: [PATCH 019/104] =?UTF-8?q?feet:=20Station=20Manager=20=EC=B4=88?= =?UTF-8?q?=EA=B8=B0=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/service/Managerbale.java | 14 +++++++-- .../java/subway/service/StationManager.java | 29 +++++++++++++------ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/main/java/subway/service/Managerbale.java b/src/main/java/subway/service/Managerbale.java index 057ada1a5..856f8d9bd 100644 --- a/src/main/java/subway/service/Managerbale.java +++ b/src/main/java/subway/service/Managerbale.java @@ -1,8 +1,16 @@ package subway.service; +import subway.domain.LineRepository; +import subway.domain.StationRepository; + public abstract class Managerbale { - public abstract void register(); - public abstract void delete(); + StationRepository stationRepo = new StationRepository(); + LineRepository lineRepo = new LineRepository(); + public abstract void register(String command); + public abstract void delete(String name); public abstract void read(); - public abstract void goBack(); + + public void goBack() { + return; + } } diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index eddf47fec..18f411bb2 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -1,24 +1,35 @@ package subway.service; -public class StationManager extends Managerbale{ +import subway.domain.Station; - @Override - public void register() { +import java.util.List; +public class StationManager extends Managerbale{ + @Override // 삽입 + public void register(String command) { +// if command.equals("station") 역만 삽입하는 모듈 연결 +// if command.equals("line") 노선 삽입하는 모듈 연결 +// if command.equals("Section") 구간 삽입하는 모듈 연결 } - @Override - public void delete() { - + public void insertStation(String node){ + // 이미 해당 역이 존재하는지 아닌지 확인하는 코드 추가 (util 로 만들기) + // 존재하지 않는다 + Station station = new Station(node); + stationRepo.addStation(station); } @Override - public void read() { - + public void delete(String name) { + stationRepo.deleteStation(name); } @Override - public void goBack() { + public void read() { +// 역 조회 + } + public List readStation(){ + return stationRepo.stations(); } } From c499133c5afc037bc02c97ff39f626d6f3927ab8 Mon Sep 17 00:00:00 2001 From: aristo Date: Tue, 7 May 2024 16:16:20 +0900 Subject: [PATCH 020/104] =?UTF-8?q?feat:=20line=20manager=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=B4=88=EA=B8=B0=20=EC=84=B8=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/domain/Line.java | 7 +++++ src/main/java/subway/service/LineManager.java | 26 ++++++++++++++----- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index f4d738d5a..225f275a9 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -2,6 +2,8 @@ public class Line { private String name; + private Station upperStation; + private Station bottomStation; public Line(String name) { this.name = name; @@ -11,5 +13,10 @@ public String getName() { return name; } + public void setStations(Station upper, Station bottom){ + this.upperStation = upper; + this.bottomStation=bottom; + } + // 추가 기능 구현 } diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index 8eb3fd618..4710834c0 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -1,15 +1,28 @@ package subway.service; +import subway.domain.Line; +import subway.domain.LineRepository; +import subway.domain.Station; + +import java.util.List; + public class LineManager extends Managerbale{ @Override - public void register() { - + public void register(String name) { + Line created = new Line(name); + lineRepo.addLine(created); + } + public void setStations(String name, String upperStation, String bottomStation){ + Line created = new Line(name); + lineRepo.addLine(created); + // upper, bottom 을 찾고 아래 매개변수에 대입하는 코드 추가 + created.setStations(new Station(upperStation), new Station(bottomStation)); } @Override - public void delete() { - + public void delete(String name) { + lineRepo.deleteLineByName(name); } @Override @@ -17,8 +30,7 @@ public void read() { } - @Override - public void goBack() { - + public List readLines(){ + return lineRepo.lines(); } } From 22b43bedfe1105c402d24c317d2e1761e3f1070a Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 8 May 2024 16:01:18 +0900 Subject: [PATCH 021/104] =?UTF-8?q?doc:=20SectionManager=20=EA=B5=AC?= =?UTF-8?q?=EC=84=B1=20=EA=B3=84=ED=9A=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/domain/Line.java | 5 ++ src/main/java/subway/service/DataManager.java | 51 +++++++++++++++++++ .../java/subway/service/SectionManager.java | 16 +++++- 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 src/main/java/subway/service/DataManager.java diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index 225f275a9..1fb17bc22 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -1,7 +1,10 @@ package subway.domain; +import java.util.LinkedList; + public class Line { private String name; + private LinkedList stations = new LinkedList<>(); private Station upperStation; private Station bottomStation; @@ -16,6 +19,8 @@ public String getName() { public void setStations(Station upper, Station bottom){ this.upperStation = upper; this.bottomStation=bottom; + stations.add(upper); + stations.add(bottom); } // 추가 기능 구현 diff --git a/src/main/java/subway/service/DataManager.java b/src/main/java/subway/service/DataManager.java new file mode 100644 index 000000000..cea84a584 --- /dev/null +++ b/src/main/java/subway/service/DataManager.java @@ -0,0 +1,51 @@ +package subway.service; + +import subway.domain.Line; +import subway.domain.LineRepository; +import subway.domain.Station; +import subway.domain.StationRepository; + +import java.util.List; + +public class DataManager extends Managerbale{ + public DataManager(){ + // 초기값 삽입 + } + @Override // 삽입 + public void register(String command) { +// if command.equals("station") 역만 삽입하는 모듈 연결 +// if command.equals("line") 노선 삽입하는 모듈 연결 +// if command.equals("Section") 구간 삽입하는 모듈 연결 + } + + public void insertStation(String node){ + // 이미 해당 역이 존재하는지 아닌지 확인하는 코드 추가 (util 로 만들기) + Station station = new Station(node); + stationRepo.addStation(station); + } + + public void insertLine(String upperStation, String bottomStation, String name){ + List stations = stationRepo.stations(); + // 이미 해당 역이 존재하는지 아닌지 확인하는 코드 추가 + Station upper = new Station(upperStation); + Station bottom = new Station(bottomStation); + stationRepo.addStation(upper); + stationRepo.addStation(bottom); + Line created = new Line(name); + lineRepo.addLine(created); + } + + public void insertSection(){ + + } + + @Override // 삭제 + public void delete(String name) { + + } + + @Override // 조회 + public void read() { + + } +} diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index ceebd82ba..03bceb5d1 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -1,13 +1,25 @@ package subway.service; +import subway.domain.Line; +import subway.domain.Station; + +import java.util.List; + public class SectionManager extends Managerbale{ @Override - public void register() { + public void register(String name) { + } + + public void insertSection(String name, int index){ + // 이미 해당 역이 등록되어 있는지 확인 + // 등록되지 않았으면 + Station created = new Station(name); + List line = lineRepo.lines(); } @Override - public void delete() { + public void delete(String name) { } From 88eaffb6ad078bd590610f0db09820a0316ae6ae Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 8 May 2024 16:20:57 +0900 Subject: [PATCH 022/104] =?UTF-8?q?feat:=20=EC=9D=B4=EB=A6=84=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=A1=B0=ED=9A=8C=ED=95=98=EB=8A=94=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/domain/Line.java | 14 ++++++++++++++ src/main/java/subway/domain/StationRepository.java | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index 1fb17bc22..48ef4a632 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -1,6 +1,7 @@ package subway.domain; import java.util.LinkedList; +import java.util.ListIterator; public class Line { private String name; @@ -23,5 +24,18 @@ public void setStations(Station upper, Station bottom){ stations.add(bottom); } + public void insertStation(Station node, int index){ + stations.add(index, node); + } + // 추가 기능 구현 + public Station getStationByName(String name){ + ListIterator iter = stations.listIterator(); + for(Station station : stations){ + if(station.getName().equals(name)){ + return station; + } + } + return null; + } } diff --git a/src/main/java/subway/domain/StationRepository.java b/src/main/java/subway/domain/StationRepository.java index b7245c0f3..e59065dcb 100644 --- a/src/main/java/subway/domain/StationRepository.java +++ b/src/main/java/subway/domain/StationRepository.java @@ -19,4 +19,12 @@ public static void addStation(Station station) { public static boolean deleteStation(String name) { return stations.removeIf(station -> Objects.equals(station.getName(), name)); } + public static Station getStationByName(String name){ + for(Station station : stations){ + if(station.getName().equals(name)){ + return station; + } + } + return null; + } } From e88d9d0e92139401ffd94999fbebf598f5963603 Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 8 May 2024 16:58:25 +0900 Subject: [PATCH 023/104] =?UTF-8?q?feat:=20view=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EC=98=A4=EB=A5=98=20=ED=95=B4=EA=B2=B0,?= =?UTF-8?q?=20stringWriter=20->=20Stringbuffer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/Application.java | 3 ++- .../subway/controller/MainController.java | 8 +----- .../subway/controller/ManageController.java | 2 +- .../subway/controller/StationController.java | 1 + src/main/java/subway/domain/Line.java | 1 - .../java/subway/domain/LineRepository.java | 9 +++++++ src/main/java/subway/view/AskView.java | 25 +++++++++++-------- 7 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/main/java/subway/Application.java b/src/main/java/subway/Application.java index 41325a810..e5efb3f57 100644 --- a/src/main/java/subway/Application.java +++ b/src/main/java/subway/Application.java @@ -5,8 +5,9 @@ import java.util.Scanner; public class Application { - public static void main(String[] args) { + public static void main(String[] args) throws Exception { // TODO: 프로그램 구현 MainController main = new MainController(); + main.headController(); } } diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index c2d76012c..c14bf185c 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -12,12 +12,7 @@ public class MainController { SectionController sectionController = new SectionController(); public MainController(){ - ask.Main(); - try{ - headController(); - }catch (Exception e){ - e.printStackTrace(); - } + ask.printMain(); } public void headController() throws Exception{ @@ -31,7 +26,6 @@ public void headController() throws Exception{ // command 가 유효한 명령이 아닐 경우 코드 추가 int validCommand = Integer.parseInt(command); CoreController(validCommand); - ask.Main(); headController(); } diff --git a/src/main/java/subway/controller/ManageController.java b/src/main/java/subway/controller/ManageController.java index 3fc66642d..2162b4b3c 100644 --- a/src/main/java/subway/controller/ManageController.java +++ b/src/main/java/subway/controller/ManageController.java @@ -10,7 +10,7 @@ public abstract class ManageController { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public void work(){ - ask.Function(); +// ask.Function(); // 관련 컨트롤러 호출 부분 } diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 27cfe0a1d..23b9f3a84 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -3,6 +3,7 @@ public class StationController extends ManageController{ @Override public void work(){ + ask.WhatToManage("역"); ask.Function(); try{ String command = br.readLine(); diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index 48ef4a632..8dcbdf61e 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -30,7 +30,6 @@ public void insertStation(Station node, int index){ // 추가 기능 구현 public Station getStationByName(String name){ - ListIterator iter = stations.listIterator(); for(Station station : stations){ if(station.getName().equals(name)){ return station; diff --git a/src/main/java/subway/domain/LineRepository.java b/src/main/java/subway/domain/LineRepository.java index 49132ddb6..ec5450017 100644 --- a/src/main/java/subway/domain/LineRepository.java +++ b/src/main/java/subway/domain/LineRepository.java @@ -19,4 +19,13 @@ public static void addLine(Line line) { public static boolean deleteLineByName(String name) { return lines.removeIf(line -> Objects.equals(line.getName(), name)); } + + public static Line getLineByName(String name){ + for(Line line : lines){ + if(line.getName().equals(name)){ + return line; + } + } + return null; + } } diff --git a/src/main/java/subway/view/AskView.java b/src/main/java/subway/view/AskView.java index 1bb8c12a7..78616bc7b 100644 --- a/src/main/java/subway/view/AskView.java +++ b/src/main/java/subway/view/AskView.java @@ -1,36 +1,39 @@ package subway.view; -import java.io.StringWriter; - public class AskView { - private StringWriter sb = new StringWriter(); + private static StringBuilder sb = new StringBuilder(); - public void Main(){ + public void printMain(){ sb.append("## 메인 화면\n"); sb.append("1. 역 관리\n"); sb.append("2. 노선 관리\n"); sb.append("3. 구간 관리\n"); sb.append("4. 지하철 노선도 출력\n"); sb.append("Q. 종료\n"); - sb.flush(); + System.out.println(sb.toString()); + + sb.setLength(0); } public void WhatToManage(String target){ sb.append("## " + target + " 관리 화면\n"); - sb.append("1. "+target + "등록\n"); - sb.append("2. "+target + "삭제\n"); - sb.append("3. "+target + "조회\n"); + sb.append("1. "+target + " 등록\n"); + sb.append("2. "+target + " 삭제\n"); + sb.append("3. "+target + " 조회\n"); sb.append("B. 돌아가기\n"); - sb.flush(); + System.out.println(sb.toString()); + sb.setLength(0); } public void Function(){ sb.append("## 원하는 기능을 선택하세요\n"); - sb.flush(); + System.out.println(sb.toString()); + sb.setLength(0); } public void Name(String work, String name){ // 등록 or 삭제 sb.append("## "+work+"할 " + name + " 이름을 입력하세요\n"); - sb.flush(); + System.out.println(sb.toString()); + sb.setLength(0); } } From c5034ae72cfbe6664abb000e8d4159f43b6f3e30 Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 8 May 2024 17:11:42 +0900 Subject: [PATCH 024/104] =?UTF-8?q?feat:=20Station=20Controller=20?= =?UTF-8?q?=ED=9D=90=EB=A6=84=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/LineController.java | 2 +- .../subway/controller/MainController.java | 5 +--- .../subway/controller/ManageController.java | 2 +- .../subway/controller/SectionController.java | 2 +- .../subway/controller/StationController.java | 30 ++++++++++++++++++- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 21676d936..e6592a594 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -24,7 +24,7 @@ public void register(String station) { } @Override - public void delete(String station) { + public void delete() { // 노선 db 에서 삭제 } } diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index c14bf185c..320281867 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -11,11 +11,8 @@ public class MainController { LineController lineController = new LineController(); SectionController sectionController = new SectionController(); - public MainController(){ - ask.printMain(); - } - public void headController() throws Exception{ + ask.printMain(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String command = br.readLine(); if(command.equals("Q")) { diff --git a/src/main/java/subway/controller/ManageController.java b/src/main/java/subway/controller/ManageController.java index 2162b4b3c..e167223af 100644 --- a/src/main/java/subway/controller/ManageController.java +++ b/src/main/java/subway/controller/ManageController.java @@ -20,7 +20,7 @@ public void work(){ public abstract void register(String station); // db 접근해서 데이터 삽입하는 서비스와 연결 - public abstract void delete(String station); + public abstract void delete(); // db 접근해서 데이터 삭제하는 서비스와 연결 // 되돌아가기 diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 7fcec3a65..e47166f6c 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -23,7 +23,7 @@ public void register(String station) { } @Override - public void delete(String station) { + public void delete() { } } diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 23b9f3a84..7148f7274 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -1,6 +1,7 @@ package subway.controller; public class StationController extends ManageController{ + private static MainController mainController = new MainController(); @Override public void work(){ ask.WhatToManage("역"); @@ -8,6 +9,24 @@ public void work(){ try{ String command = br.readLine(); // command 내용 확인하고 맞는 처리와 연결 + if(command.equals("1")){ + registerControll(); + mainController.headController(); + return; + } + if(command.equals("2")){ + delete(); + mainController.headController(); + return; + } + if(command.equals("3")){ + requestStations(); + mainController.headController(); + return; + } + if(command.equals("B")){ + mainController.headController(); + } }catch (Exception e){ e.printStackTrace(); } @@ -28,14 +47,23 @@ public void registerControll(){ @Override public void register(String station){ // db 접근해서 데이터 삽입하는 서비스와 연결 + System.out.println("register 메서드 안이다."); } @Override - public void delete(String station){ + public void delete(){ // db 접근해서 데이터 삭제하는 서비스와 연결 + ask.Name("삭제", "역"); + try{ + String command = br.readLine(); + System.out.println("delete 메서드 안이다."); + }catch (Exception e){ + e.printStackTrace(); + } } public void requestStations() throws Exception{ // 역 목록을 출력하는 서비스 코드 + System.out.println("requestStations 메서드 안이다."); } } From 922732292de65fb4e750c8bb99dea5e3c08028a2 Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 8 May 2024 20:20:10 +0900 Subject: [PATCH 025/104] =?UTF-8?q?feat:=20=EC=97=AD=20=EB=93=B1=EB=A1=9D?= =?UTF-8?q?=ED=95=98=EA=B8=B0=20=EB=B0=8F=20=EA=B4=80=EB=A0=A8=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EC=83=81=ED=99=A9=EC=97=90=20=EB=A7=9E?= =?UTF-8?q?=EA=B2=8C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/LineController.java | 8 +--- .../subway/controller/ManageController.java | 8 ++-- .../subway/controller/SectionController.java | 7 +--- .../subway/controller/StationController.java | 37 ++++++++++++------- src/main/java/subway/service/DataManager.java | 8 +++- src/main/java/subway/service/LineManager.java | 8 +++- src/main/java/subway/service/Managerbale.java | 4 +- .../java/subway/service/SectionManager.java | 8 +++- .../java/subway/service/StationManager.java | 30 +++++++++------ src/main/java/subway/view/ResponseView.java | 16 ++++++++ 10 files changed, 84 insertions(+), 50 deletions(-) create mode 100644 src/main/java/subway/view/ResponseView.java diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index e6592a594..3011ee21a 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -13,13 +13,7 @@ public void work(){ } @Override - public void registerControll() { - // 노선 등록하기 - //상행성 종점과 하행성 종점 입력 - } - - @Override - public void register(String station) { + public void register() { // 새로운 노선 db 와 연동하여 생성하기 } diff --git a/src/main/java/subway/controller/ManageController.java b/src/main/java/subway/controller/ManageController.java index e167223af..4fa211a45 100644 --- a/src/main/java/subway/controller/ManageController.java +++ b/src/main/java/subway/controller/ManageController.java @@ -1,23 +1,23 @@ package subway.controller; import subway.view.AskView; +import subway.view.ResponseView; import java.io.BufferedReader; import java.io.InputStreamReader; public abstract class ManageController { AskView ask = new AskView(); + ResponseView response = new ResponseView(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public void work(){ // ask.Function(); // 관련 컨트롤러 호출 부분 } + // 등록하기 - public abstract void registerControll() ; - // 등록하기 - - public abstract void register(String station); + public abstract void register(); // db 접근해서 데이터 삽입하는 서비스와 연결 public abstract void delete(); diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index e47166f6c..507cb9907 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -13,12 +13,7 @@ public void work() { } @Override - public void registerControll() { - - } - - @Override - public void register(String station) { + public void register() { } diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 7148f7274..1e1ecaa05 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -1,7 +1,11 @@ package subway.controller; +import subway.service.StationManager; + +// 역을 관리하는 컨트롤러 public class StationController extends ManageController{ private static MainController mainController = new MainController(); + private static StationManager stationManager = new StationManager(); @Override public void work(){ ask.WhatToManage("역"); @@ -10,44 +14,50 @@ public void work(){ String command = br.readLine(); // command 내용 확인하고 맞는 처리와 연결 if(command.equals("1")){ - registerControll(); - mainController.headController(); + register(); return; } if(command.equals("2")){ + // 관련한 뷰 (삭제, 역) + // input 받기 delete(); - mainController.headController(); return; } if(command.equals("3")){ - requestStations(); - mainController.headController(); + stationManager.read(); return; } if(command.equals("B")){ - mainController.headController(); + return; } + // 에러 발생시키기 }catch (Exception e){ e.printStackTrace(); } } @Override - public void registerControll(){ + public void register(){ ask.Name("등록", "역"); try{ String station = br.readLine(); - register(station); + stationManager.register(station); // "지하철 역이 등록되었습니다." 출력 }catch (Exception e){ e.printStackTrace(); } } - - @Override - public void register(String station){ - // db 접근해서 데이터 삽입하는 서비스와 연결 - System.out.println("register 메서드 안이다."); + public void infoMessage(String work, boolean result){ + StringBuilder sb = new StringBuilder(); + sb.append("지하철 역이 "); + if(result == true){ + sb.append(work).append("되었습니다."); + } + if(result == false){ + sb.append(work).append("되지 않았습니다."); + } + String message = sb.toString(); + response.printInfo(message); } @Override @@ -56,6 +66,7 @@ public void delete(){ ask.Name("삭제", "역"); try{ String command = br.readLine(); + stationManager.delete(command); System.out.println("delete 메서드 안이다."); }catch (Exception e){ e.printStackTrace(); diff --git a/src/main/java/subway/service/DataManager.java b/src/main/java/subway/service/DataManager.java index cea84a584..c38bad6cd 100644 --- a/src/main/java/subway/service/DataManager.java +++ b/src/main/java/subway/service/DataManager.java @@ -12,10 +12,11 @@ public DataManager(){ // 초기값 삽입 } @Override // 삽입 - public void register(String command) { + public boolean register(String command) { // if command.equals("station") 역만 삽입하는 모듈 연결 // if command.equals("line") 노선 삽입하는 모듈 연결 // if command.equals("Section") 구간 삽입하는 모듈 연결 + return true; } public void insertStation(String node){ @@ -45,7 +46,10 @@ public void delete(String name) { } @Override // 조회 - public void read() { + public StringBuilder read() { + StringBuilder sb = new StringBuilder(); + sb.append(""); + return sb; } } diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index 4710834c0..8258723ad 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -8,9 +8,10 @@ public class LineManager extends Managerbale{ @Override - public void register(String name) { + public boolean register(String name) { Line created = new Line(name); lineRepo.addLine(created); + return true; } public void setStations(String name, String upperStation, String bottomStation){ @@ -26,8 +27,11 @@ public void delete(String name) { } @Override - public void read() { + public StringBuilder read() { + StringBuilder sb = new StringBuilder(); + sb.append(""); + return sb; } public List readLines(){ diff --git a/src/main/java/subway/service/Managerbale.java b/src/main/java/subway/service/Managerbale.java index 856f8d9bd..31e4884e2 100644 --- a/src/main/java/subway/service/Managerbale.java +++ b/src/main/java/subway/service/Managerbale.java @@ -6,9 +6,9 @@ public abstract class Managerbale { StationRepository stationRepo = new StationRepository(); LineRepository lineRepo = new LineRepository(); - public abstract void register(String command); + public abstract boolean register(String command); public abstract void delete(String name); - public abstract void read(); + public abstract StringBuilder read(); public void goBack() { return; diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 03bceb5d1..62dd6ea1e 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -7,7 +7,8 @@ public class SectionManager extends Managerbale{ @Override - public void register(String name) { + public boolean register(String name) { + return true; } public void insertSection(String name, int index){ @@ -24,8 +25,11 @@ public void delete(String name) { } @Override - public void read() { + public StringBuilder read() { + StringBuilder sb = new StringBuilder(); + sb.append(""); + return sb; } @Override diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index 18f411bb2..84ce9d100 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -1,22 +1,23 @@ package subway.service; import subway.domain.Station; +import subway.domain.StationRepository; import java.util.List; +// 역 관련 기능하는 서비스 public class StationManager extends Managerbale{ + private static StationRepository stations = new StationRepository(); @Override // 삽입 - public void register(String command) { -// if command.equals("station") 역만 삽입하는 모듈 연결 -// if command.equals("line") 노선 삽입하는 모듈 연결 -// if command.equals("Section") 구간 삽입하는 모듈 연결 - } - - public void insertStation(String node){ - // 이미 해당 역이 존재하는지 아닌지 확인하는 코드 추가 (util 로 만들기) - // 존재하지 않는다 - Station station = new Station(node); - stationRepo.addStation(station); + public boolean register(String name) { +// 해당 역이 존재하는 지 확인 +// 삽입 + if(stations.getStationByName(name)!=null){ + // 이미 존재하는 역이라고 전달하기 + return false; // 문자열 반환해서 상태 메시지 출력에 활용 + } + stations.addStation(new Station(name)); + return true; } @Override @@ -25,8 +26,13 @@ public void delete(String name) { } @Override - public void read() { + public StringBuilder read() { // 역 조회 + StringBuilder sb = new StringBuilder(); + for(Station station : stations.stations()){ + sb.append("[INFO] " + station.getName()+"\n"); + } + return sb; } public List readStation(){ diff --git a/src/main/java/subway/view/ResponseView.java b/src/main/java/subway/view/ResponseView.java new file mode 100644 index 000000000..cab68aace --- /dev/null +++ b/src/main/java/subway/view/ResponseView.java @@ -0,0 +1,16 @@ +package subway.view; + +public class ResponseView { + static StringBuilder sb = new StringBuilder(); + public void printInfo(String message){ + sb.append("[INFO] ").append(message); + System.out.println(sb); + } + + public void printTitle(String title){ + sb.append("### ").append(title); + } + public void printList(StringBuilder message){ + System.out.println(message); + } +} From b23ca92c4a711f24e005173802ea1466d171fa65 Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 8 May 2024 20:27:33 +0900 Subject: [PATCH 026/104] =?UTF-8?q?feat:=20=EC=A7=80=ED=95=98=EC=B2=A0=20?= =?UTF-8?q?=EC=97=AD=20=EB=93=B1=EB=A1=9D=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/controller/StationController.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 1e1ecaa05..7262a2493 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -4,7 +4,6 @@ // 역을 관리하는 컨트롤러 public class StationController extends ManageController{ - private static MainController mainController = new MainController(); private static StationManager stationManager = new StationManager(); @Override public void work(){ @@ -12,14 +11,11 @@ public void work(){ ask.Function(); try{ String command = br.readLine(); - // command 내용 확인하고 맞는 처리와 연결 if(command.equals("1")){ register(); return; } if(command.equals("2")){ - // 관련한 뷰 (삭제, 역) - // input 받기 delete(); return; } @@ -30,7 +26,7 @@ public void work(){ if(command.equals("B")){ return; } - // 에러 발생시키기 + // 에러 발생시키 }catch (Exception e){ e.printStackTrace(); } @@ -41,8 +37,9 @@ public void register(){ ask.Name("등록", "역"); try{ String station = br.readLine(); - stationManager.register(station); + boolean result = stationManager.register(station); // "지하철 역이 등록되었습니다." 출력 + infoMessage("등록", result); }catch (Exception e){ e.printStackTrace(); } From d6759dacc92937a77426e73a96b599760eca1eba Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 8 May 2024 20:30:48 +0900 Subject: [PATCH 027/104] =?UTF-8?q?feat=20:=20delete=20=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=20=EB=B0=98=ED=99=98=ED=98=95=20=EC=88=98=EC=A0=95=20void=20->?= =?UTF-8?q?=20boolean?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/service/DataManager.java | 4 ++-- src/main/java/subway/service/LineManager.java | 4 ++-- src/main/java/subway/service/Managerbale.java | 2 +- src/main/java/subway/service/SectionManager.java | 4 ++-- src/main/java/subway/service/StationManager.java | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/subway/service/DataManager.java b/src/main/java/subway/service/DataManager.java index c38bad6cd..7f538e699 100644 --- a/src/main/java/subway/service/DataManager.java +++ b/src/main/java/subway/service/DataManager.java @@ -41,8 +41,8 @@ public void insertSection(){ } @Override // 삭제 - public void delete(String name) { - + public boolean delete(String name) { + return true; } @Override // 조회 diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index 8258723ad..ca672a22a 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -22,8 +22,8 @@ public void setStations(String name, String upperStation, String bottomStation){ } @Override - public void delete(String name) { - lineRepo.deleteLineByName(name); + public boolean delete(String name) { + return lineRepo.deleteLineByName(name); } @Override diff --git a/src/main/java/subway/service/Managerbale.java b/src/main/java/subway/service/Managerbale.java index 31e4884e2..911989f8c 100644 --- a/src/main/java/subway/service/Managerbale.java +++ b/src/main/java/subway/service/Managerbale.java @@ -7,7 +7,7 @@ public abstract class Managerbale { StationRepository stationRepo = new StationRepository(); LineRepository lineRepo = new LineRepository(); public abstract boolean register(String command); - public abstract void delete(String name); + public abstract boolean delete(String name); public abstract StringBuilder read(); public void goBack() { diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 62dd6ea1e..82f129450 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -20,8 +20,8 @@ public void insertSection(String name, int index){ } @Override - public void delete(String name) { - + public boolean delete(String name) { + return true; } @Override diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index 84ce9d100..933b68998 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -21,8 +21,8 @@ public boolean register(String name) { } @Override - public void delete(String name) { - stationRepo.deleteStation(name); + public boolean delete(String name) { + return stationRepo.deleteStation(name); } @Override From 5a042010f90ad9ec0ba18d0474e7e872195c8a6c Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 8 May 2024 20:38:51 +0900 Subject: [PATCH 028/104] =?UTF-8?q?feat=20:=20=EC=82=AD=EC=A0=9C=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20service=20=EC=BD=94=EB=93=9C=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84,=20view=20=EC=99=80=20=EC=97=B0=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/controller/StationController.java | 5 +++-- src/main/java/subway/view/ResponseView.java | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 7262a2493..d1fae2e65 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -55,6 +55,7 @@ public void infoMessage(String work, boolean result){ } String message = sb.toString(); response.printInfo(message); + sb.setLength(0); } @Override @@ -63,8 +64,8 @@ public void delete(){ ask.Name("삭제", "역"); try{ String command = br.readLine(); - stationManager.delete(command); - System.out.println("delete 메서드 안이다."); + boolean result = stationManager.delete(command); + infoMessage("삭제", result); }catch (Exception e){ e.printStackTrace(); } diff --git a/src/main/java/subway/view/ResponseView.java b/src/main/java/subway/view/ResponseView.java index cab68aace..aec3d8b10 100644 --- a/src/main/java/subway/view/ResponseView.java +++ b/src/main/java/subway/view/ResponseView.java @@ -5,6 +5,7 @@ public class ResponseView { public void printInfo(String message){ sb.append("[INFO] ").append(message); System.out.println(sb); + sb.setLength(0); } public void printTitle(String title){ From 4a8c368b6c2b5738fa62a8d272658012a4536735 Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 8 May 2024 20:44:41 +0900 Subject: [PATCH 029/104] =?UTF-8?q?feat=20:=20stationController=20?= =?UTF-8?q?=EC=99=84=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/controller/StationController.java | 9 +++++---- src/main/java/subway/view/ResponseView.java | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index d1fae2e65..37cd5723c 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -20,7 +20,7 @@ public void work(){ return; } if(command.equals("3")){ - stationManager.read(); + read(); return; } if(command.equals("B")){ @@ -71,8 +71,9 @@ public void delete(){ } } - public void requestStations() throws Exception{ - // 역 목록을 출력하는 서비스 코드 - System.out.println("requestStations 메서드 안이다."); + public void read(){ + response.printTitle("역 목록"); + StringBuilder list = stationManager.read(); + response.printList(list); } } diff --git a/src/main/java/subway/view/ResponseView.java b/src/main/java/subway/view/ResponseView.java index aec3d8b10..9101468b1 100644 --- a/src/main/java/subway/view/ResponseView.java +++ b/src/main/java/subway/view/ResponseView.java @@ -10,6 +10,8 @@ public void printInfo(String message){ public void printTitle(String title){ sb.append("### ").append(title); + System.out.println(sb); + sb.setLength(0); } public void printList(StringBuilder message){ System.out.println(message); From d5ab889dcd75861b61891f7c385fa3c64f69b2d1 Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 8 May 2024 21:33:55 +0900 Subject: [PATCH 030/104] =?UTF-8?q?feat:=20lineManager=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=201=EC=B0=A8=20=EC=99=84=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/LineController.java | 57 ++++++++- .../subway/controller/StationController.java | 5 + src/main/java/subway/domain/Line.java | 1 - src/main/java/subway/domain/Station.java | 3 + src/main/java/subway/service/DataManager.java | 110 +++++++++--------- src/main/java/subway/service/LineManager.java | 31 ++--- src/main/java/subway/service/Managerbale.java | 2 - .../java/subway/service/SectionManager.java | 3 + .../java/subway/service/StationManager.java | 8 +- 9 files changed, 143 insertions(+), 77 deletions(-) diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 3011ee21a..540f84b80 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -1,12 +1,31 @@ package subway.controller; +import subway.service.LineManager; + public class LineController extends ManageController{ + static LineManager lineManager = new LineManager(); @Override public void work(){ + ask.WhatToManage("노선"); ask.Function(); try{ String command = br.readLine(); - // command 내용 확인하고 맞는 처리와 연결 + if(command.equals("1")){ + register(); + return; + } + if(command.equals("2")){ + delete(); + return; + } + if(command.equals("3")){ + read(); + return; + } + if(command.equals("B")){ + return; + } + // 에러 발생시키 }catch (Exception e){ e.printStackTrace(); } @@ -15,10 +34,46 @@ public void work(){ @Override public void register() { // 새로운 노선 db 와 연동하여 생성하기 + ask.Name("등록", "노선"); + try{ + String line = br.readLine(); + boolean result = lineManager.register(line); + infoMessage("등록", result); + }catch (Exception e){ + e.printStackTrace(); + } + } + + public void infoMessage(String work, boolean result){ + StringBuilder sb = new StringBuilder(); + sb.append("노선이 "); + if(result == true){ + sb.append(work).append("되었습니다."); + } + if(result == false){ + sb.append(work).append("되지 않았습니다."); + } + String message = sb.toString(); + response.printInfo(message); + sb.setLength(0); } @Override public void delete() { // 노선 db 에서 삭제 + ask.Name("삭제", "노선"); + try{ + String command = br.readLine(); + boolean result = lineManager.delete(command); + infoMessage("삭제", result); + }catch (Exception e){ + e.printStackTrace(); + } + } + + public void read(){ + response.printTitle("노선 목록"); + StringBuilder list = lineManager.read(); + response.printList(list); } } diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 37cd5723c..40076119e 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -76,4 +76,9 @@ public void read(){ StringBuilder list = stationManager.read(); response.printList(list); } + + public void requestStations() throws Exception{ + // 역 목록을 출력하는 서비스 코드 + System.out.println("requestStations 메서드 안이다."); + } } diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index 8dcbdf61e..03eb87542 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -1,7 +1,6 @@ package subway.domain; import java.util.LinkedList; -import java.util.ListIterator; public class Line { private String name; diff --git a/src/main/java/subway/domain/Station.java b/src/main/java/subway/domain/Station.java index bdb142590..1bb013a36 100644 --- a/src/main/java/subway/domain/Station.java +++ b/src/main/java/subway/domain/Station.java @@ -1,7 +1,10 @@ package subway.domain; +import java.util.List; + public class Station { private String name; + private List[] line; public Station(String name) { this.name = name; diff --git a/src/main/java/subway/service/DataManager.java b/src/main/java/subway/service/DataManager.java index 7f538e699..0cf6a72fe 100644 --- a/src/main/java/subway/service/DataManager.java +++ b/src/main/java/subway/service/DataManager.java @@ -1,55 +1,55 @@ -package subway.service; - -import subway.domain.Line; -import subway.domain.LineRepository; -import subway.domain.Station; -import subway.domain.StationRepository; - -import java.util.List; - -public class DataManager extends Managerbale{ - public DataManager(){ - // 초기값 삽입 - } - @Override // 삽입 - public boolean register(String command) { -// if command.equals("station") 역만 삽입하는 모듈 연결 -// if command.equals("line") 노선 삽입하는 모듈 연결 -// if command.equals("Section") 구간 삽입하는 모듈 연결 - return true; - } - - public void insertStation(String node){ - // 이미 해당 역이 존재하는지 아닌지 확인하는 코드 추가 (util 로 만들기) - Station station = new Station(node); - stationRepo.addStation(station); - } - - public void insertLine(String upperStation, String bottomStation, String name){ - List stations = stationRepo.stations(); - // 이미 해당 역이 존재하는지 아닌지 확인하는 코드 추가 - Station upper = new Station(upperStation); - Station bottom = new Station(bottomStation); - stationRepo.addStation(upper); - stationRepo.addStation(bottom); - Line created = new Line(name); - lineRepo.addLine(created); - } - - public void insertSection(){ - - } - - @Override // 삭제 - public boolean delete(String name) { - return true; - } - - @Override // 조회 - public StringBuilder read() { - StringBuilder sb = new StringBuilder(); - - sb.append(""); - return sb; - } -} +//package subway.service; +// +//import subway.domain.Line; +//import subway.domain.LineRepository; +//import subway.domain.Station; +//import subway.domain.StationRepository; +// +//import java.util.List; +// +//public class DataManager extends Managerbale{ +// public DataManager(){ +// // 초기값 삽입 +// } +// @Override // 삽입 +// public boolean register(String command) { +//// if command.equals("station") 역만 삽입하는 모듈 연결 +//// if command.equals("line") 노선 삽입하는 모듈 연결 +//// if command.equals("Section") 구간 삽입하는 모듈 연결 +// return true; +// } +// +// public void insertStation(String node){ +// // 이미 해당 역이 존재하는지 아닌지 확인하는 코드 추가 (util 로 만들기) +// Station station = new Station(node); +// stationRepo.addStation(station); +// } +// +// public void insertLine(String upperStation, String bottomStation, String name){ +// List stations = stationRepo.stations(); +// // 이미 해당 역이 존재하는지 아닌지 확인하는 코드 추가 +// Station upper = new Station(upperStation); +// Station bottom = new Station(bottomStation); +// stationRepo.addStation(upper); +// stationRepo.addStation(bottom); +// Line created = new Line(name); +// lineRepo.addLine(created); +// } +// +// public void insertSection(){ +// +// } +// +// @Override // 삭제 +// public boolean delete(String name) { +// return true; +// } +// +// @Override // 조회 +// public StringBuilder read() { +// StringBuilder sb = new StringBuilder(); +// +// sb.append(""); +// return sb; +// } +//} diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index ca672a22a..1fd010aba 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -2,24 +2,26 @@ import subway.domain.Line; import subway.domain.LineRepository; -import subway.domain.Station; import java.util.List; public class LineManager extends Managerbale{ + static LineRepository lineRepo = new LineRepository(); @Override public boolean register(String name) { - Line created = new Line(name); - lineRepo.addLine(created); + if(lineRepo.getLineByName(name) != null){ + return false; + } + lineRepo.addLine(new Line(name)); return true; } - public void setStations(String name, String upperStation, String bottomStation){ - Line created = new Line(name); - lineRepo.addLine(created); - // upper, bottom 을 찾고 아래 매개변수에 대입하는 코드 추가 - created.setStations(new Station(upperStation), new Station(bottomStation)); - } +// public void setStations(String name, String upperStation, String bottomStation){ +// Line created = new Line(name); +// lineRepo.addLine(created); +// // upper, bottom 을 찾고 아래 매개변수에 대입하는 코드 추가 +// created.setStations(new Station(upperStation), new Station(bottomStation)); +// } @Override public boolean delete(String name) { @@ -29,12 +31,13 @@ public boolean delete(String name) { @Override public StringBuilder read() { StringBuilder sb = new StringBuilder(); - - sb.append(""); + for(Line line: lineRepo.lines()){ + sb.append("[INFO] " + line.getName()+"\n"); + } return sb; } - public List readLines(){ - return lineRepo.lines(); - } +// public List readLines(){ +// return lineRepo.lines(); +// } } diff --git a/src/main/java/subway/service/Managerbale.java b/src/main/java/subway/service/Managerbale.java index 911989f8c..afac5f706 100644 --- a/src/main/java/subway/service/Managerbale.java +++ b/src/main/java/subway/service/Managerbale.java @@ -4,8 +4,6 @@ import subway.domain.StationRepository; public abstract class Managerbale { - StationRepository stationRepo = new StationRepository(); - LineRepository lineRepo = new LineRepository(); public abstract boolean register(String command); public abstract boolean delete(String name); public abstract StringBuilder read(); diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 82f129450..9aa7673b5 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -1,11 +1,14 @@ package subway.service; import subway.domain.Line; +import subway.domain.LineRepository; import subway.domain.Station; import java.util.List; public class SectionManager extends Managerbale{ + static LineRepository lineRepo = new LineRepository(); + @Override public boolean register(String name) { return true; diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index 933b68998..b32e17780 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -22,7 +22,7 @@ public boolean register(String name) { @Override public boolean delete(String name) { - return stationRepo.deleteStation(name); + return stations.deleteStation(name); } @Override @@ -35,7 +35,7 @@ public StringBuilder read() { return sb; } - public List readStation(){ - return stationRepo.stations(); - } +// public List readStation(){ +// return stations.stations(); +// } } From e75be9bac7e592eb4e312d7b4aa8588165656442 Mon Sep 17 00:00:00 2001 From: aristo Date: Thu, 9 May 2024 13:46:14 +0900 Subject: [PATCH 031/104] =?UTF-8?q?feat:=20=EC=A0=95=EC=88=98=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/constants/initValues/lineNames.java | 5 +++++ .../config/constants/initValues/stationNames.java | 15 +++++++++++++++ src/main/java/subway/service/LineManager.java | 3 +-- 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 src/main/java/subway/config/constants/initValues/lineNames.java create mode 100644 src/main/java/subway/config/constants/initValues/stationNames.java diff --git a/src/main/java/subway/config/constants/initValues/lineNames.java b/src/main/java/subway/config/constants/initValues/lineNames.java new file mode 100644 index 000000000..1e4d6d335 --- /dev/null +++ b/src/main/java/subway/config/constants/initValues/lineNames.java @@ -0,0 +1,5 @@ +package subway.config.constants.initValues; + +public class lineNames { + String[] stations = new String []{"2호선", "3호선", "수인분당선"}; +} diff --git a/src/main/java/subway/config/constants/initValues/stationNames.java b/src/main/java/subway/config/constants/initValues/stationNames.java new file mode 100644 index 000000000..f2ed35fbd --- /dev/null +++ b/src/main/java/subway/config/constants/initValues/stationNames.java @@ -0,0 +1,15 @@ +package subway.config.constants.initValues; + +import subway.domain.Line; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +public class stationNames { + // 해당 자료형을 컨트롤할 수 잇는 코드를 추출할까 + List train2 = new LinkedList<>(); + List train3 = new LinkedList<>(); + List trainSuIn = new LinkedList<>(); + String[] stations = new String []{"교대역", "강남역", "역삼역", "남부터미널역", "양재역", "양재시민의숲역", "매봉역"}; +} diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index 1fd010aba..fd3ca7bc4 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -3,8 +3,6 @@ import subway.domain.Line; import subway.domain.LineRepository; -import java.util.List; - public class LineManager extends Managerbale{ static LineRepository lineRepo = new LineRepository(); @Override @@ -15,6 +13,7 @@ public boolean register(String name) { lineRepo.addLine(new Line(name)); return true; } +// Todo : 상행 종점, 하행 종점 입력 받기 // public void setStations(String name, String upperStation, String bottomStation){ // Line created = new Line(name); From b1446cf17507226d8ef6eccc078262a708741086 Mon Sep 17 00:00:00 2001 From: aristo Date: Thu, 9 May 2024 14:25:41 +0900 Subject: [PATCH 032/104] =?UTF-8?q?feat=20:=20=EB=85=B8=EC=84=A0=20?= =?UTF-8?q?=EA=B4=80=EB=A6=AC=20=EC=95=8C=EA=B3=A0=EB=A6=AC=EC=A6=98=20?= =?UTF-8?q?=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../constants/initValues/stationNames.java | 1 - .../java/subway/controller/LineController.java | 7 +++++++ .../subway/controller/StationController.java | 2 +- src/main/java/subway/domain/Line.java | 4 ++-- src/main/java/subway/service/LineManager.java | 18 +++++++++++------- .../java/subway/service/StationManager.java | 16 +++++++++++++++- 6 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/main/java/subway/config/constants/initValues/stationNames.java b/src/main/java/subway/config/constants/initValues/stationNames.java index f2ed35fbd..45ce8e159 100644 --- a/src/main/java/subway/config/constants/initValues/stationNames.java +++ b/src/main/java/subway/config/constants/initValues/stationNames.java @@ -2,7 +2,6 @@ import subway.domain.Line; -import java.util.ArrayList; import java.util.LinkedList; import java.util.List; diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 540f84b80..4f7aba2f4 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -38,6 +38,13 @@ public void register() { try{ String line = br.readLine(); boolean result = lineManager.register(line); + + ask.Name("등록", "상행 종점역"); + String upper = br.readLine(); + ask.Name("등록", "하행 종점역"); + String bottom = br.readLine(); + + lineManager.setStations(line, upper, bottom); infoMessage("등록", result); }catch (Exception e){ e.printStackTrace(); diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 40076119e..ffeaf9e44 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -4,7 +4,7 @@ // 역을 관리하는 컨트롤러 public class StationController extends ManageController{ - private static StationManager stationManager = new StationManager(); + private static final StationManager stationManager = new StationManager(); @Override public void work(){ ask.WhatToManage("역"); diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index 03eb87542..18c25732d 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -19,8 +19,8 @@ public String getName() { public void setStations(Station upper, Station bottom){ this.upperStation = upper; this.bottomStation=bottom; - stations.add(upper); - stations.add(bottom); + stations.addFirst(upper); + stations.addLast(bottom); } public void insertStation(Station node, int index){ diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index fd3ca7bc4..9f74c2923 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -2,9 +2,12 @@ import subway.domain.Line; import subway.domain.LineRepository; +import subway.domain.Station; public class LineManager extends Managerbale{ - static LineRepository lineRepo = new LineRepository(); + static final LineRepository lineRepo = new LineRepository(); + static StationManager stationManager = new StationManager(); + @Override public boolean register(String name) { if(lineRepo.getLineByName(name) != null){ @@ -15,12 +18,13 @@ public boolean register(String name) { } // Todo : 상행 종점, 하행 종점 입력 받기 -// public void setStations(String name, String upperStation, String bottomStation){ -// Line created = new Line(name); -// lineRepo.addLine(created); -// // upper, bottom 을 찾고 아래 매개변수에 대입하는 코드 추가 -// created.setStations(new Station(upperStation), new Station(bottomStation)); -// } + public void setStations(String name, String upperStation, String bottomStation){ + Line line = lineRepo.getLineByName(name); + Station upper = stationManager.getByName(upperStation); + Station bottom = stationManager.getByName(bottomStation); + + line.setStations(upper, bottom); + } @Override public boolean delete(String name) { diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index b32e17780..df8a4a986 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -7,11 +7,20 @@ // 역 관련 기능하는 서비스 public class StationManager extends Managerbale{ - private static StationRepository stations = new StationRepository(); + private static final StationRepository stations = new StationRepository(); @Override // 삽입 public boolean register(String name) { // 해당 역이 존재하는 지 확인 // 삽입 + if(isExist(name)){ + // 이미 존재하는 역이라고 전달하기 + return false; // 문자열 반환해서 상태 메시지 출력에 활용 + } + stations.addStation(new Station(name)); + return true; + } + + public boolean isExist(String name){ if(stations.getStationByName(name)!=null){ // 이미 존재하는 역이라고 전달하기 return false; // 문자열 반환해서 상태 메시지 출력에 활용 @@ -35,6 +44,11 @@ public StringBuilder read() { return sb; } + public Station getByName(String name){ + register(name); + return stations.getStationByName(name); + } + // public List readStation(){ // return stations.stations(); // } From f2a3c44d7afbd493163cffea62c985ff0d82955d Mon Sep 17 00:00:00 2001 From: aristo Date: Thu, 9 May 2024 15:29:35 +0900 Subject: [PATCH 033/104] =?UTF-8?q?feat:=20=EA=B5=AC=EA=B0=84=20=EA=B4=80?= =?UTF-8?q?=EB=A6=AC=20controller,=20service=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/ManageController.java | 6 ++ .../subway/controller/SectionController.java | 61 ++++++++++++++++++- .../subway/controller/StationController.java | 1 - src/main/java/subway/domain/Line.java | 4 ++ .../java/subway/domain/LineRepository.java | 3 + .../java/subway/service/SectionManager.java | 19 ++++-- .../java/subway/service/StationManager.java | 7 +-- src/main/java/subway/view/AskView.java | 10 +++ 8 files changed, 99 insertions(+), 12 deletions(-) diff --git a/src/main/java/subway/controller/ManageController.java b/src/main/java/subway/controller/ManageController.java index 4fa211a45..02e555005 100644 --- a/src/main/java/subway/controller/ManageController.java +++ b/src/main/java/subway/controller/ManageController.java @@ -1,5 +1,8 @@ package subway.controller; +import subway.domain.LineRepository; +import subway.domain.StationRepository; +import subway.service.StationManager; import subway.view.AskView; import subway.view.ResponseView; @@ -10,6 +13,9 @@ public abstract class ManageController { AskView ask = new AskView(); ResponseView response = new ResponseView(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static final StationManager stationManager = new StationManager(); + static final LineRepository lineRepo = new LineRepository(); + static final StationRepository stationRepo = new StationRepository(); public void work(){ // ask.Function(); diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 507cb9907..d59bef585 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -1,20 +1,77 @@ package subway.controller; +import subway.service.LineManager; +import subway.service.SectionManager; +import subway.service.StationManager; + public class SectionController extends ManageController{ + private static StationManager stationManager = new StationManager(); + private static LineManager lineManager = new LineManager(); + private static SectionManager sectionManager = new SectionManager(); @Override public void work() { + ask.WhatToManage("구간"); ask.Function(); try{ String command = br.readLine(); - // command 내용 확인하고 맞는 처리와 연결 + if(command.equals("1")){ // 등록 + register(); + return; + } + if(command.equals("2")){ // 삭제 + delete(); + return; + } + if(command.equals("B")){ // 되돌아가기 + return; + } + // 에러 발생시키 }catch (Exception e){ e.printStackTrace(); } } @Override - public void register() { + public void register() { // 노선, 역 이름, 순서 입력 받고 등록 + ask.orderWhat("노선"); + String line = getLine(); + + ask.orderWhat("역 이름"); + String station = getStation(); + + ask.orderWhere(); + int index = getIndex(); + + sectionManager.insertSection(line, station, index); + } + + public String getLine(){ + try{ + return br.readLine(); + }catch (Exception e){ + System.out.println("존재하지 않는 노선입니다."); + e.printStackTrace(); + } + return null; + } + public String getStation(){ + try{ + return br.readLine(); + }catch (Exception e){ + System.out.println("존재하지 않는 역입니다."); + e.printStackTrace(); + } + return null; } + public int getIndex(){ + try{ + int index = Integer.parseInt(br.readLine()); + return index; + } catch (Exception e){ + System.out.println("입력값을 확인해주세요."); + e.printStackTrace(); + } + return -1; } @Override diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index ffeaf9e44..a9a569de2 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -4,7 +4,6 @@ // 역을 관리하는 컨트롤러 public class StationController extends ManageController{ - private static final StationManager stationManager = new StationManager(); @Override public void work(){ ask.WhatToManage("역"); diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index 18c25732d..3beedaa6b 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -23,6 +23,10 @@ public void setStations(Station upper, Station bottom){ stations.addLast(bottom); } + public void addStation(Station station, int index){ + stations.add(index, station); + } + public void insertStation(Station node, int index){ stations.add(index, node); } diff --git a/src/main/java/subway/domain/LineRepository.java b/src/main/java/subway/domain/LineRepository.java index ec5450017..af54d6212 100644 --- a/src/main/java/subway/domain/LineRepository.java +++ b/src/main/java/subway/domain/LineRepository.java @@ -15,6 +15,9 @@ public static List lines() { public static void addLine(Line line) { lines.add(line); } + public static void addLine(Line line, Station station, int index) { + line.addStation(station, index); + } public static boolean deleteLineByName(String name) { return lines.removeIf(line -> Objects.equals(line.getName(), name)); diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 9aa7673b5..a02ea2bd1 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -3,22 +3,31 @@ import subway.domain.Line; import subway.domain.LineRepository; import subway.domain.Station; - -import java.util.List; +import subway.domain.StationRepository; public class SectionManager extends Managerbale{ static LineRepository lineRepo = new LineRepository(); + static StationRepository stationRepo = new StationRepository(); @Override public boolean register(String name) { return true; } - public void insertSection(String name, int index){ + public void insertSection(String name, String sName,int index){ // 이미 해당 역이 등록되어 있는지 확인 // 등록되지 않았으면 - Station created = new Station(name); - List line = lineRepo.lines(); + Line line = lineRepo.getLineByName(name); + if(line == null){ + System.out.println(" 존재하지 않는 노선입니다."); + } + + Station station = stationRepo.getStationByName(sName); + if(station == null){ + System.out.println(" 존재하지 않는 역입니다."); + } + lineRepo.addLine(line, station, index); + } diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index df8a4a986..1e5364525 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -12,7 +12,7 @@ public class StationManager extends Managerbale{ public boolean register(String name) { // 해당 역이 존재하는 지 확인 // 삽입 - if(isExist(name)){ + if(stations.getStationByName(name)!=null){ // 이미 존재하는 역이라고 전달하기 return false; // 문자열 반환해서 상태 메시지 출력에 활용 } @@ -23,10 +23,9 @@ public boolean register(String name) { public boolean isExist(String name){ if(stations.getStationByName(name)!=null){ // 이미 존재하는 역이라고 전달하기 - return false; // 문자열 반환해서 상태 메시지 출력에 활용 + return true; // 문자열 반환해서 상태 메시지 출력에 활용 } - stations.addStation(new Station(name)); - return true; + return false; } @Override diff --git a/src/main/java/subway/view/AskView.java b/src/main/java/subway/view/AskView.java index 78616bc7b..307d94ad5 100644 --- a/src/main/java/subway/view/AskView.java +++ b/src/main/java/subway/view/AskView.java @@ -36,4 +36,14 @@ public void Name(String work, String name){ // 등록 or 삭제 System.out.println(sb.toString()); sb.setLength(0); } + + public void orderWhat(String name){ // 등록 or 삭제 + sb.append("## "+ name + "을 입력하세요\n"); + System.out.println(sb.toString()); + sb.setLength(0); + } + public void orderWhere(){ // 등록 or 삭제 + System.out.println("## 순서를 입력하세요\n"); + } + } From 37738d93ea70ca8aa8e0d1cd922b5fb5f295fa11 Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 10 May 2024 16:28:30 +0900 Subject: [PATCH 034/104] =?UTF-8?q?doc:=20=EA=B8=B0=EB=8A=A5=20=EC=A0=95?= =?UTF-8?q?=EC=9D=98=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EC=83=81=EC=84=B8?= =?UTF-8?q?=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- docs/modules.md | 133 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 docs/modules.md diff --git a/docs/README.md b/docs/README.md index 22c96d5d8..979905fc3 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,4 +1,4 @@ -# 기능정의 +# 서비스 파악 및 구현 계획 > **지하철 역과 노선을 관리하는 지하철 노선도 기능을 구현한 코드** ### 사전등록정보 diff --git a/docs/modules.md b/docs/modules.md new file mode 100644 index 000000000..0af834771 --- /dev/null +++ b/docs/modules.md @@ -0,0 +1,133 @@ +# TODO + +## controller +### Maincontroller +각각의 controller 를 실행하는 객체 +사용자의 입력에 맞게 올바른 controller 배치 +- [ ] 1을 입력하면 StationController와 연결된다. +- [ ] 2을 입력하면 LineController와 연결된다. +- [ ] 3을 입력하면 SectionController와 연결된다. +- [ ] 4를 입력하면 지하철 노선을 출력하는 컨트롤러와 연결된다. +- [ ] Q 를 입력하기 전까지 사용자의 입력을 받아 서비스를 제공한다. + +### LineController +지하철 노선과 관련한 기능을 관장하는 객체 +- [ ] 1을 입력하면 노선을 등록한다. +- [ ] 2를 입력하면 노선을 삭제한다 +- [ ] 3을 입력하면 노선 목록을 조회한다. +- [ ] B 를 입력하면 메인 화면으로 되돌아간다. + +#### - 주의사항 +- [ ] 이미 등록된 이름의 노선은 추가할 수 없다. +- [ ] 노선 이름은 2글자 이상이어야 한다. +- [ ] 등록 시 상행 종점, 하행 종점을 입력받는다. +- [ ] 노선에 등록된 역 이름을 나열할 수 있다. +- [ ] 하나의 역은 여러개의 노선에 추가될 수 있다. + +### SectionController +역과 역 사이 구간을 관리하는 객체 +- [ ] 1을 입력하면 구간을 등록한다. +- [ ] 2를 입력하면 구간을 삭제한다. +- [ ] B 를 입력하면 메인 화면으로 되돌아간다. + +#### - 주의사항 +- [ ] 노선에서 갈래길이 생길 수 없다. +- [ ] 역과 역 사이에 새로운 역이 추가될 수 있다. +- [ ] 노선에 등록된 역을 삭제할 수 있다. +- [ ] 종점을 제거하면 다음 역이 종점이 된다. +- [ ] 노선에 포함된 역이 2개 이하일 때 제거할 수 없다. + +### StationController +지하철 역과 관련한 기능을 관장하는 객체 +- [ ] 1을 입력하면 역을 등록한다. +- [ ] 2를 입력하면 역을 삭제한다 +- [ ] 3을 입력하면 역 목록을 조회한다. +- [ ] B 를 입력하면 메인 화면으로 되돌아간다. + +#### - 주의사항 +- [ ] 이미 등록된 이름의 역은 추가할 수 없다. +- [ ] 역 이름은 2글자 이상이어야 한다. +- [ ] 노선에 등록된 역은 삭제할 수 없다. + +### 지하철 노선에 등록된 역 조회 기능 +- [ ] 상행 종점부터 하행 종점까지 순서대로 나열한다. + +
+
+
+ +## Service +### Managerable +각각의 Service 들중 공통적으로 필요한 메서드를 나열 하는 추상클래스 +- [ ] 등록 +- [ ] 삭제 +- [ ] 조회 +- [ ] 되돌아가기 - 메인화면 + +### LineManager +노선 관련한 서비스를 모은 집약체 +- [ ] 노선 등록 + - [ ] 이름으로 노선 객체를 불러올 수 있다. + - [ ] 노선 객체에 상행 종점과 하행 종점을 저장한다. + - [ ] 저장하는 배열의 가장 처음을 상행 종점, 가장 마지막을 하행 종점으로 지정한다. + - [ ] 사용자가 입력한 이름이 노선 목록에 존재하지 않을 경우에만 노선을 생성한다. + - [ ] 사용자가 입력한 이름이 2글자 이상 되지 않는다면 노선을 생성하지 않는다. - 메인화면으로 + - [ ] 노선에 등록된 역은 속한 노선 이름을 저장할 수 있다. +- [ ] 노선 삭제 + - [ ] 노선을 삭제한다고 역이 삭제되지는 않는다. +- [ ] 노선 목록 조회 + +### SectionManager +구간 관련한 서비스를 모은 집약체 +- [ ] 구간 등록 + - [ ] 역을 저장한 1차원 배열에서 올바른 위치에 역을 삽입할 수 있다. + - [ ] 존재하지 않는 역을 생성하여 구간을 만들 수 없다. +- [ ] 구간 삭제 + - [ ] 역을 저장한 1차원 배열에서 해당하는 역을 삭제할 수 있다. + - [ ] 노선에 포함된 역이 2개 이상일 때 삭제할 수 없다. - 메인 화면으로 + - [ ] 종점을 제거하면 다음 역이 종점이 된다. +- [ ] 되돌아가기 - 메인화면 + +### StationManager +역과 관련한 서비스를 모은 집약체 +- [ ] 역 등록 + - [ ] 2글자 이상일 때 등록이 가능하다. + - [ ] 이미 존재하는 이름으로 등록은 불가능하다. - 메인화면 + - [ ] 구간 등록을 하지 않은 역은 어떤 노선과도 연결되지 않는다. +- [ ] 역 삭제 + - [ ] 노선에 등록되어 있는 역은 삭제할 수 없다. +- [ ] 역 조회 +- [ ] 되돌아가기 - 메인화면 + +
+
+
+ +## View +- [ ] 매개변수를 활용하여 코드의 재사용성을 극대화한다. +### Askview +- [ ] \## 00할 00을 입력하세요. +- [ ] \## 원하는 기능을 선택하세요. + +### Responseview +- [ ] \[INFO] 지하철 00이 00되었습니다. +- [ ] \## 00 목록 +- [ ] \[INFO] 00 +- [ ] \## 00 화면 +- [ ] 노선/역/구간 +```aidl +// 노선/역/구간 +1. 00 등록 +2. 00 삭제 +3. 00 조회 +B. 돌아가기 +``` + +```aidl +// main 화면 +1. 역 관리 +2. 노선 관리 +3. 구간 관리 +4. 지하철 노선도 출력 +Q. 종료 +``` \ No newline at end of file From ea6d0b648a366cbd16c06cd4debf1733b340e0fb Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 10 May 2024 16:29:10 +0900 Subject: [PATCH 035/104] =?UTF-8?q?refactor:=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/service/LineManager.java | 2 +- .../subway/service/{Managerbale.java => Managerable.java} | 5 +---- src/main/java/subway/service/SectionManager.java | 2 +- src/main/java/subway/service/StationManager.java | 4 +--- 4 files changed, 4 insertions(+), 9 deletions(-) rename src/main/java/subway/service/{Managerbale.java => Managerable.java} (66%) diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index 9f74c2923..d52db0f3f 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -4,7 +4,7 @@ import subway.domain.LineRepository; import subway.domain.Station; -public class LineManager extends Managerbale{ +public class LineManager extends Managerable { static final LineRepository lineRepo = new LineRepository(); static StationManager stationManager = new StationManager(); diff --git a/src/main/java/subway/service/Managerbale.java b/src/main/java/subway/service/Managerable.java similarity index 66% rename from src/main/java/subway/service/Managerbale.java rename to src/main/java/subway/service/Managerable.java index afac5f706..9c4682797 100644 --- a/src/main/java/subway/service/Managerbale.java +++ b/src/main/java/subway/service/Managerable.java @@ -1,9 +1,6 @@ package subway.service; -import subway.domain.LineRepository; -import subway.domain.StationRepository; - -public abstract class Managerbale { +public abstract class Managerable { public abstract boolean register(String command); public abstract boolean delete(String name); public abstract StringBuilder read(); diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index a02ea2bd1..e9e53ad02 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -5,7 +5,7 @@ import subway.domain.Station; import subway.domain.StationRepository; -public class SectionManager extends Managerbale{ +public class SectionManager extends Managerable { static LineRepository lineRepo = new LineRepository(); static StationRepository stationRepo = new StationRepository(); diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index 1e5364525..5814b0f40 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -3,10 +3,8 @@ import subway.domain.Station; import subway.domain.StationRepository; -import java.util.List; - // 역 관련 기능하는 서비스 -public class StationManager extends Managerbale{ +public class StationManager extends Managerable { private static final StationRepository stations = new StationRepository(); @Override // 삽입 public boolean register(String name) { From 0074748a77c26cfc3306378a2f3bcfc1f56ecb60 Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 10 May 2024 17:05:45 +0900 Subject: [PATCH 036/104] =?UTF-8?q?feat=20:=20=EA=B5=AC=EA=B0=84=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/SectionController.java | 67 +++++++++++-------- src/main/java/subway/domain/Line.java | 9 +++ .../java/subway/service/SectionManager.java | 21 +++--- 3 files changed, 59 insertions(+), 38 deletions(-) diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index d59bef585..765dd037a 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -1,40 +1,37 @@ package subway.controller; -import subway.service.LineManager; import subway.service.SectionManager; -import subway.service.StationManager; -public class SectionController extends ManageController{ - private static StationManager stationManager = new StationManager(); - private static LineManager lineManager = new LineManager(); +public class SectionController extends ManageController { private static SectionManager sectionManager = new SectionManager(); + @Override public void work() { ask.WhatToManage("구간"); ask.Function(); - try{ + try { String command = br.readLine(); - if(command.equals("1")){ // 등록 + if (command.equals("1")) { // 등록 register(); return; } - if(command.equals("2")){ // 삭제 + if (command.equals("2")) { // 삭제 delete(); return; } - if(command.equals("B")){ // 되돌아가기 + if (command.equals("B")) { // 되돌아가기 return; } - // 에러 발생시키 - }catch (Exception e){ + // 에러 발생시키기 + } catch (Exception e) { e.printStackTrace(); } } @Override public void register() { // 노선, 역 이름, 순서 입력 받고 등록 - ask.orderWhat("노선"); String line = getLine(); + sectionManager.register(line); ask.orderWhat("역 이름"); String station = getStation(); @@ -42,32 +39,35 @@ public void register() { // 노선, 역 이름, 순서 입력 받고 등록 ask.orderWhere(); int index = getIndex(); - sectionManager.insertSection(line, station, index); + sectionManager.insertSection(station, index); } - public String getLine(){ - try{ - return br.readLine(); - }catch (Exception e){ - System.out.println("존재하지 않는 노선입니다."); + public String getLine() { + ask.orderWhat("노선"); + String line = ""; + try { + line = br.readLine(); + } catch (Exception e) { e.printStackTrace(); } - return null; + return line; } - public String getStation(){ - try{ + + public String getStation() { + try { return br.readLine(); - }catch (Exception e){ - System.out.println("존재하지 않는 역입니다."); + } catch (Exception e) { + System.out.println("역 이름을 받을 수 없습니다. 다시 시도해주세요."); e.printStackTrace(); } - return null; } + return null; + } - public int getIndex(){ - try{ + public int getIndex() { + try { int index = Integer.parseInt(br.readLine()); return index; - } catch (Exception e){ + } catch (Exception e) { System.out.println("입력값을 확인해주세요."); e.printStackTrace(); } @@ -76,6 +76,17 @@ public int getIndex(){ @Override public void delete() { - + try{ + ask.Name("삭제", "구간의 노선"); + String line = br.readLine(); + ask.Name("삭제", "구간의 역"); + String station = br.readLine(); + // manager에서 line 세팅. 함수 이름 고민중... + // 알고리즘 빡 분리해야 할 듯함 + sectionManager.register(line); + sectionManager.delete(station); + } catch (Exception e){ + e.printStackTrace(); + } } } diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index 3beedaa6b..66a5bfe7a 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -27,6 +27,15 @@ public void addStation(Station station, int index){ stations.add(index, station); } + public void deleteStation(String station){ + int index=0; + for(index =0 ;index < stations.size();index++){ + Station s = stations.get(index); + if(s.getName().equals(station)) break; + } + stations.remove(index); + } + public void insertStation(Station node, int index){ stations.add(index, node); } diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index e9e53ad02..76cf8bdcd 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -8,38 +8,39 @@ public class SectionManager extends Managerable { static LineRepository lineRepo = new LineRepository(); static StationRepository stationRepo = new StationRepository(); + private Line line; @Override public boolean register(String name) { - return true; - } - - public void insertSection(String name, String sName,int index){ - // 이미 해당 역이 등록되어 있는지 확인 - // 등록되지 않았으면 - Line line = lineRepo.getLineByName(name); + line = lineRepo.getLineByName(name); if(line == null){ System.out.println(" 존재하지 않는 노선입니다."); } + return true; + } + public void insertSection(String sName,int index){ Station station = stationRepo.getStationByName(sName); if(station == null){ System.out.println(" 존재하지 않는 역입니다."); } lineRepo.addLine(line, station, index); - - } @Override public boolean delete(String name) { + try{ + line.deleteStation(name); + }catch (Exception e){ + e.printStackTrace(); + return false; + } return true; } @Override public StringBuilder read() { StringBuilder sb = new StringBuilder(); - sb.append(""); return sb; } From a87cf19010dfb6c8ca5044a34dd4ec6ba6c41c7f Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 10 May 2024 17:34:37 +0900 Subject: [PATCH 037/104] =?UTF-8?q?feat=20:=20DataManager=20=EC=84=B8?= =?UTF-8?q?=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../constants/initValues/LineNames.java | 8 ++ .../constants/initValues/StationNames.java | 13 ++ .../constants/initValues/lineNames.java | 5 - .../constants/initValues/stationNames.java | 14 --- .../subway/controller/MainController.java | 27 +++-- src/main/java/subway/service/DataManager.java | 112 +++++++++--------- 6 files changed, 95 insertions(+), 84 deletions(-) create mode 100644 src/main/java/subway/config/constants/initValues/LineNames.java create mode 100644 src/main/java/subway/config/constants/initValues/StationNames.java delete mode 100644 src/main/java/subway/config/constants/initValues/lineNames.java delete mode 100644 src/main/java/subway/config/constants/initValues/stationNames.java diff --git a/src/main/java/subway/config/constants/initValues/LineNames.java b/src/main/java/subway/config/constants/initValues/LineNames.java new file mode 100644 index 000000000..308991b49 --- /dev/null +++ b/src/main/java/subway/config/constants/initValues/LineNames.java @@ -0,0 +1,8 @@ +package subway.config.constants.initValues; + +public class LineNames { + private String[] lines = new String []{"2호선", "3호선", "수인분당선"}; + public String[] getNames(){ + return lines; + } +} diff --git a/src/main/java/subway/config/constants/initValues/StationNames.java b/src/main/java/subway/config/constants/initValues/StationNames.java new file mode 100644 index 000000000..3c8b1da32 --- /dev/null +++ b/src/main/java/subway/config/constants/initValues/StationNames.java @@ -0,0 +1,13 @@ +package subway.config.constants.initValues; + +public class StationNames { +// // 해당 자료형을 컨트롤할 수 잇는 코드를 추출할까 +// List train2 = new LinkedList<>(); +// List train3 = new LinkedList<>(); +// List trainSuIn = new LinkedList<>(); + private String[] stations = new String []{"교대역", "강남역", "역삼역", "남부터미널역", "양재역", "양재시민의숲역", "매봉역"}; + + public String[] getNames() { + return stations; + } +} diff --git a/src/main/java/subway/config/constants/initValues/lineNames.java b/src/main/java/subway/config/constants/initValues/lineNames.java deleted file mode 100644 index 1e4d6d335..000000000 --- a/src/main/java/subway/config/constants/initValues/lineNames.java +++ /dev/null @@ -1,5 +0,0 @@ -package subway.config.constants.initValues; - -public class lineNames { - String[] stations = new String []{"2호선", "3호선", "수인분당선"}; -} diff --git a/src/main/java/subway/config/constants/initValues/stationNames.java b/src/main/java/subway/config/constants/initValues/stationNames.java deleted file mode 100644 index 45ce8e159..000000000 --- a/src/main/java/subway/config/constants/initValues/stationNames.java +++ /dev/null @@ -1,14 +0,0 @@ -package subway.config.constants.initValues; - -import subway.domain.Line; - -import java.util.LinkedList; -import java.util.List; - -public class stationNames { - // 해당 자료형을 컨트롤할 수 잇는 코드를 추출할까 - List train2 = new LinkedList<>(); - List train3 = new LinkedList<>(); - List trainSuIn = new LinkedList<>(); - String[] stations = new String []{"교대역", "강남역", "역삼역", "남부터미널역", "양재역", "양재시민의숲역", "매봉역"}; -} diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 320281867..960ebaef7 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -6,16 +6,23 @@ import java.io.InputStreamReader; public class MainController { - AskView ask = new AskView(); - StationController stationController = new StationController(); - LineController lineController = new LineController(); - SectionController sectionController = new SectionController(); + private AskView ask; + private StationController stationController; + private LineController lineController; + private SectionController sectionController; - public void headController() throws Exception{ + public MainController() { + ask = new AskView(); + stationController = new StationController(); + lineController = new LineController(); + sectionController = new SectionController(); + } + + public void headController() throws Exception { ask.printMain(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String command = br.readLine(); - if(command.equals("Q")) { + if (command.equals("Q")) { System.out.println(" 안녕히 가세요. "); return; } @@ -27,9 +34,9 @@ public void headController() throws Exception{ } // 생성자와 함께 출력과 서비스를 제공하려 했는데 무슨 일 할 때마다 클래스 생성하는 것은 비효율적인 것 같다. 메소드로 바꾸자. - public void CoreController(int command){ - if(command == 1) stationController.work(); - if(command == 2) lineController.work(); - if(command == 3) sectionController.work(); + public void CoreController(int command) { + if (command == 1) stationController.work(); + if (command == 2) lineController.work(); + if (command == 3) sectionController.work(); } } diff --git a/src/main/java/subway/service/DataManager.java b/src/main/java/subway/service/DataManager.java index 0cf6a72fe..68b35dc47 100644 --- a/src/main/java/subway/service/DataManager.java +++ b/src/main/java/subway/service/DataManager.java @@ -1,55 +1,57 @@ -//package subway.service; -// -//import subway.domain.Line; -//import subway.domain.LineRepository; -//import subway.domain.Station; -//import subway.domain.StationRepository; -// -//import java.util.List; -// -//public class DataManager extends Managerbale{ -// public DataManager(){ -// // 초기값 삽입 -// } -// @Override // 삽입 -// public boolean register(String command) { -//// if command.equals("station") 역만 삽입하는 모듈 연결 -//// if command.equals("line") 노선 삽입하는 모듈 연결 -//// if command.equals("Section") 구간 삽입하는 모듈 연결 -// return true; -// } -// -// public void insertStation(String node){ -// // 이미 해당 역이 존재하는지 아닌지 확인하는 코드 추가 (util 로 만들기) -// Station station = new Station(node); -// stationRepo.addStation(station); -// } -// -// public void insertLine(String upperStation, String bottomStation, String name){ -// List stations = stationRepo.stations(); -// // 이미 해당 역이 존재하는지 아닌지 확인하는 코드 추가 -// Station upper = new Station(upperStation); -// Station bottom = new Station(bottomStation); -// stationRepo.addStation(upper); -// stationRepo.addStation(bottom); -// Line created = new Line(name); -// lineRepo.addLine(created); -// } -// -// public void insertSection(){ -// -// } -// -// @Override // 삭제 -// public boolean delete(String name) { -// return true; -// } -// -// @Override // 조회 -// public StringBuilder read() { -// StringBuilder sb = new StringBuilder(); -// -// sb.append(""); -// return sb; -// } -//} +package subway.service; + +import subway.config.constants.initValues.*; +import subway.domain.Line; +import subway.domain.LineRepository; +import subway.domain.Station; +import subway.domain.StationRepository; + +public class DataManager{ + private final static LineNames lines = new LineNames(); // String[] + private final static StationNames stations = new StationNames(); // String[] + + final static LineRepository lineRepo = new LineRepository(); + final static StationRepository stationRepo = new StationRepository(); + + public DataManager(){ + // 초기값 삽입 +// 2. 모든 노선을 입력한다. +// 3. 모든 구간을 입력한다. + addStations();// 1. 모든 역들을 입력한다. + addLines(); + } + + public void addStations(){ + String[] names = stations.getNames(); + for(String station: names){ + stationRepo.addStation(new Station(station)); + } + } + + public void addLines(){ + String[] names = lines.getNames(); + for(String line: names){ + Line node = new Line(line); + lineSet(node); + lineRepo.addLine(node); + } + } + + public void lineSet(Line line){ + if(line.getName().equals("2호선")){ + line.setStations(stationRepo.getStationByName("교대역"), stationRepo.getStationByName("역삼역")); + line.addStation(stationRepo.getStationByName("강남역"), 1); + return; + } + if(line.getName().equals("3호선")){ + line.setStations(stationRepo.getStationByName("교대역"), stationRepo.getStationByName("매봉역")); + line.addStation(stationRepo.getStationByName("양재역"), 1); + line.addStation(stationRepo.getStationByName("남부터미널역"), 1); + return; + } + if(line.getName().equals("수인분당선")){ + line.setStations(stationRepo.getStationByName("강남역"), stationRepo.getStationByName("양재시민의숲역")); + line.addStation(stationRepo.getStationByName("양재역"), 1); + } + } +} From 827fc06ca7255c4458317e0dfca0c115682934bb Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 10 May 2024 19:37:52 +0900 Subject: [PATCH 038/104] =?UTF-8?q?fix:=20DataManager=20=EC=A0=81=EC=9A=A9?= =?UTF-8?q?=20=ED=9B=84=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EB=B6=88=EC=9D=BC?= =?UTF-8?q?=EC=B9=98=20=EB=AC=B8=EC=A0=9C=20=ED=95=B4=EA=B2=B0=20=EB=B0=8F?= =?UTF-8?q?=20=EC=A0=80=EC=9E=A5=EC=86=8C=20=EA=B8=B0=EB=B3=B8=20=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=84=B0=20=EC=82=BD=EC=9E=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../constants/initValues/LineNames.java | 2 +- .../constants/initValues/StationNames.java | 2 +- .../subway/controller/LineController.java | 6 +++++- .../subway/controller/MainController.java | 8 +++++--- .../subway/controller/ManageController.java | 9 --------- .../subway/controller/SectionController.java | 13 +++++++++++-- .../subway/controller/StationController.java | 6 ++++++ src/main/java/subway/service/DataManager.java | 18 ++++++++++++++++-- src/main/java/subway/service/LineManager.java | 6 +++--- .../java/subway/service/SectionManager.java | 5 +++-- .../java/subway/service/StationManager.java | 19 +++++++------------ src/main/java/subway/view/AskView.java | 8 ++++++++ 12 files changed, 66 insertions(+), 36 deletions(-) diff --git a/src/main/java/subway/config/constants/initValues/LineNames.java b/src/main/java/subway/config/constants/initValues/LineNames.java index 308991b49..a9ef63255 100644 --- a/src/main/java/subway/config/constants/initValues/LineNames.java +++ b/src/main/java/subway/config/constants/initValues/LineNames.java @@ -1,7 +1,7 @@ package subway.config.constants.initValues; public class LineNames { - private String[] lines = new String []{"2호선", "3호선", "수인분당선"}; + private static final String[] lines = new String []{"2호선", "3호선", "수인분당선"}; public String[] getNames(){ return lines; } diff --git a/src/main/java/subway/config/constants/initValues/StationNames.java b/src/main/java/subway/config/constants/initValues/StationNames.java index 3c8b1da32..7065b6385 100644 --- a/src/main/java/subway/config/constants/initValues/StationNames.java +++ b/src/main/java/subway/config/constants/initValues/StationNames.java @@ -5,7 +5,7 @@ public class StationNames { // List train2 = new LinkedList<>(); // List train3 = new LinkedList<>(); // List trainSuIn = new LinkedList<>(); - private String[] stations = new String []{"교대역", "강남역", "역삼역", "남부터미널역", "양재역", "양재시민의숲역", "매봉역"}; + private static final String[] stations = new String []{"교대역", "강남역", "역삼역", "남부터미널역", "양재역", "양재시민의숲역", "매봉역"}; public String[] getNames() { return stations; diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 4f7aba2f4..d626d6bc5 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -1,9 +1,13 @@ package subway.controller; +import subway.service.DataManager; import subway.service.LineManager; public class LineController extends ManageController{ - static LineManager lineManager = new LineManager(); + static LineManager lineManager; + public LineController(DataManager manager){ + lineManager = manager.getLineManager(); + } @Override public void work(){ ask.WhatToManage("노선"); diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 960ebaef7..d83f4d6b9 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -1,5 +1,6 @@ package subway.controller; +import subway.service.DataManager; import subway.view.AskView; import java.io.BufferedReader; @@ -13,9 +14,10 @@ public class MainController { public MainController() { ask = new AskView(); - stationController = new StationController(); - lineController = new LineController(); - sectionController = new SectionController(); + DataManager manager = new DataManager(); + stationController = new StationController(manager); + lineController = new LineController(manager); + sectionController = new SectionController(manager); } public void headController() throws Exception { diff --git a/src/main/java/subway/controller/ManageController.java b/src/main/java/subway/controller/ManageController.java index 02e555005..d20b237d4 100644 --- a/src/main/java/subway/controller/ManageController.java +++ b/src/main/java/subway/controller/ManageController.java @@ -13,10 +13,6 @@ public abstract class ManageController { AskView ask = new AskView(); ResponseView response = new ResponseView(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - static final StationManager stationManager = new StationManager(); - static final LineRepository lineRepo = new LineRepository(); - static final StationRepository stationRepo = new StationRepository(); - public void work(){ // ask.Function(); // 관련 컨트롤러 호출 부분 @@ -28,9 +24,4 @@ public void work(){ public abstract void delete(); // db 접근해서 데이터 삭제하는 서비스와 연결 - - // 되돌아가기 - public void goBack(){ - return; - } } diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 765dd037a..1cd56e59b 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -1,13 +1,20 @@ package subway.controller; +import subway.service.DataManager; import subway.service.SectionManager; +import subway.view.ResponseView; public class SectionController extends ManageController { - private static SectionManager sectionManager = new SectionManager(); + private static SectionManager sectionManager; + private static ResponseView response = new ResponseView(); + + public SectionController(DataManager manager){ + sectionManager = manager.getSectionManager(); + } @Override public void work() { - ask.WhatToManage("구간"); + ask.sectionManage(); ask.Function(); try { String command = br.readLine(); @@ -40,6 +47,7 @@ public void register() { // 노선, 역 이름, 순서 입력 받고 등록 int index = getIndex(); sectionManager.insertSection(station, index); + response.printInfo("구간이 등록되었습니다."); } public String getLine() { @@ -85,6 +93,7 @@ public void delete() { // 알고리즘 빡 분리해야 할 듯함 sectionManager.register(line); sectionManager.delete(station); + response.printInfo("구간이 삭제되었습니다."); } catch (Exception e){ e.printStackTrace(); } diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index a9a569de2..6b7ee882a 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -1,9 +1,15 @@ package subway.controller; +import subway.service.DataManager; import subway.service.StationManager; // 역을 관리하는 컨트롤러 public class StationController extends ManageController{ + static StationManager stationManager; + + StationController(DataManager manager){ + stationManager = manager.getStationManager(); + } @Override public void work(){ ask.WhatToManage("역"); diff --git a/src/main/java/subway/service/DataManager.java b/src/main/java/subway/service/DataManager.java index 68b35dc47..6e736c733 100644 --- a/src/main/java/subway/service/DataManager.java +++ b/src/main/java/subway/service/DataManager.java @@ -13,10 +13,24 @@ public class DataManager{ final static LineRepository lineRepo = new LineRepository(); final static StationRepository stationRepo = new StationRepository(); + final static StationManager stationManager = new StationManager(); + final static SectionManager sectionManager = new SectionManager(); + final static LineManager lineManager = new LineManager(); + + public LineManager getLineManager(){ + return lineManager; + } + + public static StationManager getStationManager() { + return stationManager; + } + + public static SectionManager getSectionManager(){ + return sectionManager; + } + public DataManager(){ // 초기값 삽입 -// 2. 모든 노선을 입력한다. -// 3. 모든 구간을 입력한다. addStations();// 1. 모든 역들을 입력한다. addLines(); } diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index d52db0f3f..fe144e895 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -4,10 +4,10 @@ import subway.domain.LineRepository; import subway.domain.Station; -public class LineManager extends Managerable { - static final LineRepository lineRepo = new LineRepository(); - static StationManager stationManager = new StationManager(); +import static subway.service.DataManager.lineRepo; +import static subway.service.DataManager.stationManager; +public class LineManager extends Managerable { @Override public boolean register(String name) { if(lineRepo.getLineByName(name) != null){ diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 76cf8bdcd..2260e3357 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -5,9 +5,10 @@ import subway.domain.Station; import subway.domain.StationRepository; +import static subway.service.DataManager.lineRepo; +import static subway.service.DataManager.stationRepo; + public class SectionManager extends Managerable { - static LineRepository lineRepo = new LineRepository(); - static StationRepository stationRepo = new StationRepository(); private Line line; @Override diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index 5814b0f40..b5c19c8c6 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -1,25 +1,24 @@ package subway.service; import subway.domain.Station; -import subway.domain.StationRepository; +import static subway.service.DataManager.stationRepo; // 역 관련 기능하는 서비스 public class StationManager extends Managerable { - private static final StationRepository stations = new StationRepository(); @Override // 삽입 public boolean register(String name) { // 해당 역이 존재하는 지 확인 // 삽입 - if(stations.getStationByName(name)!=null){ + if(stationRepo.getStationByName(name)!=null){ // 이미 존재하는 역이라고 전달하기 return false; // 문자열 반환해서 상태 메시지 출력에 활용 } - stations.addStation(new Station(name)); + stationRepo.addStation(new Station(name)); return true; } public boolean isExist(String name){ - if(stations.getStationByName(name)!=null){ + if(stationRepo.getStationByName(name)!=null){ // 이미 존재하는 역이라고 전달하기 return true; // 문자열 반환해서 상태 메시지 출력에 활용 } @@ -28,14 +27,14 @@ public boolean isExist(String name){ @Override public boolean delete(String name) { - return stations.deleteStation(name); + return stationRepo.deleteStation(name); } @Override public StringBuilder read() { // 역 조회 StringBuilder sb = new StringBuilder(); - for(Station station : stations.stations()){ + for(Station station : stationRepo.stations()){ sb.append("[INFO] " + station.getName()+"\n"); } return sb; @@ -43,10 +42,6 @@ public StringBuilder read() { public Station getByName(String name){ register(name); - return stations.getStationByName(name); + return stationRepo.getStationByName(name); } - -// public List readStation(){ -// return stations.stations(); -// } } diff --git a/src/main/java/subway/view/AskView.java b/src/main/java/subway/view/AskView.java index 307d94ad5..e502ad71c 100644 --- a/src/main/java/subway/view/AskView.java +++ b/src/main/java/subway/view/AskView.java @@ -24,6 +24,14 @@ public void WhatToManage(String target){ System.out.println(sb.toString()); sb.setLength(0); } + public void sectionManage(){ + sb.append("## 구간 관리 화면\n"); + sb.append("1. 구간 등록\n"); + sb.append("2. 구간 삭제\n"); + sb.append("B. 돌아가기\n"); + System.out.println(sb.toString()); + sb.setLength(0); + } public void Function(){ sb.append("## 원하는 기능을 선택하세요\n"); From 150ba158114995d91caf0004992fbf41bbaab67c Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 10 May 2024 20:43:46 +0900 Subject: [PATCH 039/104] =?UTF-8?q?refactor,=20feat:=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=20=EC=A0=95=EB=A6=AC=20=EB=B0=8F=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80(2=EA=B8=80=EC=9E=90,=20=EC=9C=A0=ED=9A=A8?= =?UTF-8?q?=ED=95=9C=EB=AA=85=EB=A0=B9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/handler/SubwayException.java | 24 ++++++++++++++- .../subway/controller/LineController.java | 7 ++++- .../subway/controller/MainController.java | 28 ++++++++++++++---- .../subway/controller/SectionController.java | 5 ++++ .../subway/controller/StationController.java | 5 ++++ src/main/java/subway/domain/Line.java | 29 +++++++++---------- src/main/java/subway/service/DataManager.java | 4 +++ .../java/subway/service/SectionManager.java | 21 +++++--------- 8 files changed, 86 insertions(+), 37 deletions(-) diff --git a/src/main/java/subway/config/handler/SubwayException.java b/src/main/java/subway/config/handler/SubwayException.java index 3f7d6e368..6ebe2b2d8 100644 --- a/src/main/java/subway/config/handler/SubwayException.java +++ b/src/main/java/subway/config/handler/SubwayException.java @@ -1,4 +1,26 @@ package subway.config.handler; -public class SubwayException { +public class SubwayException extends IllegalArgumentException { + public boolean isValidLength(String name) { + if (name.length() >= 2) return true; + throw new IllegalStateException("## 2글자 이상으로 입력해주세요."); + } + + public void notValidCommand() { + throw new IllegalArgumentException("## 잘못된 명령입니다."); + } + + public void checkCommand() { + throw new IllegalArgumentException("## 입력값을 확인해주세요."); + } + + public void noStation() { + throw new IllegalArgumentException("## 존재하지 않는 역입니다."); + } + + public void noLine() { + throw new IllegalArgumentException("## 존재하지 않는 노선입니다."); + } + + } diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index d626d6bc5..318ac6fae 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -1,12 +1,15 @@ package subway.controller; +import subway.config.handler.SubwayException; import subway.service.DataManager; import subway.service.LineManager; public class LineController extends ManageController{ static LineManager lineManager; + private static SubwayException subwayException; public LineController(DataManager manager){ lineManager = manager.getLineManager(); + subwayException = manager.getSubwayException(); } @Override public void work(){ @@ -29,7 +32,8 @@ public void work(){ if(command.equals("B")){ return; } - // 에러 발생시키 + // 에러 발생시키기 + subwayException.notValidCommand(); }catch (Exception e){ e.printStackTrace(); } @@ -41,6 +45,7 @@ public void register() { ask.Name("등록", "노선"); try{ String line = br.readLine(); + subwayException.isValidLength(line); boolean result = lineManager.register(line); ask.Name("등록", "상행 종점역"); diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index d83f4d6b9..37df1ca59 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -1,5 +1,6 @@ package subway.controller; +import subway.config.handler.SubwayException; import subway.service.DataManager; import subway.view.AskView; @@ -11,6 +12,7 @@ public class MainController { private StationController stationController; private LineController lineController; private SectionController sectionController; + private static SubwayException subwayException; public MainController() { ask = new AskView(); @@ -18,6 +20,7 @@ public MainController() { stationController = new StationController(manager); lineController = new LineController(manager); sectionController = new SectionController(manager); + subwayException = manager.getSubwayException(); } public void headController() throws Exception { @@ -28,17 +31,30 @@ public void headController() throws Exception { System.out.println(" 안녕히 가세요. "); return; } - + try{ + int validCommand = Integer.parseInt(command); + CoreController(validCommand); + }catch (Exception e){ + e.printStackTrace(); + } // command 가 유효한 명령이 아닐 경우 코드 추가 - int validCommand = Integer.parseInt(command); - CoreController(validCommand); headController(); } // 생성자와 함께 출력과 서비스를 제공하려 했는데 무슨 일 할 때마다 클래스 생성하는 것은 비효율적인 것 같다. 메소드로 바꾸자. public void CoreController(int command) { - if (command == 1) stationController.work(); - if (command == 2) lineController.work(); - if (command == 3) sectionController.work(); + if (command == 1) { + stationController.work(); + return; + } + if (command == 2) { + lineController.work(); + return; + } + if (command == 3) { + sectionController.work(); + return; + } + subwayException.notValidCommand(); } } diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 1cd56e59b..3436ce3b0 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -1,5 +1,6 @@ package subway.controller; +import subway.config.handler.SubwayException; import subway.service.DataManager; import subway.service.SectionManager; import subway.view.ResponseView; @@ -7,9 +8,12 @@ public class SectionController extends ManageController { private static SectionManager sectionManager; private static ResponseView response = new ResponseView(); + static SubwayException subwayException; + public SectionController(DataManager manager){ sectionManager = manager.getSectionManager(); + subwayException = manager.getSubwayException(); } @Override @@ -30,6 +34,7 @@ public void work() { return; } // 에러 발생시키기 + subwayException.notValidCommand(); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 6b7ee882a..84f6db986 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -1,14 +1,17 @@ package subway.controller; +import subway.config.handler.SubwayException; import subway.service.DataManager; import subway.service.StationManager; // 역을 관리하는 컨트롤러 public class StationController extends ManageController{ static StationManager stationManager; + static SubwayException subwayException; StationController(DataManager manager){ stationManager = manager.getStationManager(); + subwayException = manager.getSubwayException(); } @Override public void work(){ @@ -32,6 +35,7 @@ public void work(){ return; } // 에러 발생시키 + subwayException.notValidCommand(); }catch (Exception e){ e.printStackTrace(); } @@ -42,6 +46,7 @@ public void register(){ ask.Name("등록", "역"); try{ String station = br.readLine(); + subwayException.isValidLength(station); boolean result = stationManager.register(station); // "지하철 역이 등록되었습니다." 출력 infoMessage("등록", result); diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index 66a5bfe7a..2dc93117b 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -16,34 +16,33 @@ public String getName() { return name; } - public void setStations(Station upper, Station bottom){ + public void setStations(Station upper, Station bottom) { this.upperStation = upper; - this.bottomStation=bottom; + this.bottomStation = bottom; stations.addFirst(upper); stations.addLast(bottom); } - public void addStation(Station station, int index){ + public void addStation(Station station, int index) { stations.add(index, station); } - public void deleteStation(String station){ - int index=0; - for(index =0 ;index < stations.size();index++){ + public boolean deleteStation(String station) { + int index = 0; + for (index = 0; index < stations.size(); index++) { Station s = stations.get(index); - if(s.getName().equals(station)) break; + if (s.getName().equals(station)) { + stations.remove(index); + return true; + } } - stations.remove(index); - } - - public void insertStation(Station node, int index){ - stations.add(index, node); + return false; } // 추가 기능 구현 - public Station getStationByName(String name){ - for(Station station : stations){ - if(station.getName().equals(name)){ + public Station getStationByName(String name) { + for (Station station : stations) { + if (station.getName().equals(name)) { return station; } } diff --git a/src/main/java/subway/service/DataManager.java b/src/main/java/subway/service/DataManager.java index 6e736c733..e574446b9 100644 --- a/src/main/java/subway/service/DataManager.java +++ b/src/main/java/subway/service/DataManager.java @@ -1,6 +1,7 @@ package subway.service; import subway.config.constants.initValues.*; +import subway.config.handler.SubwayException; import subway.domain.Line; import subway.domain.LineRepository; import subway.domain.Station; @@ -17,6 +18,8 @@ public class DataManager{ final static SectionManager sectionManager = new SectionManager(); final static LineManager lineManager = new LineManager(); + final static SubwayException subwayException = new SubwayException(); + public LineManager getLineManager(){ return lineManager; } @@ -28,6 +31,7 @@ public static StationManager getStationManager() { public static SectionManager getSectionManager(){ return sectionManager; } + public static SubwayException getSubwayException(){return subwayException;} public DataManager(){ // 초기값 삽입 diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 2260e3357..9adceb7ae 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -1,5 +1,6 @@ package subway.service; +import subway.config.handler.SubwayException; import subway.domain.Line; import subway.domain.LineRepository; import subway.domain.Station; @@ -10,33 +11,25 @@ public class SectionManager extends Managerable { private Line line; - + private SubwayException subwayException; @Override public boolean register(String name) { line = lineRepo.getLineByName(name); - if(line == null){ - System.out.println(" 존재하지 않는 노선입니다."); - } + if(line == null) subwayException.noLine(); return true; } public void insertSection(String sName,int index){ Station station = stationRepo.getStationByName(sName); - if(station == null){ - System.out.println(" 존재하지 않는 역입니다."); - } + if(station == null) subwayException.noStation(); lineRepo.addLine(line, station, index); } @Override public boolean delete(String name) { - try{ - line.deleteStation(name); - }catch (Exception e){ - e.printStackTrace(); - return false; - } - return true; + if(line.deleteStation(name)) return true; + subwayException.checkCommand(); + return false; } @Override From 82715348681bd0fa679ba29b64ee22302451948f Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 10 May 2024 21:07:25 +0900 Subject: [PATCH 040/104] =?UTF-8?q?feat:=20=EC=A7=80=ED=95=98=EC=B2=A0=20?= =?UTF-8?q?=EB=85=B8=EC=84=A0=EB=8F=84=20=EC=B6=9C=EB=A0=A5=20=EB=B0=8F=20?= =?UTF-8?q?=EA=B5=AC=EA=B0=84=20=EB=93=B1=EB=A1=9D=20=ED=95=A0=20=EB=95=8C?= =?UTF-8?q?=20=20index=20=EC=A1=B0=EC=A0=95(=EC=8B=9C=EC=9E=910=20->=20?= =?UTF-8?q?=EC=8B=9C=EC=9E=911)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/MainController.java | 6 ++++++ .../java/subway/controller/MapController.java | 19 +++++++++++++++++++ .../subway/controller/SectionController.java | 2 +- src/main/java/subway/domain/Line.java | 7 +++++++ src/main/java/subway/service/LineManager.java | 19 +++++++++++++++---- 5 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 src/main/java/subway/controller/MapController.java diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 37df1ca59..969da61e7 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -12,6 +12,7 @@ public class MainController { private StationController stationController; private LineController lineController; private SectionController sectionController; + private MapController mapController; private static SubwayException subwayException; public MainController() { @@ -20,6 +21,7 @@ public MainController() { stationController = new StationController(manager); lineController = new LineController(manager); sectionController = new SectionController(manager); + mapController = new MapController(manager); subwayException = manager.getSubwayException(); } @@ -55,6 +57,10 @@ public void CoreController(int command) { sectionController.work(); return; } + if(command==4){ + mapController.work(); + return; + } subwayException.notValidCommand(); } } diff --git a/src/main/java/subway/controller/MapController.java b/src/main/java/subway/controller/MapController.java new file mode 100644 index 000000000..53efb67b0 --- /dev/null +++ b/src/main/java/subway/controller/MapController.java @@ -0,0 +1,19 @@ +package subway.controller; + +import subway.domain.LineRepository; +import subway.service.DataManager; +import subway.service.LineManager; + +public class MapController { + StringBuilder sb = new StringBuilder(); + static LineManager lineManager; + MapController(DataManager manager){ + lineManager = manager.getLineManager(); + } + public void work(){ + printMap(); + } + public void printMap(){ + System.out.println(lineManager.getStationLines()); + } +} diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 3436ce3b0..ddda8394e 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -51,7 +51,7 @@ public void register() { // 노선, 역 이름, 순서 입력 받고 등록 ask.orderWhere(); int index = getIndex(); - sectionManager.insertSection(station, index); + sectionManager.insertSection(station, index-1); response.printInfo("구간이 등록되었습니다."); } diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index 2dc93117b..c000ca61d 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -15,6 +15,13 @@ public Line(String name) { public String getName() { return name; } + public StringBuilder stationList(){ + StringBuilder sb = new StringBuilder(); + for(Station station:stations){ + sb.append("[INFO] "+station.getName()).append("\n"); + } + return sb; + } public void setStations(Station upper, Station bottom) { this.upperStation = upper; diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index fe144e895..8e0d12d03 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -1,11 +1,11 @@ package subway.service; import subway.domain.Line; -import subway.domain.LineRepository; import subway.domain.Station; -import static subway.service.DataManager.lineRepo; -import static subway.service.DataManager.stationManager; +import java.util.List; + +import static subway.service.DataManager.*; public class LineManager extends Managerable { @Override @@ -16,7 +16,6 @@ public boolean register(String name) { lineRepo.addLine(new Line(name)); return true; } -// Todo : 상행 종점, 하행 종점 입력 받기 public void setStations(String name, String upperStation, String bottomStation){ Line line = lineRepo.getLineByName(name); @@ -40,6 +39,18 @@ public StringBuilder read() { return sb; } + public StringBuilder getStationLines(){ + StringBuilder sb = new StringBuilder(); + List lineList = lineRepo.lines(); + for(Line line:lineList){ + sb.append("[INFO] "+line.getName()).append("\n"); + sb.append("[INFO] ---").append("\n"); + sb.append(line.stationList()).append("\n"); + } + return sb; + } + + // public List readLines(){ // return lineRepo.lines(); // } From 3d0f4ec4ae1bd4fccd636666a290639c5d62e617 Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 10 May 2024 21:13:14 +0900 Subject: [PATCH 041/104] =?UTF-8?q?doc:=20=EC=97=90=EB=9F=AC=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20'[ERROR]'=20=EB=B6=99=EC=9D=B4=EA=B3=A0=20=EC=95=88?= =?UTF-8?q?=EB=82=B4=ED=95=98=EB=8A=94=20=EB=B0=A9=EC=8B=9D=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/subway/config/handler/SubwayException.java | 10 +++++----- src/main/java/subway/controller/LineController.java | 6 +++--- src/main/java/subway/controller/MainController.java | 2 +- src/main/java/subway/controller/SectionController.java | 8 +++----- src/main/java/subway/controller/StationController.java | 6 +++--- 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/main/java/subway/config/handler/SubwayException.java b/src/main/java/subway/config/handler/SubwayException.java index 6ebe2b2d8..ebde20066 100644 --- a/src/main/java/subway/config/handler/SubwayException.java +++ b/src/main/java/subway/config/handler/SubwayException.java @@ -3,23 +3,23 @@ public class SubwayException extends IllegalArgumentException { public boolean isValidLength(String name) { if (name.length() >= 2) return true; - throw new IllegalStateException("## 2글자 이상으로 입력해주세요."); + throw new IllegalStateException("[ERROR] 2글자 이상으로 입력해주세요."); } public void notValidCommand() { - throw new IllegalArgumentException("## 잘못된 명령입니다."); + throw new IllegalArgumentException("[ERROR] 잘못된 명령입니다."); } public void checkCommand() { - throw new IllegalArgumentException("## 입력값을 확인해주세요."); + throw new IllegalArgumentException("[ERROR] 입력값을 확인해주세요."); } public void noStation() { - throw new IllegalArgumentException("## 존재하지 않는 역입니다."); + throw new IllegalArgumentException("[ERROR] 존재하지 않는 역입니다."); } public void noLine() { - throw new IllegalArgumentException("## 존재하지 않는 노선입니다."); + throw new IllegalArgumentException("[ERROR] 존재하지 않는 노선입니다."); } diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 318ac6fae..111641cfe 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -35,7 +35,7 @@ public void work(){ // 에러 발생시키기 subwayException.notValidCommand(); }catch (Exception e){ - e.printStackTrace(); + System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); } } @@ -56,7 +56,7 @@ public void register() { lineManager.setStations(line, upper, bottom); infoMessage("등록", result); }catch (Exception e){ - e.printStackTrace(); + System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); } } @@ -83,7 +83,7 @@ public void delete() { boolean result = lineManager.delete(command); infoMessage("삭제", result); }catch (Exception e){ - e.printStackTrace(); + System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); } } diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 969da61e7..d4eba6b41 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -37,7 +37,7 @@ public void headController() throws Exception { int validCommand = Integer.parseInt(command); CoreController(validCommand); }catch (Exception e){ - e.printStackTrace(); + System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); } // command 가 유효한 명령이 아닐 경우 코드 추가 headController(); diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index ddda8394e..1a2a1b48b 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -36,7 +36,7 @@ public void work() { // 에러 발생시키기 subwayException.notValidCommand(); } catch (Exception e) { - e.printStackTrace(); + System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); } } @@ -61,7 +61,7 @@ public String getLine() { try { line = br.readLine(); } catch (Exception e) { - e.printStackTrace(); + System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); } return line; } @@ -71,7 +71,6 @@ public String getStation() { return br.readLine(); } catch (Exception e) { System.out.println("역 이름을 받을 수 없습니다. 다시 시도해주세요."); - e.printStackTrace(); } return null; } @@ -82,7 +81,6 @@ public int getIndex() { return index; } catch (Exception e) { System.out.println("입력값을 확인해주세요."); - e.printStackTrace(); } return -1; } @@ -100,7 +98,7 @@ public void delete() { sectionManager.delete(station); response.printInfo("구간이 삭제되었습니다."); } catch (Exception e){ - e.printStackTrace(); + System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); } } } diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 84f6db986..9049dbeae 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -37,7 +37,7 @@ public void work(){ // 에러 발생시키 subwayException.notValidCommand(); }catch (Exception e){ - e.printStackTrace(); + System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); } } @@ -51,7 +51,7 @@ public void register(){ // "지하철 역이 등록되었습니다." 출력 infoMessage("등록", result); }catch (Exception e){ - e.printStackTrace(); + System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); } } public void infoMessage(String work, boolean result){ @@ -77,7 +77,7 @@ public void delete(){ boolean result = stationManager.delete(command); infoMessage("삭제", result); }catch (Exception e){ - e.printStackTrace(); + System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); } } From e3bbedc781ea27508eb2794abe2210da5241ca1b Mon Sep 17 00:00:00 2001 From: aristo Date: Sat, 11 May 2024 13:18:03 +0900 Subject: [PATCH 042/104] =?UTF-8?q?refactor:=20=EC=95=88=EC=93=B0=EB=8A=94?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/Application.java | 3 --- .../subway/config/constants/initValues/StationNames.java | 3 --- .../subway/config/handler/SubwayConstraintHandler.java | 4 ---- src/main/java/subway/controller/MapController.java | 1 - src/main/java/subway/controller/StationController.java | 5 ----- src/main/java/subway/service/LineManager.java | 5 ----- src/main/java/subway/service/Managerable.java | 4 ---- src/main/java/subway/service/SectionManager.java | 7 ------- 8 files changed, 32 deletions(-) delete mode 100644 src/main/java/subway/config/handler/SubwayConstraintHandler.java diff --git a/src/main/java/subway/Application.java b/src/main/java/subway/Application.java index e5efb3f57..bbf40c3bb 100644 --- a/src/main/java/subway/Application.java +++ b/src/main/java/subway/Application.java @@ -1,9 +1,6 @@ package subway; import subway.controller.MainController; - -import java.util.Scanner; - public class Application { public static void main(String[] args) throws Exception { // TODO: 프로그램 구현 diff --git a/src/main/java/subway/config/constants/initValues/StationNames.java b/src/main/java/subway/config/constants/initValues/StationNames.java index 7065b6385..80052ceff 100644 --- a/src/main/java/subway/config/constants/initValues/StationNames.java +++ b/src/main/java/subway/config/constants/initValues/StationNames.java @@ -2,9 +2,6 @@ public class StationNames { // // 해당 자료형을 컨트롤할 수 잇는 코드를 추출할까 -// List train2 = new LinkedList<>(); -// List train3 = new LinkedList<>(); -// List trainSuIn = new LinkedList<>(); private static final String[] stations = new String []{"교대역", "강남역", "역삼역", "남부터미널역", "양재역", "양재시민의숲역", "매봉역"}; public String[] getNames() { diff --git a/src/main/java/subway/config/handler/SubwayConstraintHandler.java b/src/main/java/subway/config/handler/SubwayConstraintHandler.java deleted file mode 100644 index b986bef7d..000000000 --- a/src/main/java/subway/config/handler/SubwayConstraintHandler.java +++ /dev/null @@ -1,4 +0,0 @@ -package subway.config.handler; - -public class SubwayConstraintHandler { -} diff --git a/src/main/java/subway/controller/MapController.java b/src/main/java/subway/controller/MapController.java index 53efb67b0..18e35cf9b 100644 --- a/src/main/java/subway/controller/MapController.java +++ b/src/main/java/subway/controller/MapController.java @@ -5,7 +5,6 @@ import subway.service.LineManager; public class MapController { - StringBuilder sb = new StringBuilder(); static LineManager lineManager; MapController(DataManager manager){ lineManager = manager.getLineManager(); diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 9049dbeae..35ad83748 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -86,9 +86,4 @@ public void read(){ StringBuilder list = stationManager.read(); response.printList(list); } - - public void requestStations() throws Exception{ - // 역 목록을 출력하는 서비스 코드 - System.out.println("requestStations 메서드 안이다."); - } } diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index 8e0d12d03..f540c1886 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -49,9 +49,4 @@ public StringBuilder getStationLines(){ } return sb; } - - -// public List readLines(){ -// return lineRepo.lines(); -// } } diff --git a/src/main/java/subway/service/Managerable.java b/src/main/java/subway/service/Managerable.java index 9c4682797..1bf0f8e87 100644 --- a/src/main/java/subway/service/Managerable.java +++ b/src/main/java/subway/service/Managerable.java @@ -4,8 +4,4 @@ public abstract class Managerable { public abstract boolean register(String command); public abstract boolean delete(String name); public abstract StringBuilder read(); - - public void goBack() { - return; - } } diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 9adceb7ae..a50c08382 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -2,9 +2,7 @@ import subway.config.handler.SubwayException; import subway.domain.Line; -import subway.domain.LineRepository; import subway.domain.Station; -import subway.domain.StationRepository; import static subway.service.DataManager.lineRepo; import static subway.service.DataManager.stationRepo; @@ -38,9 +36,4 @@ public StringBuilder read() { sb.append(""); return sb; } - - @Override - public void goBack() { - - } } From 9bd7864a9dca954767d8676c7c076498e5dece3b Mon Sep 17 00:00:00 2001 From: aristo Date: Sat, 11 May 2024 14:24:24 +0900 Subject: [PATCH 043/104] =?UTF-8?q?feat:=20=EB=A9=94=EC=8B=9C=EC=A7=80?= =?UTF-8?q?=EB=A5=BC=20=EC=9C=84=ED=95=9C=20enum=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/config/constants/views/Errors.java | 14 ++++++++++++++ .../subway/config/constants/views/Methods.java | 18 ++++++++++++++++++ .../config/constants/views/Prefixes.java | 11 +++++++++++ .../config/constants/views/Questions.java | 12 ++++++++++++ .../subway/config/constants/views/Targets.java | 15 +++++++++++++++ .../subway/config/constants/views/Titles.java | 17 +++++++++++++++++ 6 files changed, 87 insertions(+) create mode 100644 src/main/java/subway/config/constants/views/Errors.java create mode 100644 src/main/java/subway/config/constants/views/Methods.java create mode 100644 src/main/java/subway/config/constants/views/Prefixes.java create mode 100644 src/main/java/subway/config/constants/views/Questions.java create mode 100644 src/main/java/subway/config/constants/views/Targets.java create mode 100644 src/main/java/subway/config/constants/views/Titles.java diff --git a/src/main/java/subway/config/constants/views/Errors.java b/src/main/java/subway/config/constants/views/Errors.java new file mode 100644 index 000000000..295245a3c --- /dev/null +++ b/src/main/java/subway/config/constants/views/Errors.java @@ -0,0 +1,14 @@ +package subway.config.constants.views; + +public enum Errors { + VALID("선택할 수 없는 기능입니다."), + ADD_STATION("이미 등록된 역 이름입니다."), + ADD_LINE("이미 등록된 노선 이름입니다."), + DEL_STATION("존재하지 않은 역 이름입니다."), + DEL_LINE("존재하지 않는 노선 이름입니다."); + + private final String message; + Errors(String message){ + this.message = message; + } +} diff --git a/src/main/java/subway/config/constants/views/Methods.java b/src/main/java/subway/config/constants/views/Methods.java new file mode 100644 index 000000000..712551122 --- /dev/null +++ b/src/main/java/subway/config/constants/views/Methods.java @@ -0,0 +1,18 @@ +package subway.config.constants.views; + +public enum Methods { + REGISTER("등록"), + DELETE("삭제"), + READ("조회"), + PRINT("출력"), + END("종료"), + BACK("돌아가기"); + private final String command; + Methods(String command){ + this.command = command; + } + + public String getCommand(){ + return command; + } +} diff --git a/src/main/java/subway/config/constants/views/Prefixes.java b/src/main/java/subway/config/constants/views/Prefixes.java new file mode 100644 index 000000000..4ee601849 --- /dev/null +++ b/src/main/java/subway/config/constants/views/Prefixes.java @@ -0,0 +1,11 @@ +package subway.config.constants.views; + +public enum Prefixes { + SHARP("## "), + INFO("[INFO] "), + ERROR("[ERROR] "); + private final String prefix; + Prefixes(String prefix){ + this.prefix=prefix; + } +} diff --git a/src/main/java/subway/config/constants/views/Questions.java b/src/main/java/subway/config/constants/views/Questions.java new file mode 100644 index 000000000..0275a3277 --- /dev/null +++ b/src/main/java/subway/config/constants/views/Questions.java @@ -0,0 +1,12 @@ +package subway.config.constants.views; + +public enum Questions { + FUNC("원하는 기능을 선택하세요."), + NAME("이름을 입력하세요."), + SEQUENCE("순서를 입력하세요."); + + private final String questions; + Questions(String questions){ + this.questions = questions; + } +} diff --git a/src/main/java/subway/config/constants/views/Targets.java b/src/main/java/subway/config/constants/views/Targets.java new file mode 100644 index 000000000..68e8b3315 --- /dev/null +++ b/src/main/java/subway/config/constants/views/Targets.java @@ -0,0 +1,15 @@ +package subway.config.constants.views; + +public enum Targets { + LINE("노선"), + STATION("역"), + SECTION("구간"), + MAP("지하철 노선도"), + UPPER("상행 종점역"), + BOTTOM("하행 종점역"); + + private final String target; + Targets(String target){ + this.target = target; + } +} diff --git a/src/main/java/subway/config/constants/views/Titles.java b/src/main/java/subway/config/constants/views/Titles.java new file mode 100644 index 000000000..dd7fc2d0f --- /dev/null +++ b/src/main/java/subway/config/constants/views/Titles.java @@ -0,0 +1,17 @@ +package subway.config.constants.views; + +public enum Titles { + MAIN("메인 화면"), + STATION("역 관리 화면"), + STATIONS("역 목록"), + LINE("노선 관리 화면"), + LINES("노선 목록"), + SECTION("구간 관리 화면"), + SECTIONS("구간 목록"), + MAP("지하철 노선도"); + + private String title; + Titles(String title){ + this.title=title; + } +} From a5d6170ad6c1f16087023538d105cb65e2d51d80 Mon Sep 17 00:00:00 2001 From: aristo Date: Sat, 11 May 2024 15:16:05 +0900 Subject: [PATCH 044/104] =?UTF-8?q?refactor=20:=20enum=20=EC=9D=84=20?= =?UTF-8?q?=EC=9D=B4=EC=9A=A9=ED=95=98=EA=B8=B0=EB=8A=94=20=ED=96=87?= =?UTF-8?q?=EB=8A=94=EB=8D=B0=20=EC=BD=94=EB=93=9C=EA=B0=80=20=EB=8D=94=20?= =?UTF-8?q?=EB=B3=B5=EC=9E=A1=ED=95=B4=EC=A7=84=20=EA=B8=B0=EB=B6=84...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/constants/views/Methods.java | 12 ++-- .../config/constants/views/Prefixes.java | 3 + .../config/constants/views/Questions.java | 18 ++++-- .../config/constants/views/Targets.java | 4 ++ .../subway/config/constants/views/Titles.java | 7 ++- .../config/handler/SubwayException.java | 12 ++-- .../subway/controller/LineController.java | 11 ++-- src/main/java/subway/view/AskView.java | 59 ++++++++++++------- 8 files changed, 83 insertions(+), 43 deletions(-) diff --git a/src/main/java/subway/config/constants/views/Methods.java b/src/main/java/subway/config/constants/views/Methods.java index 712551122..9fa3c28ee 100644 --- a/src/main/java/subway/config/constants/views/Methods.java +++ b/src/main/java/subway/config/constants/views/Methods.java @@ -1,12 +1,12 @@ package subway.config.constants.views; public enum Methods { - REGISTER("등록"), - DELETE("삭제"), - READ("조회"), - PRINT("출력"), - END("종료"), - BACK("돌아가기"); + 등록("1"), + 삭제("2"), + 조회("3"), + 출력("4"), + 종료("Q"), + 돌아가기("B"); private final String command; Methods(String command){ this.command = command; diff --git a/src/main/java/subway/config/constants/views/Prefixes.java b/src/main/java/subway/config/constants/views/Prefixes.java index 4ee601849..a47710333 100644 --- a/src/main/java/subway/config/constants/views/Prefixes.java +++ b/src/main/java/subway/config/constants/views/Prefixes.java @@ -8,4 +8,7 @@ public enum Prefixes { Prefixes(String prefix){ this.prefix=prefix; } + public String getPrefix(){ + return this.prefix; + } } diff --git a/src/main/java/subway/config/constants/views/Questions.java b/src/main/java/subway/config/constants/views/Questions.java index 0275a3277..dd24081aa 100644 --- a/src/main/java/subway/config/constants/views/Questions.java +++ b/src/main/java/subway/config/constants/views/Questions.java @@ -3,10 +3,20 @@ public enum Questions { FUNC("원하는 기능을 선택하세요."), NAME("이름을 입력하세요."), - SEQUENCE("순서를 입력하세요."); + SEQUENCE("## 순서를 입력하세요.\n"); - private final String questions; - Questions(String questions){ - this.questions = questions; + private final String question; + + Questions(String question) { + this.question = question; + } + + public String Message(String m, String t) { + String message = m + "할 " + t + this.question + "\n"; + return message; + } + + public String Message() { + return this.question; } } diff --git a/src/main/java/subway/config/constants/views/Targets.java b/src/main/java/subway/config/constants/views/Targets.java index 68e8b3315..f488e9d5d 100644 --- a/src/main/java/subway/config/constants/views/Targets.java +++ b/src/main/java/subway/config/constants/views/Targets.java @@ -12,4 +12,8 @@ public enum Targets { Targets(String target){ this.target = target; } + + public String getTarget(){ + return this.target; + } } diff --git a/src/main/java/subway/config/constants/views/Titles.java b/src/main/java/subway/config/constants/views/Titles.java index dd7fc2d0f..09e2900da 100644 --- a/src/main/java/subway/config/constants/views/Titles.java +++ b/src/main/java/subway/config/constants/views/Titles.java @@ -3,10 +3,10 @@ public enum Titles { MAIN("메인 화면"), STATION("역 관리 화면"), - STATIONS("역 목록"), LINE("노선 관리 화면"), - LINES("노선 목록"), SECTION("구간 관리 화면"), + STATIONS("역 목록"), + LINES("노선 목록"), SECTIONS("구간 목록"), MAP("지하철 노선도"); @@ -14,4 +14,7 @@ public enum Titles { Titles(String title){ this.title=title; } + public String getTitle(){ + return this.title +"\n"; + } } diff --git a/src/main/java/subway/config/handler/SubwayException.java b/src/main/java/subway/config/handler/SubwayException.java index ebde20066..47e26b366 100644 --- a/src/main/java/subway/config/handler/SubwayException.java +++ b/src/main/java/subway/config/handler/SubwayException.java @@ -1,25 +1,27 @@ package subway.config.handler; +import subway.config.constants.views.Prefixes; + public class SubwayException extends IllegalArgumentException { public boolean isValidLength(String name) { if (name.length() >= 2) return true; - throw new IllegalStateException("[ERROR] 2글자 이상으로 입력해주세요."); + throw new IllegalStateException(Prefixes.ERROR+"2글자 이상으로 입력해주세요."); } public void notValidCommand() { - throw new IllegalArgumentException("[ERROR] 잘못된 명령입니다."); + throw new IllegalArgumentException(Prefixes.ERROR+"잘못된 명령입니다."); } public void checkCommand() { - throw new IllegalArgumentException("[ERROR] 입력값을 확인해주세요."); + throw new IllegalArgumentException(Prefixes.ERROR+ "입력값을 확인해주세요."); } public void noStation() { - throw new IllegalArgumentException("[ERROR] 존재하지 않는 역입니다."); + throw new IllegalArgumentException(Prefixes.ERROR+"존재하지 않는 역입니다."); } public void noLine() { - throw new IllegalArgumentException("[ERROR] 존재하지 않는 노선입니다."); + throw new IllegalArgumentException(Prefixes.ERROR+"존재하지 않는 노선입니다."); } diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 111641cfe..6ce02e105 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -1,5 +1,7 @@ package subway.controller; +import subway.config.constants.views.Methods; +import subway.config.constants.views.Targets; import subway.config.handler.SubwayException; import subway.service.DataManager; import subway.service.LineManager; @@ -41,20 +43,21 @@ public void work(){ @Override public void register() { + String command = Methods.등록.toString(); // 새로운 노선 db 와 연동하여 생성하기 - ask.Name("등록", "노선"); + ask.Name(command, "노선"); try{ String line = br.readLine(); subwayException.isValidLength(line); boolean result = lineManager.register(line); - ask.Name("등록", "상행 종점역"); + ask.Name(command, Targets.UPPER.getTarget()); String upper = br.readLine(); - ask.Name("등록", "하행 종점역"); + ask.Name(command, "하행 종점역"); String bottom = br.readLine(); lineManager.setStations(line, upper, bottom); - infoMessage("등록", result); + infoMessage(command, result); }catch (Exception e){ System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); } diff --git a/src/main/java/subway/view/AskView.java b/src/main/java/subway/view/AskView.java index e502ad71c..a840f682f 100644 --- a/src/main/java/subway/view/AskView.java +++ b/src/main/java/subway/view/AskView.java @@ -1,30 +1,44 @@ package subway.view; +import subway.config.constants.views.*; + public class AskView { private static StringBuilder sb = new StringBuilder(); + static final String SHARP = Prefixes.SHARP.getPrefix(); + static final Methods REGISTER = Methods.등록; + static final Methods DELETE = Methods.삭제; + static final Methods READ = Methods.조회; + static final Methods PRINT = Methods.출력; + static final Methods END = Methods.종료; + static final Methods BACK = Methods.돌아가기; + public void printMain() { + sb.append(SHARP).append(Titles.MAIN.getTitle()); - public void printMain(){ - sb.append("## 메인 화면\n"); - sb.append("1. 역 관리\n"); - sb.append("2. 노선 관리\n"); - sb.append("3. 구간 관리\n"); - sb.append("4. 지하철 노선도 출력\n"); - sb.append("Q. 종료\n"); + sb.append(SHARP).append(Titles.MAIN.getTitle()); + sb.append("1. ").append(Titles.STATION.getTitle()); + sb.append("2. ").append(Titles.LINE.getTitle()); + sb.append("3. ").append(Titles.SECTION.getTitle()); + sb.append("4. ").append(Titles.MAP.getTitle()); + sb.append(END.getCommand()+". ").append(END+"\n"); System.out.println(sb.toString()); sb.setLength(0); } - public void WhatToManage(String target){ - sb.append("## " + target + " 관리 화면\n"); - sb.append("1. "+target + " 등록\n"); - sb.append("2. "+target + " 삭제\n"); - sb.append("3. "+target + " 조회\n"); - sb.append("B. 돌아가기\n"); + public void WhatToManage(String target) { + sb.append(SHARP + target + " 관리 화면\n"); + sb.append("1. " + target + " 등록\n"); + sb.append("2. " + target + " 삭제\n"); + sb.append("3. " + target + " 조회\n"); +// sb.append(REGISTER.getCommand()+". ").append(target+" ").append(REGISTER+"\n"); +// sb.append(DELETE.getCommand()+". ").append(target+" ").append(DELETE+"\n"); +// sb.append(READ.getCommand()+". ").append(target+" ").append(READ+"\n"); +// sb.append(BACK.getCommand()+". ").append(target+" ").append(BACK+"\n"); System.out.println(sb.toString()); sb.setLength(0); } - public void sectionManage(){ + + public void sectionManage() { sb.append("## 구간 관리 화면\n"); sb.append("1. 구간 등록\n"); sb.append("2. 구간 삭제\n"); @@ -33,25 +47,26 @@ public void sectionManage(){ sb.setLength(0); } - public void Function(){ - sb.append("## 원하는 기능을 선택하세요\n"); + public void Function() { + sb.append(SHARP).append(Questions.FUNC.Message()); System.out.println(sb.toString()); sb.setLength(0); } - public void Name(String work, String name){ // 등록 or 삭제 - sb.append("## "+work+"할 " + name + " 이름을 입력하세요\n"); + public void Name(String work, String name) { // 등록 or 삭제 + sb.append(SHARP).append(Questions.NAME.Message(work, name)); System.out.println(sb.toString()); sb.setLength(0); } - public void orderWhat(String name){ // 등록 or 삭제 - sb.append("## "+ name + "을 입력하세요\n"); + public void orderWhat(String name) { // 등록 or 삭제 + sb.append(SHARP).append(name + "을 입력하세요\n"); System.out.println(sb.toString()); sb.setLength(0); } - public void orderWhere(){ // 등록 or 삭제 - System.out.println("## 순서를 입력하세요\n"); + + public void orderWhere() { // 등록 or 삭제 + System.out.println(Questions.SEQUENCE.Message()); } } From 45ef8c7080405a797ab93c8e4c6398e898770acc Mon Sep 17 00:00:00 2001 From: aristo Date: Sat, 11 May 2024 15:38:04 +0900 Subject: [PATCH 045/104] =?UTF-8?q?doc:=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81=20=ED=95=A0=20=EB=B6=80=EB=B6=84=20=EA=B3=A0=EB=AF=BC?= =?UTF-8?q?=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/StationController.java | 22 +++++++++++-------- src/main/java/subway/service/DataManager.java | 2 +- src/main/java/subway/service/LineManager.java | 10 +++++---- .../java/subway/service/SectionManager.java | 1 - src/main/java/subway/view/AskView.java | 2 +- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 35ad83748..cd7e2e27b 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -1,5 +1,8 @@ package subway.controller; +import subway.config.constants.views.Methods; +import subway.config.constants.views.Prefixes; +import subway.config.constants.views.Targets; import subway.config.handler.SubwayException; import subway.service.DataManager; import subway.service.StationManager; @@ -8,26 +11,27 @@ public class StationController extends ManageController{ static StationManager stationManager; static SubwayException subwayException; + String prefix = Prefixes.ERROR.getPrefix(); // 모든 변수의 인스턴스가 저장하는 클래스를 만들어? StationController(DataManager manager){ stationManager = manager.getStationManager(); subwayException = manager.getSubwayException(); } @Override - public void work(){ - ask.WhatToManage("역"); + public void work(){ // 이 부분을 모듈로 빼버릴까. work 의 매개변수에 따라서 역, 노선, 구간 나눠서 서비스 제공하기? + ask.WhatToManage(Targets.STATION.getTarget()); ask.Function(); try{ String command = br.readLine(); - if(command.equals("1")){ + if(command.equals(Methods.등록.getCommand())){ register(); return; } - if(command.equals("2")){ + if(command.equals(Methods.삭제.getCommand())){ delete(); return; } - if(command.equals("3")){ + if(command.equals(Methods.조회.getCommand())){ read(); return; } @@ -37,7 +41,7 @@ public void work(){ // 에러 발생시키 subwayException.notValidCommand(); }catch (Exception e){ - System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); + System.out.println(prefix+"예상치 못한 에러가 발생했습니다."); } } @@ -51,12 +55,12 @@ public void register(){ // "지하철 역이 등록되었습니다." 출력 infoMessage("등록", result); }catch (Exception e){ - System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); + System.out.println(prefix+"예상치 못한 에러가 발생했습니다."); } } public void infoMessage(String work, boolean result){ StringBuilder sb = new StringBuilder(); - sb.append("지하철 역이 "); + sb.append("지하철 역이 "); // 쓸데없이 긴 것 같은데 string 다루는 모듈을 따로 만들까. controller 기능이 잘 안보이는 것 같음 if(result == true){ sb.append(work).append("되었습니다."); } @@ -77,7 +81,7 @@ public void delete(){ boolean result = stationManager.delete(command); infoMessage("삭제", result); }catch (Exception e){ - System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); + System.out.println(prefix + "예상치 못한 에러가 발생했습니다."); } } diff --git a/src/main/java/subway/service/DataManager.java b/src/main/java/subway/service/DataManager.java index e574446b9..bd2f05ad5 100644 --- a/src/main/java/subway/service/DataManager.java +++ b/src/main/java/subway/service/DataManager.java @@ -35,7 +35,7 @@ public static SectionManager getSectionManager(){ public DataManager(){ // 초기값 삽입 - addStations();// 1. 모든 역들을 입력한다. + addStations(); addLines(); } diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index f540c1886..695077dd4 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -1,5 +1,6 @@ package subway.service; +import subway.config.constants.views.Prefixes; import subway.domain.Line; import subway.domain.Station; @@ -8,6 +9,7 @@ import static subway.service.DataManager.*; public class LineManager extends Managerable { + Prefixes INFO = Prefixes.INFO; @Override public boolean register(String name) { if(lineRepo.getLineByName(name) != null){ @@ -34,17 +36,17 @@ public boolean delete(String name) { public StringBuilder read() { StringBuilder sb = new StringBuilder(); for(Line line: lineRepo.lines()){ - sb.append("[INFO] " + line.getName()+"\n"); + sb.append(INFO + line.getName()+"\n"); } return sb; } public StringBuilder getStationLines(){ - StringBuilder sb = new StringBuilder(); + StringBuilder sb = new StringBuilder(); // 반복되는데 String으로바꿀까. 반환값 바꾸려면 이것저것 많이 건들어야 할 듯 List lineList = lineRepo.lines(); for(Line line:lineList){ - sb.append("[INFO] "+line.getName()).append("\n"); - sb.append("[INFO] ---").append("\n"); + sb.append(INFO+line.getName()).append("\n"); + sb.append(INFO+"---").append("\n"); sb.append(line.stationList()).append("\n"); } return sb; diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index a50c08382..adcafa302 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -33,7 +33,6 @@ public boolean delete(String name) { @Override public StringBuilder read() { StringBuilder sb = new StringBuilder(); - sb.append(""); return sb; } } diff --git a/src/main/java/subway/view/AskView.java b/src/main/java/subway/view/AskView.java index a840f682f..3d4316b30 100644 --- a/src/main/java/subway/view/AskView.java +++ b/src/main/java/subway/view/AskView.java @@ -2,7 +2,7 @@ import subway.config.constants.views.*; -public class AskView { +public class AskView { // view 가 너무 중구난방인 거 같은데 controller, service 처럼 분할할까. private static StringBuilder sb = new StringBuilder(); static final String SHARP = Prefixes.SHARP.getPrefix(); static final Methods REGISTER = Methods.등록; From d75fe5bffd353254b496621d381c90a98b661d28 Mon Sep 17 00:00:00 2001 From: aristo Date: Sun, 12 May 2024 22:52:02 +0900 Subject: [PATCH 046/104] =?UTF-8?q?fix:=20=EB=A9=94=EC=9D=B8=20=ED=83=80?= =?UTF-8?q?=EC=9D=B4=ED=8B=80=20=EC=A4=91=EB=B3=B5=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/view/AskView.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/subway/view/AskView.java b/src/main/java/subway/view/AskView.java index 3d4316b30..960cad0a8 100644 --- a/src/main/java/subway/view/AskView.java +++ b/src/main/java/subway/view/AskView.java @@ -12,8 +12,6 @@ public class AskView { // view 가 너무 중구난방인 거 같은데 controll static final Methods END = Methods.종료; static final Methods BACK = Methods.돌아가기; public void printMain() { - sb.append(SHARP).append(Titles.MAIN.getTitle()); - sb.append(SHARP).append(Titles.MAIN.getTitle()); sb.append("1. ").append(Titles.STATION.getTitle()); sb.append("2. ").append(Titles.LINE.getTitle()); From 4f5953fc09568d05a705e6d8d7cb42d28f6836f5 Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 13 May 2024 10:47:38 +0900 Subject: [PATCH 047/104] =?UTF-8?q?refactor=20:=20parameter=EB=A5=BC=20fin?= =?UTF-8?q?al=20=EB=B3=80=EC=88=98=EB=A1=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/subway/config/constants/views/Titles.java | 1 + src/main/java/subway/controller/LineController.java | 4 ++-- src/main/java/subway/controller/MainController.java | 4 ++-- .../java/subway/controller/SectionController.java | 5 +++-- .../java/subway/controller/StationController.java | 4 ++-- src/main/java/subway/domain/Line.java | 10 +++++----- src/main/java/subway/domain/LineRepository.java | 13 +++++++------ src/main/java/subway/domain/Station.java | 2 +- src/main/java/subway/domain/StationRepository.java | 6 +++--- src/main/java/subway/service/DataManager.java | 2 +- src/main/java/subway/service/LineManager.java | 6 +++--- src/main/java/subway/service/Managerable.java | 4 ++-- src/main/java/subway/service/SectionManager.java | 6 +++--- src/main/java/subway/service/StationManager.java | 8 ++++---- src/main/java/subway/view/AskView.java | 6 +++--- src/main/java/subway/view/ResponseView.java | 6 +++--- 16 files changed, 45 insertions(+), 42 deletions(-) diff --git a/src/main/java/subway/config/constants/views/Titles.java b/src/main/java/subway/config/constants/views/Titles.java index 09e2900da..7c0e6dd72 100644 --- a/src/main/java/subway/config/constants/views/Titles.java +++ b/src/main/java/subway/config/constants/views/Titles.java @@ -1,4 +1,5 @@ package subway.config.constants.views; +// 상수도 화면 별로 3개 분할할까? public enum Titles { MAIN("메인 화면"), diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 6ce02e105..ac23985be 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -9,7 +9,7 @@ public class LineController extends ManageController{ static LineManager lineManager; private static SubwayException subwayException; - public LineController(DataManager manager){ + public LineController(final DataManager manager){ lineManager = manager.getLineManager(); subwayException = manager.getSubwayException(); } @@ -63,7 +63,7 @@ public void register() { } } - public void infoMessage(String work, boolean result){ + public void infoMessage(final String work, final boolean result){ StringBuilder sb = new StringBuilder(); sb.append("노선이 "); if(result == true){ diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index d4eba6b41..77effc658 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -43,8 +43,8 @@ public void headController() throws Exception { headController(); } - // 생성자와 함께 출력과 서비스를 제공하려 했는데 무슨 일 할 때마다 클래스 생성하는 것은 비효율적인 것 같다. 메소드로 바꾸자. - public void CoreController(int command) { + // 생성자와 함께 출력과 서비스를 제공하려 했는데 무슨 일 할 때마다 클래스 생성하는 것은 비효율적인 것 같다. 메소드로 바꾸자. 아니 이게 맞나? + public void CoreController(final int command) { if (command == 1) { stationController.work(); return; diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 1a2a1b48b..5a84848cf 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -1,5 +1,6 @@ package subway.controller; +import subway.config.constants.views.Targets; import subway.config.handler.SubwayException; import subway.service.DataManager; import subway.service.SectionManager; @@ -11,7 +12,7 @@ public class SectionController extends ManageController { static SubwayException subwayException; - public SectionController(DataManager manager){ + public SectionController(final DataManager manager){ sectionManager = manager.getSectionManager(); subwayException = manager.getSubwayException(); } @@ -56,7 +57,7 @@ public void register() { // 노선, 역 이름, 순서 입력 받고 등록 } public String getLine() { - ask.orderWhat("노선"); + ask.orderWhat(Targets.LINE.getTarget()); String line = ""; try { line = br.readLine(); diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index cd7e2e27b..4c98be8bf 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -13,7 +13,7 @@ public class StationController extends ManageController{ static SubwayException subwayException; String prefix = Prefixes.ERROR.getPrefix(); // 모든 변수의 인스턴스가 저장하는 클래스를 만들어? - StationController(DataManager manager){ + StationController(final DataManager manager){ stationManager = manager.getStationManager(); subwayException = manager.getSubwayException(); } @@ -58,7 +58,7 @@ public void register(){ System.out.println(prefix+"예상치 못한 에러가 발생했습니다."); } } - public void infoMessage(String work, boolean result){ + public void infoMessage(final String work, final boolean result){ StringBuilder sb = new StringBuilder(); sb.append("지하철 역이 "); // 쓸데없이 긴 것 같은데 string 다루는 모듈을 따로 만들까. controller 기능이 잘 안보이는 것 같음 if(result == true){ diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index c000ca61d..cebb9807e 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -8,7 +8,7 @@ public class Line { private Station upperStation; private Station bottomStation; - public Line(String name) { + public Line(final String name) { this.name = name; } @@ -23,18 +23,18 @@ public StringBuilder stationList(){ return sb; } - public void setStations(Station upper, Station bottom) { + public void setStations(final Station upper, final Station bottom) { this.upperStation = upper; this.bottomStation = bottom; stations.addFirst(upper); stations.addLast(bottom); } - public void addStation(Station station, int index) { + public void addStation(final Station station, final int index) { stations.add(index, station); } - public boolean deleteStation(String station) { + public boolean deleteStation(final String station) { int index = 0; for (index = 0; index < stations.size(); index++) { Station s = stations.get(index); @@ -47,7 +47,7 @@ public boolean deleteStation(String station) { } // 추가 기능 구현 - public Station getStationByName(String name) { + public Station getStationByName(final String name) { for (Station station : stations) { if (station.getName().equals(name)) { return station; diff --git a/src/main/java/subway/domain/LineRepository.java b/src/main/java/subway/domain/LineRepository.java index af54d6212..7785c0596 100644 --- a/src/main/java/subway/domain/LineRepository.java +++ b/src/main/java/subway/domain/LineRepository.java @@ -12,20 +12,21 @@ public static List lines() { return Collections.unmodifiableList(lines); } - public static void addLine(Line line) { + public static void addLine(final Line line) { lines.add(line); } - public static void addLine(Line line, Station station, int index) { + + public static void addLine(final Line line, final Station station, final int index) { line.addStation(station, index); } - public static boolean deleteLineByName(String name) { + public static boolean deleteLineByName(final String name) { return lines.removeIf(line -> Objects.equals(line.getName(), name)); } - public static Line getLineByName(String name){ - for(Line line : lines){ - if(line.getName().equals(name)){ + public static Line getLineByName(final String name) { + for (Line line : lines) { + if (line.getName().equals(name)) { return line; } } diff --git a/src/main/java/subway/domain/Station.java b/src/main/java/subway/domain/Station.java index 1bb013a36..b5da343f2 100644 --- a/src/main/java/subway/domain/Station.java +++ b/src/main/java/subway/domain/Station.java @@ -6,7 +6,7 @@ public class Station { private String name; private List[] line; - public Station(String name) { + public Station(final String name) { this.name = name; } diff --git a/src/main/java/subway/domain/StationRepository.java b/src/main/java/subway/domain/StationRepository.java index e59065dcb..576f56531 100644 --- a/src/main/java/subway/domain/StationRepository.java +++ b/src/main/java/subway/domain/StationRepository.java @@ -12,14 +12,14 @@ public static List stations() { return Collections.unmodifiableList(stations); } - public static void addStation(Station station) { + public static void addStation(final Station station) { stations.add(station); } - public static boolean deleteStation(String name) { + public static boolean deleteStation(final String name) { return stations.removeIf(station -> Objects.equals(station.getName(), name)); } - public static Station getStationByName(String name){ + public static Station getStationByName(final String name){ for(Station station : stations){ if(station.getName().equals(name)){ return station; diff --git a/src/main/java/subway/service/DataManager.java b/src/main/java/subway/service/DataManager.java index bd2f05ad5..e08bb3137 100644 --- a/src/main/java/subway/service/DataManager.java +++ b/src/main/java/subway/service/DataManager.java @@ -55,7 +55,7 @@ public void addLines(){ } } - public void lineSet(Line line){ + public void lineSet(final Line line){ if(line.getName().equals("2호선")){ line.setStations(stationRepo.getStationByName("교대역"), stationRepo.getStationByName("역삼역")); line.addStation(stationRepo.getStationByName("강남역"), 1); diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index 695077dd4..f0b6b126a 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -11,7 +11,7 @@ public class LineManager extends Managerable { Prefixes INFO = Prefixes.INFO; @Override - public boolean register(String name) { + public boolean register(final String name) { if(lineRepo.getLineByName(name) != null){ return false; } @@ -19,7 +19,7 @@ public boolean register(String name) { return true; } - public void setStations(String name, String upperStation, String bottomStation){ + public void setStations(final String name, final String upperStation, final String bottomStation){ Line line = lineRepo.getLineByName(name); Station upper = stationManager.getByName(upperStation); Station bottom = stationManager.getByName(bottomStation); @@ -28,7 +28,7 @@ public void setStations(String name, String upperStation, String bottomStation){ } @Override - public boolean delete(String name) { + public boolean delete(final String name) { return lineRepo.deleteLineByName(name); } diff --git a/src/main/java/subway/service/Managerable.java b/src/main/java/subway/service/Managerable.java index 1bf0f8e87..f5cd62e82 100644 --- a/src/main/java/subway/service/Managerable.java +++ b/src/main/java/subway/service/Managerable.java @@ -1,7 +1,7 @@ package subway.service; public abstract class Managerable { - public abstract boolean register(String command); - public abstract boolean delete(String name); + public abstract boolean register(final String command); + public abstract boolean delete(final String name); public abstract StringBuilder read(); } diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index adcafa302..41f56069d 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -11,20 +11,20 @@ public class SectionManager extends Managerable { private Line line; private SubwayException subwayException; @Override - public boolean register(String name) { + public boolean register(final String name) { line = lineRepo.getLineByName(name); if(line == null) subwayException.noLine(); return true; } - public void insertSection(String sName,int index){ + public void insertSection(final String sName,final int index){ Station station = stationRepo.getStationByName(sName); if(station == null) subwayException.noStation(); lineRepo.addLine(line, station, index); } @Override - public boolean delete(String name) { + public boolean delete(final String name) { if(line.deleteStation(name)) return true; subwayException.checkCommand(); return false; diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index b5c19c8c6..9f62e3ad3 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -6,7 +6,7 @@ // 역 관련 기능하는 서비스 public class StationManager extends Managerable { @Override // 삽입 - public boolean register(String name) { + public boolean register(final String name) { // 해당 역이 존재하는 지 확인 // 삽입 if(stationRepo.getStationByName(name)!=null){ @@ -17,7 +17,7 @@ public boolean register(String name) { return true; } - public boolean isExist(String name){ + public boolean isExist(final String name){ if(stationRepo.getStationByName(name)!=null){ // 이미 존재하는 역이라고 전달하기 return true; // 문자열 반환해서 상태 메시지 출력에 활용 @@ -26,7 +26,7 @@ public boolean isExist(String name){ } @Override - public boolean delete(String name) { + public boolean delete(final String name) { return stationRepo.deleteStation(name); } @@ -40,7 +40,7 @@ public StringBuilder read() { return sb; } - public Station getByName(String name){ + public Station getByName(final String name){ register(name); return stationRepo.getStationByName(name); } diff --git a/src/main/java/subway/view/AskView.java b/src/main/java/subway/view/AskView.java index 960cad0a8..056314836 100644 --- a/src/main/java/subway/view/AskView.java +++ b/src/main/java/subway/view/AskView.java @@ -23,7 +23,7 @@ public void printMain() { sb.setLength(0); } - public void WhatToManage(String target) { + public void WhatToManage(final String target) { sb.append(SHARP + target + " 관리 화면\n"); sb.append("1. " + target + " 등록\n"); sb.append("2. " + target + " 삭제\n"); @@ -51,13 +51,13 @@ public void Function() { sb.setLength(0); } - public void Name(String work, String name) { // 등록 or 삭제 + public void Name(final String work, final String name) { // 등록 or 삭제 sb.append(SHARP).append(Questions.NAME.Message(work, name)); System.out.println(sb.toString()); sb.setLength(0); } - public void orderWhat(String name) { // 등록 or 삭제 + public void orderWhat(final String name) { // 등록 or 삭제 sb.append(SHARP).append(name + "을 입력하세요\n"); System.out.println(sb.toString()); sb.setLength(0); diff --git a/src/main/java/subway/view/ResponseView.java b/src/main/java/subway/view/ResponseView.java index 9101468b1..5ce9cd02b 100644 --- a/src/main/java/subway/view/ResponseView.java +++ b/src/main/java/subway/view/ResponseView.java @@ -2,18 +2,18 @@ public class ResponseView { static StringBuilder sb = new StringBuilder(); - public void printInfo(String message){ + public void printInfo(final String message){ sb.append("[INFO] ").append(message); System.out.println(sb); sb.setLength(0); } - public void printTitle(String title){ + public void printTitle(final String title){ sb.append("### ").append(title); System.out.println(sb); sb.setLength(0); } - public void printList(StringBuilder message){ + public void printList(final StringBuilder message){ System.out.println(message); } } From 0b4bb15ff4f349a41a30d7ce5279eed6e1c67e73 Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 13 May 2024 13:41:16 +0900 Subject: [PATCH 048/104] =?UTF-8?q?refactor:=20AskView=20=EC=A0=95?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/constants/views/Indexes.java | 44 ++++++++++++ .../config/constants/views/Questions.java | 26 ++++--- .../subway/controller/SectionController.java | 2 +- src/main/java/subway/view/AskView.java | 68 ++++++------------- .../java/subway/view/util/MakeString.java | 58 ++++++++++++++++ 5 files changed, 140 insertions(+), 58 deletions(-) create mode 100644 src/main/java/subway/config/constants/views/Indexes.java create mode 100644 src/main/java/subway/view/util/MakeString.java diff --git a/src/main/java/subway/config/constants/views/Indexes.java b/src/main/java/subway/config/constants/views/Indexes.java new file mode 100644 index 000000000..a9197ff7d --- /dev/null +++ b/src/main/java/subway/config/constants/views/Indexes.java @@ -0,0 +1,44 @@ +package subway.config.constants.views; + +import java.util.Arrays; +import java.util.List; + +public enum Indexes { + MAIN("메인", Arrays.asList("역 관리", "노선 관리", "구간 관리", "지하철 노선도 출력")), + METHOD("기능", Arrays.asList("등록", "삭제", "조회")); + + private String role; + private List values; + + Indexes(String role, List values) { + this.role = role; + this.values = values; + } + public String getRole(){ + return this.role; + } + + public String getIndex(){ + StringBuilder sb= new StringBuilder(); + for(int i = 0;i< values.size();i++){ + sb.append(i+1).append(". "); + sb.append(values.get(i)).append("\n"); + } + sb.append("Q. 종료\n"); + return sb.toString(); + } + public String getIndex(String target){ + int size = values.size(); + if(target.equals("구간")){ + size = 2; + } + StringBuilder sb = new StringBuilder(); + for(int i = 0;i< size;i++){ + sb.append(i+1).append(". "); + sb.append(target).append(" "); + sb.append(values.get(i)).append("\n"); + } + sb.append("B. 돌아가기\n"); + return sb.toString(); + } +} diff --git a/src/main/java/subway/config/constants/views/Questions.java b/src/main/java/subway/config/constants/views/Questions.java index dd24081aa..5fac64e59 100644 --- a/src/main/java/subway/config/constants/views/Questions.java +++ b/src/main/java/subway/config/constants/views/Questions.java @@ -1,22 +1,32 @@ package subway.config.constants.views; public enum Questions { - FUNC("원하는 기능을 선택하세요."), - NAME("이름을 입력하세요."), - SEQUENCE("## 순서를 입력하세요.\n"); + FUNC("원하는 기능을 선택하세요.\n"), + NAME("을 입력하세요.\n"), + SEQUENCE("순서를 입력하세요.\n"); private final String question; + static final String SHARP = Prefixes.SHARP.getPrefix(); Questions(String question) { this.question = question; } - public String Message(String m, String t) { - String message = m + "할 " + t + this.question + "\n"; - return message; + public String Message(){ + StringBuilder sb = new StringBuilder(); + sb.append(SHARP).append(this.question); + return sb.toString(); + } + + public String Message(String target) { + StringBuilder sb= new StringBuilder(); + sb.append(SHARP); + sb.append(target).append(this.question); + return sb.toString(); } - public String Message() { - return this.question; + public String Message(String work, String target) { + String message = work + "할 " + target + this.question; + return message; } } diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 5a84848cf..d2cc5cc52 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -19,7 +19,7 @@ public SectionController(final DataManager manager){ @Override public void work() { - ask.sectionManage(); + ask.WhatToManage("구역"); ask.Function(); try { String command = br.readLine(); diff --git a/src/main/java/subway/view/AskView.java b/src/main/java/subway/view/AskView.java index 056314836..4b87b3dde 100644 --- a/src/main/java/subway/view/AskView.java +++ b/src/main/java/subway/view/AskView.java @@ -1,70 +1,40 @@ package subway.view; -import subway.config.constants.views.*; +import subway.config.constants.views.Indexes; +import subway.view.util.MakeString; public class AskView { // view 가 너무 중구난방인 거 같은데 controller, service 처럼 분할할까. - private static StringBuilder sb = new StringBuilder(); - static final String SHARP = Prefixes.SHARP.getPrefix(); - static final Methods REGISTER = Methods.등록; - static final Methods DELETE = Methods.삭제; - static final Methods READ = Methods.조회; - static final Methods PRINT = Methods.출력; - static final Methods END = Methods.종료; - static final Methods BACK = Methods.돌아가기; + static final String MAIN = Indexes.MAIN.getRole(); + static final MakeString makeString = new MakeString(); public void printMain() { - sb.append(SHARP).append(Titles.MAIN.getTitle()); - sb.append("1. ").append(Titles.STATION.getTitle()); - sb.append("2. ").append(Titles.LINE.getTitle()); - sb.append("3. ").append(Titles.SECTION.getTitle()); - sb.append("4. ").append(Titles.MAP.getTitle()); - sb.append(END.getCommand()+". ").append(END+"\n"); - System.out.println(sb.toString()); - - sb.setLength(0); + System.out.println(makeString.showTitle(MAIN)); + System.out.println(makeString.showIndex(MAIN)); } public void WhatToManage(final String target) { - sb.append(SHARP + target + " 관리 화면\n"); - sb.append("1. " + target + " 등록\n"); - sb.append("2. " + target + " 삭제\n"); - sb.append("3. " + target + " 조회\n"); -// sb.append(REGISTER.getCommand()+". ").append(target+" ").append(REGISTER+"\n"); -// sb.append(DELETE.getCommand()+". ").append(target+" ").append(DELETE+"\n"); -// sb.append(READ.getCommand()+". ").append(target+" ").append(READ+"\n"); -// sb.append(BACK.getCommand()+". ").append(target+" ").append(BACK+"\n"); - System.out.println(sb.toString()); - sb.setLength(0); - } - - public void sectionManage() { - sb.append("## 구간 관리 화면\n"); - sb.append("1. 구간 등록\n"); - sb.append("2. 구간 삭제\n"); - sb.append("B. 돌아가기\n"); - System.out.println(sb.toString()); - sb.setLength(0); + System.out.println(makeString.showTitle(target)); + System.out.println(makeString.showIndex(target)); } + //"원하는 기능을 선택하세요." public void Function() { - sb.append(SHARP).append(Questions.FUNC.Message()); - System.out.println(sb.toString()); - sb.setLength(0); + System.out.println(makeString.askFunction()); } - public void Name(final String work, final String name) { // 등록 or 삭제 - sb.append(SHARP).append(Questions.NAME.Message(work, name)); - System.out.println(sb.toString()); - sb.setLength(0); + //## 등록/삭제할 역/노선을 입력하세요 + public void Name(final String work, final String name) { + String sentence = makeString.askName(work, name); + System.out.println(sentence); } - public void orderWhat(final String name) { // 등록 or 삭제 - sb.append(SHARP).append(name + "을 입력하세요\n"); - System.out.println(sb.toString()); - sb.setLength(0); + // 역/노선을 입력하세요 + public void orderWhat(final String name) { + String sentence = makeString.askName(name); + System.out.println(sentence); } public void orderWhere() { // 등록 or 삭제 - System.out.println(Questions.SEQUENCE.Message()); + System.out.println(makeString.askSequence()); } } diff --git a/src/main/java/subway/view/util/MakeString.java b/src/main/java/subway/view/util/MakeString.java new file mode 100644 index 000000000..1959fab8b --- /dev/null +++ b/src/main/java/subway/view/util/MakeString.java @@ -0,0 +1,58 @@ +package subway.view.util; + +import subway.config.constants.views.Indexes; +import subway.config.constants.views.Prefixes; +import subway.config.constants.views.Questions; + +public class MakeString { + static final String SHARP = Prefixes.SHARP.getPrefix(); + static final Indexes MAIN = Indexes.MAIN; + static final Indexes METHOD = Indexes.METHOD; + static final Questions SEQUENCE = Questions.SEQUENCE; + static final Questions TARGET = Questions.NAME; + static final Questions FUNC = Questions.FUNC; + + + // ## ~~ 화면 + public String showTitle(final String title){ + String role = MAIN.getRole(); + return getTitle(role, title); + } + + public String getTitle(final String role, final String title){ + StringBuilder sb = new StringBuilder(); + if(title.equals(role)){ + sb.append(SHARP).append(title); + sb.append(" 화면").append("\n"); + return sb.toString(); + } + sb.append(SHARP).append(title); + sb.append("관리 화면").append("\n"); + return sb.toString(); + } + + public String showIndex(final String target){ + if(target.equals(MAIN.getRole())){ + return MAIN.getIndex(); + } + return METHOD.getIndex(target); + } + + // 노선/역이름 을 입력하세요. + public String askName(final String target){ + return TARGET.Message(target); + } + + // 등록/삭제할 노선/역이름 을 입력하세요. + public String askName(final String work, final String name){ + return TARGET.Message(work, name); + } + + public String askFunction(){ + return FUNC.Message(); + } + + public String askSequence(){ + return SEQUENCE.Message(); + } +} From 25dd38a36b06763ec7dbdd9de4cc30ffc760e848 Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 13 May 2024 15:01:08 +0900 Subject: [PATCH 049/104] =?UTF-8?q?refactor:=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EA=B4=80=EB=A0=A8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/constants/views/Indexes.java | 20 ++++++++++-------- .../subway/config/constants/views/Titles.java | 21 ------------------- .../config/handler/SubwayException.java | 12 ++++++----- .../subway/controller/LineController.java | 5 +++-- .../subway/controller/SectionController.java | 8 +++++-- .../subway/controller/StationController.java | 2 +- src/main/java/subway/service/LineManager.java | 12 ++++++----- .../java/subway/service/SectionManager.java | 2 ++ .../subway/service/util/LineMakeString.java | 20 ++++++++++++++++++ .../java/subway/view/util/MakeString.java | 1 + 10 files changed, 58 insertions(+), 45 deletions(-) delete mode 100644 src/main/java/subway/config/constants/views/Titles.java create mode 100644 src/main/java/subway/service/util/LineMakeString.java diff --git a/src/main/java/subway/config/constants/views/Indexes.java b/src/main/java/subway/config/constants/views/Indexes.java index a9197ff7d..209faf549 100644 --- a/src/main/java/subway/config/constants/views/Indexes.java +++ b/src/main/java/subway/config/constants/views/Indexes.java @@ -14,27 +14,29 @@ public enum Indexes { this.role = role; this.values = values; } - public String getRole(){ + + public String getRole() { return this.role; } - public String getIndex(){ - StringBuilder sb= new StringBuilder(); - for(int i = 0;i< values.size();i++){ - sb.append(i+1).append(". "); + public String getIndex() { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < values.size(); i++) { + sb.append(i + 1).append(". "); sb.append(values.get(i)).append("\n"); } sb.append("Q. 종료\n"); return sb.toString(); } - public String getIndex(String target){ + + public String getIndex(String target) { int size = values.size(); - if(target.equals("구간")){ + if (target.equals("구간")) { size = 2; } StringBuilder sb = new StringBuilder(); - for(int i = 0;i< size;i++){ - sb.append(i+1).append(". "); + for (int i = 0; i < size; i++) { + sb.append(i + 1).append(". "); sb.append(target).append(" "); sb.append(values.get(i)).append("\n"); } diff --git a/src/main/java/subway/config/constants/views/Titles.java b/src/main/java/subway/config/constants/views/Titles.java deleted file mode 100644 index 7c0e6dd72..000000000 --- a/src/main/java/subway/config/constants/views/Titles.java +++ /dev/null @@ -1,21 +0,0 @@ -package subway.config.constants.views; -// 상수도 화면 별로 3개 분할할까? - -public enum Titles { - MAIN("메인 화면"), - STATION("역 관리 화면"), - LINE("노선 관리 화면"), - SECTION("구간 관리 화면"), - STATIONS("역 목록"), - LINES("노선 목록"), - SECTIONS("구간 목록"), - MAP("지하철 노선도"); - - private String title; - Titles(String title){ - this.title=title; - } - public String getTitle(){ - return this.title +"\n"; - } -} diff --git a/src/main/java/subway/config/handler/SubwayException.java b/src/main/java/subway/config/handler/SubwayException.java index 47e26b366..536d0f4b9 100644 --- a/src/main/java/subway/config/handler/SubwayException.java +++ b/src/main/java/subway/config/handler/SubwayException.java @@ -3,15 +3,17 @@ import subway.config.constants.views.Prefixes; public class SubwayException extends IllegalArgumentException { - public boolean isValidLength(String name) { - if (name.length() >= 2) return true; - throw new IllegalStateException(Prefixes.ERROR+"2글자 이상으로 입력해주세요."); - } - public void notValidCommand() { throw new IllegalArgumentException(Prefixes.ERROR+"잘못된 명령입니다."); } + public boolean isBack(String command){ + if(command.equals('B')){ + return true; + } + return false; + } + public void checkCommand() { throw new IllegalArgumentException(Prefixes.ERROR+ "입력값을 확인해주세요."); } diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index ac23985be..a1fe0850e 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -36,6 +36,7 @@ public void work(){ } // 에러 발생시키기 subwayException.notValidCommand(); + subwayException.notValidCommand(); }catch (Exception e){ System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); } @@ -48,7 +49,7 @@ public void register() { ask.Name(command, "노선"); try{ String line = br.readLine(); - subwayException.isValidLength(line); + subwayException.notValidCommand(); boolean result = lineManager.register(line); ask.Name(command, Targets.UPPER.getTarget()); @@ -91,7 +92,7 @@ public void delete() { } public void read(){ - response.printTitle("노선 목록"); + response.printTitle("지하철 노선도"); StringBuilder list = lineManager.read(); response.printList(list); } diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index d2cc5cc52..90ed20764 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -19,7 +19,7 @@ public SectionController(final DataManager manager){ @Override public void work() { - ask.WhatToManage("구역"); + ask.WhatToManage("구간"); ask.Function(); try { String command = br.readLine(); @@ -78,7 +78,9 @@ public String getStation() { public int getIndex() { try { - int index = Integer.parseInt(br.readLine()); + String input = br.readLine(); + if(subwayException.isBack(input) == true) return -1; + int index = Integer.parseInt(input); return index; } catch (Exception e) { System.out.println("입력값을 확인해주세요."); @@ -91,8 +93,10 @@ public void delete() { try{ ask.Name("삭제", "구간의 노선"); String line = br.readLine(); + if(line.equals("B")) return; ask.Name("삭제", "구간의 역"); String station = br.readLine(); + if(station.equals("B")) return; // manager에서 line 세팅. 함수 이름 고민중... // 알고리즘 빡 분리해야 할 듯함 sectionManager.register(line); diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 4c98be8bf..7820027b8 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -50,7 +50,7 @@ public void register(){ ask.Name("등록", "역"); try{ String station = br.readLine(); - subwayException.isValidLength(station); + subwayException.notValidCommand(); boolean result = stationManager.register(station); // "지하철 역이 등록되었습니다." 출력 infoMessage("등록", result); diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index f0b6b126a..fb4f1c566 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -1,15 +1,15 @@ package subway.service; -import subway.config.constants.views.Prefixes; import subway.domain.Line; import subway.domain.Station; +import subway.service.util.LineMakeString; import java.util.List; import static subway.service.DataManager.*; public class LineManager extends Managerable { - Prefixes INFO = Prefixes.INFO; + LineMakeString makeString = new LineMakeString(); @Override public boolean register(final String name) { if(lineRepo.getLineByName(name) != null){ @@ -36,7 +36,8 @@ public boolean delete(final String name) { public StringBuilder read() { StringBuilder sb = new StringBuilder(); for(Line line: lineRepo.lines()){ - sb.append(INFO + line.getName()+"\n"); + String name = line.getName(); + sb.append(makeString.lines(name)).append("\n"); } return sb; } @@ -45,8 +46,9 @@ public StringBuilder getStationLines(){ StringBuilder sb = new StringBuilder(); // 반복되는데 String으로바꿀까. 반환값 바꾸려면 이것저것 많이 건들어야 할 듯 List lineList = lineRepo.lines(); for(Line line:lineList){ - sb.append(INFO+line.getName()).append("\n"); - sb.append(INFO+"---").append("\n"); + String rail = line.getName(); + String title = makeString.title(rail); + sb.append(title); sb.append(line.stationList()).append("\n"); } return sb; diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 41f56069d..203c787a7 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -12,6 +12,7 @@ public class SectionManager extends Managerable { private SubwayException subwayException; @Override public boolean register(final String name) { + if(subwayException.isBack(name) == true) return true; line = lineRepo.getLineByName(name); if(line == null) subwayException.noLine(); return true; @@ -25,6 +26,7 @@ public void insertSection(final String sName,final int index){ @Override public boolean delete(final String name) { + if(subwayException.isBack(name) == true) return true; if(line.deleteStation(name)) return true; subwayException.checkCommand(); return false; diff --git a/src/main/java/subway/service/util/LineMakeString.java b/src/main/java/subway/service/util/LineMakeString.java new file mode 100644 index 000000000..8d246fff1 --- /dev/null +++ b/src/main/java/subway/service/util/LineMakeString.java @@ -0,0 +1,20 @@ +package subway.service.util; + +import subway.config.constants.views.Prefixes; + +public class LineMakeString { + Prefixes INFO = Prefixes.INFO; + public String title(String rail){ + StringBuilder sb= new StringBuilder(); + sb.append(INFO).append(rail).append("\n"); + sb.append(INFO).append("---"); + return sb.toString(); + } + + public String lines(String name){ + StringBuilder sb = new StringBuilder(); + sb.append(INFO); + sb.append(name); + return sb.toString(); + } +} diff --git a/src/main/java/subway/view/util/MakeString.java b/src/main/java/subway/view/util/MakeString.java index 1959fab8b..e27cf7b00 100644 --- a/src/main/java/subway/view/util/MakeString.java +++ b/src/main/java/subway/view/util/MakeString.java @@ -35,6 +35,7 @@ public String showIndex(final String target){ if(target.equals(MAIN.getRole())){ return MAIN.getIndex(); } + return METHOD.getIndex(target); } From 447e15f3c45ba7471cce05acb889ff36b8a5ac69 Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 13 May 2024 15:34:27 +0900 Subject: [PATCH 050/104] =?UTF-8?q?refactor:=20SubwayException=20=EC=A4=91?= =?UTF-8?q?=EC=8B=AC=20=EC=BD=94=EB=93=9C=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/handler/SubwayException.java | 8 +++++ .../subway/controller/LineController.java | 3 +- .../subway/controller/SectionController.java | 25 ++++++++------- .../subway/controller/StationController.java | 31 ++++++------------- src/main/java/subway/domain/Line.java | 24 ++++++++------ src/main/java/subway/service/LineManager.java | 9 ++++-- src/main/java/subway/service/Managerable.java | 2 +- .../java/subway/service/SectionManager.java | 10 ++++-- .../java/subway/service/StationManager.java | 4 +-- .../java/subway/view/util/MakeString.java | 11 +++++++ 10 files changed, 75 insertions(+), 52 deletions(-) diff --git a/src/main/java/subway/config/handler/SubwayException.java b/src/main/java/subway/config/handler/SubwayException.java index 536d0f4b9..e7c0662b5 100644 --- a/src/main/java/subway/config/handler/SubwayException.java +++ b/src/main/java/subway/config/handler/SubwayException.java @@ -7,6 +7,14 @@ public void notValidCommand() { throw new IllegalArgumentException(Prefixes.ERROR+"잘못된 명령입니다."); } + public void areadyCreated(){ + throw new IllegalArgumentException(Prefixes.ERROR+"이미 존재하는 노선입니다."); + } + + + public void unexpected(){ + throw new IllegalArgumentException(Prefixes.ERROR+"예상치 못한 에러가 발생했습니다ㅏ."); + } public boolean isBack(String command){ if(command.equals('B')){ return true; diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index a1fe0850e..d73f95167 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -50,7 +50,8 @@ public void register() { try{ String line = br.readLine(); subwayException.notValidCommand(); - boolean result = lineManager.register(line); + boolean result = lineManager.isValid(line); + if(result!= true) return ; ask.Name(command, Targets.UPPER.getTarget()); String upper = br.readLine(); diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 90ed20764..909e74a53 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -37,14 +37,16 @@ public void work() { // 에러 발생시키기 subwayException.notValidCommand(); } catch (Exception e) { - System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); + subwayException.unexpected(); } } @Override public void register() { // 노선, 역 이름, 순서 입력 받고 등록 String line = getLine(); - sectionManager.register(line); + if(!sectionManager.isValid(line)){ + return; + } ask.orderWhat("역 이름"); String station = getStation(); @@ -62,7 +64,7 @@ public String getLine() { try { line = br.readLine(); } catch (Exception e) { - System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); + System.out.println("[ERROR] 노선 이름을 받을 수 없습니다. 다시 시도해주세요."); } return line; } @@ -71,7 +73,7 @@ public String getStation() { try { return br.readLine(); } catch (Exception e) { - System.out.println("역 이름을 받을 수 없습니다. 다시 시도해주세요."); + System.out.println("[ERROR] 역 이름을 받을 수 없습니다. 다시 시도해주세요."); } return null; } @@ -93,17 +95,16 @@ public void delete() { try{ ask.Name("삭제", "구간의 노선"); String line = br.readLine(); - if(line.equals("B")) return; + ask.Name("삭제", "구간의 역"); String station = br.readLine(); - if(station.equals("B")) return; - // manager에서 line 세팅. 함수 이름 고민중... - // 알고리즘 빡 분리해야 할 듯함 - sectionManager.register(line); - sectionManager.delete(station); - response.printInfo("구간이 삭제되었습니다."); + + if(sectionManager.isValid(line)){ + sectionManager.delete(station); + response.printInfo("구간이 삭제되었습니다."); + } } catch (Exception e){ - System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); + subwayException.unexpected(); } } } diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 7820027b8..da6ba404a 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -6,11 +6,13 @@ import subway.config.handler.SubwayException; import subway.service.DataManager; import subway.service.StationManager; +import subway.view.util.MakeString; // 역을 관리하는 컨트롤러 public class StationController extends ManageController{ static StationManager stationManager; static SubwayException subwayException; + static MakeString makeString; String prefix = Prefixes.ERROR.getPrefix(); // 모든 변수의 인스턴스가 저장하는 클래스를 만들어? StationController(final DataManager manager){ @@ -41,7 +43,7 @@ public void work(){ // 이 부분을 모듈로 빼버릴까. work 의 매개변 // 에러 발생시키 subwayException.notValidCommand(); }catch (Exception e){ - System.out.println(prefix+"예상치 못한 에러가 발생했습니다."); + subwayException.unexpected(); } } @@ -50,26 +52,12 @@ public void register(){ ask.Name("등록", "역"); try{ String station = br.readLine(); - subwayException.notValidCommand(); - boolean result = stationManager.register(station); - // "지하철 역이 등록되었습니다." 출력 - infoMessage("등록", result); + boolean result = stationManager.isValid(station); + String message = makeString.infoMessage("등록", result); // "지하철 역이 등록되었습니다." 출력 + response.printInfo(message); }catch (Exception e){ - System.out.println(prefix+"예상치 못한 에러가 발생했습니다."); - } - } - public void infoMessage(final String work, final boolean result){ - StringBuilder sb = new StringBuilder(); - sb.append("지하철 역이 "); // 쓸데없이 긴 것 같은데 string 다루는 모듈을 따로 만들까. controller 기능이 잘 안보이는 것 같음 - if(result == true){ - sb.append(work).append("되었습니다."); - } - if(result == false){ - sb.append(work).append("되지 않았습니다."); + subwayException.unexpected(); } - String message = sb.toString(); - response.printInfo(message); - sb.setLength(0); } @Override @@ -79,9 +67,10 @@ public void delete(){ try{ String command = br.readLine(); boolean result = stationManager.delete(command); - infoMessage("삭제", result); + String message = makeString.infoMessage("삭제", result); + response.printInfo(message); }catch (Exception e){ - System.out.println(prefix + "예상치 못한 에러가 발생했습니다."); + subwayException.unexpected(); } } diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index cebb9807e..82cd2a058 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -31,12 +31,19 @@ public void setStations(final Station upper, final Station bottom) { } public void addStation(final Station station, final int index) { + if(index==stations.size()){ + bottomStation = station; + return; + } + if(index==0){ + upperStation=station; + return; + } stations.add(index, station); } public boolean deleteStation(final String station) { - int index = 0; - for (index = 0; index < stations.size(); index++) { + for (int index = 0; index < stations.size(); index++) { Station s = stations.get(index); if (s.getName().equals(station)) { stations.remove(index); @@ -46,13 +53,10 @@ public boolean deleteStation(final String station) { return false; } - // 추가 기능 구현 - public Station getStationByName(final String name) { - for (Station station : stations) { - if (station.getName().equals(name)) { - return station; - } - } - return null; + public Station getUpperStation(){ + return this.upperStation; + } + public Station getBottomStation(){ + return this.bottomStation; } } diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index fb4f1c566..ee180931d 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -1,5 +1,6 @@ package subway.service; +import subway.config.handler.SubwayException; import subway.domain.Line; import subway.domain.Station; import subway.service.util.LineMakeString; @@ -10,11 +11,15 @@ public class LineManager extends Managerable { LineMakeString makeString = new LineMakeString(); + SubwayException subwayException = new SubwayException(); @Override - public boolean register(final String name) { - if(lineRepo.getLineByName(name) != null){ + public boolean isValid(final String name) { + if(subwayException.isBack(name) == true){ return false; } + if(lineRepo.getLineByName(name) != null){ + subwayException.areadyCreated(); + } lineRepo.addLine(new Line(name)); return true; } diff --git a/src/main/java/subway/service/Managerable.java b/src/main/java/subway/service/Managerable.java index f5cd62e82..3d5c3186d 100644 --- a/src/main/java/subway/service/Managerable.java +++ b/src/main/java/subway/service/Managerable.java @@ -1,7 +1,7 @@ package subway.service; public abstract class Managerable { - public abstract boolean register(final String command); + public abstract boolean isValid(final String command); public abstract boolean delete(final String name); public abstract StringBuilder read(); } diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 203c787a7..ade40e032 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -11,10 +11,14 @@ public class SectionManager extends Managerable { private Line line; private SubwayException subwayException; @Override - public boolean register(final String name) { - if(subwayException.isBack(name) == true) return true; + public boolean isValid(final String name) { + if(subwayException.isBack(name) == true) + return false; + line = lineRepo.getLineByName(name); - if(line == null) subwayException.noLine(); + + if(line == null) + subwayException.noLine(); return true; } diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index 9f62e3ad3..e28d5650b 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -6,7 +6,7 @@ // 역 관련 기능하는 서비스 public class StationManager extends Managerable { @Override // 삽입 - public boolean register(final String name) { + public boolean isValid(final String name) { // 해당 역이 존재하는 지 확인 // 삽입 if(stationRepo.getStationByName(name)!=null){ @@ -41,7 +41,7 @@ public StringBuilder read() { } public Station getByName(final String name){ - register(name); + isValid(name); return stationRepo.getStationByName(name); } } diff --git a/src/main/java/subway/view/util/MakeString.java b/src/main/java/subway/view/util/MakeString.java index e27cf7b00..afbeefecd 100644 --- a/src/main/java/subway/view/util/MakeString.java +++ b/src/main/java/subway/view/util/MakeString.java @@ -38,6 +38,17 @@ public String showIndex(final String target){ return METHOD.getIndex(target); } + public String infoMessage(final String work, final boolean result){ + StringBuilder sb = new StringBuilder(); + sb.append("지하철 역이 "); // 쓸데없이 긴 것 같은데 string 다루는 모듈을 따로 만들까. controller 기능이 잘 안보이는 것 같음 + if(result == true){ + sb.append(work).append("되었습니다."); + } + if(result == false){ + sb.append(work).append("되지 않았습니다."); + } + return sb.toString(); + } // 노선/역이름 을 입력하세요. public String askName(final String target){ From f989513ce412e113e896f6f377177df0b6096214 Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 13 May 2024 15:50:47 +0900 Subject: [PATCH 051/104] =?UTF-8?q?refactor:=20Controller=20=EB=82=B4?= =?UTF-8?q?=EB=B6=80=20work=20=ED=95=A8=EC=88=98=20=EB=B6=80=EB=AA=A8=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=EC=97=90=EC=84=9C=20=EC=A0=95?= =?UTF-8?q?=EC=9D=98,=20sendRequest=20=ED=95=A8=EC=88=98=20=EC=98=A4?= =?UTF-8?q?=EB=B2=84=EB=9D=BC=EC=9D=B4=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/LineController.java | 43 ++++++++----------- .../subway/controller/MainController.java | 11 +++-- .../subway/controller/ManageController.java | 19 ++++++-- .../java/subway/controller/MapController.java | 1 - .../subway/controller/SectionController.java | 33 ++++++-------- .../subway/controller/StationController.java | 42 +++++++----------- 6 files changed, 70 insertions(+), 79 deletions(-) diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index d73f95167..e7ea4dee5 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -9,37 +9,30 @@ public class LineController extends ManageController{ static LineManager lineManager; private static SubwayException subwayException; + Targets LINE = Targets.LINE; public LineController(final DataManager manager){ lineManager = manager.getLineManager(); subwayException = manager.getSubwayException(); } + @Override - public void work(){ - ask.WhatToManage("노선"); - ask.Function(); - try{ - String command = br.readLine(); - if(command.equals("1")){ - register(); - return; - } - if(command.equals("2")){ - delete(); - return; - } - if(command.equals("3")){ - read(); - return; - } - if(command.equals("B")){ - return; - } - // 에러 발생시키기 - subwayException.notValidCommand(); - subwayException.notValidCommand(); - }catch (Exception e){ - System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); + public void sendRequest(String command){ + if(command.equals("1")){ + register(); + return; + } + if(command.equals("2")){ + delete(); + return; + } + if(command.equals("3")){ + read(); + return; + } + if(command.equals("B")){ + return; } + subwayException.notValidCommand(); } @Override diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 77effc658..c4bb4d4bf 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -1,5 +1,6 @@ package subway.controller; +import subway.config.constants.views.Targets; import subway.config.handler.SubwayException; import subway.service.DataManager; import subway.view.AskView; @@ -14,6 +15,10 @@ public class MainController { private SectionController sectionController; private MapController mapController; private static SubwayException subwayException; + private static Targets LINE = Targets.LINE; + private static Targets STATION = Targets.STATION; + private static Targets SECTION = Targets.SECTION; + public MainController() { ask = new AskView(); @@ -46,15 +51,15 @@ public void headController() throws Exception { // 생성자와 함께 출력과 서비스를 제공하려 했는데 무슨 일 할 때마다 클래스 생성하는 것은 비효율적인 것 같다. 메소드로 바꾸자. 아니 이게 맞나? public void CoreController(final int command) { if (command == 1) { - stationController.work(); + stationController.work(STATION.getTarget()); return; } if (command == 2) { - lineController.work(); + lineController.work(LINE.getTarget()); return; } if (command == 3) { - sectionController.work(); + sectionController.work(SECTION.getTarget()); return; } if(command==4){ diff --git a/src/main/java/subway/controller/ManageController.java b/src/main/java/subway/controller/ManageController.java index d20b237d4..4d4436e07 100644 --- a/src/main/java/subway/controller/ManageController.java +++ b/src/main/java/subway/controller/ManageController.java @@ -9,19 +9,30 @@ import java.io.BufferedReader; import java.io.InputStreamReader; +import static subway.controller.StationController.subwayException; + public abstract class ManageController { AskView ask = new AskView(); ResponseView response = new ResponseView(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - public void work(){ -// ask.Function(); - // 관련 컨트롤러 호출 부분 + public void work(String target){ + ask.WhatToManage(target); + ask.Function(); + try{ + String command = br.readLine(); + sendRequest(command); + }catch (Exception e){ + subwayException.unexpected(); + } } // 등록하기 + public abstract void sendRequest(String command); - public abstract void register(); + public abstract void register(); // db 접근해서 데이터 삽입하는 서비스와 연결 public abstract void delete(); // db 접근해서 데이터 삭제하는 서비스와 연결 + +// public void read(){}; } diff --git a/src/main/java/subway/controller/MapController.java b/src/main/java/subway/controller/MapController.java index 18e35cf9b..0958ec55d 100644 --- a/src/main/java/subway/controller/MapController.java +++ b/src/main/java/subway/controller/MapController.java @@ -1,6 +1,5 @@ package subway.controller; -import subway.domain.LineRepository; import subway.service.DataManager; import subway.service.LineManager; diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 909e74a53..81c8befcd 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -10,6 +10,7 @@ public class SectionController extends ManageController { private static SectionManager sectionManager; private static ResponseView response = new ResponseView(); static SubwayException subwayException; + private Targets SECTION= Targets.SECTION; public SectionController(final DataManager manager){ @@ -18,27 +19,19 @@ public SectionController(final DataManager manager){ } @Override - public void work() { - ask.WhatToManage("구간"); - ask.Function(); - try { - String command = br.readLine(); - if (command.equals("1")) { // 등록 - register(); - return; - } - if (command.equals("2")) { // 삭제 - delete(); - return; - } - if (command.equals("B")) { // 되돌아가기 - return; - } - // 에러 발생시키기 - subwayException.notValidCommand(); - } catch (Exception e) { - subwayException.unexpected(); + public void sendRequest(String command){ + if (command.equals("1")) { // 등록 + register(); + return; + } + if (command.equals("2")) { // 삭제 + delete(); + return; + } + if (command.equals("B")) { // 되돌아가기 + return; } + subwayException.notValidCommand(); } @Override diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index da6ba404a..b827d5ca4 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -1,7 +1,6 @@ package subway.controller; import subway.config.constants.views.Methods; -import subway.config.constants.views.Prefixes; import subway.config.constants.views.Targets; import subway.config.handler.SubwayException; import subway.service.DataManager; @@ -13,38 +12,29 @@ public class StationController extends ManageController{ static StationManager stationManager; static SubwayException subwayException; static MakeString makeString; - String prefix = Prefixes.ERROR.getPrefix(); // 모든 변수의 인스턴스가 저장하는 클래스를 만들어? StationController(final DataManager manager){ stationManager = manager.getStationManager(); subwayException = manager.getSubwayException(); } @Override - public void work(){ // 이 부분을 모듈로 빼버릴까. work 의 매개변수에 따라서 역, 노선, 구간 나눠서 서비스 제공하기? - ask.WhatToManage(Targets.STATION.getTarget()); - ask.Function(); - try{ - String command = br.readLine(); - if(command.equals(Methods.등록.getCommand())){ - register(); - return; - } - if(command.equals(Methods.삭제.getCommand())){ - delete(); - return; - } - if(command.equals(Methods.조회.getCommand())){ - read(); - return; - } - if(command.equals("B")){ - return; - } - // 에러 발생시키 - subwayException.notValidCommand(); - }catch (Exception e){ - subwayException.unexpected(); + public void sendRequest(String command){ + if(command.equals(Methods.등록.getCommand())){ + register(); + return; + } + if(command.equals(Methods.삭제.getCommand())){ + delete(); + return; + } + if(command.equals(Methods.조회.getCommand())){ + read(); + return; + } + if(command.equals("B")){ + return; } + subwayException.notValidCommand(); } @Override From 3cb8d29c2b00218250532446955cb4dab6cfdaae Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 13 May 2024 16:12:09 +0900 Subject: [PATCH 052/104] =?UTF-8?q?refactor:=20MainController=20Command=20?= =?UTF-8?q?=EB=B9=84=EA=B5=90=EB=A5=BC=20enum=20=EA=B3=BC=20=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/constants/views/Targets.java | 20 ++++++++++------ .../subway/controller/LineController.java | 20 ---------------- .../subway/controller/MainController.java | 15 ++++++------ .../subway/controller/ManageController.java | 24 +++++++++++++++++-- .../subway/controller/SectionController.java | 2 -- .../subway/controller/StationController.java | 21 ---------------- 6 files changed, 43 insertions(+), 59 deletions(-) diff --git a/src/main/java/subway/config/constants/views/Targets.java b/src/main/java/subway/config/constants/views/Targets.java index f488e9d5d..f9c049ee9 100644 --- a/src/main/java/subway/config/constants/views/Targets.java +++ b/src/main/java/subway/config/constants/views/Targets.java @@ -1,19 +1,25 @@ package subway.config.constants.views; public enum Targets { - LINE("노선"), - STATION("역"), - SECTION("구간"), - MAP("지하철 노선도"), - UPPER("상행 종점역"), - BOTTOM("하행 종점역"); + STATION("역", 1), + LINE("노선",2), + SECTION("구간", 3), + MAP("지하철 노선도", 4), + UPPER("상행 종점역", 5), + BOTTOM("하행 종점역", 6); private final String target; - Targets(String target){ + private final int command; + Targets(String target, int command){ this.target = target; + this.command=command; } public String getTarget(){ return this.target; } + + public int getCommand(){ + return this.command; + } } diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index e7ea4dee5..15d86654c 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -15,26 +15,6 @@ public LineController(final DataManager manager){ subwayException = manager.getSubwayException(); } - @Override - public void sendRequest(String command){ - if(command.equals("1")){ - register(); - return; - } - if(command.equals("2")){ - delete(); - return; - } - if(command.equals("3")){ - read(); - return; - } - if(command.equals("B")){ - return; - } - subwayException.notValidCommand(); - } - @Override public void register() { String command = Methods.등록.toString(); diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index c4bb4d4bf..6a515582a 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -18,6 +18,7 @@ public class MainController { private static Targets LINE = Targets.LINE; private static Targets STATION = Targets.STATION; private static Targets SECTION = Targets.SECTION; + private static Targets MAP = Targets.MAP; public MainController() { @@ -39,8 +40,7 @@ public void headController() throws Exception { return; } try{ - int validCommand = Integer.parseInt(command); - CoreController(validCommand); + CoreController(command); }catch (Exception e){ System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); } @@ -49,20 +49,21 @@ public void headController() throws Exception { } // 생성자와 함께 출력과 서비스를 제공하려 했는데 무슨 일 할 때마다 클래스 생성하는 것은 비효율적인 것 같다. 메소드로 바꾸자. 아니 이게 맞나? - public void CoreController(final int command) { - if (command == 1) { + public void CoreController(final String line) { + int command = Integer.parseInt(line); + if (command == STATION.getCommand()) { stationController.work(STATION.getTarget()); return; } - if (command == 2) { + if (command == LINE.getCommand()) { lineController.work(LINE.getTarget()); return; } - if (command == 3) { + if (command == SECTION.getCommand()) { sectionController.work(SECTION.getTarget()); return; } - if(command==4){ + if(command== MAP.getCommand()){ mapController.work(); return; } diff --git a/src/main/java/subway/controller/ManageController.java b/src/main/java/subway/controller/ManageController.java index 4d4436e07..85ea93021 100644 --- a/src/main/java/subway/controller/ManageController.java +++ b/src/main/java/subway/controller/ManageController.java @@ -1,5 +1,7 @@ package subway.controller; +import subway.config.constants.views.Methods; +import subway.config.constants.views.Targets; import subway.domain.LineRepository; import subway.domain.StationRepository; import subway.service.StationManager; @@ -14,6 +16,7 @@ public abstract class ManageController { AskView ask = new AskView(); ResponseView response = new ResponseView(); + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public void work(String target){ ask.WhatToManage(target); @@ -26,7 +29,24 @@ public void work(String target){ } } // 등록하기 - public abstract void sendRequest(String command); + public void sendRequest(String command){ + if(command.equals(Methods.등록.getCommand())){ + register(); + return; + } + if(command.equals(Methods.삭제.getCommand())){ + delete(); + return; + } + if(command.equals(Methods.조회.getCommand())){ + read(); + return; + } + if(command.equals("B")){ + return; + } + subwayException.notValidCommand(); + } public abstract void register(); // db 접근해서 데이터 삽입하는 서비스와 연결 @@ -34,5 +54,5 @@ public void work(String target){ public abstract void delete(); // db 접근해서 데이터 삭제하는 서비스와 연결 -// public void read(){}; + public void read(){}; } diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 81c8befcd..2f3d80eba 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -10,8 +10,6 @@ public class SectionController extends ManageController { private static SectionManager sectionManager; private static ResponseView response = new ResponseView(); static SubwayException subwayException; - private Targets SECTION= Targets.SECTION; - public SectionController(final DataManager manager){ sectionManager = manager.getSectionManager(); diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index b827d5ca4..a3d1fc77f 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -1,7 +1,5 @@ package subway.controller; -import subway.config.constants.views.Methods; -import subway.config.constants.views.Targets; import subway.config.handler.SubwayException; import subway.service.DataManager; import subway.service.StationManager; @@ -17,25 +15,6 @@ public class StationController extends ManageController{ stationManager = manager.getStationManager(); subwayException = manager.getSubwayException(); } - @Override - public void sendRequest(String command){ - if(command.equals(Methods.등록.getCommand())){ - register(); - return; - } - if(command.equals(Methods.삭제.getCommand())){ - delete(); - return; - } - if(command.equals(Methods.조회.getCommand())){ - read(); - return; - } - if(command.equals("B")){ - return; - } - subwayException.notValidCommand(); - } @Override public void register(){ From dc1b6c58e3d7e6f2e95f9252604131154ef4590f Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 13 May 2024 16:14:47 +0900 Subject: [PATCH 053/104] =?UTF-8?q?refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EC=A3=BC=EC=84=9D=20=EC=82=AD=EC=A0=9C=20=EB=B0=8F?= =?UTF-8?q?=20=EC=B6=9C=EB=A0=A5=20=EC=8B=9C=20=EC=97=94=ED=84=B0=20?= =?UTF-8?q?=EC=A1=B0=EC=A0=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/controller/MainController.java | 2 -- src/main/java/subway/view/util/MakeString.java | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 6a515582a..e0c863de7 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -44,11 +44,9 @@ public void headController() throws Exception { }catch (Exception e){ System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); } - // command 가 유효한 명령이 아닐 경우 코드 추가 headController(); } - // 생성자와 함께 출력과 서비스를 제공하려 했는데 무슨 일 할 때마다 클래스 생성하는 것은 비효율적인 것 같다. 메소드로 바꾸자. 아니 이게 맞나? public void CoreController(final String line) { int command = Integer.parseInt(line); if (command == STATION.getCommand()) { diff --git a/src/main/java/subway/view/util/MakeString.java b/src/main/java/subway/view/util/MakeString.java index afbeefecd..87a242aa1 100644 --- a/src/main/java/subway/view/util/MakeString.java +++ b/src/main/java/subway/view/util/MakeString.java @@ -23,11 +23,11 @@ public String getTitle(final String role, final String title){ StringBuilder sb = new StringBuilder(); if(title.equals(role)){ sb.append(SHARP).append(title); - sb.append(" 화면").append("\n"); + sb.append(" 화면"); return sb.toString(); } sb.append(SHARP).append(title); - sb.append("관리 화면").append("\n"); + sb.append("관리 화면"); return sb.toString(); } From a33676e3fcb00a279694fc93914cdc63f49eb693 Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 13 May 2024 16:27:25 +0900 Subject: [PATCH 054/104] refactor: add Error Handler --- src/main/java/subway/controller/LineController.java | 4 ++-- src/main/java/subway/controller/MainController.java | 2 +- src/main/java/subway/controller/SectionController.java | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 15d86654c..7a62934ba 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -34,7 +34,7 @@ public void register() { lineManager.setStations(line, upper, bottom); infoMessage(command, result); }catch (Exception e){ - System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); + subwayException.unexpected(); } } @@ -61,7 +61,7 @@ public void delete() { boolean result = lineManager.delete(command); infoMessage("삭제", result); }catch (Exception e){ - System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); + subwayException.unexpected(); } } diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index e0c863de7..b986c4f21 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -42,7 +42,7 @@ public void headController() throws Exception { try{ CoreController(command); }catch (Exception e){ - System.out.println("[ERROR] 예상치 못한 에러가 발생했습니다."); + subwayException.unexpected(); } headController(); } diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 2f3d80eba..8264be5dd 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -55,7 +55,7 @@ public String getLine() { try { line = br.readLine(); } catch (Exception e) { - System.out.println("[ERROR] 노선 이름을 받을 수 없습니다. 다시 시도해주세요."); + subwayException.unexpected(); } return line; } @@ -64,7 +64,7 @@ public String getStation() { try { return br.readLine(); } catch (Exception e) { - System.out.println("[ERROR] 역 이름을 받을 수 없습니다. 다시 시도해주세요."); + subwayException.unexpected(); } return null; } @@ -76,7 +76,7 @@ public int getIndex() { int index = Integer.parseInt(input); return index; } catch (Exception e) { - System.out.println("입력값을 확인해주세요."); + subwayException.checkCommand(); } return -1; } From f08a8928269cd91cc540b125b374898cf7d2a19a Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 13 May 2024 20:30:18 +0900 Subject: [PATCH 055/104] =?UTF-8?q?refactor:=20ManageController=20work,=20?= =?UTF-8?q?sendRquest=20=EB=A7=A4=EA=B0=9C=EB=B3=80=EC=88=98=20controller?= =?UTF-8?q?=20=EC=B6=94=EC=83=81=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/handler/SubwayException.java | 12 ++++---- .../java/subway/controller/Controller.java | 7 +++++ .../subway/controller/LineController.java | 2 +- .../subway/controller/MainController.java | 6 ++-- .../subway/controller/ManageController.java | 28 ++++++------------- .../subway/controller/SectionController.java | 20 +++++++------ src/main/java/subway/service/LineManager.java | 2 +- .../subway/service/util/LineMakeString.java | 10 +++---- 8 files changed, 43 insertions(+), 44 deletions(-) create mode 100644 src/main/java/subway/controller/Controller.java diff --git a/src/main/java/subway/config/handler/SubwayException.java b/src/main/java/subway/config/handler/SubwayException.java index e7c0662b5..fae9d6231 100644 --- a/src/main/java/subway/config/handler/SubwayException.java +++ b/src/main/java/subway/config/handler/SubwayException.java @@ -4,16 +4,16 @@ public class SubwayException extends IllegalArgumentException { public void notValidCommand() { - throw new IllegalArgumentException(Prefixes.ERROR+"잘못된 명령입니다."); + throw new IllegalArgumentException(Prefixes.ERROR.getPrefix()+"잘못된 명령입니다."); } public void areadyCreated(){ - throw new IllegalArgumentException(Prefixes.ERROR+"이미 존재하는 노선입니다."); + throw new IllegalArgumentException(Prefixes.ERROR.getPrefix()+"이미 존재하는 노선입니다."); } public void unexpected(){ - throw new IllegalArgumentException(Prefixes.ERROR+"예상치 못한 에러가 발생했습니다ㅏ."); + throw new IllegalArgumentException(Prefixes.ERROR.getPrefix()+"예상치 못한 에러가 발생했습니다."); } public boolean isBack(String command){ if(command.equals('B')){ @@ -23,15 +23,15 @@ public boolean isBack(String command){ } public void checkCommand() { - throw new IllegalArgumentException(Prefixes.ERROR+ "입력값을 확인해주세요."); + throw new IllegalArgumentException(Prefixes.ERROR.getPrefix()+ "입력값을 확인해주세요."); } public void noStation() { - throw new IllegalArgumentException(Prefixes.ERROR+"존재하지 않는 역입니다."); + throw new IllegalArgumentException(Prefixes.ERROR.getPrefix()+"존재하지 않는 역입니다."); } public void noLine() { - throw new IllegalArgumentException(Prefixes.ERROR+"존재하지 않는 노선입니다."); + throw new IllegalArgumentException(Prefixes.ERROR.getPrefix()+"존재하지 않는 노선입니다."); } diff --git a/src/main/java/subway/controller/Controller.java b/src/main/java/subway/controller/Controller.java new file mode 100644 index 000000000..c4a05b72e --- /dev/null +++ b/src/main/java/subway/controller/Controller.java @@ -0,0 +1,7 @@ +package subway.controller; + +public interface Controller { + public void register(); + public void delete(); + public void read(); +} diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 7a62934ba..6dabd5976 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -28,7 +28,7 @@ public void register() { ask.Name(command, Targets.UPPER.getTarget()); String upper = br.readLine(); - ask.Name(command, "하행 종점역"); + ask.Name(command, Targets.BOTTOM.getTarget()); String bottom = br.readLine(); lineManager.setStations(line, upper, bottom); diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index b986c4f21..fd3671e38 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -50,15 +50,15 @@ public void headController() throws Exception { public void CoreController(final String line) { int command = Integer.parseInt(line); if (command == STATION.getCommand()) { - stationController.work(STATION.getTarget()); + stationController.work(stationController, STATION.getTarget()); return; } if (command == LINE.getCommand()) { - lineController.work(LINE.getTarget()); + lineController.work(lineController, LINE.getTarget()); return; } if (command == SECTION.getCommand()) { - sectionController.work(SECTION.getTarget()); + sectionController.work(sectionController, SECTION.getTarget()); return; } if(command== MAP.getCommand()){ diff --git a/src/main/java/subway/controller/ManageController.java b/src/main/java/subway/controller/ManageController.java index 85ea93021..c0b5c17d7 100644 --- a/src/main/java/subway/controller/ManageController.java +++ b/src/main/java/subway/controller/ManageController.java @@ -1,10 +1,6 @@ package subway.controller; import subway.config.constants.views.Methods; -import subway.config.constants.views.Targets; -import subway.domain.LineRepository; -import subway.domain.StationRepository; -import subway.service.StationManager; import subway.view.AskView; import subway.view.ResponseView; @@ -13,46 +9,38 @@ import static subway.controller.StationController.subwayException; -public abstract class ManageController { +public abstract class ManageController implements Controller{ AskView ask = new AskView(); ResponseView response = new ResponseView(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - public void work(String target){ + public void work(Controller controller, String target){ ask.WhatToManage(target); ask.Function(); try{ String command = br.readLine(); - sendRequest(command); + sendRequest(controller, command); }catch (Exception e){ subwayException.unexpected(); } } // 등록하기 - public void sendRequest(String command){ + public void sendRequest(Controller controller, String command){ if(command.equals(Methods.등록.getCommand())){ - register(); + controller.register(); return; } if(command.equals(Methods.삭제.getCommand())){ - delete(); + controller.delete(); return; } if(command.equals(Methods.조회.getCommand())){ - read(); + controller.read(); return; } - if(command.equals("B")){ + if(command.equals(Methods.돌아가기.getCommand())){ return; } subwayException.notValidCommand(); } - - public abstract void register(); - // db 접근해서 데이터 삽입하는 서비스와 연결 - - public abstract void delete(); - // db 접근해서 데이터 삭제하는 서비스와 연결 - - public void read(){}; } diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 8264be5dd..640f5683d 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -1,5 +1,6 @@ package subway.controller; +import subway.config.constants.views.Methods; import subway.config.constants.views.Targets; import subway.config.handler.SubwayException; import subway.service.DataManager; @@ -17,16 +18,16 @@ public SectionController(final DataManager manager){ } @Override - public void sendRequest(String command){ - if (command.equals("1")) { // 등록 - register(); + public void sendRequest(final Controller controller, final String command){ + if (command.equals(Methods.등록.getCommand())) { // 등록 + this.register(); return; } - if (command.equals("2")) { // 삭제 - delete(); + if (command.equals(Methods.삭제.getCommand())) { // 삭제 + this.delete(); return; } - if (command.equals("B")) { // 되돌아가기 + if (command.equals(Methods.돌아가기.getCommand())) { // 되돌아가기 return; } subwayException.notValidCommand(); @@ -84,10 +85,10 @@ public int getIndex() { @Override public void delete() { try{ - ask.Name("삭제", "구간의 노선"); + ask.Name(Methods.삭제.toString(), "구간의 노선"); String line = br.readLine(); - ask.Name("삭제", "구간의 역"); + ask.Name(Methods.삭제.toString(), "구간의 역"); String station = br.readLine(); if(sectionManager.isValid(line)){ @@ -98,4 +99,7 @@ public void delete() { subwayException.unexpected(); } } + + @Override + public void read() {} } diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index ee180931d..eb3bdf610 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -48,7 +48,7 @@ public StringBuilder read() { } public StringBuilder getStationLines(){ - StringBuilder sb = new StringBuilder(); // 반복되는데 String으로바꿀까. 반환값 바꾸려면 이것저것 많이 건들어야 할 듯 + StringBuilder sb = new StringBuilder(); List lineList = lineRepo.lines(); for(Line line:lineList){ String rail = line.getName(); diff --git a/src/main/java/subway/service/util/LineMakeString.java b/src/main/java/subway/service/util/LineMakeString.java index 8d246fff1..6e547ab41 100644 --- a/src/main/java/subway/service/util/LineMakeString.java +++ b/src/main/java/subway/service/util/LineMakeString.java @@ -4,16 +4,16 @@ public class LineMakeString { Prefixes INFO = Prefixes.INFO; - public String title(String rail){ + public String title(final String rail){ StringBuilder sb= new StringBuilder(); - sb.append(INFO).append(rail).append("\n"); - sb.append(INFO).append("---"); + sb.append(INFO.getPrefix()).append(rail).append("\n"); + sb.append(INFO.getPrefix()).append("---"); return sb.toString(); } - public String lines(String name){ + public String lines(final String name){ StringBuilder sb = new StringBuilder(); - sb.append(INFO); + sb.append(INFO.getPrefix()); sb.append(name); return sb.toString(); } From 5cf777fad5389da471cb5a194a8417809aba35af Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 13 May 2024 21:16:25 +0900 Subject: [PATCH 056/104] =?UTF-8?q?refactor:=20enum=20=EA=B8=B0=EB=B0=98?= =?UTF-8?q?=20=EC=83=81=EC=88=98=20=EC=9D=B8=ED=84=B0=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=8A=A4=20=EC=83=9D=EC=84=B1=20=EB=B0=8F=20=ED=99=95=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/subway/controller/Constants.java | 21 +++++++ .../java/subway/controller/Controller.java | 2 + .../subway/controller/LineController.java | 14 ++--- .../subway/controller/ManageController.java | 55 ++++++++++--------- .../subway/controller/SectionController.java | 13 ++--- .../subway/controller/StationController.java | 9 +-- src/main/java/subway/service/DataManager.java | 2 + .../java/subway/service/StationManager.java | 15 +---- .../subway/service/util/LineMakeString.java | 8 +-- src/main/java/subway/view/AskView.java | 8 +-- src/main/java/subway/view/ResponseView.java | 11 ++-- src/main/java/subway/view/util/Constants.java | 17 ++++++ .../java/subway/view/util/MakeString.java | 21 ++----- 13 files changed, 108 insertions(+), 88 deletions(-) create mode 100644 src/main/java/subway/controller/Constants.java create mode 100644 src/main/java/subway/view/util/Constants.java diff --git a/src/main/java/subway/controller/Constants.java b/src/main/java/subway/controller/Constants.java new file mode 100644 index 000000000..81007ff95 --- /dev/null +++ b/src/main/java/subway/controller/Constants.java @@ -0,0 +1,21 @@ +package subway.controller; + +import subway.config.constants.views.Methods; +import subway.config.constants.views.Targets; + +public interface Constants { + static final String REGISTER_COMMAND = Methods.등록.getCommand(); + static final String DELETE_COMMAND = Methods.삭제.getCommand(); + static final String READ_COMMAND = Methods.조회.getCommand(); + static final String BACK_COMMAND = Methods.돌아가기.getCommand(); + + static final String REGISTER = Methods.등록.toString(); + static final String DELETE = Methods.삭제.toString(); + static final String READ = Methods.조회.toString(); + static final String BACK = Methods.돌아가기.toString(); + + static final String STATION = Targets.STATION.getTarget(); + static final String LINE = Targets.LINE.getTarget(); + static final String UPPER = Targets.UPPER.getTarget(); + static final String BOTTOM = Targets.BOTTOM.getTarget(); +} diff --git a/src/main/java/subway/controller/Controller.java b/src/main/java/subway/controller/Controller.java index c4a05b72e..21645e73d 100644 --- a/src/main/java/subway/controller/Controller.java +++ b/src/main/java/subway/controller/Controller.java @@ -1,5 +1,7 @@ package subway.controller; +import subway.config.constants.views.Methods; + public interface Controller { public void register(); public void delete(); diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 6dabd5976..f07e065a7 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -1,6 +1,5 @@ package subway.controller; -import subway.config.constants.views.Methods; import subway.config.constants.views.Targets; import subway.config.handler.SubwayException; import subway.service.DataManager; @@ -9,7 +8,6 @@ public class LineController extends ManageController{ static LineManager lineManager; private static SubwayException subwayException; - Targets LINE = Targets.LINE; public LineController(final DataManager manager){ lineManager = manager.getLineManager(); subwayException = manager.getSubwayException(); @@ -17,18 +15,18 @@ public LineController(final DataManager manager){ @Override public void register() { - String command = Methods.등록.toString(); + String command = REGISTER; // 새로운 노선 db 와 연동하여 생성하기 - ask.Name(command, "노선"); + ask.Name(command, LINE); try{ String line = br.readLine(); subwayException.notValidCommand(); boolean result = lineManager.isValid(line); if(result!= true) return ; - ask.Name(command, Targets.UPPER.getTarget()); + ask.Name(command, UPPER); String upper = br.readLine(); - ask.Name(command, Targets.BOTTOM.getTarget()); + ask.Name(command, BOTTOM); String bottom = br.readLine(); lineManager.setStations(line, upper, bottom); @@ -55,11 +53,11 @@ public void infoMessage(final String work, final boolean result){ @Override public void delete() { // 노선 db 에서 삭제 - ask.Name("삭제", "노선"); + ask.Name(DELETE, LINE); try{ String command = br.readLine(); boolean result = lineManager.delete(command); - infoMessage("삭제", result); + infoMessage(DELETE, result); }catch (Exception e){ subwayException.unexpected(); } diff --git a/src/main/java/subway/controller/ManageController.java b/src/main/java/subway/controller/ManageController.java index c0b5c17d7..bd8b5ce06 100644 --- a/src/main/java/subway/controller/ManageController.java +++ b/src/main/java/subway/controller/ManageController.java @@ -1,6 +1,5 @@ package subway.controller; -import subway.config.constants.views.Methods; import subway.view.AskView; import subway.view.ResponseView; @@ -9,38 +8,40 @@ import static subway.controller.StationController.subwayException; -public abstract class ManageController implements Controller{ - AskView ask = new AskView(); - ResponseView response = new ResponseView(); +public abstract class ManageController implements Controller, Constants { + final static AskView ask = new AskView(); + final static ResponseView response = new ResponseView(); - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - public void work(Controller controller, String target){ + protected static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + public void work(Controller controller, String target) { ask.WhatToManage(target); ask.Function(); - try{ + try { String command = br.readLine(); sendRequest(controller, command); - }catch (Exception e){ + } catch (Exception e) { subwayException.unexpected(); } } - // 등록하기 - public void sendRequest(Controller controller, String command){ - if(command.equals(Methods.등록.getCommand())){ - controller.register(); - return; - } - if(command.equals(Methods.삭제.getCommand())){ - controller.delete(); - return; - } - if(command.equals(Methods.조회.getCommand())){ - controller.read(); - return; - } - if(command.equals(Methods.돌아가기.getCommand())){ - return; - } - subwayException.notValidCommand(); - } + + // 등록하기 + public void sendRequest(final Controller controller, final String command) { + if (command.equals(REGISTER_COMMAND)) { + controller.register(); + return; + } + if (command.equals(DELETE_COMMAND)) { + controller.delete(); + return; + } + if (command.equals(READ_COMMAND)) { + controller.read(); + return; + } + if (command.equals(BACK_COMMAND)) { + return; + } + subwayException.notValidCommand(); + } } diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 640f5683d..5e999d84b 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -1,6 +1,5 @@ package subway.controller; -import subway.config.constants.views.Methods; import subway.config.constants.views.Targets; import subway.config.handler.SubwayException; import subway.service.DataManager; @@ -19,15 +18,15 @@ public SectionController(final DataManager manager){ @Override public void sendRequest(final Controller controller, final String command){ - if (command.equals(Methods.등록.getCommand())) { // 등록 + if (command.equals(REGISTER_COMMAND)) { // 등록 this.register(); return; } - if (command.equals(Methods.삭제.getCommand())) { // 삭제 + if (command.equals(DELETE_COMMAND)) { // 삭제 this.delete(); return; } - if (command.equals(Methods.돌아가기.getCommand())) { // 되돌아가기 + if (command.equals(BACK_COMMAND)) { // 되돌아가기 return; } subwayException.notValidCommand(); @@ -51,7 +50,7 @@ public void register() { // 노선, 역 이름, 순서 입력 받고 등록 } public String getLine() { - ask.orderWhat(Targets.LINE.getTarget()); + ask.orderWhat(LINE); String line = ""; try { line = br.readLine(); @@ -85,10 +84,10 @@ public int getIndex() { @Override public void delete() { try{ - ask.Name(Methods.삭제.toString(), "구간의 노선"); + ask.Name(DELETE, "구간의 노선"); String line = br.readLine(); - ask.Name(Methods.삭제.toString(), "구간의 역"); + ask.Name(DELETE, "구간의 역"); String station = br.readLine(); if(sectionManager.isValid(line)){ diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index a3d1fc77f..b37cc8b07 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -1,5 +1,6 @@ package subway.controller; +import subway.config.constants.views.Targets; import subway.config.handler.SubwayException; import subway.service.DataManager; import subway.service.StationManager; @@ -18,11 +19,11 @@ public class StationController extends ManageController{ @Override public void register(){ - ask.Name("등록", "역"); + ask.Name(REGISTER, Targets.STATION.getTarget()); try{ String station = br.readLine(); boolean result = stationManager.isValid(station); - String message = makeString.infoMessage("등록", result); // "지하철 역이 등록되었습니다." 출력 + String message = makeString.infoMessage(REGISTER, result); // "지하철 역이 등록되었습니다." 출력 response.printInfo(message); }catch (Exception e){ subwayException.unexpected(); @@ -32,11 +33,11 @@ public void register(){ @Override public void delete(){ // db 접근해서 데이터 삭제하는 서비스와 연결 - ask.Name("삭제", "역"); + ask.Name(DELETE, STATION); try{ String command = br.readLine(); boolean result = stationManager.delete(command); - String message = makeString.infoMessage("삭제", result); + String message = makeString.infoMessage(DELETE, result); response.printInfo(message); }catch (Exception e){ subwayException.unexpected(); diff --git a/src/main/java/subway/service/DataManager.java b/src/main/java/subway/service/DataManager.java index e08bb3137..54bcdebbe 100644 --- a/src/main/java/subway/service/DataManager.java +++ b/src/main/java/subway/service/DataManager.java @@ -7,6 +7,8 @@ import subway.domain.Station; import subway.domain.StationRepository; + +// TODO code 리팩토링 public class DataManager{ private final static LineNames lines = new LineNames(); // String[] private final static StationNames stations = new StationNames(); // String[] diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index e28d5650b..f97003060 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -7,24 +7,13 @@ public class StationManager extends Managerable { @Override // 삽입 public boolean isValid(final String name) { -// 해당 역이 존재하는 지 확인 -// 삽입 if(stationRepo.getStationByName(name)!=null){ - // 이미 존재하는 역이라고 전달하기 - return false; // 문자열 반환해서 상태 메시지 출력에 활용 + // 이미 존재하는 역 + return false; } stationRepo.addStation(new Station(name)); return true; } - - public boolean isExist(final String name){ - if(stationRepo.getStationByName(name)!=null){ - // 이미 존재하는 역이라고 전달하기 - return true; // 문자열 반환해서 상태 메시지 출력에 활용 - } - return false; - } - @Override public boolean delete(final String name) { return stationRepo.deleteStation(name); diff --git a/src/main/java/subway/service/util/LineMakeString.java b/src/main/java/subway/service/util/LineMakeString.java index 6e547ab41..e82fa9e30 100644 --- a/src/main/java/subway/service/util/LineMakeString.java +++ b/src/main/java/subway/service/util/LineMakeString.java @@ -3,17 +3,17 @@ import subway.config.constants.views.Prefixes; public class LineMakeString { - Prefixes INFO = Prefixes.INFO; + String INFO = Prefixes.INFO.getPrefix(); public String title(final String rail){ StringBuilder sb= new StringBuilder(); - sb.append(INFO.getPrefix()).append(rail).append("\n"); - sb.append(INFO.getPrefix()).append("---"); + sb.append(INFO).append(rail).append("\n"); + sb.append(INFO).append("---"); return sb.toString(); } public String lines(final String name){ StringBuilder sb = new StringBuilder(); - sb.append(INFO.getPrefix()); + sb.append(INFO); sb.append(name); return sb.toString(); } diff --git a/src/main/java/subway/view/AskView.java b/src/main/java/subway/view/AskView.java index 4b87b3dde..665073bf5 100644 --- a/src/main/java/subway/view/AskView.java +++ b/src/main/java/subway/view/AskView.java @@ -1,11 +1,11 @@ package subway.view; -import subway.config.constants.views.Indexes; +import subway.view.util.Constants; import subway.view.util.MakeString; -public class AskView { // view 가 너무 중구난방인 거 같은데 controller, service 처럼 분할할까. - static final String MAIN = Indexes.MAIN.getRole(); +public class AskView implements Constants { // view 가 너무 중구난방인 거 같은데 controller, service 처럼 분할할까. static final MakeString makeString = new MakeString(); + public void printMain() { System.out.println(makeString.showTitle(MAIN)); System.out.println(makeString.showIndex(MAIN)); @@ -28,7 +28,7 @@ public void Name(final String work, final String name) { } // 역/노선을 입력하세요 - public void orderWhat(final String name) { + public void orderWhat(final String name) { String sentence = makeString.askName(name); System.out.println(sentence); } diff --git a/src/main/java/subway/view/ResponseView.java b/src/main/java/subway/view/ResponseView.java index 5ce9cd02b..182e32c5a 100644 --- a/src/main/java/subway/view/ResponseView.java +++ b/src/main/java/subway/view/ResponseView.java @@ -1,15 +1,18 @@ package subway.view; -public class ResponseView { - static StringBuilder sb = new StringBuilder(); +import subway.view.util.Constants; + +public class ResponseView implements Constants { public void printInfo(final String message){ - sb.append("[INFO] ").append(message); + StringBuilder sb = new StringBuilder(); + sb.append(INFO).append(message); System.out.println(sb); sb.setLength(0); } public void printTitle(final String title){ - sb.append("### ").append(title); + StringBuilder sb = new StringBuilder(); + sb.append(SHARP).append(title); System.out.println(sb); sb.setLength(0); } diff --git a/src/main/java/subway/view/util/Constants.java b/src/main/java/subway/view/util/Constants.java new file mode 100644 index 000000000..84e92d1fc --- /dev/null +++ b/src/main/java/subway/view/util/Constants.java @@ -0,0 +1,17 @@ +package subway.view.util; + +import subway.config.constants.views.Indexes; +import subway.config.constants.views.Prefixes; +import subway.config.constants.views.Questions; + +public interface Constants { + static final String SHARP = Prefixes.SHARP.getPrefix(); + static final String INFO = Prefixes.INFO.getPrefix(); + static final Indexes METHOD = Indexes.METHOD; + static final Questions SEQUENCE = Questions.SEQUENCE; + static final Questions TARGET = Questions.NAME; + static final Questions FUNC = Questions.FUNC; + static final String MAIN = Indexes.MAIN.getRole(); + static final String MAIN_INDEX = Indexes.MAIN.getIndex(); + +} diff --git a/src/main/java/subway/view/util/MakeString.java b/src/main/java/subway/view/util/MakeString.java index 87a242aa1..e4ba99847 100644 --- a/src/main/java/subway/view/util/MakeString.java +++ b/src/main/java/subway/view/util/MakeString.java @@ -1,22 +1,9 @@ package subway.view.util; -import subway.config.constants.views.Indexes; -import subway.config.constants.views.Prefixes; -import subway.config.constants.views.Questions; - -public class MakeString { - static final String SHARP = Prefixes.SHARP.getPrefix(); - static final Indexes MAIN = Indexes.MAIN; - static final Indexes METHOD = Indexes.METHOD; - static final Questions SEQUENCE = Questions.SEQUENCE; - static final Questions TARGET = Questions.NAME; - static final Questions FUNC = Questions.FUNC; - - +public class MakeString implements Constants{ // ## ~~ 화면 public String showTitle(final String title){ - String role = MAIN.getRole(); - return getTitle(role, title); + return getTitle(MAIN, title); } public String getTitle(final String role, final String title){ @@ -32,8 +19,8 @@ public String getTitle(final String role, final String title){ } public String showIndex(final String target){ - if(target.equals(MAIN.getRole())){ - return MAIN.getIndex(); + if(target.equals(MAIN)){ + return MAIN_INDEX; } return METHOD.getIndex(target); From 938675866a13efbde185dda57d3e8f9a38947024 Mon Sep 17 00:00:00 2001 From: aristo Date: Mon, 13 May 2024 21:27:22 +0900 Subject: [PATCH 057/104] refactor: error handler based on Errors ENUM file --- .../java/subway/config/constants/views/Errors.java | 6 +++++- .../subway/config/handler/SubwayException.java | 14 ++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/subway/config/constants/views/Errors.java b/src/main/java/subway/config/constants/views/Errors.java index 295245a3c..e9532b78c 100644 --- a/src/main/java/subway/config/constants/views/Errors.java +++ b/src/main/java/subway/config/constants/views/Errors.java @@ -1,14 +1,18 @@ package subway.config.constants.views; public enum Errors { - VALID("선택할 수 없는 기능입니다."), + VALID("잘못된 명령입니다. 입력값을 확인해주세요."), ADD_STATION("이미 등록된 역 이름입니다."), ADD_LINE("이미 등록된 노선 이름입니다."), DEL_STATION("존재하지 않은 역 이름입니다."), + UNEXPECTED("예상치 못한 에러가 발생했습니다."), DEL_LINE("존재하지 않는 노선 이름입니다."); private final String message; Errors(String message){ this.message = message; } + public String getError(){ + return this.message; + } } diff --git a/src/main/java/subway/config/handler/SubwayException.java b/src/main/java/subway/config/handler/SubwayException.java index fae9d6231..beb6aa73a 100644 --- a/src/main/java/subway/config/handler/SubwayException.java +++ b/src/main/java/subway/config/handler/SubwayException.java @@ -1,19 +1,21 @@ package subway.config.handler; +import subway.config.constants.views.Errors; import subway.config.constants.views.Prefixes; public class SubwayException extends IllegalArgumentException { + private static String ERROR = Prefixes.ERROR.getPrefix(); public void notValidCommand() { - throw new IllegalArgumentException(Prefixes.ERROR.getPrefix()+"잘못된 명령입니다."); + throw new IllegalArgumentException(ERROR+ Errors.VALID.getError()); } public void areadyCreated(){ - throw new IllegalArgumentException(Prefixes.ERROR.getPrefix()+"이미 존재하는 노선입니다."); + throw new IllegalArgumentException(ERROR+Errors.ADD_LINE.getError()); } public void unexpected(){ - throw new IllegalArgumentException(Prefixes.ERROR.getPrefix()+"예상치 못한 에러가 발생했습니다."); + throw new IllegalArgumentException(ERROR+Errors.UNEXPECTED.getError()); } public boolean isBack(String command){ if(command.equals('B')){ @@ -23,15 +25,15 @@ public boolean isBack(String command){ } public void checkCommand() { - throw new IllegalArgumentException(Prefixes.ERROR.getPrefix()+ "입력값을 확인해주세요."); + throw new IllegalArgumentException(ERROR+ Errors.VALID.getError()); } public void noStation() { - throw new IllegalArgumentException(Prefixes.ERROR.getPrefix()+"존재하지 않는 역입니다."); + throw new IllegalArgumentException(ERROR+Errors.DEL_STATION.getError()); } public void noLine() { - throw new IllegalArgumentException(Prefixes.ERROR.getPrefix()+"존재하지 않는 노선입니다."); + throw new IllegalArgumentException(ERROR+Errors.DEL_LINE.getError()); } From 8d352fbc0c126e9e91dcefcd80c93725e53289bb Mon Sep 17 00:00:00 2001 From: aristo Date: Tue, 14 May 2024 14:16:42 +0900 Subject: [PATCH 058/104] =?UTF-8?q?refactor:=20=EC=A7=80=ED=95=98=EC=B2=A0?= =?UTF-8?q?=20=EC=9D=B4=EB=A6=84=20Enum=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/constants/initValues/Lines.java | 27 +++++++++++++++++++ .../config/constants/initValues/Stations.java | 9 +++++++ src/main/java/subway/service/DataManager.java | 6 ++--- 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 src/main/java/subway/config/constants/initValues/Lines.java create mode 100644 src/main/java/subway/config/constants/initValues/Stations.java diff --git a/src/main/java/subway/config/constants/initValues/Lines.java b/src/main/java/subway/config/constants/initValues/Lines.java new file mode 100644 index 000000000..ecc8216c4 --- /dev/null +++ b/src/main/java/subway/config/constants/initValues/Lines.java @@ -0,0 +1,27 @@ +package subway.config.constants.initValues; + +import java.util.Arrays; +import java.util.List; + +public enum Lines { +// - 2호선: 교대역 - 강남역 - 역삼역 +// - 3호선: 교대역 - 남부터미널역 - 양재역 - 매봉역 +// - 신분당선: 강남역 - 양재역 - 양재시민의숲역 + LINE_2("2호선", Arrays.asList("교대역", "강남역", "역삼역")), + LINE_3("3호선", Arrays.asList("교대역", "남부터미널역", "양재역","매봉역")), + 신분당선("신분당선", Arrays.asList("강남역", "양재역", "양재시민의숲역")); + private String name; + private List values; + Lines(String name, List values){ + this.name = name; + this.values=values; + } + + public String getName(){ + return this.name; + } + + public List getValues(){ + return this.values; + } +} diff --git a/src/main/java/subway/config/constants/initValues/Stations.java b/src/main/java/subway/config/constants/initValues/Stations.java new file mode 100644 index 000000000..ea7c39d33 --- /dev/null +++ b/src/main/java/subway/config/constants/initValues/Stations.java @@ -0,0 +1,9 @@ +package subway.config.constants.initValues; + +// 1. 지하철역으로 +// 교대역, 강남역, 역삼역, 남부터미널역, 양재역, 양재시민의숲역, 매봉역 +// 이 등록되어 있다. + +public enum Stations { + 교대역, 강남역, 역삼역, 남부터미널역, 양재역, 양재시민의숲역, 매봉역; +} diff --git a/src/main/java/subway/service/DataManager.java b/src/main/java/subway/service/DataManager.java index 54bcdebbe..17e2ea963 100644 --- a/src/main/java/subway/service/DataManager.java +++ b/src/main/java/subway/service/DataManager.java @@ -42,9 +42,9 @@ public DataManager(){ } public void addStations(){ - String[] names = stations.getNames(); - for(String station: names){ - stationRepo.addStation(new Station(station)); + Stations[] names = Stations.values(); + for(Stations name: names){ + stationRepo.addStation(new Station(String.valueOf(name))); } } From 27f8b99d51f72805a1a52f3c6d695cd1aa9bffd6 Mon Sep 17 00:00:00 2001 From: aristo Date: Tue, 14 May 2024 16:11:47 +0900 Subject: [PATCH 059/104] =?UTF-8?q?fix=20&=20refactor:=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81=20=EC=A4=91=20=EB=B0=9C=EC=83=9D?= =?UTF-8?q?=ED=95=9C=20=EC=98=A4=EB=A5=98=20=ED=95=B4=EA=B2=B0=20=EB=B0=8F?= =?UTF-8?q?=20=EC=84=9C=EB=B9=84=EC=8A=A4=20=EC=B4=88=EA=B8=B0=ED=99=94=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EC=BD=94=EB=93=9C=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../constants/initValues/LineNames.java | 8 -- .../config/constants/initValues/Lines.java | 3 - .../constants/initValues/StationName.java | 7 ++ .../constants/initValues/StationNames.java | 10 --- .../config/constants/initValues/Stations.java | 9 --- .../config/handler/SubwayException.java | 1 - .../subway/controller/LineController.java | 7 +- .../subway/controller/MainController.java | 4 +- .../subway/controller/ManageController.java | 8 +- .../java/subway/controller/MapController.java | 4 +- .../subway/controller/SectionController.java | 16 ++-- .../subway/controller/StationController.java | 7 +- src/main/java/subway/domain/Line.java | 10 +-- src/main/java/subway/domain/Station.java | 2 +- .../java/subway/domain/StationRepository.java | 2 +- src/main/java/subway/service/Constants.java | 17 ++++ src/main/java/subway/service/DataManager.java | 77 ------------------- src/main/java/subway/service/InitManager.java | 65 ++++++++++++++++ src/main/java/subway/service/LineManager.java | 8 +- src/main/java/subway/service/Managerable.java | 8 +- .../java/subway/service/SectionManager.java | 8 +- .../java/subway/service/StationManager.java | 4 +- .../subway/service/util/LineMakeString.java | 2 +- 23 files changed, 134 insertions(+), 153 deletions(-) delete mode 100644 src/main/java/subway/config/constants/initValues/LineNames.java create mode 100644 src/main/java/subway/config/constants/initValues/StationName.java delete mode 100644 src/main/java/subway/config/constants/initValues/StationNames.java delete mode 100644 src/main/java/subway/config/constants/initValues/Stations.java create mode 100644 src/main/java/subway/service/Constants.java delete mode 100644 src/main/java/subway/service/DataManager.java create mode 100644 src/main/java/subway/service/InitManager.java diff --git a/src/main/java/subway/config/constants/initValues/LineNames.java b/src/main/java/subway/config/constants/initValues/LineNames.java deleted file mode 100644 index a9ef63255..000000000 --- a/src/main/java/subway/config/constants/initValues/LineNames.java +++ /dev/null @@ -1,8 +0,0 @@ -package subway.config.constants.initValues; - -public class LineNames { - private static final String[] lines = new String []{"2호선", "3호선", "수인분당선"}; - public String[] getNames(){ - return lines; - } -} diff --git a/src/main/java/subway/config/constants/initValues/Lines.java b/src/main/java/subway/config/constants/initValues/Lines.java index ecc8216c4..4c83cb165 100644 --- a/src/main/java/subway/config/constants/initValues/Lines.java +++ b/src/main/java/subway/config/constants/initValues/Lines.java @@ -4,9 +4,6 @@ import java.util.List; public enum Lines { -// - 2호선: 교대역 - 강남역 - 역삼역 -// - 3호선: 교대역 - 남부터미널역 - 양재역 - 매봉역 -// - 신분당선: 강남역 - 양재역 - 양재시민의숲역 LINE_2("2호선", Arrays.asList("교대역", "강남역", "역삼역")), LINE_3("3호선", Arrays.asList("교대역", "남부터미널역", "양재역","매봉역")), 신분당선("신분당선", Arrays.asList("강남역", "양재역", "양재시민의숲역")); diff --git a/src/main/java/subway/config/constants/initValues/StationName.java b/src/main/java/subway/config/constants/initValues/StationName.java new file mode 100644 index 000000000..e4c65d08f --- /dev/null +++ b/src/main/java/subway/config/constants/initValues/StationName.java @@ -0,0 +1,7 @@ +package subway.config.constants.initValues; + +public enum StationName { + 교대역, 강남역, 역삼역, 남부터미널역, 양재역, 양재시민의숲역, 매봉역; + + private String station; +} diff --git a/src/main/java/subway/config/constants/initValues/StationNames.java b/src/main/java/subway/config/constants/initValues/StationNames.java deleted file mode 100644 index 80052ceff..000000000 --- a/src/main/java/subway/config/constants/initValues/StationNames.java +++ /dev/null @@ -1,10 +0,0 @@ -package subway.config.constants.initValues; - -public class StationNames { -// // 해당 자료형을 컨트롤할 수 잇는 코드를 추출할까 - private static final String[] stations = new String []{"교대역", "강남역", "역삼역", "남부터미널역", "양재역", "양재시민의숲역", "매봉역"}; - - public String[] getNames() { - return stations; - } -} diff --git a/src/main/java/subway/config/constants/initValues/Stations.java b/src/main/java/subway/config/constants/initValues/Stations.java deleted file mode 100644 index ea7c39d33..000000000 --- a/src/main/java/subway/config/constants/initValues/Stations.java +++ /dev/null @@ -1,9 +0,0 @@ -package subway.config.constants.initValues; - -// 1. 지하철역으로 -// 교대역, 강남역, 역삼역, 남부터미널역, 양재역, 양재시민의숲역, 매봉역 -// 이 등록되어 있다. - -public enum Stations { - 교대역, 강남역, 역삼역, 남부터미널역, 양재역, 양재시민의숲역, 매봉역; -} diff --git a/src/main/java/subway/config/handler/SubwayException.java b/src/main/java/subway/config/handler/SubwayException.java index beb6aa73a..46f49ddcb 100644 --- a/src/main/java/subway/config/handler/SubwayException.java +++ b/src/main/java/subway/config/handler/SubwayException.java @@ -8,7 +8,6 @@ public class SubwayException extends IllegalArgumentException { public void notValidCommand() { throw new IllegalArgumentException(ERROR+ Errors.VALID.getError()); } - public void areadyCreated(){ throw new IllegalArgumentException(ERROR+Errors.ADD_LINE.getError()); } diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index f07e065a7..5eda2dc38 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -1,14 +1,13 @@ package subway.controller; -import subway.config.constants.views.Targets; import subway.config.handler.SubwayException; -import subway.service.DataManager; +import subway.service.InitManager; import subway.service.LineManager; public class LineController extends ManageController{ static LineManager lineManager; private static SubwayException subwayException; - public LineController(final DataManager manager){ + public LineController(final InitManager manager){ lineManager = manager.getLineManager(); subwayException = manager.getSubwayException(); } @@ -20,7 +19,6 @@ public void register() { ask.Name(command, LINE); try{ String line = br.readLine(); - subwayException.notValidCommand(); boolean result = lineManager.isValid(line); if(result!= true) return ; @@ -32,6 +30,7 @@ public void register() { lineManager.setStations(line, upper, bottom); infoMessage(command, result); }catch (Exception e){ + e.printStackTrace(); subwayException.unexpected(); } } diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index fd3671e38..ab43d1eb2 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -2,7 +2,7 @@ import subway.config.constants.views.Targets; import subway.config.handler.SubwayException; -import subway.service.DataManager; +import subway.service.InitManager; import subway.view.AskView; import java.io.BufferedReader; @@ -23,7 +23,7 @@ public class MainController { public MainController() { ask = new AskView(); - DataManager manager = new DataManager(); + InitManager manager = new InitManager(); stationController = new StationController(manager); lineController = new LineController(manager); sectionController = new SectionController(manager); diff --git a/src/main/java/subway/controller/ManageController.java b/src/main/java/subway/controller/ManageController.java index bd8b5ce06..9299a8f39 100644 --- a/src/main/java/subway/controller/ManageController.java +++ b/src/main/java/subway/controller/ManageController.java @@ -2,6 +2,7 @@ import subway.view.AskView; import subway.view.ResponseView; +import subway.view.util.MakeString; import java.io.BufferedReader; import java.io.InputStreamReader; @@ -9,12 +10,13 @@ import static subway.controller.StationController.subwayException; public abstract class ManageController implements Controller, Constants { - final static AskView ask = new AskView(); - final static ResponseView response = new ResponseView(); + static final AskView ask = new AskView(); + static final ResponseView response = new ResponseView(); + static final MakeString makeString = new MakeString(); protected static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - public void work(Controller controller, String target) { + public void work(final Controller controller, final String target) { ask.WhatToManage(target); ask.Function(); try { diff --git a/src/main/java/subway/controller/MapController.java b/src/main/java/subway/controller/MapController.java index 0958ec55d..9ac1cf62c 100644 --- a/src/main/java/subway/controller/MapController.java +++ b/src/main/java/subway/controller/MapController.java @@ -1,11 +1,11 @@ package subway.controller; -import subway.service.DataManager; +import subway.service.InitManager; import subway.service.LineManager; public class MapController { static LineManager lineManager; - MapController(DataManager manager){ + MapController(InitManager manager){ lineManager = manager.getLineManager(); } public void work(){ diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 5e999d84b..eb5b9d4e7 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -1,8 +1,7 @@ package subway.controller; -import subway.config.constants.views.Targets; import subway.config.handler.SubwayException; -import subway.service.DataManager; +import subway.service.InitManager; import subway.service.SectionManager; import subway.view.ResponseView; @@ -11,7 +10,7 @@ public class SectionController extends ManageController { private static ResponseView response = new ResponseView(); static SubwayException subwayException; - public SectionController(final DataManager manager){ + public SectionController(final InitManager manager){ sectionManager = manager.getSectionManager(); subwayException = manager.getSubwayException(); } @@ -19,11 +18,11 @@ public SectionController(final DataManager manager){ @Override public void sendRequest(final Controller controller, final String command){ if (command.equals(REGISTER_COMMAND)) { // 등록 - this.register(); + controller.register(); return; } if (command.equals(DELETE_COMMAND)) { // 삭제 - this.delete(); + controller.delete(); return; } if (command.equals(BACK_COMMAND)) { // 되돌아가기 @@ -51,13 +50,14 @@ public void register() { // 노선, 역 이름, 순서 입력 받고 등록 public String getLine() { ask.orderWhat(LINE); - String line = ""; try { - line = br.readLine(); + String line = br.readLine(); + return line; } catch (Exception e) { + e.printStackTrace(); subwayException.unexpected(); } - return line; + return null; } public String getStation() { diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index b37cc8b07..78deb8c08 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -2,17 +2,15 @@ import subway.config.constants.views.Targets; import subway.config.handler.SubwayException; -import subway.service.DataManager; +import subway.service.InitManager; import subway.service.StationManager; -import subway.view.util.MakeString; // 역을 관리하는 컨트롤러 public class StationController extends ManageController{ static StationManager stationManager; static SubwayException subwayException; - static MakeString makeString; - StationController(final DataManager manager){ + StationController(final InitManager manager){ stationManager = manager.getStationManager(); subwayException = manager.getSubwayException(); } @@ -26,6 +24,7 @@ public void register(){ String message = makeString.infoMessage(REGISTER, result); // "지하철 역이 등록되었습니다." 출력 response.printInfo(message); }catch (Exception e){ + e.printStackTrace(); subwayException.unexpected(); } } diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index 82cd2a058..f42b1ea10 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -31,15 +31,15 @@ public void setStations(final Station upper, final Station bottom) { } public void addStation(final Station station, final int index) { - if(index==stations.size()){ - bottomStation = station; - return; - } if(index==0){ upperStation=station; return; } - stations.add(index, station); + if(index==stations.size()){ + bottomStation = station; + return; + } + stations.add(index-1, station); } public boolean deleteStation(final String station) { diff --git a/src/main/java/subway/domain/Station.java b/src/main/java/subway/domain/Station.java index b5da343f2..cc8ce99f4 100644 --- a/src/main/java/subway/domain/Station.java +++ b/src/main/java/subway/domain/Station.java @@ -11,7 +11,7 @@ public Station(final String name) { } public String getName() { - return name; + return this.name; } // 추가 기능 구현 diff --git a/src/main/java/subway/domain/StationRepository.java b/src/main/java/subway/domain/StationRepository.java index 576f56531..b98407e88 100644 --- a/src/main/java/subway/domain/StationRepository.java +++ b/src/main/java/subway/domain/StationRepository.java @@ -12,7 +12,7 @@ public static List stations() { return Collections.unmodifiableList(stations); } - public static void addStation(final Station station) { + public static void addStation(Station station) { stations.add(station); } diff --git a/src/main/java/subway/service/Constants.java b/src/main/java/subway/service/Constants.java new file mode 100644 index 000000000..03c8b7469 --- /dev/null +++ b/src/main/java/subway/service/Constants.java @@ -0,0 +1,17 @@ +package subway.service; + +import subway.config.handler.SubwayException; +import subway.domain.LineRepository; +import subway.domain.StationRepository; + +public interface Constants { + static final LineRepository lineRepo = new LineRepository(); + static final StationRepository stationRepo = new StationRepository(); + + static final StationManager stationManager = new StationManager(); + static final SectionManager sectionManager = new SectionManager(); + static final LineManager lineManager = new LineManager(); + + static final SubwayException subwayException = new SubwayException(); + +} diff --git a/src/main/java/subway/service/DataManager.java b/src/main/java/subway/service/DataManager.java deleted file mode 100644 index 17e2ea963..000000000 --- a/src/main/java/subway/service/DataManager.java +++ /dev/null @@ -1,77 +0,0 @@ -package subway.service; - -import subway.config.constants.initValues.*; -import subway.config.handler.SubwayException; -import subway.domain.Line; -import subway.domain.LineRepository; -import subway.domain.Station; -import subway.domain.StationRepository; - - -// TODO code 리팩토링 -public class DataManager{ - private final static LineNames lines = new LineNames(); // String[] - private final static StationNames stations = new StationNames(); // String[] - - final static LineRepository lineRepo = new LineRepository(); - final static StationRepository stationRepo = new StationRepository(); - - final static StationManager stationManager = new StationManager(); - final static SectionManager sectionManager = new SectionManager(); - final static LineManager lineManager = new LineManager(); - - final static SubwayException subwayException = new SubwayException(); - - public LineManager getLineManager(){ - return lineManager; - } - - public static StationManager getStationManager() { - return stationManager; - } - - public static SectionManager getSectionManager(){ - return sectionManager; - } - public static SubwayException getSubwayException(){return subwayException;} - - public DataManager(){ - // 초기값 삽입 - addStations(); - addLines(); - } - - public void addStations(){ - Stations[] names = Stations.values(); - for(Stations name: names){ - stationRepo.addStation(new Station(String.valueOf(name))); - } - } - - public void addLines(){ - String[] names = lines.getNames(); - for(String line: names){ - Line node = new Line(line); - lineSet(node); - lineRepo.addLine(node); - } - } - - public void lineSet(final Line line){ - if(line.getName().equals("2호선")){ - line.setStations(stationRepo.getStationByName("교대역"), stationRepo.getStationByName("역삼역")); - line.addStation(stationRepo.getStationByName("강남역"), 1); - return; - } - if(line.getName().equals("3호선")){ - line.setStations(stationRepo.getStationByName("교대역"), stationRepo.getStationByName("매봉역")); - line.addStation(stationRepo.getStationByName("양재역"), 1); - line.addStation(stationRepo.getStationByName("남부터미널역"), 1); - return; - } - if(line.getName().equals("수인분당선")){ - line.setStations(stationRepo.getStationByName("강남역"), stationRepo.getStationByName("양재시민의숲역")); - line.addStation(stationRepo.getStationByName("양재역"), 1); - } - } -} diff --git a/src/main/java/subway/service/InitManager.java b/src/main/java/subway/service/InitManager.java new file mode 100644 index 000000000..883f3183e --- /dev/null +++ b/src/main/java/subway/service/InitManager.java @@ -0,0 +1,65 @@ +package subway.service; + +import subway.config.constants.initValues.*; +import subway.config.handler.SubwayException; +import subway.domain.Line; +import subway.domain.Station; + +import java.util.List; + + +public class InitManager implements Constants{ + + public LineManager getLineManager(){ + return lineManager; + } + + public static StationManager getStationManager() { + return stationManager; + } + + public static SectionManager getSectionManager(){ + return sectionManager; + } + public static SubwayException getSubwayException(){return subwayException;} + + public InitManager(){ + // 초기값 삽입 + addStations(); + addLines(); + } + + public void addStations(){ + StationName[] names = StationName.values(); + for(StationName name: names){ + String station = name.toString(); + stationRepo.addStation(new Station(station)); + } + } + + public void addLines(){ + Lines[] lines = Lines.values(); + for(Lines line : lines){ + Line node = initLine(line); + lineRepo.addLine(node); + } + } + + public Line initLine(final Lines line){ + Line node = new Line(line.getName()); + List values = line.getValues(); + + for(int i =0 ;i < values.size();i++){ + String station = values.get(i); + node = addStation(node, station, i); + } + + return node; + } + + public Line addStation(Line line , final String value, final int index ){ + Station station = stationRepo.getStationByName(value); + line.addStation(station, index); + return line; + } +} diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index eb3bdf610..9a6fe623e 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -7,9 +7,9 @@ import java.util.List; -import static subway.service.DataManager.*; +import static subway.service.InitManager.*; -public class LineManager extends Managerable { +public class LineManager implements Managerable { LineMakeString makeString = new LineMakeString(); SubwayException subwayException = new SubwayException(); @Override @@ -17,8 +17,8 @@ public boolean isValid(final String name) { if(subwayException.isBack(name) == true){ return false; } - if(lineRepo.getLineByName(name) != null){ - subwayException.areadyCreated(); + if(lineRepo.getLineByName(name) == null){ + subwayException.noStation(); } lineRepo.addLine(new Line(name)); return true; diff --git a/src/main/java/subway/service/Managerable.java b/src/main/java/subway/service/Managerable.java index 3d5c3186d..0adec28e6 100644 --- a/src/main/java/subway/service/Managerable.java +++ b/src/main/java/subway/service/Managerable.java @@ -1,7 +1,7 @@ package subway.service; -public abstract class Managerable { - public abstract boolean isValid(final String command); - public abstract boolean delete(final String name); - public abstract StringBuilder read(); +public interface Managerable { + public boolean isValid(final String command); + public boolean delete(final String name); + public StringBuilder read(); } diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index ade40e032..72374c728 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -4,12 +4,12 @@ import subway.domain.Line; import subway.domain.Station; -import static subway.service.DataManager.lineRepo; -import static subway.service.DataManager.stationRepo; +import static subway.service.InitManager.lineRepo; +import static subway.service.InitManager.stationRepo; -public class SectionManager extends Managerable { +public class SectionManager implements Managerable { private Line line; - private SubwayException subwayException; + SubwayException subwayException = new SubwayException(); @Override public boolean isValid(final String name) { if(subwayException.isBack(name) == true) diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index f97003060..d8c9b0638 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -1,10 +1,10 @@ package subway.service; import subway.domain.Station; -import static subway.service.DataManager.stationRepo; +import static subway.service.InitManager.stationRepo; // 역 관련 기능하는 서비스 -public class StationManager extends Managerable { +public class StationManager implements Managerable { @Override // 삽입 public boolean isValid(final String name) { if(stationRepo.getStationByName(name)!=null){ diff --git a/src/main/java/subway/service/util/LineMakeString.java b/src/main/java/subway/service/util/LineMakeString.java index e82fa9e30..9e42c360a 100644 --- a/src/main/java/subway/service/util/LineMakeString.java +++ b/src/main/java/subway/service/util/LineMakeString.java @@ -7,7 +7,7 @@ public class LineMakeString { public String title(final String rail){ StringBuilder sb= new StringBuilder(); sb.append(INFO).append(rail).append("\n"); - sb.append(INFO).append("---"); + sb.append(INFO).append("---").append("\n"); return sb.toString(); } From abe24f147cef8678b9269e077ce5cb6383efb63b Mon Sep 17 00:00:00 2001 From: aristo Date: Tue, 14 May 2024 17:33:21 +0900 Subject: [PATCH 060/104] =?UTF-8?q?refactor:=20service=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20=EC=83=81=EC=88=98=20=EA=B4=80=EB=A0=A8=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20util=20=EB=A1=9C=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/service/InitManager.java | 3 ++- src/main/java/subway/service/StationManager.java | 1 + src/main/java/subway/service/{ => util}/Constants.java | 5 ++++- 3 files changed, 7 insertions(+), 2 deletions(-) rename src/main/java/subway/service/{ => util}/Constants.java (80%) diff --git a/src/main/java/subway/service/InitManager.java b/src/main/java/subway/service/InitManager.java index 883f3183e..979f7e338 100644 --- a/src/main/java/subway/service/InitManager.java +++ b/src/main/java/subway/service/InitManager.java @@ -4,11 +4,12 @@ import subway.config.handler.SubwayException; import subway.domain.Line; import subway.domain.Station; +import subway.service.util.Constants; import java.util.List; -public class InitManager implements Constants{ +public class InitManager implements Constants { public LineManager getLineManager(){ return lineManager; diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index d8c9b0638..49af5a16a 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -9,6 +9,7 @@ public class StationManager implements Managerable { public boolean isValid(final String name) { if(stationRepo.getStationByName(name)!=null){ // 이미 존재하는 역 + return false; } stationRepo.addStation(new Station(name)); diff --git a/src/main/java/subway/service/Constants.java b/src/main/java/subway/service/util/Constants.java similarity index 80% rename from src/main/java/subway/service/Constants.java rename to src/main/java/subway/service/util/Constants.java index 03c8b7469..2dd651270 100644 --- a/src/main/java/subway/service/Constants.java +++ b/src/main/java/subway/service/util/Constants.java @@ -1,8 +1,11 @@ -package subway.service; +package subway.service.util; import subway.config.handler.SubwayException; import subway.domain.LineRepository; import subway.domain.StationRepository; +import subway.service.LineManager; +import subway.service.SectionManager; +import subway.service.StationManager; public interface Constants { static final LineRepository lineRepo = new LineRepository(); From 8685e0290eebbfb9bf6607c324d5782d9f2dbeeb Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 15 May 2024 11:44:45 +0900 Subject: [PATCH 061/104] =?UTF-8?q?refactor:=20MainController=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/MainController.java | 64 +++++++++++++------ .../subway/controller/ManageController.java | 4 +- .../java/subway/controller/MapController.java | 3 +- 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index ab43d1eb2..9fb96d3fd 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -19,52 +19,74 @@ public class MainController { private static Targets STATION = Targets.STATION; private static Targets SECTION = Targets.SECTION; private static Targets MAP = Targets.MAP; + private static int command; public MainController() { ask = new AskView(); InitManager manager = new InitManager(); + setControllers(manager); + subwayException = manager.getSubwayException(); + } + + public void setControllers(InitManager manager){ stationController = new StationController(manager); lineController = new LineController(manager); sectionController = new SectionController(manager); mapController = new MapController(manager); - subwayException = manager.getSubwayException(); } public void headController() throws Exception { - ask.printMain(); - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - String command = br.readLine(); - if (command.equals("Q")) { + while (true) { + ask.printMain(); + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String node = br.readLine(); + if (isEnd(node)) return; + nextStep(node); + } + } + + public boolean isEnd(String node) { + if (node.equals("Q")) { System.out.println(" 안녕히 가세요. "); - return; + return true; } - try{ + return false; + } + + public void nextStep(String node) { + try { + if (!strToInt(node)) return; CoreController(command); - }catch (Exception e){ + } catch (Exception e) { subwayException.unexpected(); } - headController(); } - public void CoreController(final String line) { - int command = Integer.parseInt(line); + public boolean CoreController(int command) { if (command == STATION.getCommand()) { - stationController.work(stationController, STATION.getTarget()); - return; + return stationController.work(stationController, STATION.getTarget()); } if (command == LINE.getCommand()) { - lineController.work(lineController, LINE.getTarget()); - return; + return lineController.work(lineController, LINE.getTarget()); } if (command == SECTION.getCommand()) { - sectionController.work(sectionController, SECTION.getTarget()); - return; + return sectionController.work(sectionController, SECTION.getTarget()); } - if(command== MAP.getCommand()){ - mapController.work(); - return; + if (command == MAP.getCommand()) { + return mapController.work(); + } + System.err.println("[ERROR] 유효한 범위 내의 명령어를 입력해주세요."); + return false; + } + + public boolean strToInt(final String node) { + try { + command = Integer.parseInt(node); + return true; + } catch (NumberFormatException e) { + System.err.println("[ERROR] 4 이하의 자연수나 'Q' 만 입력 가능합니다."); } - subwayException.notValidCommand(); + return false; } } diff --git a/src/main/java/subway/controller/ManageController.java b/src/main/java/subway/controller/ManageController.java index 9299a8f39..8e9672384 100644 --- a/src/main/java/subway/controller/ManageController.java +++ b/src/main/java/subway/controller/ManageController.java @@ -16,15 +16,17 @@ public abstract class ManageController implements Controller, Constants { protected static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - public void work(final Controller controller, final String target) { + public boolean work(final Controller controller, final String target) { ask.WhatToManage(target); ask.Function(); try { String command = br.readLine(); sendRequest(controller, command); + return true; } catch (Exception e) { subwayException.unexpected(); } + return false; } // 등록하기 diff --git a/src/main/java/subway/controller/MapController.java b/src/main/java/subway/controller/MapController.java index 9ac1cf62c..4a23a561c 100644 --- a/src/main/java/subway/controller/MapController.java +++ b/src/main/java/subway/controller/MapController.java @@ -8,8 +8,9 @@ public class MapController { MapController(InitManager manager){ lineManager = manager.getLineManager(); } - public void work(){ + public boolean work(){ printMap(); + return true; } public void printMap(){ System.out.println(lineManager.getStationLines()); From e5d9f0744c850c9ea1c7b3c6e181076b8e9c01e0 Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 15 May 2024 12:02:37 +0900 Subject: [PATCH 062/104] =?UTF-8?q?refactor:=20View=20=EB=82=B4=EB=B6=80?= =?UTF-8?q?=20=EC=9E=85=EB=A0=A5=20=EB=B0=9B=EB=8A=94=20=ED=95=A8=EC=88=98?= =?UTF-8?q?=20=EB=AA=85=20=EC=98=A4=EB=B2=84=EB=A1=9C=EB=94=A9=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/controller/LineController.java | 10 ++++++---- .../java/subway/controller/SectionController.java | 12 ++++++------ .../java/subway/controller/StationController.java | 4 ++-- src/main/java/subway/view/AskView.java | 4 ++-- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 5eda2dc38..1eb8f6f3f 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -16,15 +16,15 @@ public LineController(final InitManager manager){ public void register() { String command = REGISTER; // 새로운 노선 db 와 연동하여 생성하기 - ask.Name(command, LINE); + ask.orderWhere(command, LINE); try{ String line = br.readLine(); boolean result = lineManager.isValid(line); if(result!= true) return ; - ask.Name(command, UPPER); + ask.orderWhere(command, UPPER); String upper = br.readLine(); - ask.Name(command, BOTTOM); + ask.orderWhere(command, BOTTOM); String bottom = br.readLine(); lineManager.setStations(line, upper, bottom); @@ -35,6 +35,8 @@ public void register() { } } + + // view 로 보내도 ㄱㅊ할듯? public void infoMessage(final String work, final boolean result){ StringBuilder sb = new StringBuilder(); sb.append("노선이 "); @@ -52,7 +54,7 @@ public void infoMessage(final String work, final boolean result){ @Override public void delete() { // 노선 db 에서 삭제 - ask.Name(DELETE, LINE); + ask.orderWhere(DELETE, LINE); try{ String command = br.readLine(); boolean result = lineManager.delete(command); diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index eb5b9d4e7..db95f79b3 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -38,7 +38,7 @@ public void register() { // 노선, 역 이름, 순서 입력 받고 등록 return; } - ask.orderWhat("역 이름"); + ask.orderWhere("역 이름"); String station = getStation(); ask.orderWhere(); @@ -48,8 +48,8 @@ public void register() { // 노선, 역 이름, 순서 입력 받고 등록 response.printInfo("구간이 등록되었습니다."); } - public String getLine() { - ask.orderWhat(LINE); + public String getLine() { // util 로 뺄까? + ask.orderWhere(LINE); try { String line = br.readLine(); return line; @@ -69,7 +69,7 @@ public String getStation() { return null; } - public int getIndex() { + public int getIndex() { // util? try { String input = br.readLine(); if(subwayException.isBack(input) == true) return -1; @@ -84,10 +84,10 @@ public int getIndex() { @Override public void delete() { try{ - ask.Name(DELETE, "구간의 노선"); + ask.orderWhere(DELETE, "구간의 노선"); String line = br.readLine(); - ask.Name(DELETE, "구간의 역"); + ask.orderWhere(DELETE, "구간의 역"); String station = br.readLine(); if(sectionManager.isValid(line)){ diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 78deb8c08..8ef4225f5 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -17,7 +17,7 @@ public class StationController extends ManageController{ @Override public void register(){ - ask.Name(REGISTER, Targets.STATION.getTarget()); + ask.orderWhere(REGISTER, Targets.STATION.getTarget()); try{ String station = br.readLine(); boolean result = stationManager.isValid(station); @@ -32,7 +32,7 @@ public void register(){ @Override public void delete(){ // db 접근해서 데이터 삭제하는 서비스와 연결 - ask.Name(DELETE, STATION); + ask.orderWhere(DELETE, STATION); try{ String command = br.readLine(); boolean result = stationManager.delete(command); diff --git a/src/main/java/subway/view/AskView.java b/src/main/java/subway/view/AskView.java index 665073bf5..cd2381cb1 100644 --- a/src/main/java/subway/view/AskView.java +++ b/src/main/java/subway/view/AskView.java @@ -22,13 +22,13 @@ public void Function() { } //## 등록/삭제할 역/노선을 입력하세요 - public void Name(final String work, final String name) { + public void orderWhere(final String work, final String name) { String sentence = makeString.askName(work, name); System.out.println(sentence); } // 역/노선을 입력하세요 - public void orderWhat(final String name) { + public void orderWhere(final String name) { String sentence = makeString.askName(name); System.out.println(sentence); } From d95692e0829c1ed4affc46b039046dc996940d66 Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 15 May 2024 12:04:04 +0900 Subject: [PATCH 063/104] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=20=ED=98=B8=EC=B6=9C=20=EB=B0=A9=EC=8B=9D=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=83=81=EC=88=98=20=ED=98=B8=EC=B6=9C=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/subway/controller/Constants.java | 8 +++++-- .../subway/controller/LineController.java | 9 ++++---- .../subway/controller/MainController.java | 21 +++++++------------ .../subway/controller/StationController.java | 2 +- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/main/java/subway/controller/Constants.java b/src/main/java/subway/controller/Constants.java index 81007ff95..c7c778c61 100644 --- a/src/main/java/subway/controller/Constants.java +++ b/src/main/java/subway/controller/Constants.java @@ -11,11 +11,15 @@ public interface Constants { static final String REGISTER = Methods.등록.toString(); static final String DELETE = Methods.삭제.toString(); - static final String READ = Methods.조회.toString(); - static final String BACK = Methods.돌아가기.toString(); static final String STATION = Targets.STATION.getTarget(); static final String LINE = Targets.LINE.getTarget(); static final String UPPER = Targets.UPPER.getTarget(); static final String BOTTOM = Targets.BOTTOM.getTarget(); + static final String SECTION = Targets.SECTION.getTarget(); + + static final int STATION_COMMAND = Targets.STATION.getCommand(); + static final int LINE_COMMAND = Targets.LINE.getCommand(); + static final int SECTION_COMMAND = Targets.SECTION.getCommand(); + static final int MAP_COMMAND = Targets.MAP.getCommand(); } diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 1eb8f6f3f..867d28c84 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -14,21 +14,20 @@ public LineController(final InitManager manager){ @Override public void register() { - String command = REGISTER; // 새로운 노선 db 와 연동하여 생성하기 - ask.orderWhere(command, LINE); + ask.orderWhere(REGISTER, LINE); try{ String line = br.readLine(); boolean result = lineManager.isValid(line); if(result!= true) return ; - ask.orderWhere(command, UPPER); + ask.orderWhere(REGISTER, UPPER); String upper = br.readLine(); - ask.orderWhere(command, BOTTOM); + ask.orderWhere(REGISTER, BOTTOM); String bottom = br.readLine(); lineManager.setStations(line, upper, bottom); - infoMessage(command, result); + infoMessage(REGISTER, result); }catch (Exception e){ e.printStackTrace(); subwayException.unexpected(); diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 9fb96d3fd..a4f919ac8 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -1,6 +1,5 @@ package subway.controller; -import subway.config.constants.views.Targets; import subway.config.handler.SubwayException; import subway.service.InitManager; import subway.view.AskView; @@ -8,17 +7,13 @@ import java.io.BufferedReader; import java.io.InputStreamReader; -public class MainController { +public class MainController implements Constants{ private AskView ask; private StationController stationController; private LineController lineController; private SectionController sectionController; private MapController mapController; private static SubwayException subwayException; - private static Targets LINE = Targets.LINE; - private static Targets STATION = Targets.STATION; - private static Targets SECTION = Targets.SECTION; - private static Targets MAP = Targets.MAP; private static int command; @@ -64,16 +59,16 @@ public void nextStep(String node) { } public boolean CoreController(int command) { - if (command == STATION.getCommand()) { - return stationController.work(stationController, STATION.getTarget()); + if (command == STATION_COMMAND) { + return stationController.work(stationController, STATION); } - if (command == LINE.getCommand()) { - return lineController.work(lineController, LINE.getTarget()); + if (command == LINE_COMMAND) { + return lineController.work(lineController, LINE); } - if (command == SECTION.getCommand()) { - return sectionController.work(sectionController, SECTION.getTarget()); + if (command == SECTION_COMMAND) { + return sectionController.work(sectionController, SECTION); } - if (command == MAP.getCommand()) { + if (command == MAP_COMMAND) { return mapController.work(); } System.err.println("[ERROR] 유효한 범위 내의 명령어를 입력해주세요."); diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 8ef4225f5..c254b3fd9 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -17,7 +17,7 @@ public class StationController extends ManageController{ @Override public void register(){ - ask.orderWhere(REGISTER, Targets.STATION.getTarget()); + ask.orderWhere(REGISTER, STATION); try{ String station = br.readLine(); boolean result = stationManager.isValid(station); From 9d1fec4052ce0551c6a0dbc058e25cacfc1ef6bf Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 15 May 2024 12:45:41 +0900 Subject: [PATCH 064/104] =?UTF-8?q?refactor:=20Line=20Controller=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/subway/controller/Controller.java | 2 +- .../subway/controller/LineController.java | 69 ++++++++++++------- .../subway/controller/SectionController.java | 5 +- .../subway/controller/StationController.java | 1 - src/main/java/subway/service/LineManager.java | 17 +++-- 5 files changed, 59 insertions(+), 35 deletions(-) diff --git a/src/main/java/subway/controller/Controller.java b/src/main/java/subway/controller/Controller.java index 21645e73d..6f43834de 100644 --- a/src/main/java/subway/controller/Controller.java +++ b/src/main/java/subway/controller/Controller.java @@ -1,6 +1,6 @@ package subway.controller; -import subway.config.constants.views.Methods; +import subway.config.constants.views.Targets; public interface Controller { public void register(); diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 867d28c84..64c38f8d2 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -4,10 +4,11 @@ import subway.service.InitManager; import subway.service.LineManager; -public class LineController extends ManageController{ +public class LineController extends ManageController { static LineManager lineManager; private static SubwayException subwayException; - public LineController(final InitManager manager){ + + public LineController(final InitManager manager) { lineManager = manager.getLineManager(); subwayException = manager.getSubwayException(); } @@ -15,55 +16,75 @@ public LineController(final InitManager manager){ @Override public void register() { // 새로운 노선 db 와 연동하여 생성하기 - ask.orderWhere(REGISTER, LINE); - try{ - String line = br.readLine(); - boolean result = lineManager.isValid(line); - if(result!= true) return ; - - ask.orderWhere(REGISTER, UPPER); - String upper = br.readLine(); - ask.orderWhere(REGISTER, BOTTOM); - String bottom = br.readLine(); - + try { + String line = getLineName(); + if (lineManager.isValid(line) != true) return; // 에러 커스텀 + String upper = getSubStation(REGISTER, UPPER); + String bottom = getSubStation(REGISTER, BOTTOM); + if(upper == null || bottom == null) return; // ERROR 커스텀해서 적용해야겟다 lineManager.setStations(line, upper, bottom); - infoMessage(REGISTER, result); - }catch (Exception e){ + } catch (Exception e) { + infoMessage(REGISTER, false); e.printStackTrace(); subwayException.unexpected(); } + infoMessage(REGISTER, true); + } + + public String getLineName() { + try { + ask.orderWhere(REGISTER, LINE); + return br.readLine(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public String getSubStation(String function, String station) { + try { + ask.orderWhere(function, station); + String node = br.readLine(); + if(lineManager.isValid(node)!= true){ // 에러 커스텀 + System.err.println("[ERROR] 존재하지 않는 역입니다."); + return null; + } + return node; + } catch (Exception e) { + e.printStackTrace(); + } + return null; } - // view 로 보내도 ㄱㅊ할듯? - public void infoMessage(final String work, final boolean result){ + // view 로 보내도 ㄱㅊ할듯? // TODO: MakeString.java 에 비슷한 코드있음 확인 요망 + public void infoMessage(final String work, final boolean result) { StringBuilder sb = new StringBuilder(); sb.append("노선이 "); - if(result == true){ + if (result == true) { sb.append(work).append("되었습니다."); } - if(result == false){ + if (result == false) { sb.append(work).append("되지 않았습니다."); } String message = sb.toString(); response.printInfo(message); - sb.setLength(0); } @Override public void delete() { // 노선 db 에서 삭제 ask.orderWhere(DELETE, LINE); - try{ - String command = br.readLine(); + try { + String command = br.readLine();// 존재하지 않는 노선을 받았을 때 예외처리 커스텀 boolean result = lineManager.delete(command); infoMessage(DELETE, result); - }catch (Exception e){ + } catch (Exception e) { subwayException.unexpected(); } } - public void read(){ + public void read() { response.printTitle("지하철 노선도"); StringBuilder list = lineManager.read(); response.printList(list); diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index db95f79b3..3daf45753 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -38,10 +38,7 @@ public void register() { // 노선, 역 이름, 순서 입력 받고 등록 return; } - ask.orderWhere("역 이름"); String station = getStation(); - - ask.orderWhere(); int index = getIndex(); sectionManager.insertSection(station, index-1); @@ -61,6 +58,7 @@ public String getLine() { // util 로 뺄까? } public String getStation() { + ask.orderWhere("역 이름"); try { return br.readLine(); } catch (Exception e) { @@ -70,6 +68,7 @@ public String getStation() { } public int getIndex() { // util? + ask.orderWhere(); try { String input = br.readLine(); if(subwayException.isBack(input) == true) return -1; diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index c254b3fd9..62be8e5f0 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -1,6 +1,5 @@ package subway.controller; -import subway.config.constants.views.Targets; import subway.config.handler.SubwayException; import subway.service.InitManager; import subway.service.StationManager; diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index 9a6fe623e..6841e5e54 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -14,21 +14,26 @@ public class LineManager implements Managerable { SubwayException subwayException = new SubwayException(); @Override public boolean isValid(final String name) { - if(subwayException.isBack(name) == true){ + if(subwayException.isBack(name)){ return false; } - if(lineRepo.getLineByName(name) == null){ - subwayException.noStation(); + if(lineRepo.getLineByName(name) != null){ + subwayException.areadyCreated(); } - lineRepo.addLine(new Line(name)); return true; } + public Line setStation(final String name){ + Line node = new Line(name); + lineRepo.addLine(node); + return node; + } + public void setStations(final String name, final String upperStation, final String bottomStation){ - Line line = lineRepo.getLineByName(name); + Line line = setStation(name); Station upper = stationManager.getByName(upperStation); Station bottom = stationManager.getByName(bottomStation); - +// 존재하지 않는 역일 때 예외 처리 line.setStations(upper, bottom); } From 54daf249ab93a992225e449a070b764c6eddad95 Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 15 May 2024 13:18:08 +0900 Subject: [PATCH 065/104] =?UTF-8?q?refactor:=20=EB=B0=98=ED=99=98=20?= =?UTF-8?q?=EC=9E=90=EB=A3=8C=ED=98=95=20void=20-=20boolean=20=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD,=20=EC=84=B1=EA=B3=B5=20?= =?UTF-8?q?=EC=97=AC=EB=B6=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/subway/controller/Controller.java | 6 +++--- .../subway/controller/LineController.java | 14 ++++++++----- .../subway/controller/ManageController.java | 14 ++++++------- .../subway/controller/SectionController.java | 21 ++++++++++--------- .../subway/controller/StationController.java | 11 +++++++--- 5 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/main/java/subway/controller/Controller.java b/src/main/java/subway/controller/Controller.java index 6f43834de..faa700a9f 100644 --- a/src/main/java/subway/controller/Controller.java +++ b/src/main/java/subway/controller/Controller.java @@ -3,7 +3,7 @@ import subway.config.constants.views.Targets; public interface Controller { - public void register(); - public void delete(); - public void read(); + public boolean register(); + public boolean delete(); + public boolean read(); } diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 64c38f8d2..6d96378f8 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -14,14 +14,14 @@ public LineController(final InitManager manager) { } @Override - public void register() { + public boolean register() { // 새로운 노선 db 와 연동하여 생성하기 try { String line = getLineName(); - if (lineManager.isValid(line) != true) return; // 에러 커스텀 + if (lineManager.isValid(line) != true) return false; // 에러 커스텀 String upper = getSubStation(REGISTER, UPPER); String bottom = getSubStation(REGISTER, BOTTOM); - if(upper == null || bottom == null) return; // ERROR 커스텀해서 적용해야겟다 + if(upper == null || bottom == null) return false; // ERROR 커스텀해서 적용해야겟다 lineManager.setStations(line, upper, bottom); } catch (Exception e) { infoMessage(REGISTER, false); @@ -29,6 +29,7 @@ public void register() { subwayException.unexpected(); } infoMessage(REGISTER, true); + return true; } public String getLineName() { @@ -72,7 +73,7 @@ public void infoMessage(final String work, final boolean result) { } @Override - public void delete() { + public boolean delete() { // 노선 db 에서 삭제 ask.orderWhere(DELETE, LINE); try { @@ -81,12 +82,15 @@ public void delete() { infoMessage(DELETE, result); } catch (Exception e) { subwayException.unexpected(); + return false; } + return true; } - public void read() { + public boolean read() { response.printTitle("지하철 노선도"); StringBuilder list = lineManager.read(); response.printList(list); + return true; } } diff --git a/src/main/java/subway/controller/ManageController.java b/src/main/java/subway/controller/ManageController.java index 8e9672384..fa9793301 100644 --- a/src/main/java/subway/controller/ManageController.java +++ b/src/main/java/subway/controller/ManageController.java @@ -30,22 +30,20 @@ public boolean work(final Controller controller, final String target) { } // 등록하기 - public void sendRequest(final Controller controller, final String command) { + public boolean sendRequest(final Controller controller, final String command) { if (command.equals(REGISTER_COMMAND)) { - controller.register(); - return; + return controller.register(); } if (command.equals(DELETE_COMMAND)) { - controller.delete(); - return; + return controller.delete(); } if (command.equals(READ_COMMAND)) { - controller.read(); - return; + return controller.read(); } if (command.equals(BACK_COMMAND)) { - return; + return true; } subwayException.notValidCommand(); + return false; } } diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 3daf45753..c3bc54e7a 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -16,26 +16,24 @@ public SectionController(final InitManager manager){ } @Override - public void sendRequest(final Controller controller, final String command){ + public boolean sendRequest(final Controller controller, final String command){ if (command.equals(REGISTER_COMMAND)) { // 등록 - controller.register(); - return; + return controller.register(); } if (command.equals(DELETE_COMMAND)) { // 삭제 - controller.delete(); - return; + return controller.delete(); } if (command.equals(BACK_COMMAND)) { // 되돌아가기 - return; } subwayException.notValidCommand(); + return false; } @Override - public void register() { // 노선, 역 이름, 순서 입력 받고 등록 + public boolean register() { // 노선, 역 이름, 순서 입력 받고 등록 String line = getLine(); if(!sectionManager.isValid(line)){ - return; + return false; } String station = getStation(); @@ -43,6 +41,7 @@ public void register() { // 노선, 역 이름, 순서 입력 받고 등록 sectionManager.insertSection(station, index-1); response.printInfo("구간이 등록되었습니다."); + return true; } public String getLine() { // util 로 뺄까? @@ -81,7 +80,7 @@ public int getIndex() { // util? } @Override - public void delete() { + public boolean delete() { try{ ask.orderWhere(DELETE, "구간의 노선"); String line = br.readLine(); @@ -95,9 +94,11 @@ public void delete() { } } catch (Exception e){ subwayException.unexpected(); + return false; } + return true; } @Override - public void read() {} + public boolean read() {return false;} } diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 62be8e5f0..b90e5d8a0 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -15,21 +15,23 @@ public class StationController extends ManageController{ } @Override - public void register(){ + public boolean register(){ ask.orderWhere(REGISTER, STATION); try{ String station = br.readLine(); boolean result = stationManager.isValid(station); String message = makeString.infoMessage(REGISTER, result); // "지하철 역이 등록되었습니다." 출력 response.printInfo(message); + return false; }catch (Exception e){ e.printStackTrace(); subwayException.unexpected(); } + return true; } @Override - public void delete(){ + public boolean delete(){ // db 접근해서 데이터 삭제하는 서비스와 연결 ask.orderWhere(DELETE, STATION); try{ @@ -39,12 +41,15 @@ public void delete(){ response.printInfo(message); }catch (Exception e){ subwayException.unexpected(); + return false; } + return true; } - public void read(){ + public boolean read(){ response.printTitle("역 목록"); StringBuilder list = stationManager.read(); response.printList(list); + return true; } } From fbda8043441c7e1c128570049558bccb5ded4cc7 Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 15 May 2024 13:32:59 +0900 Subject: [PATCH 066/104] =?UTF-8?q?refactor:=20SectionController=20?= =?UTF-8?q?=EC=A4=91=EC=8B=AC=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/SectionController.java | 52 ++++++++++++------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index c3bc54e7a..b7e760ea7 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -3,11 +3,9 @@ import subway.config.handler.SubwayException; import subway.service.InitManager; import subway.service.SectionManager; -import subway.view.ResponseView; public class SectionController extends ManageController { private static SectionManager sectionManager; - private static ResponseView response = new ResponseView(); static SubwayException subwayException; public SectionController(final InitManager manager){ @@ -24,6 +22,7 @@ public boolean sendRequest(final Controller controller, final String command){ return controller.delete(); } if (command.equals(BACK_COMMAND)) { // 되돌아가기 + return true; } subwayException.notValidCommand(); return false; @@ -44,18 +43,6 @@ public boolean register() { // 노선, 역 이름, 순서 입력 받고 등록 return true; } - public String getLine() { // util 로 뺄까? - ask.orderWhere(LINE); - try { - String line = br.readLine(); - return line; - } catch (Exception e) { - e.printStackTrace(); - subwayException.unexpected(); - } - return null; - } - public String getStation() { ask.orderWhere("역 이름"); try { @@ -82,11 +69,8 @@ public int getIndex() { // util? @Override public boolean delete() { try{ - ask.orderWhere(DELETE, "구간의 노선"); - String line = br.readLine(); - - ask.orderWhere(DELETE, "구간의 역"); - String station = br.readLine(); + String line = getLine(DELETE); + String station = getStation(DELETE, "구간의 역"); if(sectionManager.isValid(line)){ sectionManager.delete(station); @@ -98,7 +82,37 @@ public boolean delete() { } return true; } + public String getLine() { // util 로 뺄까? + ask.orderWhere(LINE); + try { + String line = br.readLine(); + return line; + } catch (Exception e) { + e.printStackTrace(); + subwayException.unexpected(); + } + return null; + } + public String getLine(String Target){ + try{ + ask.orderWhere(Target, "구간의 노선"); + return br.readLine(); + }catch (Exception e){ + e.printStackTrace(); + } + return null; + } + + public String getStation(String function, String target){ + try{ + ask.orderWhere(function, target); + return br.readLine(); + }catch (Exception e){ + e.printStackTrace(); + } + return null; + } @Override public boolean read() {return false;} } From 6effba91e12ea455a448625e1274deb4f433e10e Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 15 May 2024 13:44:02 +0900 Subject: [PATCH 067/104] =?UTF-8?q?refactor:=20=ED=8F=B4=EB=8D=94=20?= =?UTF-8?q?=EA=B5=AC=EC=A1=B0=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=20?= =?UTF-8?q?=EB=B0=8F=20ManageController=20=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EC=84=A0=EC=96=B8=EB=B6=80=EB=B6=84=20=EC=9D=B8=ED=84=B0?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=8A=A4=EB=A1=9C=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/controller/Controller.java | 9 --------- .../java/subway/controller/MainController.java | 3 ++- .../java/subway/controller/ManageController.java | 12 +++--------- .../subway/controller/SectionController.java | 1 + .../subway/controller/{ => utils}/Constants.java | 2 +- .../java/subway/controller/utils/Controller.java | 16 ++++++++++++++++ .../java/subway/controller/utils/Methods.java | 6 ++++++ src/main/java/subway/service/InitManager.java | 2 +- src/main/java/subway/service/LineManager.java | 2 +- .../service/{util => utils}/Constants.java | 2 +- .../service/{util => utils}/LineMakeString.java | 2 +- 11 files changed, 33 insertions(+), 24 deletions(-) delete mode 100644 src/main/java/subway/controller/Controller.java rename src/main/java/subway/controller/{ => utils}/Constants.java (97%) create mode 100644 src/main/java/subway/controller/utils/Controller.java create mode 100644 src/main/java/subway/controller/utils/Methods.java rename src/main/java/subway/service/{util => utils}/Constants.java (95%) rename src/main/java/subway/service/{util => utils}/LineMakeString.java (94%) diff --git a/src/main/java/subway/controller/Controller.java b/src/main/java/subway/controller/Controller.java deleted file mode 100644 index faa700a9f..000000000 --- a/src/main/java/subway/controller/Controller.java +++ /dev/null @@ -1,9 +0,0 @@ -package subway.controller; - -import subway.config.constants.views.Targets; - -public interface Controller { - public boolean register(); - public boolean delete(); - public boolean read(); -} diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index a4f919ac8..395cef509 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -1,13 +1,14 @@ package subway.controller; import subway.config.handler.SubwayException; +import subway.controller.utils.Constants; import subway.service.InitManager; import subway.view.AskView; import java.io.BufferedReader; import java.io.InputStreamReader; -public class MainController implements Constants{ +public class MainController implements Constants { private AskView ask; private StationController stationController; private LineController lineController; diff --git a/src/main/java/subway/controller/ManageController.java b/src/main/java/subway/controller/ManageController.java index fa9793301..2295afa38 100644 --- a/src/main/java/subway/controller/ManageController.java +++ b/src/main/java/subway/controller/ManageController.java @@ -1,8 +1,7 @@ package subway.controller; -import subway.view.AskView; -import subway.view.ResponseView; -import subway.view.util.MakeString; +import subway.controller.utils.Constants; +import subway.controller.utils.Controller; import java.io.BufferedReader; import java.io.InputStreamReader; @@ -10,10 +9,6 @@ import static subway.controller.StationController.subwayException; public abstract class ManageController implements Controller, Constants { - static final AskView ask = new AskView(); - static final ResponseView response = new ResponseView(); - static final MakeString makeString = new MakeString(); - protected static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public boolean work(final Controller controller, final String target) { @@ -21,8 +16,7 @@ public boolean work(final Controller controller, final String target) { ask.Function(); try { String command = br.readLine(); - sendRequest(controller, command); - return true; + return sendRequest(controller, command); } catch (Exception e) { subwayException.unexpected(); } diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index b7e760ea7..0bdabf69d 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -1,6 +1,7 @@ package subway.controller; import subway.config.handler.SubwayException; +import subway.controller.utils.Controller; import subway.service.InitManager; import subway.service.SectionManager; diff --git a/src/main/java/subway/controller/Constants.java b/src/main/java/subway/controller/utils/Constants.java similarity index 97% rename from src/main/java/subway/controller/Constants.java rename to src/main/java/subway/controller/utils/Constants.java index c7c778c61..7b765c754 100644 --- a/src/main/java/subway/controller/Constants.java +++ b/src/main/java/subway/controller/utils/Constants.java @@ -1,4 +1,4 @@ -package subway.controller; +package subway.controller.utils; import subway.config.constants.views.Methods; import subway.config.constants.views.Targets; diff --git a/src/main/java/subway/controller/utils/Controller.java b/src/main/java/subway/controller/utils/Controller.java new file mode 100644 index 000000000..c3f6c31a3 --- /dev/null +++ b/src/main/java/subway/controller/utils/Controller.java @@ -0,0 +1,16 @@ +package subway.controller.utils; + +import subway.view.AskView; +import subway.view.ResponseView; +import subway.view.util.MakeString; + +public interface Controller { + static final Methods method = new Methods(); + static final AskView ask = new AskView(); + static final ResponseView response = new ResponseView(); + static final MakeString makeString = new MakeString(); + + public boolean register(); + public boolean delete(); + public boolean read(); +} diff --git a/src/main/java/subway/controller/utils/Methods.java b/src/main/java/subway/controller/utils/Methods.java new file mode 100644 index 000000000..a3f9872ad --- /dev/null +++ b/src/main/java/subway/controller/utils/Methods.java @@ -0,0 +1,6 @@ +package subway.controller.utils; + +public class Methods { + public Methods(){} + +} diff --git a/src/main/java/subway/service/InitManager.java b/src/main/java/subway/service/InitManager.java index 979f7e338..20b0e6282 100644 --- a/src/main/java/subway/service/InitManager.java +++ b/src/main/java/subway/service/InitManager.java @@ -4,7 +4,7 @@ import subway.config.handler.SubwayException; import subway.domain.Line; import subway.domain.Station; -import subway.service.util.Constants; +import subway.service.utils.Constants; import java.util.List; diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index 6841e5e54..f0f4bfdb1 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -3,7 +3,7 @@ import subway.config.handler.SubwayException; import subway.domain.Line; import subway.domain.Station; -import subway.service.util.LineMakeString; +import subway.service.utils.LineMakeString; import java.util.List; diff --git a/src/main/java/subway/service/util/Constants.java b/src/main/java/subway/service/utils/Constants.java similarity index 95% rename from src/main/java/subway/service/util/Constants.java rename to src/main/java/subway/service/utils/Constants.java index 2dd651270..8b2644267 100644 --- a/src/main/java/subway/service/util/Constants.java +++ b/src/main/java/subway/service/utils/Constants.java @@ -1,4 +1,4 @@ -package subway.service.util; +package subway.service.utils; import subway.config.handler.SubwayException; import subway.domain.LineRepository; diff --git a/src/main/java/subway/service/util/LineMakeString.java b/src/main/java/subway/service/utils/LineMakeString.java similarity index 94% rename from src/main/java/subway/service/util/LineMakeString.java rename to src/main/java/subway/service/utils/LineMakeString.java index 9e42c360a..469d0fd67 100644 --- a/src/main/java/subway/service/util/LineMakeString.java +++ b/src/main/java/subway/service/utils/LineMakeString.java @@ -1,4 +1,4 @@ -package subway.service.util; +package subway.service.utils; import subway.config.constants.views.Prefixes; From 2bfa4d25eb83ec3951e51832ae07a3ffd8b572d8 Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 15 May 2024 14:15:12 +0900 Subject: [PATCH 068/104] =?UTF-8?q?refactor:=20=EB=B0=98=EB=B3=B5=EB=90=98?= =?UTF-8?q?=EB=8A=94=20=20=EB=A9=94=EC=86=8C=EB=93=9C=20=EC=B6=94=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/LineController.java | 33 ++------- .../subway/controller/SectionController.java | 64 ++--------------- .../subway/controller/utils/Constants.java | 7 ++ .../subway/controller/utils/Controller.java | 3 - .../java/subway/controller/utils/Methods.java | 68 ++++++++++++++++++- src/main/java/subway/domain/Station.java | 1 + 6 files changed, 85 insertions(+), 91 deletions(-) diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 6d96378f8..5385add72 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -15,12 +15,14 @@ public LineController(final InitManager manager) { @Override public boolean register() { + String upper, bottom; // 새로운 노선 db 와 연동하여 생성하기 try { - String line = getLineName(); + String line = method.getLine(REGISTER, LINE); if (lineManager.isValid(line) != true) return false; // 에러 커스텀 - String upper = getSubStation(REGISTER, UPPER); - String bottom = getSubStation(REGISTER, BOTTOM); + + upper = method.getStation(lineManager, REGISTER, UPPER); + bottom = method.getStation(lineManager, REGISTER, BOTTOM); if(upper == null || bottom == null) return false; // ERROR 커스텀해서 적용해야겟다 lineManager.setStations(line, upper, bottom); } catch (Exception e) { @@ -32,31 +34,6 @@ public boolean register() { return true; } - public String getLineName() { - try { - ask.orderWhere(REGISTER, LINE); - return br.readLine(); - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - public String getSubStation(String function, String station) { - try { - ask.orderWhere(function, station); - String node = br.readLine(); - if(lineManager.isValid(node)!= true){ // 에러 커스텀 - System.err.println("[ERROR] 존재하지 않는 역입니다."); - return null; - } - return node; - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - // view 로 보내도 ㄱㅊ할듯? // TODO: MakeString.java 에 비슷한 코드있음 확인 요망 public void infoMessage(final String work, final boolean result) { diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 0bdabf69d..b37d3dc57 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -31,47 +31,24 @@ public boolean sendRequest(final Controller controller, final String command){ @Override public boolean register() { // 노선, 역 이름, 순서 입력 받고 등록 - String line = getLine(); + String line = method.getLine(); if(!sectionManager.isValid(line)){ return false; } - String station = getStation(); - int index = getIndex(); + String station = method.getStation(); + int index = method.getIndex(); sectionManager.insertSection(station, index-1); response.printInfo("구간이 등록되었습니다."); return true; } - public String getStation() { - ask.orderWhere("역 이름"); - try { - return br.readLine(); - } catch (Exception e) { - subwayException.unexpected(); - } - return null; - } - - public int getIndex() { // util? - ask.orderWhere(); - try { - String input = br.readLine(); - if(subwayException.isBack(input) == true) return -1; - int index = Integer.parseInt(input); - return index; - } catch (Exception e) { - subwayException.checkCommand(); - } - return -1; - } - @Override public boolean delete() { try{ - String line = getLine(DELETE); - String station = getStation(DELETE, "구간의 역"); + String line = method.getLine(DELETE, "구간의 노선"); + String station = method.getStation(sectionManager, DELETE, "구간의 역"); if(sectionManager.isValid(line)){ sectionManager.delete(station); @@ -83,37 +60,6 @@ public boolean delete() { } return true; } - public String getLine() { // util 로 뺄까? - ask.orderWhere(LINE); - try { - String line = br.readLine(); - return line; - } catch (Exception e) { - e.printStackTrace(); - subwayException.unexpected(); - } - return null; - } - - public String getLine(String Target){ - try{ - ask.orderWhere(Target, "구간의 노선"); - return br.readLine(); - }catch (Exception e){ - e.printStackTrace(); - } - return null; - } - - public String getStation(String function, String target){ - try{ - ask.orderWhere(function, target); - return br.readLine(); - }catch (Exception e){ - e.printStackTrace(); - } - return null; - } @Override public boolean read() {return false;} } diff --git a/src/main/java/subway/controller/utils/Constants.java b/src/main/java/subway/controller/utils/Constants.java index 7b765c754..4907562ef 100644 --- a/src/main/java/subway/controller/utils/Constants.java +++ b/src/main/java/subway/controller/utils/Constants.java @@ -2,6 +2,9 @@ import subway.config.constants.views.Methods; import subway.config.constants.views.Targets; +import subway.view.AskView; +import subway.view.ResponseView; +import subway.view.util.MakeString; public interface Constants { static final String REGISTER_COMMAND = Methods.등록.getCommand(); @@ -22,4 +25,8 @@ public interface Constants { static final int LINE_COMMAND = Targets.LINE.getCommand(); static final int SECTION_COMMAND = Targets.SECTION.getCommand(); static final int MAP_COMMAND = Targets.MAP.getCommand(); + + static final AskView ask = new AskView(); + static final ResponseView response = new ResponseView(); + static final MakeString makeString = new MakeString(); } diff --git a/src/main/java/subway/controller/utils/Controller.java b/src/main/java/subway/controller/utils/Controller.java index c3f6c31a3..519b293a8 100644 --- a/src/main/java/subway/controller/utils/Controller.java +++ b/src/main/java/subway/controller/utils/Controller.java @@ -6,9 +6,6 @@ public interface Controller { static final Methods method = new Methods(); - static final AskView ask = new AskView(); - static final ResponseView response = new ResponseView(); - static final MakeString makeString = new MakeString(); public boolean register(); public boolean delete(); diff --git a/src/main/java/subway/controller/utils/Methods.java b/src/main/java/subway/controller/utils/Methods.java index a3f9872ad..30dcecca5 100644 --- a/src/main/java/subway/controller/utils/Methods.java +++ b/src/main/java/subway/controller/utils/Methods.java @@ -1,6 +1,72 @@ package subway.controller.utils; -public class Methods { +import subway.service.Managerable; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +import static subway.service.utils.Constants.subwayException; + +public class Methods implements Constants{ + private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public Methods(){} + public int getIndex() { // numberformat error 추가 + ask.orderWhere(); + try { + String input = br.readLine(); + if(subwayException.isBack(input) == true) return -1; + int index = Integer.parseInt(input); + return index; + } catch (Exception e) { + subwayException.checkCommand(); + } + return -1; + } + + public String getLine() { + ask.orderWhere(LINE); + try { + String line = br.readLine(); + return line; + } catch (Exception e) { + e.printStackTrace(); + subwayException.unexpected(); + } + return null; + } + + public String getLine(String function, String target){ + try { + ask.orderWhere(function, target); + return br.readLine(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public String getStation() { + ask.orderWhere("역 이름"); + try { + return br.readLine(); + } catch (Exception e) { + subwayException.unexpected(); + } + return null; + } + public String getStation(Managerable manager, String function, String station) { + try { + ask.orderWhere(function, station); + String node = br.readLine(); + if(manager.isValid(node)!= true){ // 에러 커스텀 + System.err.println("[ERROR] 존재하지 않는 역입니다."); + return null; + } + return node; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } } diff --git a/src/main/java/subway/domain/Station.java b/src/main/java/subway/domain/Station.java index cc8ce99f4..d600cbb52 100644 --- a/src/main/java/subway/domain/Station.java +++ b/src/main/java/subway/domain/Station.java @@ -4,6 +4,7 @@ public class Station { private String name; + //TODO : 노선 관리에서 연결시키기 -> 삭제 관련 기능추가 구현 private List[] line; public Station(final String name) { From 242c1ec4eaaccff12c0091e230ac6649c1c3eab3 Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 15 May 2024 14:25:38 +0900 Subject: [PATCH 069/104] =?UTF-8?q?refactor:=20=EC=A4=91=EB=B3=B5=EB=90=98?= =?UTF-8?q?=EB=8A=94=20=EC=BD=94=EB=93=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/LineController.java | 32 +++++++------------ .../subway/controller/MainController.java | 2 +- .../subway/controller/StationController.java | 4 +-- .../java/subway/view/util/MakeString.java | 4 +-- 4 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 5385add72..1c5e81c13 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -15,7 +15,7 @@ public LineController(final InitManager manager) { @Override public boolean register() { - String upper, bottom; + String upper, bottom, message; // 새로운 노선 db 와 연동하여 생성하기 try { String line = method.getLine(REGISTER, LINE); @@ -26,41 +26,33 @@ public boolean register() { if(upper == null || bottom == null) return false; // ERROR 커스텀해서 적용해야겟다 lineManager.setStations(line, upper, bottom); } catch (Exception e) { - infoMessage(REGISTER, false); + message= makeString.infoMessage(REGISTER, LINE,false); + System.out.println(message); e.printStackTrace(); subwayException.unexpected(); } - infoMessage(REGISTER, true); + message= makeString.infoMessage(REGISTER, LINE, true); + System.out.println(message); return true; } - - // view 로 보내도 ㄱㅊ할듯? // TODO: MakeString.java 에 비슷한 코드있음 확인 요망 - public void infoMessage(final String work, final boolean result) { - StringBuilder sb = new StringBuilder(); - sb.append("노선이 "); - if (result == true) { - sb.append(work).append("되었습니다."); - } - if (result == false) { - sb.append(work).append("되지 않았습니다."); - } - String message = sb.toString(); - response.printInfo(message); - } - @Override public boolean delete() { + String message; + boolean result=false; // 노선 db 에서 삭제 ask.orderWhere(DELETE, LINE); try { String command = br.readLine();// 존재하지 않는 노선을 받았을 때 예외처리 커스텀 - boolean result = lineManager.delete(command); - infoMessage(DELETE, result); + result = lineManager.delete(command); + message=makeString.infoMessage(DELETE,LINE, result); } catch (Exception e) { + message=makeString.infoMessage(DELETE,LINE, result); + System.out.println(message); subwayException.unexpected(); return false; } + System.out.println(message); return true; } diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 395cef509..3f6207c36 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -43,7 +43,7 @@ public void headController() throws Exception { } public boolean isEnd(String node) { - if (node.equals("Q")) { + if (node.equals("Q")|| node.equals("q")) { System.out.println(" 안녕히 가세요. "); return true; } diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index b90e5d8a0..eee5e4e87 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -20,7 +20,7 @@ public boolean register(){ try{ String station = br.readLine(); boolean result = stationManager.isValid(station); - String message = makeString.infoMessage(REGISTER, result); // "지하철 역이 등록되었습니다." 출력 + String message = makeString.infoMessage(REGISTER, STATION,result); // "지하철 역이 등록되었습니다." 출력 response.printInfo(message); return false; }catch (Exception e){ @@ -37,7 +37,7 @@ public boolean delete(){ try{ String command = br.readLine(); boolean result = stationManager.delete(command); - String message = makeString.infoMessage(DELETE, result); + String message = makeString.infoMessage(DELETE, STATION, result); response.printInfo(message); }catch (Exception e){ subwayException.unexpected(); diff --git a/src/main/java/subway/view/util/MakeString.java b/src/main/java/subway/view/util/MakeString.java index e4ba99847..afed74ff5 100644 --- a/src/main/java/subway/view/util/MakeString.java +++ b/src/main/java/subway/view/util/MakeString.java @@ -25,9 +25,9 @@ public String showIndex(final String target){ return METHOD.getIndex(target); } - public String infoMessage(final String work, final boolean result){ + public String infoMessage(final String work, final String target, final boolean result){ StringBuilder sb = new StringBuilder(); - sb.append("지하철 역이 "); // 쓸데없이 긴 것 같은데 string 다루는 모듈을 따로 만들까. controller 기능이 잘 안보이는 것 같음 + sb.append("지하철 ").append(target).append("이 "); // 쓸데없이 긴 것 같은데 string 다루는 모듈을 따로 만들까. controller 기능이 잘 안보이는 것 같음 if(result == true){ sb.append(work).append("되었습니다."); } From 11ae6edf8998aa2a00887c9d05d325ea5cb6355d Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 15 May 2024 14:36:02 +0900 Subject: [PATCH 070/104] =?UTF-8?q?doc:=20=EC=A3=BC=EC=84=9D=20=EB=82=B4?= =?UTF-8?q?=EC=9A=A9=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/Application.java | 1 - src/main/java/subway/controller/LineController.java | 7 +++---- src/main/java/subway/controller/MainController.java | 2 +- src/main/java/subway/controller/StationController.java | 3 +-- src/main/java/subway/domain/Station.java | 2 -- src/main/java/subway/service/LineManager.java | 2 +- src/main/java/subway/service/StationManager.java | 3 +-- src/main/java/subway/view/AskView.java | 2 +- src/main/java/subway/view/util/MakeString.java | 2 +- 9 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/main/java/subway/Application.java b/src/main/java/subway/Application.java index bbf40c3bb..4e44bba62 100644 --- a/src/main/java/subway/Application.java +++ b/src/main/java/subway/Application.java @@ -3,7 +3,6 @@ import subway.controller.MainController; public class Application { public static void main(String[] args) throws Exception { - // TODO: 프로그램 구현 MainController main = new MainController(); main.headController(); } diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 1c5e81c13..5b54ab2c7 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -16,14 +16,13 @@ public LineController(final InitManager manager) { @Override public boolean register() { String upper, bottom, message; - // 새로운 노선 db 와 연동하여 생성하기 try { String line = method.getLine(REGISTER, LINE); - if (lineManager.isValid(line) != true) return false; // 에러 커스텀 + if (lineManager.isValid(line) != true) return false; //TODO: 에러 커스텀 upper = method.getStation(lineManager, REGISTER, UPPER); bottom = method.getStation(lineManager, REGISTER, BOTTOM); - if(upper == null || bottom == null) return false; // ERROR 커스텀해서 적용해야겟다 + if(upper == null || bottom == null) return false; // TODO: ERROR 커스텀해서 적용해야겟다 lineManager.setStations(line, upper, bottom); } catch (Exception e) { message= makeString.infoMessage(REGISTER, LINE,false); @@ -43,7 +42,7 @@ public boolean delete() { // 노선 db 에서 삭제 ask.orderWhere(DELETE, LINE); try { - String command = br.readLine();// 존재하지 않는 노선을 받았을 때 예외처리 커스텀 + String command = br.readLine();// TODO: 존재하지 않는 노선을 받았을 때 예외처리 커스텀 result = lineManager.delete(command); message=makeString.infoMessage(DELETE,LINE, result); } catch (Exception e) { diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 3f6207c36..2c9eac6ca 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -72,7 +72,7 @@ public boolean CoreController(int command) { if (command == MAP_COMMAND) { return mapController.work(); } - System.err.println("[ERROR] 유효한 범위 내의 명령어를 입력해주세요."); + System.err.println("[ERROR] 유효한 범위 내의 명령어를 입력해주세요."); // TODO: return false; } diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index eee5e4e87..771017cfd 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -20,7 +20,7 @@ public boolean register(){ try{ String station = br.readLine(); boolean result = stationManager.isValid(station); - String message = makeString.infoMessage(REGISTER, STATION,result); // "지하철 역이 등록되었습니다." 출력 + String message = makeString.infoMessage(REGISTER, STATION,result); response.printInfo(message); return false; }catch (Exception e){ @@ -32,7 +32,6 @@ public boolean register(){ @Override public boolean delete(){ - // db 접근해서 데이터 삭제하는 서비스와 연결 ask.orderWhere(DELETE, STATION); try{ String command = br.readLine(); diff --git a/src/main/java/subway/domain/Station.java b/src/main/java/subway/domain/Station.java index d600cbb52..a2f7a2bdd 100644 --- a/src/main/java/subway/domain/Station.java +++ b/src/main/java/subway/domain/Station.java @@ -14,6 +14,4 @@ public Station(final String name) { public String getName() { return this.name; } - - // 추가 기능 구현 } diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index f0f4bfdb1..0299d3f7e 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -33,7 +33,7 @@ public void setStations(final String name, final String upperStation, final Stri Line line = setStation(name); Station upper = stationManager.getByName(upperStation); Station bottom = stationManager.getByName(bottomStation); -// 존재하지 않는 역일 때 예외 처리 +// TODO: 존재하지 않는 역일 때 예외 처리 line.setStations(upper, bottom); } diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index 49af5a16a..f50e2efbf 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -8,7 +8,7 @@ public class StationManager implements Managerable { @Override // 삽입 public boolean isValid(final String name) { if(stationRepo.getStationByName(name)!=null){ - // 이미 존재하는 역 + // TODO: 이미 존재하는 역 return false; } @@ -22,7 +22,6 @@ public boolean delete(final String name) { @Override public StringBuilder read() { -// 역 조회 StringBuilder sb = new StringBuilder(); for(Station station : stationRepo.stations()){ sb.append("[INFO] " + station.getName()+"\n"); diff --git a/src/main/java/subway/view/AskView.java b/src/main/java/subway/view/AskView.java index cd2381cb1..69adc5e2c 100644 --- a/src/main/java/subway/view/AskView.java +++ b/src/main/java/subway/view/AskView.java @@ -3,7 +3,7 @@ import subway.view.util.Constants; import subway.view.util.MakeString; -public class AskView implements Constants { // view 가 너무 중구난방인 거 같은데 controller, service 처럼 분할할까. +public class AskView implements Constants { // TODO: (고민) view 가 너무 중구난방인 거 같은데 controller, service 처럼 분할할까. static final MakeString makeString = new MakeString(); public void printMain() { diff --git a/src/main/java/subway/view/util/MakeString.java b/src/main/java/subway/view/util/MakeString.java index afed74ff5..e207ac6bd 100644 --- a/src/main/java/subway/view/util/MakeString.java +++ b/src/main/java/subway/view/util/MakeString.java @@ -27,7 +27,7 @@ public String showIndex(final String target){ } public String infoMessage(final String work, final String target, final boolean result){ StringBuilder sb = new StringBuilder(); - sb.append("지하철 ").append(target).append("이 "); // 쓸데없이 긴 것 같은데 string 다루는 모듈을 따로 만들까. controller 기능이 잘 안보이는 것 같음 + sb.append("지하철 ").append(target).append("이 "); if(result == true){ sb.append(work).append("되었습니다."); } From 950825a6344033be604dba12280e66158e4f9305 Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 15 May 2024 18:02:29 +0900 Subject: [PATCH 071/104] =?UTF-8?q?refactor:=20=EC=97=90=EB=9F=AC=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EB=82=B4=EC=9A=A9=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/handler/InputExceptionError.java | 26 ++++++++++++++ .../config/handler/SubwayException.java | 34 +++++++++++++------ .../subway/controller/LineController.java | 16 ++++++--- .../subway/controller/MainController.java | 22 ++++++------ .../subway/controller/SectionController.java | 7 ++-- .../subway/controller/StationController.java | 9 +++-- .../java/subway/controller/utils/Methods.java | 23 ++++++++----- src/main/java/subway/service/LineManager.java | 9 +++-- src/main/java/subway/service/Managerable.java | 2 +- .../java/subway/service/SectionManager.java | 2 +- .../java/subway/service/StationManager.java | 12 ++++--- 11 files changed, 112 insertions(+), 50 deletions(-) create mode 100644 src/main/java/subway/config/handler/InputExceptionError.java diff --git a/src/main/java/subway/config/handler/InputExceptionError.java b/src/main/java/subway/config/handler/InputExceptionError.java new file mode 100644 index 000000000..3bb8ecfdc --- /dev/null +++ b/src/main/java/subway/config/handler/InputExceptionError.java @@ -0,0 +1,26 @@ +package subway.config.handler; + +import subway.config.constants.views.Prefixes; + +public class InputExceptionError extends RuntimeException{ + public enum ErrorMessage{ + PUT_VAILD_VALUE("유효한 범위 내의 명령어를 입력해주세요."), + PUT_ONLY_VAILD_NUMBER("자연수만 입력 가능합니다."), + PUT_UNDER_4_OR_Q_VALUE("4 이하 자연수나 Q만 입력 가능합니다."), + CAN_NOT_CHOICE_THIS_FUNCTION("선택할 수 없는 기능입니다."), + THERE_IS_NO_SUCH_STATION("존재하지 않은 역입니다."), + THERE_IS_NO_SUCH_LINE("존재하지 않은 노선입니다."), + THERE_IS_AREADY_THE_SAME_STATION("이미 생성된 역입니다."), + THERE_IS_AREADY_THE_SAME_LINE("이미 생성된 노선입니다."); + + private final String message; + private final static String prefix=Prefixes.ERROR.getPrefix(); + ErrorMessage(String message){ + this.message = message; + } + + public String getMessage() { + return prefix+message; + } + } +} diff --git a/src/main/java/subway/config/handler/SubwayException.java b/src/main/java/subway/config/handler/SubwayException.java index 46f49ddcb..a06e48c75 100644 --- a/src/main/java/subway/config/handler/SubwayException.java +++ b/src/main/java/subway/config/handler/SubwayException.java @@ -3,16 +3,25 @@ import subway.config.constants.views.Errors; import subway.config.constants.views.Prefixes; -public class SubwayException extends IllegalArgumentException { +import static subway.config.handler.InputExceptionError.ErrorMessage.*; + +public class SubwayException extends RuntimeException { private static String ERROR = Prefixes.ERROR.getPrefix(); - public void notValidCommand() { - throw new IllegalArgumentException(ERROR+ Errors.VALID.getError()); + + public boolean isNotNumber(){ + throw new NumberFormatException(PUT_ONLY_VAILD_NUMBER.getMessage()); } - public void areadyCreated(){ - throw new IllegalArgumentException(ERROR+Errors.ADD_LINE.getError()); + + public boolean isNotUnder4OrQ(){ + throw new IllegalArgumentException(PUT_UNDER_4_OR_Q_VALUE.getMessage()); + } + + public void notValidCommand() { + throw new IllegalArgumentException(PUT_VAILD_VALUE.getMessage()); } + public void unexpected(){ throw new IllegalArgumentException(ERROR+Errors.UNEXPECTED.getError()); } @@ -27,13 +36,18 @@ public void checkCommand() { throw new IllegalArgumentException(ERROR+ Errors.VALID.getError()); } - public void noStation() { - throw new IllegalArgumentException(ERROR+Errors.DEL_STATION.getError()); + public boolean alreadyCreatedLine(){ + throw new IllegalArgumentException(THERE_IS_AREADY_THE_SAME_LINE.getMessage()); } - - public void noLine() { - throw new IllegalArgumentException(ERROR+Errors.DEL_LINE.getError()); + public boolean alreadyCreatedStation(){ + throw new IllegalArgumentException(THERE_IS_AREADY_THE_SAME_STATION.getMessage()); } + public boolean noStation() { + throw new IllegalArgumentException(THERE_IS_NO_SUCH_STATION.getMessage()); + } + public void noLine() { + throw new IllegalArgumentException(THERE_IS_NO_SUCH_LINE.getMessage()); + } } diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 5b54ab2c7..3205aef46 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -4,6 +4,9 @@ import subway.service.InitManager; import subway.service.LineManager; +import java.io.BufferedReader; +import java.io.InputStreamReader; + public class LineController extends ManageController { static LineManager lineManager; private static SubwayException subwayException; @@ -18,11 +21,16 @@ public boolean register() { String upper, bottom, message; try { String line = method.getLine(REGISTER, LINE); - if (lineManager.isValid(line) != true) return false; //TODO: 에러 커스텀 + if (lineManager.isEmpty(line) != true) { + subwayException.alreadyCreatedLine(); + } upper = method.getStation(lineManager, REGISTER, UPPER); bottom = method.getStation(lineManager, REGISTER, BOTTOM); - if(upper == null || bottom == null) return false; // TODO: ERROR 커스텀해서 적용해야겟다 + if(upper == null || bottom == null){ + subwayException.noStation(); + } + lineManager.setStations(line, upper, bottom); } catch (Exception e) { message= makeString.infoMessage(REGISTER, LINE,false); @@ -42,13 +50,13 @@ public boolean delete() { // 노선 db 에서 삭제 ask.orderWhere(DELETE, LINE); try { - String command = br.readLine();// TODO: 존재하지 않는 노선을 받았을 때 예외처리 커스텀 + String command = br.readLine(); result = lineManager.delete(command); message=makeString.infoMessage(DELETE,LINE, result); } catch (Exception e) { message=makeString.infoMessage(DELETE,LINE, result); System.out.println(message); - subwayException.unexpected(); + subwayException.noLine(); return false; } System.out.println(message); diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 2c9eac6ca..8d675b1d2 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -5,8 +5,9 @@ import subway.service.InitManager; import subway.view.AskView; -import java.io.BufferedReader; -import java.io.InputStreamReader; +import java.io.IOException; + +import static subway.controller.ManageController.br; public class MainController implements Constants { private AskView ask; @@ -25,7 +26,7 @@ public MainController() { subwayException = manager.getSubwayException(); } - public void setControllers(InitManager manager){ + public void setControllers(InitManager manager) { stationController = new StationController(manager); lineController = new LineController(manager); sectionController = new SectionController(manager); @@ -35,16 +36,16 @@ public void setControllers(InitManager manager){ public void headController() throws Exception { while (true) { ask.printMain(); - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String node = br.readLine(); if (isEnd(node)) return; nextStep(node); } } - public boolean isEnd(String node) { - if (node.equals("Q")|| node.equals("q")) { + public boolean isEnd(String node) throws IOException { + if (node.equals("Q") || node.equals("q")) { System.out.println(" 안녕히 가세요. "); + br.close(); return true; } return false; @@ -54,8 +55,8 @@ public void nextStep(String node) { try { if (!strToInt(node)) return; CoreController(command); - } catch (Exception e) { - subwayException.unexpected(); + } catch (SubwayException e) { + subwayException.isNotUnder4OrQ(); } } @@ -72,8 +73,7 @@ public boolean CoreController(int command) { if (command == MAP_COMMAND) { return mapController.work(); } - System.err.println("[ERROR] 유효한 범위 내의 명령어를 입력해주세요."); // TODO: - return false; + throw new SubwayException(); } public boolean strToInt(final String node) { @@ -81,7 +81,7 @@ public boolean strToInt(final String node) { command = Integer.parseInt(node); return true; } catch (NumberFormatException e) { - System.err.println("[ERROR] 4 이하의 자연수나 'Q' 만 입력 가능합니다."); + subwayException.isNotUnder4OrQ(); } return false; } diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index b37d3dc57..2c9e2d4e0 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -32,12 +32,15 @@ public boolean sendRequest(final Controller controller, final String command){ @Override public boolean register() { // 노선, 역 이름, 순서 입력 받고 등록 String line = method.getLine(); - if(!sectionManager.isValid(line)){ + if(!sectionManager.isEmpty(line)){ return false; } String station = method.getStation(); int index = method.getIndex(); + if(index == -1){ + return false; + } sectionManager.insertSection(station, index-1); response.printInfo("구간이 등록되었습니다."); @@ -50,7 +53,7 @@ public boolean delete() { String line = method.getLine(DELETE, "구간의 노선"); String station = method.getStation(sectionManager, DELETE, "구간의 역"); - if(sectionManager.isValid(line)){ + if(sectionManager.isEmpty(line)){ sectionManager.delete(station); response.printInfo("구간이 삭제되었습니다."); } diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 771017cfd..1a5abd711 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -4,6 +4,9 @@ import subway.service.InitManager; import subway.service.StationManager; +import java.io.BufferedReader; +import java.io.InputStreamReader; + // 역을 관리하는 컨트롤러 public class StationController extends ManageController{ static StationManager stationManager; @@ -17,9 +20,9 @@ public class StationController extends ManageController{ @Override public boolean register(){ ask.orderWhere(REGISTER, STATION); - try{ + try { String station = br.readLine(); - boolean result = stationManager.isValid(station); + boolean result = stationManager.isEmpty(station); String message = makeString.infoMessage(REGISTER, STATION,result); response.printInfo(message); return false; @@ -33,7 +36,7 @@ public boolean register(){ @Override public boolean delete(){ ask.orderWhere(DELETE, STATION); - try{ + try { String command = br.readLine(); boolean result = stationManager.delete(command); String message = makeString.infoMessage(DELETE, STATION, result); diff --git a/src/main/java/subway/controller/utils/Methods.java b/src/main/java/subway/controller/utils/Methods.java index 30dcecca5..facfc4bc4 100644 --- a/src/main/java/subway/controller/utils/Methods.java +++ b/src/main/java/subway/controller/utils/Methods.java @@ -1,5 +1,6 @@ package subway.controller.utils; +import subway.config.handler.SubwayException; import subway.service.Managerable; import java.io.BufferedReader; @@ -7,16 +8,19 @@ import static subway.service.utils.Constants.subwayException; -public class Methods implements Constants{ - private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - public Methods(){} - public int getIndex() { // numberformat error 추가 +public class Methods implements Constants { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + public Methods() {} + + public int getIndex() { ask.orderWhere(); try { String input = br.readLine(); - if(subwayException.isBack(input) == true) return -1; + if (subwayException.isBack(input) == true) return -1; int index = Integer.parseInt(input); return index; + } catch (SubwayException e) { + e.isNotNumber(); } catch (Exception e) { subwayException.checkCommand(); } @@ -35,7 +39,8 @@ public String getLine() { return null; } - public String getLine(String function, String target){ + public String getLine(String function, String target) { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { ask.orderWhere(function, target); return br.readLine(); @@ -47,6 +52,7 @@ public String getLine(String function, String target){ public String getStation() { ask.orderWhere("역 이름"); + try { return br.readLine(); } catch (Exception e) { @@ -59,9 +65,8 @@ public String getStation(Managerable manager, String function, String station) { try { ask.orderWhere(function, station); String node = br.readLine(); - if(manager.isValid(node)!= true){ // 에러 커스텀 - System.err.println("[ERROR] 존재하지 않는 역입니다."); - return null; + if (manager.isEmpty(node) != true) { + subwayException.noStation(); } return node; } catch (Exception e) { diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index 0299d3f7e..44c518e52 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -13,14 +13,14 @@ public class LineManager implements Managerable { LineMakeString makeString = new LineMakeString(); SubwayException subwayException = new SubwayException(); @Override - public boolean isValid(final String name) { + public boolean isEmpty(final String name) { if(subwayException.isBack(name)){ return false; } - if(lineRepo.getLineByName(name) != null){ - subwayException.areadyCreated(); + if(lineRepo.getLineByName(name) == null){ + return true; } - return true; + return false; } public Line setStation(final String name){ @@ -33,7 +33,6 @@ public void setStations(final String name, final String upperStation, final Stri Line line = setStation(name); Station upper = stationManager.getByName(upperStation); Station bottom = stationManager.getByName(bottomStation); -// TODO: 존재하지 않는 역일 때 예외 처리 line.setStations(upper, bottom); } diff --git a/src/main/java/subway/service/Managerable.java b/src/main/java/subway/service/Managerable.java index 0adec28e6..f336ca05c 100644 --- a/src/main/java/subway/service/Managerable.java +++ b/src/main/java/subway/service/Managerable.java @@ -1,7 +1,7 @@ package subway.service; public interface Managerable { - public boolean isValid(final String command); + public boolean isEmpty(final String command); public boolean delete(final String name); public StringBuilder read(); } diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 72374c728..43e935e36 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -11,7 +11,7 @@ public class SectionManager implements Managerable { private Line line; SubwayException subwayException = new SubwayException(); @Override - public boolean isValid(final String name) { + public boolean isEmpty(final String name) { if(subwayException.isBack(name) == true) return false; diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index f50e2efbf..c00468bbe 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -1,16 +1,16 @@ package subway.service; +import subway.config.handler.SubwayException; import subway.domain.Station; import static subway.service.InitManager.stationRepo; // 역 관련 기능하는 서비스 public class StationManager implements Managerable { @Override // 삽입 - public boolean isValid(final String name) { + public boolean isEmpty(final String name) { if(stationRepo.getStationByName(name)!=null){ // TODO: 이미 존재하는 역 - - return false; + throw new SubwayException(); } stationRepo.addStation(new Station(name)); return true; @@ -30,7 +30,11 @@ public StringBuilder read() { } public Station getByName(final String name){ - isValid(name); + try{ + isEmpty(name); + }catch (SubwayException e){ + e.alreadyCreatedStation(); + } return stationRepo.getStationByName(name); } } From 97c1554fc4d42c0e1c807810b824221d6edd2e40 Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 15 May 2024 18:37:04 +0900 Subject: [PATCH 072/104] =?UTF-8?q?feat:=20=EC=97=AD=EA=B3=BC=20=EB=85=B8?= =?UTF-8?q?=EC=84=A0=EC=9D=B4=20=EC=97=B0=EA=B2=B0=EB=90=98=EC=96=B4=20?= =?UTF-8?q?=EC=9E=88=EC=9D=84=20=EC=8B=9C=20=EC=97=AD=EC=9D=84=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=ED=95=A0=20=EC=88=98=20=EC=97=86=EA=B2=8C=20?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=20=EC=BD=94=EB=93=9C=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/domain/Line.java | 11 +++++++++++ src/main/java/subway/domain/Station.java | 17 ++++++++++++++++- .../java/subway/domain/StationRepository.java | 2 +- src/main/java/subway/service/InitManager.java | 1 + src/main/java/subway/service/LineManager.java | 11 +++++++++-- src/main/java/subway/view/util/MakeString.java | 2 +- 6 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index f42b1ea10..7e9334dbb 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -53,6 +53,17 @@ public boolean deleteStation(final String station) { return false; } + public Object[] getStations(){ + return this.stations.toArray(); + } + public int getSize(){ + return this.stations.size(); + } + + public Station getStation(int index){ + return this.stations.get(index); + } + public Station getUpperStation(){ return this.upperStation; } diff --git a/src/main/java/subway/domain/Station.java b/src/main/java/subway/domain/Station.java index a2f7a2bdd..660c839b1 100644 --- a/src/main/java/subway/domain/Station.java +++ b/src/main/java/subway/domain/Station.java @@ -1,11 +1,13 @@ package subway.domain; +import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; public class Station { private String name; //TODO : 노선 관리에서 연결시키기 -> 삭제 관련 기능추가 구현 - private List[] line; + private List line = new LinkedList(); public Station(final String name) { this.name = name; @@ -14,4 +16,17 @@ public Station(final String name) { public String getName() { return this.name; } + public void addLine(Line lineNode){ + line.add(lineNode); + } + public void deleteLine(Line lineNode){ + line.remove(lineNode); + } + + public boolean hasNoLine(){ + if(line.size()==0){ + return true; + } + return false; + } } diff --git a/src/main/java/subway/domain/StationRepository.java b/src/main/java/subway/domain/StationRepository.java index b98407e88..7a64e2633 100644 --- a/src/main/java/subway/domain/StationRepository.java +++ b/src/main/java/subway/domain/StationRepository.java @@ -17,7 +17,7 @@ public static void addStation(Station station) { } public static boolean deleteStation(final String name) { - return stations.removeIf(station -> Objects.equals(station.getName(), name)); + return stations.removeIf(station -> Objects.equals(station.getName(), name) && station.hasNoLine()); } public static Station getStationByName(final String name){ for(Station station : stations){ diff --git a/src/main/java/subway/service/InitManager.java b/src/main/java/subway/service/InitManager.java index 20b0e6282..02024bd1c 100644 --- a/src/main/java/subway/service/InitManager.java +++ b/src/main/java/subway/service/InitManager.java @@ -60,6 +60,7 @@ public Line initLine(final Lines line){ public Line addStation(Line line , final String value, final int index ){ Station station = stationRepo.getStationByName(value); + station.addLine(line); line.addStation(station, index); return line; } diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index 44c518e52..66c29c1f3 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -23,21 +23,28 @@ public boolean isEmpty(final String name) { return false; } - public Line setStation(final String name){ + public Line setLine(final String name){ Line node = new Line(name); lineRepo.addLine(node); return node; } public void setStations(final String name, final String upperStation, final String bottomStation){ - Line line = setStation(name); + Line line = setLine(name); Station upper = stationManager.getByName(upperStation); Station bottom = stationManager.getByName(bottomStation); + upper.addLine(line); + bottom.addLine(line); line.setStations(upper, bottom); } @Override public boolean delete(final String name) { + Line line = lineRepo.getLineByName(name); + for(int i=0;i Date: Wed, 15 May 2024 18:51:15 +0900 Subject: [PATCH 073/104] =?UTF-8?q?feat:=20=EB=85=B8=EC=84=A0=EC=97=90=20?= =?UTF-8?q?=EC=86=8D=ED=95=9C=20=EC=97=AD=EC=9D=B4=202=EA=B0=9C=20?= =?UTF-8?q?=EC=9D=B4=ED=95=98=EC=9D=BC=20=EB=95=8C=20=EB=85=B8=EC=84=A0?= =?UTF-8?q?=EC=9D=84=20=EC=82=AD=EC=A0=9C=20=ED=95=A0=20=EC=88=98=20?= =?UTF-8?q?=EC=97=86=EA=B2=8C=20=ED=95=98=EB=8A=94=20=20=EC=BD=94=EB=93=9C?= =?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/domain/LineRepository.java | 2 +- src/main/java/subway/domain/Station.java | 12 ++++++++++-- src/main/java/subway/service/InitManager.java | 5 +++++ src/main/java/subway/view/util/MakeString.java | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/subway/domain/LineRepository.java b/src/main/java/subway/domain/LineRepository.java index 7785c0596..727b4ada3 100644 --- a/src/main/java/subway/domain/LineRepository.java +++ b/src/main/java/subway/domain/LineRepository.java @@ -21,7 +21,7 @@ public static void addLine(final Line line, final Station station, final int ind } public static boolean deleteLineByName(final String name) { - return lines.removeIf(line -> Objects.equals(line.getName(), name)); + return lines.removeIf(line -> Objects.equals(line.getName(), name) && line.getSize()>2); } public static Line getLineByName(final String name) { diff --git a/src/main/java/subway/domain/Station.java b/src/main/java/subway/domain/Station.java index 660c839b1..413c6ddaf 100644 --- a/src/main/java/subway/domain/Station.java +++ b/src/main/java/subway/domain/Station.java @@ -6,8 +6,8 @@ public class Station { private String name; - //TODO : 노선 관리에서 연결시키기 -> 삭제 관련 기능추가 구현 private List line = new LinkedList(); + private int canDelete=0; public Station(final String name) { this.name = name; @@ -24,9 +24,17 @@ public void deleteLine(Line lineNode){ } public boolean hasNoLine(){ - if(line.size()==0){ + if(canDelete==0){ return true; } return false; } + + public void canDelete(){ + this.canDelete-=1; + } + + public void neverDelete(){ + this.canDelete+=1; + } } diff --git a/src/main/java/subway/service/InitManager.java b/src/main/java/subway/service/InitManager.java index 02024bd1c..eccbc489c 100644 --- a/src/main/java/subway/service/InitManager.java +++ b/src/main/java/subway/service/InitManager.java @@ -62,6 +62,11 @@ public Line addStation(Line line , final String value, final int index ){ Station station = stationRepo.getStationByName(value); station.addLine(line); line.addStation(station, index); + if(line.getSize()<=2){ + station.neverDelete(); + return line; + } + station.canDelete(); return line; } } diff --git a/src/main/java/subway/view/util/MakeString.java b/src/main/java/subway/view/util/MakeString.java index 8e0ece457..7ecb14a41 100644 --- a/src/main/java/subway/view/util/MakeString.java +++ b/src/main/java/subway/view/util/MakeString.java @@ -32,7 +32,7 @@ public String infoMessage(final String work, final String target, final boolean sb.append(work).append("되었습니다."); } if(result == false){ - sb.append(work).append("되지 않았습니다. 연결된 노선을 확인해주세요."); + sb.append(work).append("되지 않았습니다. 연결된 노선이나 해당 노선에 속한 역을 확인해주세요."); } return sb.toString(); } From 0c3e4edce097e328e045f1526f49f3c6c647e76c Mon Sep 17 00:00:00 2001 From: aristo Date: Wed, 15 May 2024 19:01:22 +0900 Subject: [PATCH 074/104] =?UTF-8?q?refactor:=20=EC=9E=90=EC=9E=98=ED=95=9C?= =?UTF-8?q?=20=EC=97=90=EB=9F=AC=20=EC=B6=9C=EB=A0=A5=20=EB=AC=B8=EA=B5=AC?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/ManageController.java | 4 ++-- .../subway/controller/SectionController.java | 2 +- .../java/subway/controller/utils/Methods.java | 10 +++++----- src/main/java/subway/domain/Line.java | 18 ++---------------- 4 files changed, 10 insertions(+), 24 deletions(-) diff --git a/src/main/java/subway/controller/ManageController.java b/src/main/java/subway/controller/ManageController.java index 2295afa38..3053d8fef 100644 --- a/src/main/java/subway/controller/ManageController.java +++ b/src/main/java/subway/controller/ManageController.java @@ -16,9 +16,9 @@ public boolean work(final Controller controller, final String target) { ask.Function(); try { String command = br.readLine(); - return sendRequest(controller, command); + sendRequest(controller, command); } catch (Exception e) { - subwayException.unexpected(); + subwayException.checkCommand(); } return false; } diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 2c9e2d4e0..8607f4022 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -42,7 +42,7 @@ public boolean register() { // 노선, 역 이름, 순서 입력 받고 등록 return false; } - sectionManager.insertSection(station, index-1); + sectionManager.insertSection(station, index); response.printInfo("구간이 등록되었습니다."); return true; } diff --git a/src/main/java/subway/controller/utils/Methods.java b/src/main/java/subway/controller/utils/Methods.java index facfc4bc4..909e6dacc 100644 --- a/src/main/java/subway/controller/utils/Methods.java +++ b/src/main/java/subway/controller/utils/Methods.java @@ -1,6 +1,7 @@ package subway.controller.utils; import subway.config.handler.SubwayException; +import subway.controller.StationController; import subway.service.Managerable; import java.io.BufferedReader; @@ -33,8 +34,7 @@ public String getLine() { String line = br.readLine(); return line; } catch (Exception e) { - e.printStackTrace(); - subwayException.unexpected(); + subwayException.checkCommand(); } return null; } @@ -45,7 +45,7 @@ public String getLine(String function, String target) { ask.orderWhere(function, target); return br.readLine(); } catch (Exception e) { - e.printStackTrace(); + subwayException.checkCommand(); } return null; } @@ -56,7 +56,7 @@ public String getStation() { try { return br.readLine(); } catch (Exception e) { - subwayException.unexpected(); + subwayException.noStation(); } return null; } @@ -70,7 +70,7 @@ public String getStation(Managerable manager, String function, String station) { } return node; } catch (Exception e) { - e.printStackTrace(); + subwayException.unexpected(); } return null; } diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index 7e9334dbb..f7ebefa14 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -5,8 +5,6 @@ public class Line { private String name; private LinkedList stations = new LinkedList<>(); - private Station upperStation; - private Station bottomStation; public Line(final String name) { this.name = name; @@ -24,19 +22,17 @@ public StringBuilder stationList(){ } public void setStations(final Station upper, final Station bottom) { - this.upperStation = upper; - this.bottomStation = bottom; stations.addFirst(upper); stations.addLast(bottom); } public void addStation(final Station station, final int index) { if(index==0){ - upperStation=station; + stations.addFirst(station); return; } if(index==stations.size()){ - bottomStation = station; + stations.addLast(station); return; } stations.add(index-1, station); @@ -53,9 +49,6 @@ public boolean deleteStation(final String station) { return false; } - public Object[] getStations(){ - return this.stations.toArray(); - } public int getSize(){ return this.stations.size(); } @@ -63,11 +56,4 @@ public int getSize(){ public Station getStation(int index){ return this.stations.get(index); } - - public Station getUpperStation(){ - return this.upperStation; - } - public Station getBottomStation(){ - return this.bottomStation; - } } From bab1d06a6886cc1b83b480815912d45548b227b9 Mon Sep 17 00:00:00 2001 From: aristo Date: Thu, 16 May 2024 17:18:22 +0900 Subject: [PATCH 075/104] =?UTF-8?q?refactor:=20=EA=B0=80=EB=8F=85=EC=84=B1?= =?UTF-8?q?=EC=9D=84=20=EC=9C=84=ED=95=B4=20=ED=95=A8=EC=88=98=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=EA=B5=AC=EC=A1=B0?= =?UTF-8?q?=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/Application.java | 3 +- .../config/constants/{views => }/Targets.java | 2 +- .../subway/controller/LineController.java | 7 +- .../subway/controller/MainController.java | 65 ++++++++----------- .../java/subway/controller/MapController.java | 4 +- .../subway/controller/SectionController.java | 4 +- .../subway/controller/StationController.java | 7 +- .../subway/controller/utils/CheckCommand.java | 45 +++++++++++++ .../subway/controller/utils/Constants.java | 2 +- ...InitManager.java => InitSubwayValues.java} | 4 +- src/main/java/subway/service/LineManager.java | 2 +- .../java/subway/service/SectionManager.java | 4 +- .../java/subway/service/StationManager.java | 2 +- 13 files changed, 91 insertions(+), 60 deletions(-) rename src/main/java/subway/config/constants/{views => }/Targets.java (92%) create mode 100644 src/main/java/subway/controller/utils/CheckCommand.java rename src/main/java/subway/service/{InitManager.java => InitSubwayValues.java} (95%) diff --git a/src/main/java/subway/Application.java b/src/main/java/subway/Application.java index 4e44bba62..7e87708e5 100644 --- a/src/main/java/subway/Application.java +++ b/src/main/java/subway/Application.java @@ -4,6 +4,7 @@ public class Application { public static void main(String[] args) throws Exception { MainController main = new MainController(); - main.headController(); + main.headController(0); + System.out.println(" 안녕히 가세요. "); } } diff --git a/src/main/java/subway/config/constants/views/Targets.java b/src/main/java/subway/config/constants/Targets.java similarity index 92% rename from src/main/java/subway/config/constants/views/Targets.java rename to src/main/java/subway/config/constants/Targets.java index f9c049ee9..338e8e7fd 100644 --- a/src/main/java/subway/config/constants/views/Targets.java +++ b/src/main/java/subway/config/constants/Targets.java @@ -1,4 +1,4 @@ -package subway.config.constants.views; +package subway.config.constants; public enum Targets { STATION("역", 1), diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 3205aef46..932f7330e 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -1,17 +1,14 @@ package subway.controller; import subway.config.handler.SubwayException; -import subway.service.InitManager; +import subway.service.InitSubwayValues; import subway.service.LineManager; -import java.io.BufferedReader; -import java.io.InputStreamReader; - public class LineController extends ManageController { static LineManager lineManager; private static SubwayException subwayException; - public LineController(final InitManager manager) { + public LineController(final InitSubwayValues manager) { lineManager = manager.getLineManager(); subwayException = manager.getSubwayException(); } diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 8d675b1d2..7b6850b58 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -1,14 +1,11 @@ package subway.controller; import subway.config.handler.SubwayException; +import subway.controller.utils.CheckCommand; import subway.controller.utils.Constants; -import subway.service.InitManager; +import subway.service.InitSubwayValues; import subway.view.AskView; -import java.io.IOException; - -import static subway.controller.ManageController.br; - public class MainController implements Constants { private AskView ask; private StationController stationController; @@ -16,51 +13,55 @@ public class MainController implements Constants { private SectionController sectionController; private MapController mapController; private static SubwayException subwayException; - private static int command; + private CheckCommand isValidCommand = new CheckCommand(); public MainController() { ask = new AskView(); - InitManager manager = new InitManager(); - setControllers(manager); + InitSubwayValues manager = new InitSubwayValues(); + prepareControl(manager); subwayException = manager.getSubwayException(); } - public void setControllers(InitManager manager) { + public void prepareControl(InitSubwayValues manager) { stationController = new StationController(manager); lineController = new LineController(manager); sectionController = new SectionController(manager); mapController = new MapController(manager); } - public void headController() throws Exception { - while (true) { + public void headController(int tryCount) { + String node; + if (tryCount == 3) return; + do { ask.printMain(); - String node = br.readLine(); - if (isEnd(node)) return; - nextStep(node); - } + node = isValidCommand.getCommand(); + } while (isValidCommand.isMainCommand(node) && serviceOn(node)); + if (!isValidCommand.isQ(node)) reService(tryCount); } - public boolean isEnd(String node) throws IOException { - if (node.equals("Q") || node.equals("q")) { - System.out.println(" 안녕히 가세요. "); - br.close(); + + public boolean serviceOn(String node) { + int command = Integer.parseInt(node); + try { + commandMapping(command); return true; + } catch (SubwayException e) { + subwayException.isNotUnder4OrQ(); // 에러 메시지 변경하기, while 조건문에서 검증 마침 } return false; } - public void nextStep(String node) { - try { - if (!strToInt(node)) return; - CoreController(command); - } catch (SubwayException e) { - subwayException.isNotUnder4OrQ(); - } + public void reService(int tryCount) { + StringBuilder sb = new StringBuilder(); + sb.append("오류가 발생했습니다. 다시 시도해주세요.") // view 랑 연결해도 될 것 같은데 + .append("\n남은 시도 횟수 : ") + .append(2-tryCount).append("회"); + System.out.println(sb); + headController(tryCount+1); } - public boolean CoreController(int command) { + public boolean commandMapping(int command) { if (command == STATION_COMMAND) { return stationController.work(stationController, STATION); } @@ -73,16 +74,6 @@ public boolean CoreController(int command) { if (command == MAP_COMMAND) { return mapController.work(); } - throw new SubwayException(); - } - - public boolean strToInt(final String node) { - try { - command = Integer.parseInt(node); - return true; - } catch (NumberFormatException e) { - subwayException.isNotUnder4OrQ(); - } return false; } } diff --git a/src/main/java/subway/controller/MapController.java b/src/main/java/subway/controller/MapController.java index 4a23a561c..576ec930f 100644 --- a/src/main/java/subway/controller/MapController.java +++ b/src/main/java/subway/controller/MapController.java @@ -1,11 +1,11 @@ package subway.controller; -import subway.service.InitManager; +import subway.service.InitSubwayValues; import subway.service.LineManager; public class MapController { static LineManager lineManager; - MapController(InitManager manager){ + MapController(InitSubwayValues manager){ lineManager = manager.getLineManager(); } public boolean work(){ diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 8607f4022..808d94fb8 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -2,14 +2,14 @@ import subway.config.handler.SubwayException; import subway.controller.utils.Controller; -import subway.service.InitManager; +import subway.service.InitSubwayValues; import subway.service.SectionManager; public class SectionController extends ManageController { private static SectionManager sectionManager; static SubwayException subwayException; - public SectionController(final InitManager manager){ + public SectionController(final InitSubwayValues manager){ sectionManager = manager.getSectionManager(); subwayException = manager.getSubwayException(); } diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 1a5abd711..4a890c3c9 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -1,18 +1,15 @@ package subway.controller; import subway.config.handler.SubwayException; -import subway.service.InitManager; +import subway.service.InitSubwayValues; import subway.service.StationManager; -import java.io.BufferedReader; -import java.io.InputStreamReader; - // 역을 관리하는 컨트롤러 public class StationController extends ManageController{ static StationManager stationManager; static SubwayException subwayException; - StationController(final InitManager manager){ + StationController(final InitSubwayValues manager){ stationManager = manager.getStationManager(); subwayException = manager.getSubwayException(); } diff --git a/src/main/java/subway/controller/utils/CheckCommand.java b/src/main/java/subway/controller/utils/CheckCommand.java new file mode 100644 index 000000000..ad14394d7 --- /dev/null +++ b/src/main/java/subway/controller/utils/CheckCommand.java @@ -0,0 +1,45 @@ +package subway.controller.utils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +import static subway.service.utils.Constants.subwayException; + +public class CheckCommand { + public boolean isQ(String node) { + if (node.equals("Q") || node.equals("q")) { + return true; + } + return false; + } + + public boolean isMainCommand(String node){ + int command = strToInt(node); + if(command == 0) + return false; + if(command < 0 || command > 4) + return false; + return true; + } + + public int strToInt(final String node) { + try { + return Integer.parseInt(node); + } catch (NumberFormatException e) { + subwayException.isNotUnder4OrQ(); + } + return 0; + } + + public String getCommand(){ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + try{ + String command = br.readLine(); + return command; + }catch (IOException e){ + System.err.println("입력 오류 발생"); + return null; + } + } +} diff --git a/src/main/java/subway/controller/utils/Constants.java b/src/main/java/subway/controller/utils/Constants.java index 4907562ef..11feb8664 100644 --- a/src/main/java/subway/controller/utils/Constants.java +++ b/src/main/java/subway/controller/utils/Constants.java @@ -1,7 +1,7 @@ package subway.controller.utils; import subway.config.constants.views.Methods; -import subway.config.constants.views.Targets; +import subway.config.constants.Targets; import subway.view.AskView; import subway.view.ResponseView; import subway.view.util.MakeString; diff --git a/src/main/java/subway/service/InitManager.java b/src/main/java/subway/service/InitSubwayValues.java similarity index 95% rename from src/main/java/subway/service/InitManager.java rename to src/main/java/subway/service/InitSubwayValues.java index eccbc489c..f4ec022c8 100644 --- a/src/main/java/subway/service/InitManager.java +++ b/src/main/java/subway/service/InitSubwayValues.java @@ -9,7 +9,7 @@ import java.util.List; -public class InitManager implements Constants { +public class InitSubwayValues implements Constants { public LineManager getLineManager(){ return lineManager; @@ -24,7 +24,7 @@ public static SectionManager getSectionManager(){ } public static SubwayException getSubwayException(){return subwayException;} - public InitManager(){ + public InitSubwayValues(){ // 초기값 삽입 addStations(); addLines(); diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index 66c29c1f3..bd4977eba 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -7,7 +7,7 @@ import java.util.List; -import static subway.service.InitManager.*; +import static subway.service.InitSubwayValues.*; public class LineManager implements Managerable { LineMakeString makeString = new LineMakeString(); diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 43e935e36..089babf25 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -4,8 +4,8 @@ import subway.domain.Line; import subway.domain.Station; -import static subway.service.InitManager.lineRepo; -import static subway.service.InitManager.stationRepo; +import static subway.service.InitSubwayValues.lineRepo; +import static subway.service.InitSubwayValues.stationRepo; public class SectionManager implements Managerable { private Line line; diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index c00468bbe..35220ee0d 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -2,7 +2,7 @@ import subway.config.handler.SubwayException; import subway.domain.Station; -import static subway.service.InitManager.stationRepo; +import static subway.service.InitSubwayValues.stationRepo; // 역 관련 기능하는 서비스 public class StationManager implements Managerable { From f91e001d1fa49cf4d63779ac805d9e05b879d67c Mon Sep 17 00:00:00 2001 From: aristo Date: Thu, 16 May 2024 17:36:00 +0900 Subject: [PATCH 076/104] =?UTF-8?q?refactor:=20=EC=B6=94=EC=83=81=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=EC=97=90=EC=84=9C=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EB=B6=84=EB=A6=AC=EC=99=80=20MainController=20?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=EA=B4=80=EB=A6=AC=20=EB=8C=80=EC=83=81=20?= =?UTF-8?q?=EB=A7=A4=ED=95=91=EC=9D=B4=20=EA=B5=AC=EB=B6=84=EB=90=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...{ManageController.java => ClassifyMethods.java} | 2 +- .../java/subway/controller/LineController.java | 2 +- .../java/subway/controller/MainController.java | 14 +++++++------- .../java/subway/controller/SectionController.java | 2 +- .../java/subway/controller/StationController.java | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) rename src/main/java/subway/controller/{ManageController.java => ClassifyMethods.java} (94%) diff --git a/src/main/java/subway/controller/ManageController.java b/src/main/java/subway/controller/ClassifyMethods.java similarity index 94% rename from src/main/java/subway/controller/ManageController.java rename to src/main/java/subway/controller/ClassifyMethods.java index 3053d8fef..17269b1f5 100644 --- a/src/main/java/subway/controller/ManageController.java +++ b/src/main/java/subway/controller/ClassifyMethods.java @@ -8,7 +8,7 @@ import static subway.controller.StationController.subwayException; -public abstract class ManageController implements Controller, Constants { +public abstract class ClassifyMethods implements Controller, Constants { protected static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public boolean work(final Controller controller, final String target) { diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/LineController.java index 932f7330e..cfaf6d071 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/LineController.java @@ -4,7 +4,7 @@ import subway.service.InitSubwayValues; import subway.service.LineManager; -public class LineController extends ManageController { +public class LineController extends ClassifyMethods { static LineManager lineManager; private static SubwayException subwayException; diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 7b6850b58..1f6ec0506 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -19,11 +19,11 @@ public class MainController implements Constants { public MainController() { ask = new AskView(); InitSubwayValues manager = new InitSubwayValues(); - prepareControl(manager); + prepareServer(manager); subwayException = manager.getSubwayException(); } - public void prepareControl(InitSubwayValues manager) { + public void prepareServer(InitSubwayValues manager) { stationController = new StationController(manager); lineController = new LineController(manager); sectionController = new SectionController(manager); @@ -61,17 +61,17 @@ public void reService(int tryCount) { headController(tryCount+1); } - public boolean commandMapping(int command) { - if (command == STATION_COMMAND) { + public boolean commandMapping(int target) { + if (target == STATION_COMMAND) { return stationController.work(stationController, STATION); } - if (command == LINE_COMMAND) { + if (target == LINE_COMMAND) { return lineController.work(lineController, LINE); } - if (command == SECTION_COMMAND) { + if (target == SECTION_COMMAND) { return sectionController.work(sectionController, SECTION); } - if (command == MAP_COMMAND) { + if (target == MAP_COMMAND) { return mapController.work(); } return false; diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/SectionController.java index 808d94fb8..e11e25af9 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/SectionController.java @@ -5,7 +5,7 @@ import subway.service.InitSubwayValues; import subway.service.SectionManager; -public class SectionController extends ManageController { +public class SectionController extends ClassifyMethods { private static SectionManager sectionManager; static SubwayException subwayException; diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/StationController.java index 4a890c3c9..f9cd223b0 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/StationController.java @@ -5,7 +5,7 @@ import subway.service.StationManager; // 역을 관리하는 컨트롤러 -public class StationController extends ManageController{ +public class StationController extends ClassifyMethods { static StationManager stationManager; static SubwayException subwayException; From e11deaac275d0fa5162fc422f6dbc0f86ae882c9 Mon Sep 17 00:00:00 2001 From: aristo Date: Thu, 16 May 2024 18:02:40 +0900 Subject: [PATCH 077/104] =?UTF-8?q?refactor:=20subwayException=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=20=EC=9D=B8=ED=84=B0=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=8A=A4=EB=82=98=20=EC=B6=94=EC=83=81=ED=99=94=EB=A1=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/Application.java | 12 ++++++++---- .../java/subway/controller/MainController.java | 4 ++++ .../{ => controllers}/LineController.java | 8 ++++++-- .../{ => controllers}/MapController.java | 4 ++-- .../{ => controllers}/SectionController.java | 3 ++- .../{ => controllers}/StationController.java | 14 +++++++++----- .../subway/controller/utils/CheckCommand.java | 3 +-- .../controller/{ => utils}/ClassifyMethods.java | 11 ++++------- .../java/subway/controller/utils/Controller.java | 4 ---- .../java/subway/controller/utils/Methods.java | 16 +++++++--------- .../java/subway/service/InitSubwayValues.java | 1 + src/main/java/subway/service/LineManager.java | 2 -- src/main/java/subway/service/Managerable.java | 3 +++ src/main/java/subway/service/SectionManager.java | 2 +- .../java/subway/service/utils/Constants.java | 4 ---- 15 files changed, 48 insertions(+), 43 deletions(-) rename src/main/java/subway/controller/{ => controllers}/LineController.java (89%) rename src/main/java/subway/controller/{ => controllers}/MapController.java (80%) rename src/main/java/subway/controller/{ => controllers}/SectionController.java (95%) rename src/main/java/subway/controller/{ => controllers}/StationController.java (77%) rename src/main/java/subway/controller/{ => utils}/ClassifyMethods.java (78%) diff --git a/src/main/java/subway/Application.java b/src/main/java/subway/Application.java index 7e87708e5..ad3c075d5 100644 --- a/src/main/java/subway/Application.java +++ b/src/main/java/subway/Application.java @@ -2,9 +2,13 @@ import subway.controller.MainController; public class Application { - public static void main(String[] args) throws Exception { - MainController main = new MainController(); - main.headController(0); - System.out.println(" 안녕히 가세요. "); + public static void main(String[] args) { + try{ + MainController main = new MainController(); + main.headController(0); + System.out.println(" 안녕히 가세요. "); + } catch (Exception e){ + System.err.println("에러가 발생했습니다. 관리자에게 문의해주세요."); + } } } diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 1f6ec0506..db92e6d2f 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -1,6 +1,10 @@ package subway.controller; import subway.config.handler.SubwayException; +import subway.controller.controllers.LineController; +import subway.controller.controllers.MapController; +import subway.controller.controllers.SectionController; +import subway.controller.controllers.StationController; import subway.controller.utils.CheckCommand; import subway.controller.utils.Constants; import subway.service.InitSubwayValues; diff --git a/src/main/java/subway/controller/LineController.java b/src/main/java/subway/controller/controllers/LineController.java similarity index 89% rename from src/main/java/subway/controller/LineController.java rename to src/main/java/subway/controller/controllers/LineController.java index cfaf6d071..760bb6d40 100644 --- a/src/main/java/subway/controller/LineController.java +++ b/src/main/java/subway/controller/controllers/LineController.java @@ -1,9 +1,13 @@ -package subway.controller; +package subway.controller.controllers; import subway.config.handler.SubwayException; +import subway.controller.utils.ClassifyMethods; import subway.service.InitSubwayValues; import subway.service.LineManager; +import java.io.BufferedReader; +import java.io.InputStreamReader; + public class LineController extends ClassifyMethods { static LineManager lineManager; private static SubwayException subwayException; @@ -46,7 +50,7 @@ public boolean delete() { boolean result=false; // 노선 db 에서 삭제 ask.orderWhere(DELETE, LINE); - try { + try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { String command = br.readLine(); result = lineManager.delete(command); message=makeString.infoMessage(DELETE,LINE, result); diff --git a/src/main/java/subway/controller/MapController.java b/src/main/java/subway/controller/controllers/MapController.java similarity index 80% rename from src/main/java/subway/controller/MapController.java rename to src/main/java/subway/controller/controllers/MapController.java index 576ec930f..c9a4d015a 100644 --- a/src/main/java/subway/controller/MapController.java +++ b/src/main/java/subway/controller/controllers/MapController.java @@ -1,11 +1,11 @@ -package subway.controller; +package subway.controller.controllers; import subway.service.InitSubwayValues; import subway.service.LineManager; public class MapController { static LineManager lineManager; - MapController(InitSubwayValues manager){ + public MapController(InitSubwayValues manager){ lineManager = manager.getLineManager(); } public boolean work(){ diff --git a/src/main/java/subway/controller/SectionController.java b/src/main/java/subway/controller/controllers/SectionController.java similarity index 95% rename from src/main/java/subway/controller/SectionController.java rename to src/main/java/subway/controller/controllers/SectionController.java index e11e25af9..724e4c5ee 100644 --- a/src/main/java/subway/controller/SectionController.java +++ b/src/main/java/subway/controller/controllers/SectionController.java @@ -1,6 +1,7 @@ -package subway.controller; +package subway.controller.controllers; import subway.config.handler.SubwayException; +import subway.controller.utils.ClassifyMethods; import subway.controller.utils.Controller; import subway.service.InitSubwayValues; import subway.service.SectionManager; diff --git a/src/main/java/subway/controller/StationController.java b/src/main/java/subway/controller/controllers/StationController.java similarity index 77% rename from src/main/java/subway/controller/StationController.java rename to src/main/java/subway/controller/controllers/StationController.java index f9cd223b0..67829950d 100644 --- a/src/main/java/subway/controller/StationController.java +++ b/src/main/java/subway/controller/controllers/StationController.java @@ -1,15 +1,19 @@ -package subway.controller; +package subway.controller.controllers; import subway.config.handler.SubwayException; +import subway.controller.utils.ClassifyMethods; import subway.service.InitSubwayValues; import subway.service.StationManager; +import java.io.BufferedReader; +import java.io.InputStreamReader; + // 역을 관리하는 컨트롤러 public class StationController extends ClassifyMethods { static StationManager stationManager; - static SubwayException subwayException; + SubwayException subwayException; - StationController(final InitSubwayValues manager){ + public StationController(final InitSubwayValues manager){ stationManager = manager.getStationManager(); subwayException = manager.getSubwayException(); } @@ -17,7 +21,7 @@ public class StationController extends ClassifyMethods { @Override public boolean register(){ ask.orderWhere(REGISTER, STATION); - try { + try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { String station = br.readLine(); boolean result = stationManager.isEmpty(station); String message = makeString.infoMessage(REGISTER, STATION,result); @@ -33,7 +37,7 @@ public boolean register(){ @Override public boolean delete(){ ask.orderWhere(DELETE, STATION); - try { + try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { String command = br.readLine(); boolean result = stationManager.delete(command); String message = makeString.infoMessage(DELETE, STATION, result); diff --git a/src/main/java/subway/controller/utils/CheckCommand.java b/src/main/java/subway/controller/utils/CheckCommand.java index ad14394d7..64b704541 100644 --- a/src/main/java/subway/controller/utils/CheckCommand.java +++ b/src/main/java/subway/controller/utils/CheckCommand.java @@ -33,8 +33,7 @@ public int strToInt(final String node) { } public String getCommand(){ - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - try{ + try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { String command = br.readLine(); return command; }catch (IOException e){ diff --git a/src/main/java/subway/controller/ClassifyMethods.java b/src/main/java/subway/controller/utils/ClassifyMethods.java similarity index 78% rename from src/main/java/subway/controller/ClassifyMethods.java rename to src/main/java/subway/controller/utils/ClassifyMethods.java index 17269b1f5..cd939e497 100644 --- a/src/main/java/subway/controller/ClassifyMethods.java +++ b/src/main/java/subway/controller/utils/ClassifyMethods.java @@ -1,20 +1,17 @@ -package subway.controller; +package subway.controller.utils; -import subway.controller.utils.Constants; -import subway.controller.utils.Controller; +import subway.config.handler.SubwayException; import java.io.BufferedReader; import java.io.InputStreamReader; -import static subway.controller.StationController.subwayException; - public abstract class ClassifyMethods implements Controller, Constants { - protected static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static SubwayException subwayException = new SubwayException(); public boolean work(final Controller controller, final String target) { ask.WhatToManage(target); ask.Function(); - try { + try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { String command = br.readLine(); sendRequest(controller, command); } catch (Exception e) { diff --git a/src/main/java/subway/controller/utils/Controller.java b/src/main/java/subway/controller/utils/Controller.java index 519b293a8..82e0ba4cf 100644 --- a/src/main/java/subway/controller/utils/Controller.java +++ b/src/main/java/subway/controller/utils/Controller.java @@ -1,9 +1,5 @@ package subway.controller.utils; -import subway.view.AskView; -import subway.view.ResponseView; -import subway.view.util.MakeString; - public interface Controller { static final Methods method = new Methods(); diff --git a/src/main/java/subway/controller/utils/Methods.java b/src/main/java/subway/controller/utils/Methods.java index 909e6dacc..2f9f17de5 100644 --- a/src/main/java/subway/controller/utils/Methods.java +++ b/src/main/java/subway/controller/utils/Methods.java @@ -1,7 +1,6 @@ package subway.controller.utils; import subway.config.handler.SubwayException; -import subway.controller.StationController; import subway.service.Managerable; import java.io.BufferedReader; @@ -10,12 +9,12 @@ import static subway.service.utils.Constants.subwayException; public class Methods implements Constants { - static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - public Methods() {} + public Methods() { + } public int getIndex() { ask.orderWhere(); - try { + try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { String input = br.readLine(); if (subwayException.isBack(input) == true) return -1; int index = Integer.parseInt(input); @@ -30,7 +29,7 @@ public int getIndex() { public String getLine() { ask.orderWhere(LINE); - try { + try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { String line = br.readLine(); return line; } catch (Exception e) { @@ -40,8 +39,7 @@ public String getLine() { } public String getLine(String function, String target) { - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - try { + try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { ask.orderWhere(function, target); return br.readLine(); } catch (Exception e) { @@ -53,7 +51,7 @@ public String getLine(String function, String target) { public String getStation() { ask.orderWhere("역 이름"); - try { + try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { return br.readLine(); } catch (Exception e) { subwayException.noStation(); @@ -62,7 +60,7 @@ public String getStation() { } public String getStation(Managerable manager, String function, String station) { - try { + try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { ask.orderWhere(function, station); String node = br.readLine(); if (manager.isEmpty(node) != true) { diff --git a/src/main/java/subway/service/InitSubwayValues.java b/src/main/java/subway/service/InitSubwayValues.java index f4ec022c8..19535c153 100644 --- a/src/main/java/subway/service/InitSubwayValues.java +++ b/src/main/java/subway/service/InitSubwayValues.java @@ -10,6 +10,7 @@ public class InitSubwayValues implements Constants { + static final SubwayException subwayException = new SubwayException(); public LineManager getLineManager(){ return lineManager; diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index bd4977eba..4eba33642 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -1,6 +1,5 @@ package subway.service; -import subway.config.handler.SubwayException; import subway.domain.Line; import subway.domain.Station; import subway.service.utils.LineMakeString; @@ -11,7 +10,6 @@ public class LineManager implements Managerable { LineMakeString makeString = new LineMakeString(); - SubwayException subwayException = new SubwayException(); @Override public boolean isEmpty(final String name) { if(subwayException.isBack(name)){ diff --git a/src/main/java/subway/service/Managerable.java b/src/main/java/subway/service/Managerable.java index f336ca05c..55525866e 100644 --- a/src/main/java/subway/service/Managerable.java +++ b/src/main/java/subway/service/Managerable.java @@ -1,7 +1,10 @@ package subway.service; +import subway.config.handler.SubwayException; + public interface Managerable { public boolean isEmpty(final String command); public boolean delete(final String name); public StringBuilder read(); + static final SubwayException subwayException = new SubwayException(); } diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 089babf25..2404ae3d6 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -9,7 +9,7 @@ public class SectionManager implements Managerable { private Line line; - SubwayException subwayException = new SubwayException(); + SubwayException subwayException ; @Override public boolean isEmpty(final String name) { if(subwayException.isBack(name) == true) diff --git a/src/main/java/subway/service/utils/Constants.java b/src/main/java/subway/service/utils/Constants.java index 8b2644267..5aba15a48 100644 --- a/src/main/java/subway/service/utils/Constants.java +++ b/src/main/java/subway/service/utils/Constants.java @@ -1,6 +1,5 @@ package subway.service.utils; -import subway.config.handler.SubwayException; import subway.domain.LineRepository; import subway.domain.StationRepository; import subway.service.LineManager; @@ -14,7 +13,4 @@ public interface Constants { static final StationManager stationManager = new StationManager(); static final SectionManager sectionManager = new SectionManager(); static final LineManager lineManager = new LineManager(); - - static final SubwayException subwayException = new SubwayException(); - } From 4d5c43804e924e72ed06a2830c15858e78b8a81d Mon Sep 17 00:00:00 2001 From: aristo Date: Thu, 16 May 2024 21:43:24 +0900 Subject: [PATCH 078/104] =?UTF-8?q?refactor:=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EC=BD=94=EB=93=9C=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/constants/initValues/Lines.java | 2 +- .../subway/controller/MainController.java | 24 +++++++++---------- .../LineController.java | 7 ++++-- .../MapController.java | 2 +- .../SectionController.java | 2 +- .../StationController.java | 9 ++++--- .../subway/controller/utils/CheckCommand.java | 9 ++++--- .../controller/utils/ClassifyMethods.java | 5 +++- .../java/subway/controller/utils/Methods.java | 23 ++++++++++++------ 9 files changed, 52 insertions(+), 31 deletions(-) rename src/main/java/subway/controller/{controllers => subControllers}/LineController.java (92%) rename src/main/java/subway/controller/{controllers => subControllers}/MapController.java (90%) rename src/main/java/subway/controller/{controllers => subControllers}/SectionController.java (98%) rename src/main/java/subway/controller/{controllers => subControllers}/StationController.java (88%) diff --git a/src/main/java/subway/config/constants/initValues/Lines.java b/src/main/java/subway/config/constants/initValues/Lines.java index 4c83cb165..e4314f6c7 100644 --- a/src/main/java/subway/config/constants/initValues/Lines.java +++ b/src/main/java/subway/config/constants/initValues/Lines.java @@ -3,7 +3,7 @@ import java.util.Arrays; import java.util.List; -public enum Lines { +public enum Lines { // TODO: 해시맵 생각해보기 LINE_2("2호선", Arrays.asList("교대역", "강남역", "역삼역")), LINE_3("3호선", Arrays.asList("교대역", "남부터미널역", "양재역","매봉역")), 신분당선("신분당선", Arrays.asList("강남역", "양재역", "양재시민의숲역")); diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index db92e6d2f..9ebf06c4d 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -1,10 +1,10 @@ package subway.controller; import subway.config.handler.SubwayException; -import subway.controller.controllers.LineController; -import subway.controller.controllers.MapController; -import subway.controller.controllers.SectionController; -import subway.controller.controllers.StationController; +import subway.controller.subControllers.LineController; +import subway.controller.subControllers.MapController; +import subway.controller.subControllers.SectionController; +import subway.controller.subControllers.StationController; import subway.controller.utils.CheckCommand; import subway.controller.utils.Constants; import subway.service.InitSubwayValues; @@ -17,17 +17,17 @@ public class MainController implements Constants { private SectionController sectionController; private MapController mapController; private static SubwayException subwayException; - private CheckCommand isValidCommand = new CheckCommand(); + private CheckCommand checkCommand = new CheckCommand(); public MainController() { ask = new AskView(); InitSubwayValues manager = new InitSubwayValues(); - prepareServer(manager); + createSubControllers(manager); subwayException = manager.getSubwayException(); } - public void prepareServer(InitSubwayValues manager) { + public void createSubControllers(InitSubwayValues manager) { stationController = new StationController(manager); lineController = new LineController(manager); sectionController = new SectionController(manager); @@ -39,9 +39,9 @@ public void headController(int tryCount) { if (tryCount == 3) return; do { ask.printMain(); - node = isValidCommand.getCommand(); - } while (isValidCommand.isMainCommand(node) && serviceOn(node)); - if (!isValidCommand.isQ(node)) reService(tryCount); + node = checkCommand.getCommand(); + } while (checkCommand.isMainCommand(node) && serviceOn(node)); + if (!checkCommand.isQ(node)) reService(tryCount); } @@ -58,11 +58,11 @@ public boolean serviceOn(String node) { public void reService(int tryCount) { StringBuilder sb = new StringBuilder(); - sb.append("오류가 발생했습니다. 다시 시도해주세요.") // view 랑 연결해도 될 것 같은데 + sb.append("오류가 발생했습니다.") // view 랑 연결해도 될 것 같은데 .append("\n남은 시도 횟수 : ") .append(2-tryCount).append("회"); System.out.println(sb); - headController(tryCount+1); + headController(tryCount + 1); } public boolean commandMapping(int target) { diff --git a/src/main/java/subway/controller/controllers/LineController.java b/src/main/java/subway/controller/subControllers/LineController.java similarity index 92% rename from src/main/java/subway/controller/controllers/LineController.java rename to src/main/java/subway/controller/subControllers/LineController.java index 760bb6d40..3930e4e40 100644 --- a/src/main/java/subway/controller/controllers/LineController.java +++ b/src/main/java/subway/controller/subControllers/LineController.java @@ -1,7 +1,8 @@ -package subway.controller.controllers; +package subway.controller.subControllers; import subway.config.handler.SubwayException; import subway.controller.utils.ClassifyMethods; +import subway.controller.utils.Methods; import subway.service.InitSubwayValues; import subway.service.LineManager; @@ -9,6 +10,8 @@ import java.io.InputStreamReader; public class LineController extends ClassifyMethods { + private BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); + static LineManager lineManager; private static SubwayException subwayException; @@ -50,7 +53,7 @@ public boolean delete() { boolean result=false; // 노선 db 에서 삭제 ask.orderWhere(DELETE, LINE); - try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { + try { String command = br.readLine(); result = lineManager.delete(command); message=makeString.infoMessage(DELETE,LINE, result); diff --git a/src/main/java/subway/controller/controllers/MapController.java b/src/main/java/subway/controller/subControllers/MapController.java similarity index 90% rename from src/main/java/subway/controller/controllers/MapController.java rename to src/main/java/subway/controller/subControllers/MapController.java index c9a4d015a..eee5a3016 100644 --- a/src/main/java/subway/controller/controllers/MapController.java +++ b/src/main/java/subway/controller/subControllers/MapController.java @@ -1,4 +1,4 @@ -package subway.controller.controllers; +package subway.controller.subControllers; import subway.service.InitSubwayValues; import subway.service.LineManager; diff --git a/src/main/java/subway/controller/controllers/SectionController.java b/src/main/java/subway/controller/subControllers/SectionController.java similarity index 98% rename from src/main/java/subway/controller/controllers/SectionController.java rename to src/main/java/subway/controller/subControllers/SectionController.java index 724e4c5ee..3ddcfc0ab 100644 --- a/src/main/java/subway/controller/controllers/SectionController.java +++ b/src/main/java/subway/controller/subControllers/SectionController.java @@ -1,4 +1,4 @@ -package subway.controller.controllers; +package subway.controller.subControllers; import subway.config.handler.SubwayException; import subway.controller.utils.ClassifyMethods; diff --git a/src/main/java/subway/controller/controllers/StationController.java b/src/main/java/subway/controller/subControllers/StationController.java similarity index 88% rename from src/main/java/subway/controller/controllers/StationController.java rename to src/main/java/subway/controller/subControllers/StationController.java index 67829950d..ee2b65007 100644 --- a/src/main/java/subway/controller/controllers/StationController.java +++ b/src/main/java/subway/controller/subControllers/StationController.java @@ -1,7 +1,8 @@ -package subway.controller.controllers; +package subway.controller.subControllers; import subway.config.handler.SubwayException; import subway.controller.utils.ClassifyMethods; +import subway.controller.utils.Methods; import subway.service.InitSubwayValues; import subway.service.StationManager; @@ -10,6 +11,8 @@ // 역을 관리하는 컨트롤러 public class StationController extends ClassifyMethods { + private BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); + static StationManager stationManager; SubwayException subwayException; @@ -21,7 +24,7 @@ public StationController(final InitSubwayValues manager){ @Override public boolean register(){ ask.orderWhere(REGISTER, STATION); - try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { + try { String station = br.readLine(); boolean result = stationManager.isEmpty(station); String message = makeString.infoMessage(REGISTER, STATION,result); @@ -37,7 +40,7 @@ public boolean register(){ @Override public boolean delete(){ ask.orderWhere(DELETE, STATION); - try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { + try { String command = br.readLine(); boolean result = stationManager.delete(command); String message = makeString.infoMessage(DELETE, STATION, result); diff --git a/src/main/java/subway/controller/utils/CheckCommand.java b/src/main/java/subway/controller/utils/CheckCommand.java index 64b704541..b8924af08 100644 --- a/src/main/java/subway/controller/utils/CheckCommand.java +++ b/src/main/java/subway/controller/utils/CheckCommand.java @@ -4,9 +4,11 @@ import java.io.IOException; import java.io.InputStreamReader; -import static subway.service.utils.Constants.subwayException; +import static subway.controller.utils.ClassifyMethods.subwayException; public class CheckCommand { + private BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); + public boolean isQ(String node) { if (node.equals("Q") || node.equals("q")) { return true; @@ -24,6 +26,7 @@ public boolean isMainCommand(String node){ } public int strToInt(final String node) { + if(node == null) return 0; try { return Integer.parseInt(node); } catch (NumberFormatException e) { @@ -33,12 +36,12 @@ public int strToInt(final String node) { } public String getCommand(){ - try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { + try { String command = br.readLine(); return command; }catch (IOException e){ System.err.println("입력 오류 발생"); - return null; } + return null; } } diff --git a/src/main/java/subway/controller/utils/ClassifyMethods.java b/src/main/java/subway/controller/utils/ClassifyMethods.java index cd939e497..ff6695228 100644 --- a/src/main/java/subway/controller/utils/ClassifyMethods.java +++ b/src/main/java/subway/controller/utils/ClassifyMethods.java @@ -7,11 +7,14 @@ public abstract class ClassifyMethods implements Controller, Constants { static SubwayException subwayException = new SubwayException(); + private BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); + public boolean work(final Controller controller, final String target) { ask.WhatToManage(target); ask.Function(); - try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { + + try { String command = br.readLine(); sendRequest(controller, command); } catch (Exception e) { diff --git a/src/main/java/subway/controller/utils/Methods.java b/src/main/java/subway/controller/utils/Methods.java index 2f9f17de5..5bf29ab11 100644 --- a/src/main/java/subway/controller/utils/Methods.java +++ b/src/main/java/subway/controller/utils/Methods.java @@ -4,17 +4,26 @@ import subway.service.Managerable; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStreamReader; -import static subway.service.utils.Constants.subwayException; +import static subway.controller.utils.ClassifyMethods.subwayException; public class Methods implements Constants { - public Methods() { + private BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); + public Methods() {} + + public void closeBuffer(){ + try{ + br.close(); + }catch (IOException e){ + System.err.println("[ERROR] BufferedReader를 닫는 중 에러가 발생했습니다."); + } } public int getIndex() { ask.orderWhere(); - try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { + try { String input = br.readLine(); if (subwayException.isBack(input) == true) return -1; int index = Integer.parseInt(input); @@ -29,7 +38,7 @@ public int getIndex() { public String getLine() { ask.orderWhere(LINE); - try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { + try { String line = br.readLine(); return line; } catch (Exception e) { @@ -39,7 +48,7 @@ public String getLine() { } public String getLine(String function, String target) { - try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { + try { ask.orderWhere(function, target); return br.readLine(); } catch (Exception e) { @@ -51,7 +60,7 @@ public String getLine(String function, String target) { public String getStation() { ask.orderWhere("역 이름"); - try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { + try { return br.readLine(); } catch (Exception e) { subwayException.noStation(); @@ -60,7 +69,7 @@ public String getStation() { } public String getStation(Managerable manager, String function, String station) { - try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) { + try { ask.orderWhere(function, station); String node = br.readLine(); if (manager.isEmpty(node) != true) { From bc9a72f9f0a3b650c0c3af461eb0e225f2e35cf9 Mon Sep 17 00:00:00 2001 From: aristo Date: Thu, 16 May 2024 22:15:17 +0900 Subject: [PATCH 079/104] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=EC=9E=90?= =?UTF-8?q?=20=EA=B0=92=20=EC=9D=BD=EC=96=B4=EC=98=A4=EB=8A=94=20BufferedR?= =?UTF-8?q?eader=20=EB=AA=A8=EB=93=88=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/MainController.java | 12 ++++++---- .../subControllers/LineController.java | 8 +------ .../subControllers/StationController.java | 4 ++-- .../subway/controller/utils/CheckCommand.java | 23 +++++++++---------- .../controller/utils/ClassifyMethods.java | 15 ++++-------- .../java/subway/controller/utils/Methods.java | 15 +++++++++--- src/main/java/subway/service/LineManager.java | 4 ++-- .../java/subway/service/SectionManager.java | 2 +- .../java/subway/service/StationManager.java | 7 +----- 9 files changed, 42 insertions(+), 48 deletions(-) diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 9ebf06c4d..8ab9efb6d 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -7,6 +7,7 @@ import subway.controller.subControllers.StationController; import subway.controller.utils.CheckCommand; import subway.controller.utils.Constants; +import subway.controller.utils.Methods; import subway.service.InitSubwayValues; import subway.view.AskView; @@ -18,6 +19,7 @@ public class MainController implements Constants { private MapController mapController; private static SubwayException subwayException; private CheckCommand checkCommand = new CheckCommand(); + private Methods method=new Methods(); public MainController() { @@ -39,8 +41,8 @@ public void headController(int tryCount) { if (tryCount == 3) return; do { ask.printMain(); - node = checkCommand.getCommand(); - } while (checkCommand.isMainCommand(node) && serviceOn(node)); + node = method.getUserInput(); + } while (checkCommand.isMainCommand(node, false) && serviceOn(node)); if (!checkCommand.isQ(node)) reService(tryCount); } @@ -67,13 +69,13 @@ public void reService(int tryCount) { public boolean commandMapping(int target) { if (target == STATION_COMMAND) { - return stationController.work(stationController, STATION); + return stationController.work(stationController, STATION, false); } if (target == LINE_COMMAND) { - return lineController.work(lineController, LINE); + return lineController.work(lineController, LINE, false); } if (target == SECTION_COMMAND) { - return sectionController.work(sectionController, SECTION); + return sectionController.work(sectionController, SECTION, true); } if (target == MAP_COMMAND) { return mapController.work(); diff --git a/src/main/java/subway/controller/subControllers/LineController.java b/src/main/java/subway/controller/subControllers/LineController.java index 3930e4e40..efb1e4ca4 100644 --- a/src/main/java/subway/controller/subControllers/LineController.java +++ b/src/main/java/subway/controller/subControllers/LineController.java @@ -2,16 +2,10 @@ import subway.config.handler.SubwayException; import subway.controller.utils.ClassifyMethods; -import subway.controller.utils.Methods; import subway.service.InitSubwayValues; import subway.service.LineManager; -import java.io.BufferedReader; -import java.io.InputStreamReader; - public class LineController extends ClassifyMethods { - private BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); - static LineManager lineManager; private static SubwayException subwayException; @@ -54,7 +48,7 @@ public boolean delete() { // 노선 db 에서 삭제 ask.orderWhere(DELETE, LINE); try { - String command = br.readLine(); + String command = method.getUserInput(); result = lineManager.delete(command); message=makeString.infoMessage(DELETE,LINE, result); } catch (Exception e) { diff --git a/src/main/java/subway/controller/subControllers/StationController.java b/src/main/java/subway/controller/subControllers/StationController.java index ee2b65007..3037c1c9f 100644 --- a/src/main/java/subway/controller/subControllers/StationController.java +++ b/src/main/java/subway/controller/subControllers/StationController.java @@ -25,7 +25,7 @@ public StationController(final InitSubwayValues manager){ public boolean register(){ ask.orderWhere(REGISTER, STATION); try { - String station = br.readLine(); + String station = method.getUserInput(); boolean result = stationManager.isEmpty(station); String message = makeString.infoMessage(REGISTER, STATION,result); response.printInfo(message); @@ -41,7 +41,7 @@ public boolean register(){ public boolean delete(){ ask.orderWhere(DELETE, STATION); try { - String command = br.readLine(); + String command = method.getUserInput(); boolean result = stationManager.delete(command); String message = makeString.infoMessage(DELETE, STATION, result); response.printInfo(message); diff --git a/src/main/java/subway/controller/utils/CheckCommand.java b/src/main/java/subway/controller/utils/CheckCommand.java index b8924af08..ad1862120 100644 --- a/src/main/java/subway/controller/utils/CheckCommand.java +++ b/src/main/java/subway/controller/utils/CheckCommand.java @@ -8,7 +8,6 @@ public class CheckCommand { private BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); - public boolean isQ(String node) { if (node.equals("Q") || node.equals("q")) { return true; @@ -16,7 +15,8 @@ public boolean isQ(String node) { return false; } - public boolean isMainCommand(String node){ + public boolean isMainCommand(String node, boolean isSection){ + if(isSection) return isSectionCommand(node); int command = strToInt(node); if(command == 0) return false; @@ -25,6 +25,15 @@ public boolean isMainCommand(String node){ return true; } + public boolean isSectionCommand(String node){ + int command = strToInt(node); + if(command == 0) + return false; + if(command < 0 || command > 2) + return false; + return true; + } + public int strToInt(final String node) { if(node == null) return 0; try { @@ -34,14 +43,4 @@ public int strToInt(final String node) { } return 0; } - - public String getCommand(){ - try { - String command = br.readLine(); - return command; - }catch (IOException e){ - System.err.println("입력 오류 발생"); - } - return null; - } } diff --git a/src/main/java/subway/controller/utils/ClassifyMethods.java b/src/main/java/subway/controller/utils/ClassifyMethods.java index ff6695228..69c8e9f3a 100644 --- a/src/main/java/subway/controller/utils/ClassifyMethods.java +++ b/src/main/java/subway/controller/utils/ClassifyMethods.java @@ -2,23 +2,18 @@ import subway.config.handler.SubwayException; -import java.io.BufferedReader; -import java.io.InputStreamReader; public abstract class ClassifyMethods implements Controller, Constants { static SubwayException subwayException = new SubwayException(); - private BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); + static CheckCommand checkCommand =new CheckCommand(); - - public boolean work(final Controller controller, final String target) { + public boolean work(final Controller controller, final String target, boolean isSection) { ask.WhatToManage(target); ask.Function(); - - try { - String command = br.readLine(); + String command = method.getUserInput(); + if(checkCommand.isMainCommand(command, isSection)){ sendRequest(controller, command); - } catch (Exception e) { - subwayException.checkCommand(); + return true; } return false; } diff --git a/src/main/java/subway/controller/utils/Methods.java b/src/main/java/subway/controller/utils/Methods.java index 5bf29ab11..45951c94a 100644 --- a/src/main/java/subway/controller/utils/Methods.java +++ b/src/main/java/subway/controller/utils/Methods.java @@ -21,10 +21,19 @@ public void closeBuffer(){ } } + public String getUserInput(){ + try{ + return br.readLine(); + }catch (IOException e){ + System.err.println("[ERROR] 사용자 입력을 받는 중 에러가 발생했습니다."); + } + return null; + } + public int getIndex() { ask.orderWhere(); try { - String input = br.readLine(); + String input = getUserInput(); if (subwayException.isBack(input) == true) return -1; int index = Integer.parseInt(input); return index; @@ -39,7 +48,7 @@ public int getIndex() { public String getLine() { ask.orderWhere(LINE); try { - String line = br.readLine(); + String line = getUserInput(); return line; } catch (Exception e) { subwayException.checkCommand(); @@ -71,7 +80,7 @@ public String getStation() { public String getStation(Managerable manager, String function, String station) { try { ask.orderWhere(function, station); - String node = br.readLine(); + String node = getUserInput(); if (manager.isEmpty(node) != true) { subwayException.noStation(); } diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index 4eba33642..e10f30439 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -29,8 +29,8 @@ public Line setLine(final String name){ public void setStations(final String name, final String upperStation, final String bottomStation){ Line line = setLine(name); - Station upper = stationManager.getByName(upperStation); - Station bottom = stationManager.getByName(bottomStation); + Station upper = stationManager.getByStationName(upperStation); + Station bottom = stationManager.getByStationName(bottomStation); upper.addLine(line); bottom.addLine(line); line.setStations(upper, bottom); diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 2404ae3d6..25d02f854 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -9,7 +9,7 @@ public class SectionManager implements Managerable { private Line line; - SubwayException subwayException ; + SubwayException subwayException =new SubwayException(); @Override public boolean isEmpty(final String name) { if(subwayException.isBack(name) == true) diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index 35220ee0d..de4a7ee77 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -29,12 +29,7 @@ public StringBuilder read() { return sb; } - public Station getByName(final String name){ - try{ - isEmpty(name); - }catch (SubwayException e){ - e.alreadyCreatedStation(); - } + public Station getByStationName(final String name){ return stationRepo.getStationByName(name); } } From d706211cba20955618737c17a7907401cc97a50e Mon Sep 17 00:00:00 2001 From: aristo Date: Thu, 16 May 2024 22:27:29 +0900 Subject: [PATCH 080/104] =?UTF-8?q?refactor:=20String=20to=20Integer=20Met?= =?UTF-8?q?hod=20=EC=82=AC=EC=9A=A9=20=EB=B2=94=EC=9C=84=20=ED=99=95?= =?UTF-8?q?=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/Application.java | 3 ++- .../subway/controller/MainController.java | 10 +++++++--- .../subway/controller/utils/CheckCommand.java | 11 +++++----- .../java/subway/controller/utils/Methods.java | 20 +++++-------------- 4 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/main/java/subway/Application.java b/src/main/java/subway/Application.java index ad3c075d5..a464040bb 100644 --- a/src/main/java/subway/Application.java +++ b/src/main/java/subway/Application.java @@ -5,7 +5,8 @@ public class Application { public static void main(String[] args) { try{ MainController main = new MainController(); - main.headController(0); + main.startService(0); + main.closeService(); System.out.println(" 안녕히 가세요. "); } catch (Exception e){ System.err.println("에러가 발생했습니다. 관리자에게 문의해주세요."); diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 8ab9efb6d..ee931bccf 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -36,7 +36,7 @@ public void createSubControllers(InitSubwayValues manager) { mapController = new MapController(manager); } - public void headController(int tryCount) { + public void startService(int tryCount) { String node; if (tryCount == 3) return; do { @@ -46,9 +46,13 @@ public void headController(int tryCount) { if (!checkCommand.isQ(node)) reService(tryCount); } + public void closeService(){ + method.closeBuffer(); + } + public boolean serviceOn(String node) { - int command = Integer.parseInt(node); + int command = checkCommand.strToInt(node); try { commandMapping(command); return true; @@ -64,7 +68,7 @@ public void reService(int tryCount) { .append("\n남은 시도 횟수 : ") .append(2-tryCount).append("회"); System.out.println(sb); - headController(tryCount + 1); + startService(tryCount + 1); } public boolean commandMapping(int target) { diff --git a/src/main/java/subway/controller/utils/CheckCommand.java b/src/main/java/subway/controller/utils/CheckCommand.java index ad1862120..90fcc6eb2 100644 --- a/src/main/java/subway/controller/utils/CheckCommand.java +++ b/src/main/java/subway/controller/utils/CheckCommand.java @@ -1,7 +1,6 @@ package subway.controller.utils; import java.io.BufferedReader; -import java.io.IOException; import java.io.InputStreamReader; import static subway.controller.utils.ClassifyMethods.subwayException; @@ -16,17 +15,17 @@ public boolean isQ(String node) { } public boolean isMainCommand(String node, boolean isSection){ - if(isSection) return isSectionCommand(node); int command = strToInt(node); - if(command == 0) + if(isSection) + return isSectionCommand(command); + if(command <= 0) return false; - if(command < 0 || command > 4) + if(command > 4) return false; return true; } - public boolean isSectionCommand(String node){ - int command = strToInt(node); + public boolean isSectionCommand(int command){ if(command == 0) return false; if(command < 0 || command > 2) diff --git a/src/main/java/subway/controller/utils/Methods.java b/src/main/java/subway/controller/utils/Methods.java index 45951c94a..62adeb306 100644 --- a/src/main/java/subway/controller/utils/Methods.java +++ b/src/main/java/subway/controller/utils/Methods.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.io.InputStreamReader; +import static subway.controller.utils.ClassifyMethods.checkCommand; import static subway.controller.utils.ClassifyMethods.subwayException; public class Methods implements Constants { @@ -35,7 +36,7 @@ public int getIndex() { try { String input = getUserInput(); if (subwayException.isBack(input) == true) return -1; - int index = Integer.parseInt(input); + int index = checkCommand.strToInt(input); return index; } catch (SubwayException e) { e.isNotNumber(); @@ -57,24 +58,13 @@ public String getLine() { } public String getLine(String function, String target) { - try { - ask.orderWhere(function, target); - return br.readLine(); - } catch (Exception e) { - subwayException.checkCommand(); - } - return null; + ask.orderWhere(function, target); + return getUserInput(); } public String getStation() { ask.orderWhere("역 이름"); - - try { - return br.readLine(); - } catch (Exception e) { - subwayException.noStation(); - } - return null; + return getUserInput(); } public String getStation(Managerable manager, String function, String station) { From d2a4fa06266d4fc2d243eec970a99a697edf654b Mon Sep 17 00:00:00 2001 From: aristo Date: Thu, 16 May 2024 22:41:15 +0900 Subject: [PATCH 081/104] =?UTF-8?q?refactor:=20=ED=94=84=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EB=9E=A8=20=EC=A2=85=EB=A3=8C=20=EB=B0=8F=20=EB=92=A4=EB=A1=9C?= =?UTF-8?q?=EA=B0=80=EA=B8=B0=20=EB=B6=80=EB=B6=84=20=EC=97=90=EB=9F=AC=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/config/handler/SubwayException.java | 2 +- .../java/subway/controller/MainController.java | 2 +- .../subControllers/SectionController.java | 2 +- .../subControllers/StationController.java | 1 - .../subway/controller/utils/CheckCommand.java | 6 ++++-- .../subway/controller/utils/ClassifyMethods.java | 4 +++- .../java/subway/controller/utils/Methods.java | 15 ++++----------- 7 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/main/java/subway/config/handler/SubwayException.java b/src/main/java/subway/config/handler/SubwayException.java index a06e48c75..e826ae3ee 100644 --- a/src/main/java/subway/config/handler/SubwayException.java +++ b/src/main/java/subway/config/handler/SubwayException.java @@ -26,7 +26,7 @@ public void unexpected(){ throw new IllegalArgumentException(ERROR+Errors.UNEXPECTED.getError()); } public boolean isBack(String command){ - if(command.equals('B')){ + if(command.equals("B")){ return true; } return false; diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index ee931bccf..c21678e00 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -42,7 +42,7 @@ public void startService(int tryCount) { do { ask.printMain(); node = method.getUserInput(); - } while (checkCommand.isMainCommand(node, false) && serviceOn(node)); + } while (checkCommand.isValidInteger(node, false) && serviceOn(node)); if (!checkCommand.isQ(node)) reService(tryCount); } diff --git a/src/main/java/subway/controller/subControllers/SectionController.java b/src/main/java/subway/controller/subControllers/SectionController.java index 3ddcfc0ab..0d3d53d8d 100644 --- a/src/main/java/subway/controller/subControllers/SectionController.java +++ b/src/main/java/subway/controller/subControllers/SectionController.java @@ -39,7 +39,7 @@ public boolean register() { // 노선, 역 이름, 순서 입력 받고 등록 String station = method.getStation(); int index = method.getIndex(); - if(index == -1){ + if(index < 0){ return false; } diff --git a/src/main/java/subway/controller/subControllers/StationController.java b/src/main/java/subway/controller/subControllers/StationController.java index 3037c1c9f..3a823d585 100644 --- a/src/main/java/subway/controller/subControllers/StationController.java +++ b/src/main/java/subway/controller/subControllers/StationController.java @@ -2,7 +2,6 @@ import subway.config.handler.SubwayException; import subway.controller.utils.ClassifyMethods; -import subway.controller.utils.Methods; import subway.service.InitSubwayValues; import subway.service.StationManager; diff --git a/src/main/java/subway/controller/utils/CheckCommand.java b/src/main/java/subway/controller/utils/CheckCommand.java index 90fcc6eb2..064653119 100644 --- a/src/main/java/subway/controller/utils/CheckCommand.java +++ b/src/main/java/subway/controller/utils/CheckCommand.java @@ -14,7 +14,7 @@ public boolean isQ(String node) { return false; } - public boolean isMainCommand(String node, boolean isSection){ + public boolean isValidInteger(String node, boolean isSection){ int command = strToInt(node); if(isSection) return isSectionCommand(command); @@ -38,7 +38,9 @@ public int strToInt(final String node) { try { return Integer.parseInt(node); } catch (NumberFormatException e) { - subwayException.isNotUnder4OrQ(); + return 0; + }catch (Exception e){ + subwayException.unexpected(); } return 0; } diff --git a/src/main/java/subway/controller/utils/ClassifyMethods.java b/src/main/java/subway/controller/utils/ClassifyMethods.java index 69c8e9f3a..cf74acad1 100644 --- a/src/main/java/subway/controller/utils/ClassifyMethods.java +++ b/src/main/java/subway/controller/utils/ClassifyMethods.java @@ -11,7 +11,9 @@ public boolean work(final Controller controller, final String target, boolean is ask.WhatToManage(target); ask.Function(); String command = method.getUserInput(); - if(checkCommand.isMainCommand(command, isSection)){ + if (subwayException.isBack(command)) return true; + + if(checkCommand.isValidInteger(command, isSection)){ sendRequest(controller, command); return true; } diff --git a/src/main/java/subway/controller/utils/Methods.java b/src/main/java/subway/controller/utils/Methods.java index 62adeb306..d657164f5 100644 --- a/src/main/java/subway/controller/utils/Methods.java +++ b/src/main/java/subway/controller/utils/Methods.java @@ -33,17 +33,10 @@ public String getUserInput(){ public int getIndex() { ask.orderWhere(); - try { - String input = getUserInput(); - if (subwayException.isBack(input) == true) return -1; - int index = checkCommand.strToInt(input); - return index; - } catch (SubwayException e) { - e.isNotNumber(); - } catch (Exception e) { - subwayException.checkCommand(); - } - return -1; + String input = getUserInput(); + if (subwayException.isBack(input) == true) return -1; + int index = checkCommand.strToInt(input); + return index; } public String getLine() { From 1fd46c711832f939086a7cf2f5bf6c49ad985068 Mon Sep 17 00:00:00 2001 From: aristo Date: Thu, 16 May 2024 23:14:51 +0900 Subject: [PATCH 082/104] =?UTF-8?q?refactor:=20while=20=EB=AC=B8=20?= =?UTF-8?q?=EC=A1=B0=EA=B1=B4=20=EA=B0=9C=EC=84=A0=EC=9D=84=20=EC=9C=84?= =?UTF-8?q?=ED=95=B4=20repeatService=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/MainController.java | 29 ++++++++++++------- .../controller/utils/ClassifyMethods.java | 1 + .../java/subway/controller/utils/Methods.java | 1 - src/main/java/subway/view/AskView.java | 2 +- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index c21678e00..97eb9dbe8 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -37,24 +37,29 @@ public void createSubControllers(InitSubwayValues manager) { } public void startService(int tryCount) { - String node; if (tryCount == 3) return; - do { - ask.printMain(); - node = method.getUserInput(); - } while (checkCommand.isValidInteger(node, false) && serviceOn(node)); + ask.manageTarget(); + String node = method.getUserInput(); + if(checkCommand.isValidInteger(node,false)){ + node = repeatService(node); + } if (!checkCommand.isQ(node)) reService(tryCount); } - public void closeService(){ - method.closeBuffer(); + public String repeatService(String node){ + do { + int command = checkCommand.strToInt(node); + serviceOn(command); + ask.manageTarget(); + node = method.getUserInput(); + } while (checkCommand.isValidInteger(node, false)); + return node; } - public boolean serviceOn(String node) { - int command = checkCommand.strToInt(node); + public boolean serviceOn(int node) { try { - commandMapping(command); + commandMapping(node); return true; } catch (SubwayException e) { subwayException.isNotUnder4OrQ(); // 에러 메시지 변경하기, while 조건문에서 검증 마침 @@ -86,4 +91,8 @@ public boolean commandMapping(int target) { } return false; } + + public void closeService(){ + method.closeBuffer(); + } } diff --git a/src/main/java/subway/controller/utils/ClassifyMethods.java b/src/main/java/subway/controller/utils/ClassifyMethods.java index cf74acad1..72ffa4198 100644 --- a/src/main/java/subway/controller/utils/ClassifyMethods.java +++ b/src/main/java/subway/controller/utils/ClassifyMethods.java @@ -17,6 +17,7 @@ public boolean work(final Controller controller, final String target, boolean is sendRequest(controller, command); return true; } + //에러 발생하는 부분 : 유효하지 않은 입력값 return false; } diff --git a/src/main/java/subway/controller/utils/Methods.java b/src/main/java/subway/controller/utils/Methods.java index d657164f5..e6a3fce13 100644 --- a/src/main/java/subway/controller/utils/Methods.java +++ b/src/main/java/subway/controller/utils/Methods.java @@ -1,6 +1,5 @@ package subway.controller.utils; -import subway.config.handler.SubwayException; import subway.service.Managerable; import java.io.BufferedReader; diff --git a/src/main/java/subway/view/AskView.java b/src/main/java/subway/view/AskView.java index 69adc5e2c..77a5bdc90 100644 --- a/src/main/java/subway/view/AskView.java +++ b/src/main/java/subway/view/AskView.java @@ -6,7 +6,7 @@ public class AskView implements Constants { // TODO: (고민) view 가 너무 중구난방인 거 같은데 controller, service 처럼 분할할까. static final MakeString makeString = new MakeString(); - public void printMain() { + public void manageTarget() { System.out.println(makeString.showTitle(MAIN)); System.out.println(makeString.showIndex(MAIN)); } From 6780cfa126bf9e7dcbe23ad0f3a868751771efd5 Mon Sep 17 00:00:00 2001 From: aristo Date: Thu, 16 May 2024 23:17:54 +0900 Subject: [PATCH 083/104] =?UTF-8?q?refactor:=20=EB=B3=80=EC=88=98=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/MainController.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 97eb9dbe8..12e3e3d57 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -13,10 +13,10 @@ public class MainController implements Constants { private AskView ask; - private StationController stationController; - private LineController lineController; - private SectionController sectionController; - private MapController mapController; + private StationController station; + private LineController line; + private SectionController section; + private MapController map; private static SubwayException subwayException; private CheckCommand checkCommand = new CheckCommand(); private Methods method=new Methods(); @@ -30,10 +30,10 @@ public MainController() { } public void createSubControllers(InitSubwayValues manager) { - stationController = new StationController(manager); - lineController = new LineController(manager); - sectionController = new SectionController(manager); - mapController = new MapController(manager); + station = new StationController(manager); + line = new LineController(manager); + section = new SectionController(manager); + map = new MapController(manager); } public void startService(int tryCount) { @@ -78,16 +78,16 @@ public void reService(int tryCount) { public boolean commandMapping(int target) { if (target == STATION_COMMAND) { - return stationController.work(stationController, STATION, false); + return station.work(station, STATION, false); } if (target == LINE_COMMAND) { - return lineController.work(lineController, LINE, false); + return line.work(line, LINE, false); } if (target == SECTION_COMMAND) { - return sectionController.work(sectionController, SECTION, true); + return section.work(section, SECTION, true); } if (target == MAP_COMMAND) { - return mapController.work(); + return map.work(); } return false; } From 85da7f13fecb4473ac1829b6081947600b31f5a0 Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 17 May 2024 15:32:34 +0900 Subject: [PATCH 084/104] =?UTF-8?q?feat:=20=EC=98=88=EC=99=B8=EA=B0=80=20?= =?UTF-8?q?=EB=B0=9C=EC=83=9D=ED=95=B4=EB=8F=84=20=ED=94=84=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EB=9E=A8=EC=9D=B4=20=EC=A2=85=EB=A3=8C=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8F=84=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/controller/utils/ClassifyMethods.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/subway/controller/utils/ClassifyMethods.java b/src/main/java/subway/controller/utils/ClassifyMethods.java index 72ffa4198..d4646e445 100644 --- a/src/main/java/subway/controller/utils/ClassifyMethods.java +++ b/src/main/java/subway/controller/utils/ClassifyMethods.java @@ -35,7 +35,7 @@ public boolean sendRequest(final Controller controller, final String command) { if (command.equals(BACK_COMMAND)) { return true; } - subwayException.notValidCommand(); +// subwayException.notValidCommand(); return false; } } From 3d5f98d78b25507c2bd80c7c5319d753f9b03d5a Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 17 May 2024 15:59:20 +0900 Subject: [PATCH 085/104] =?UTF-8?q?refactor:=20line=20register=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC=20=EC=B2=98=EB=A6=AC=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/config/handler/InputException.java | 19 +++++++++++++++ .../subControllers/LineController.java | 24 +++++++------------ .../java/subway/controller/utils/Methods.java | 17 ++++++------- .../java/subway/service/StationManager.java | 6 ++++- 4 files changed, 39 insertions(+), 27 deletions(-) create mode 100644 src/main/java/subway/config/handler/InputException.java diff --git a/src/main/java/subway/config/handler/InputException.java b/src/main/java/subway/config/handler/InputException.java new file mode 100644 index 000000000..e92bf066e --- /dev/null +++ b/src/main/java/subway/config/handler/InputException.java @@ -0,0 +1,19 @@ +package subway.config.handler; + +import static subway.config.constants.views.Errors.ADD_LINE; +import static subway.config.constants.views.Errors.DEL_STATION; +import static subway.config.constants.views.Prefixes.ERROR; + +public class InputException { + public void noStation(){ + StringBuilder sb = new StringBuilder(); + sb.append(DEL_STATION.getError()); + System.out.println(sb); + } + + public void alreadyCreatedLine(){ + StringBuilder sb = new StringBuilder(); + sb.append(ADD_LINE.getError()); + System.out.println(sb); + } +} diff --git a/src/main/java/subway/controller/subControllers/LineController.java b/src/main/java/subway/controller/subControllers/LineController.java index efb1e4ca4..5be25f652 100644 --- a/src/main/java/subway/controller/subControllers/LineController.java +++ b/src/main/java/subway/controller/subControllers/LineController.java @@ -1,5 +1,6 @@ package subway.controller.subControllers; +import subway.config.handler.InputException; import subway.config.handler.SubwayException; import subway.controller.utils.ClassifyMethods; import subway.service.InitSubwayValues; @@ -7,6 +8,7 @@ public class LineController extends ClassifyMethods { static LineManager lineManager; + InputException inputException = new InputException(); private static SubwayException subwayException; public LineController(final InitSubwayValues manager) { @@ -17,24 +19,17 @@ public LineController(final InitSubwayValues manager) { @Override public boolean register() { String upper, bottom, message; + String line = method.getLine(REGISTER, LINE); try { - String line = method.getLine(REGISTER, LINE); if (lineManager.isEmpty(line) != true) { - subwayException.alreadyCreatedLine(); + inputException.alreadyCreatedLine(); + return false; } - upper = method.getStation(lineManager, REGISTER, UPPER); bottom = method.getStation(lineManager, REGISTER, BOTTOM); - if(upper == null || bottom == null){ - subwayException.noStation(); - } - lineManager.setStations(line, upper, bottom); - } catch (Exception e) { - message= makeString.infoMessage(REGISTER, LINE,false); - System.out.println(message); - e.printStackTrace(); - subwayException.unexpected(); + } catch (IllegalArgumentException e) { + inputException.noStation(); } message= makeString.infoMessage(REGISTER, LINE, true); System.out.println(message); @@ -45,18 +40,15 @@ public boolean register() { public boolean delete() { String message; boolean result=false; - // 노선 db 에서 삭제 ask.orderWhere(DELETE, LINE); try { String command = method.getUserInput(); result = lineManager.delete(command); - message=makeString.infoMessage(DELETE,LINE, result); } catch (Exception e) { - message=makeString.infoMessage(DELETE,LINE, result); - System.out.println(message); subwayException.noLine(); return false; } + message=makeString.infoMessage(DELETE,LINE, result); System.out.println(message); return true; } diff --git a/src/main/java/subway/controller/utils/Methods.java b/src/main/java/subway/controller/utils/Methods.java index e6a3fce13..122b84b55 100644 --- a/src/main/java/subway/controller/utils/Methods.java +++ b/src/main/java/subway/controller/utils/Methods.java @@ -1,5 +1,6 @@ package subway.controller.utils; +import subway.config.handler.InputException; import subway.service.Managerable; import java.io.BufferedReader; @@ -11,6 +12,7 @@ public class Methods implements Constants { private BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); + InputException inputException = new InputException(); public Methods() {} public void closeBuffer(){ @@ -60,16 +62,11 @@ public String getStation() { } public String getStation(Managerable manager, String function, String station) { - try { - ask.orderWhere(function, station); - String node = getUserInput(); - if (manager.isEmpty(node) != true) { - subwayException.noStation(); - } - return node; - } catch (Exception e) { - subwayException.unexpected(); + ask.orderWhere(function, station); + String node = getUserInput(); + if (manager.isEmpty(node) != true) { + subwayException.noStation(); } - return null; + return node; } } diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index de4a7ee77..1ad8ffec9 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -30,6 +30,10 @@ public StringBuilder read() { } public Station getByStationName(final String name){ - return stationRepo.getStationByName(name); + Station station =stationRepo.getStationByName(name); + if(station == null){ + subwayException.noStation(); + } + return station; } } From dba61ce1616786b25a3db9c930e724dc53842dd4 Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 17 May 2024 16:09:02 +0900 Subject: [PATCH 086/104] =?UTF-8?q?refactor=20:StationController=20?= =?UTF-8?q?=EC=97=90=EB=9F=AC=20=EC=B2=98=EB=A6=AC=20=EC=A4=91=EC=8B=AC=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/config/handler/InputException.java | 19 ++++++-- .../subControllers/StationController.java | 47 +++++++++---------- .../java/subway/service/StationManager.java | 6 ++- 3 files changed, 41 insertions(+), 31 deletions(-) diff --git a/src/main/java/subway/config/handler/InputException.java b/src/main/java/subway/config/handler/InputException.java index e92bf066e..eb3f05fcf 100644 --- a/src/main/java/subway/config/handler/InputException.java +++ b/src/main/java/subway/config/handler/InputException.java @@ -1,19 +1,30 @@ package subway.config.handler; -import static subway.config.constants.views.Errors.ADD_LINE; -import static subway.config.constants.views.Errors.DEL_STATION; +import static subway.config.constants.views.Errors.*; import static subway.config.constants.views.Prefixes.ERROR; public class InputException { public void noStation(){ StringBuilder sb = new StringBuilder(); sb.append(DEL_STATION.getError()); - System.out.println(sb); + System.err.println(sb); } public void alreadyCreatedLine(){ StringBuilder sb = new StringBuilder(); sb.append(ADD_LINE.getError()); - System.out.println(sb); + System.err.println(sb); + } + + public void alreadyCreatedStation(){ + StringBuilder sb = new StringBuilder(); + sb.append(ADD_STATION.getError()); + System.err.println(sb); + } + + public void unExpectedError(){ + StringBuilder sb = new StringBuilder(); + sb.append(UNEXPECTED.getError()); + System.err.println(sb); } } diff --git a/src/main/java/subway/controller/subControllers/StationController.java b/src/main/java/subway/controller/subControllers/StationController.java index 3a823d585..fe62f8025 100644 --- a/src/main/java/subway/controller/subControllers/StationController.java +++ b/src/main/java/subway/controller/subControllers/StationController.java @@ -1,57 +1,54 @@ package subway.controller.subControllers; +import subway.config.handler.InputException; import subway.config.handler.SubwayException; import subway.controller.utils.ClassifyMethods; import subway.service.InitSubwayValues; import subway.service.StationManager; -import java.io.BufferedReader; -import java.io.InputStreamReader; // 역을 관리하는 컨트롤러 public class StationController extends ClassifyMethods { - private BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); - static StationManager stationManager; SubwayException subwayException; - - public StationController(final InitSubwayValues manager){ + InputException inputException = new InputException(); + public StationController(final InitSubwayValues manager) { stationManager = manager.getStationManager(); subwayException = manager.getSubwayException(); } @Override - public boolean register(){ + public boolean register() { ask.orderWhere(REGISTER, STATION); + boolean result = false; + String station = method.getUserInput(); try { - String station = method.getUserInput(); - boolean result = stationManager.isEmpty(station); - String message = makeString.infoMessage(REGISTER, STATION,result); - response.printInfo(message); - return false; - }catch (Exception e){ - e.printStackTrace(); - subwayException.unexpected(); + result = stationManager.isEmpty(station); + } catch (Exception e) { + inputException.alreadyCreatedStation(); } - return true; + String message = makeString.infoMessage(REGISTER, STATION, result); + response.printInfo(message); + return result; } @Override - public boolean delete(){ - ask.orderWhere(DELETE, STATION); + public boolean delete() { + ask.orderWhere(DELETE, STATION); + String command = method.getUserInput(); + boolean result=false; try { - String command = method.getUserInput(); - boolean result = stationManager.delete(command); - String message = makeString.infoMessage(DELETE, STATION, result); - response.printInfo(message); - }catch (Exception e){ - subwayException.unexpected(); + result = stationManager.delete(command); + } catch (Exception e) { + inputException.unExpectedError(); return false; } + String message = makeString.infoMessage(DELETE, STATION, result); + response.printInfo(message); return true; } - public boolean read(){ + public boolean read() { response.printTitle("역 목록"); StringBuilder list = stationManager.read(); response.printList(list); diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index 1ad8ffec9..22455904f 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -1,16 +1,18 @@ package subway.service; +import subway.config.handler.InputException; import subway.config.handler.SubwayException; import subway.domain.Station; import static subway.service.InitSubwayValues.stationRepo; // 역 관련 기능하는 서비스 public class StationManager implements Managerable { + InputException inputException= new InputException(); @Override // 삽입 public boolean isEmpty(final String name) { if(stationRepo.getStationByName(name)!=null){ - // TODO: 이미 존재하는 역 - throw new SubwayException(); + inputException.alreadyCreatedStation(); + return false; } stationRepo.addStation(new Station(name)); return true; From c86d8009c4a0339e28da5059f527cafc38085624 Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 17 May 2024 17:24:32 +0900 Subject: [PATCH 087/104] =?UTF-8?q?fix:=20SectionManager=20field=20line=20?= =?UTF-8?q?=EC=84=B8=ED=8C=85=ED=95=98=EC=97=AC=20=EA=B5=AC=EA=B0=84?= =?UTF-8?q?=EC=9D=B4=20=EC=82=AD=EC=A0=9C=EB=90=98=EC=A7=80=20=EC=95=8A?= =?UTF-8?q?=EB=8A=94=20=EC=98=A4=EB=A5=98=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/config/constants/views/Errors.java | 1 + .../subway/config/handler/InputException.java | 6 +++ .../subControllers/SectionController.java | 40 ++++++++------- .../java/subway/controller/utils/Methods.java | 3 +- src/main/java/subway/domain/Line.java | 2 +- .../java/subway/service/SectionManager.java | 51 ++++++++++++++----- 6 files changed, 72 insertions(+), 31 deletions(-) diff --git a/src/main/java/subway/config/constants/views/Errors.java b/src/main/java/subway/config/constants/views/Errors.java index e9532b78c..4b5118571 100644 --- a/src/main/java/subway/config/constants/views/Errors.java +++ b/src/main/java/subway/config/constants/views/Errors.java @@ -4,6 +4,7 @@ public enum Errors { VALID("잘못된 명령입니다. 입력값을 확인해주세요."), ADD_STATION("이미 등록된 역 이름입니다."), ADD_LINE("이미 등록된 노선 이름입니다."), + ADDED("이미 등록된 이름입니다."), DEL_STATION("존재하지 않은 역 이름입니다."), UNEXPECTED("예상치 못한 에러가 발생했습니다."), DEL_LINE("존재하지 않는 노선 이름입니다."); diff --git a/src/main/java/subway/config/handler/InputException.java b/src/main/java/subway/config/handler/InputException.java index eb3f05fcf..df9774df2 100644 --- a/src/main/java/subway/config/handler/InputException.java +++ b/src/main/java/subway/config/handler/InputException.java @@ -22,6 +22,12 @@ public void alreadyCreatedStation(){ System.err.println(sb); } + public void alreadyCreated(String target){ + StringBuilder sb = new StringBuilder(); + sb.append(target).append("는 ").append(ADDED.getError()); + System.err.println(sb); + } + public void unExpectedError(){ StringBuilder sb = new StringBuilder(); sb.append(UNEXPECTED.getError()); diff --git a/src/main/java/subway/controller/subControllers/SectionController.java b/src/main/java/subway/controller/subControllers/SectionController.java index 0d3d53d8d..e026ca6c4 100644 --- a/src/main/java/subway/controller/subControllers/SectionController.java +++ b/src/main/java/subway/controller/subControllers/SectionController.java @@ -1,5 +1,6 @@ package subway.controller.subControllers; +import subway.config.handler.InputException; import subway.config.handler.SubwayException; import subway.controller.utils.ClassifyMethods; import subway.controller.utils.Controller; @@ -9,14 +10,15 @@ public class SectionController extends ClassifyMethods { private static SectionManager sectionManager; static SubwayException subwayException; + InputException inputException = new InputException(); - public SectionController(final InitSubwayValues manager){ + public SectionController(final InitSubwayValues manager) { sectionManager = manager.getSectionManager(); subwayException = manager.getSubwayException(); } - @Override - public boolean sendRequest(final Controller controller, final String command){ + @Override // 구간 조회 없 + public boolean sendRequest(final Controller controller, final String command) { if (command.equals(REGISTER_COMMAND)) { // 등록 return controller.register(); } @@ -33,37 +35,41 @@ public boolean sendRequest(final Controller controller, final String command){ @Override public boolean register() { // 노선, 역 이름, 순서 입력 받고 등록 String line = method.getLine(); - if(!sectionManager.isEmpty(line)){ - return false; - } + if (!sectionManager.isEmpty(line)) subwayException.noLine(); - String station = method.getStation(); - int index = method.getIndex(); - if(index < 0){ - return false; + String sName = method.getStation(); + if (!sectionManager.isEmpty(sName)) { + int index = method.getIndex(); + sectionManager.insertSection(sName, index); } - - sectionManager.insertSection(station, index); response.printInfo("구간이 등록되었습니다."); return true; } @Override public boolean delete() { - try{ + String target = ""; + try { String line = method.getLine(DELETE, "구간의 노선"); + sectionManager.getLine(line); String station = method.getStation(sectionManager, DELETE, "구간의 역"); - if(sectionManager.isEmpty(line)){ + if (!sectionManager.isEmpty(station)) { // TODO: 존재하지 않는 구간을 삭제하려 할 때 예외 처리 추가 sectionManager.delete(station); response.printInfo("구간이 삭제되었습니다."); } - } catch (Exception e){ - subwayException.unexpected(); + } catch (IllegalArgumentException e) { + System.err.println("IllegalArgumentException"); return false; + } catch (Exception e) { + System.err.println("noooooooooooo"); } + System.out.println("success?"); return true; } + @Override - public boolean read() {return false;} + public boolean read() { + return false; + } } diff --git a/src/main/java/subway/controller/utils/Methods.java b/src/main/java/subway/controller/utils/Methods.java index 122b84b55..5c1cf7fd6 100644 --- a/src/main/java/subway/controller/utils/Methods.java +++ b/src/main/java/subway/controller/utils/Methods.java @@ -37,6 +37,7 @@ public int getIndex() { String input = getUserInput(); if (subwayException.isBack(input) == true) return -1; int index = checkCommand.strToInt(input); + if(index <= 0) subwayException.checkCommand(); return index; } @@ -64,7 +65,7 @@ public String getStation() { public String getStation(Managerable manager, String function, String station) { ask.orderWhere(function, station); String node = getUserInput(); - if (manager.isEmpty(node) != true) { + if (manager.isEmpty(node) == true) { subwayException.noStation(); } return node; diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index f7ebefa14..dfef43ba7 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -42,7 +42,7 @@ public boolean deleteStation(final String station) { for (int index = 0; index < stations.size(); index++) { Station s = stations.get(index); if (s.getName().equals(station)) { - stations.remove(index); + stations.remove(s); return true; } } diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 25d02f854..c724ba1b2 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -1,37 +1,64 @@ package subway.service; +import subway.config.constants.Targets; import subway.config.handler.SubwayException; import subway.domain.Line; import subway.domain.Station; import static subway.service.InitSubwayValues.lineRepo; import static subway.service.InitSubwayValues.stationRepo; +import static subway.service.utils.Constants.stationManager; public class SectionManager implements Managerable { - private Line line; + private Line line=null; SubwayException subwayException =new SubwayException(); @Override - public boolean isEmpty(final String name) { - if(subwayException.isBack(name) == true) - return false; - - line = lineRepo.getLineByName(name); - - if(line == null) - subwayException.noLine(); + public boolean isEmpty(final String station) { + if(subwayException.isBack(station) == true) return false; // TODO: 종료시키는 에러? 커스텀? + return isStationEmpty(station); + } + public boolean isEmpty(final String type, final String name){ + if(type.equals(Targets.STATION.getTarget())) return isStationEmpty(name); + if(type.equals(Targets.LINE.getTarget())) return isStationEmpty(name); return true; } + boolean isStationEmpty(final String station){ + if(stationRepo.getStationByName(station) == null ) return true; + return false; + } + boolean isLineEmpty(final String line){ + if(subwayException.isBack(line) == true) return false; // TODO: 종료시키는 에러? 커스텀? + if(lineRepo.getLineByName(line) == null ) return true; + return false; + } + public void insertSection(final String sName,final int index){ Station station = stationRepo.getStationByName(sName); if(station == null) subwayException.noStation(); lineRepo.addLine(line, station, index); } + public boolean getStation(String name){ + try{ + stationRepo.getStationByName(name); + return true; + }catch (Exception e) { + } + return false; + } + + public Line getLine(String name){ + Line line = lineRepo.getLineByName(name); + if(line == null) subwayException.noLine(); + this.line=line; + return line; + } + @Override - public boolean delete(final String name) { - if(subwayException.isBack(name) == true) return true; - if(line.deleteStation(name)) return true; + public boolean delete(final String station) { + if(subwayException.isBack(station) == true) return true; + if(line.deleteStation(station)) return true; subwayException.checkCommand(); return false; } From de2a6cd3b2208492fd50cb2b0668d5319b9ec6c3 Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 17 May 2024 17:34:02 +0900 Subject: [PATCH 088/104] =?UTF-8?q?refactor:=20SectionController=20?= =?UTF-8?q?=EC=A4=91=EC=8B=AC=20=EC=BD=94=EB=93=9C=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/config/handler/InputException.java | 7 +++++-- .../subControllers/SectionController.java | 17 +++++++++-------- .../java/subway/service/SectionManager.java | 13 ++----------- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/main/java/subway/config/handler/InputException.java b/src/main/java/subway/config/handler/InputException.java index df9774df2..abfbb8a72 100644 --- a/src/main/java/subway/config/handler/InputException.java +++ b/src/main/java/subway/config/handler/InputException.java @@ -22,9 +22,9 @@ public void alreadyCreatedStation(){ System.err.println(sb); } - public void alreadyCreated(String target){ + public void noCreatedLine(){ StringBuilder sb = new StringBuilder(); - sb.append(target).append("는 ").append(ADDED.getError()); + sb.append(DEL_STATION.getError()); System.err.println(sb); } @@ -33,4 +33,7 @@ public void unExpectedError(){ sb.append(UNEXPECTED.getError()); System.err.println(sb); } + + + } diff --git a/src/main/java/subway/controller/subControllers/SectionController.java b/src/main/java/subway/controller/subControllers/SectionController.java index e026ca6c4..6b5b8428b 100644 --- a/src/main/java/subway/controller/subControllers/SectionController.java +++ b/src/main/java/subway/controller/subControllers/SectionController.java @@ -1,5 +1,7 @@ package subway.controller.subControllers; +import subway.config.constants.Targets; +import subway.config.constants.views.Errors; import subway.config.handler.InputException; import subway.config.handler.SubwayException; import subway.controller.utils.ClassifyMethods; @@ -35,10 +37,13 @@ public boolean sendRequest(final Controller controller, final String command) { @Override public boolean register() { // 노선, 역 이름, 순서 입력 받고 등록 String line = method.getLine(); - if (!sectionManager.isEmpty(line)) subwayException.noLine(); + if (!sectionManager.isEmpty(Targets.LINE.getTarget(), line)) { + inputException.noCreatedLine(); + return false; + } String sName = method.getStation(); - if (!sectionManager.isEmpty(sName)) { + if (!sectionManager.isEmpty(Targets.STATION.getTarget(), sName)) { int index = method.getIndex(); sectionManager.insertSection(sName, index); } @@ -48,23 +53,19 @@ public boolean register() { // 노선, 역 이름, 순서 입력 받고 등록 @Override public boolean delete() { - String target = ""; try { String line = method.getLine(DELETE, "구간의 노선"); sectionManager.getLine(line); String station = method.getStation(sectionManager, DELETE, "구간의 역"); - if (!sectionManager.isEmpty(station)) { // TODO: 존재하지 않는 구간을 삭제하려 할 때 예외 처리 추가 + if (!sectionManager.isEmpty(Targets.STATION.getTarget(), station)) { // TODO: 존재하지 않는 구간을 삭제하려 할 때 예외 처리 추가 sectionManager.delete(station); response.printInfo("구간이 삭제되었습니다."); } } catch (IllegalArgumentException e) { - System.err.println("IllegalArgumentException"); + System.err.println(Errors.VALID.getError()); return false; - } catch (Exception e) { - System.err.println("noooooooooooo"); } - System.out.println("success?"); return true; } diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index c724ba1b2..f398bd002 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -7,7 +7,6 @@ import static subway.service.InitSubwayValues.lineRepo; import static subway.service.InitSubwayValues.stationRepo; -import static subway.service.utils.Constants.stationManager; public class SectionManager implements Managerable { private Line line=null; @@ -18,8 +17,9 @@ public boolean isEmpty(final String station) { return isStationEmpty(station); } public boolean isEmpty(final String type, final String name){ + if(subwayException.isBack(name) == true) return false; // TODO: 종료시키는 에러? 커스텀? if(type.equals(Targets.STATION.getTarget())) return isStationEmpty(name); - if(type.equals(Targets.LINE.getTarget())) return isStationEmpty(name); + if(type.equals(Targets.LINE.getTarget())) return isLineEmpty(name); return true; } @@ -39,15 +39,6 @@ public void insertSection(final String sName,final int index){ lineRepo.addLine(line, station, index); } - public boolean getStation(String name){ - try{ - stationRepo.getStationByName(name); - return true; - }catch (Exception e) { - } - return false; - } - public Line getLine(String name){ Line line = lineRepo.getLineByName(name); if(line == null) subwayException.noLine(); From f74b20c94810478bec8ac0df6377f7d55f14f990 Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 17 May 2024 17:55:07 +0900 Subject: [PATCH 089/104] =?UTF-8?q?fix:=20=EC=82=AD=EC=A0=9C=20=ED=9B=84?= =?UTF-8?q?=20=EB=85=B8=EC=84=A0=EA=B3=BC=20=EC=97=B0=EA=B2=B0=20=ED=95=B4?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/domain/Station.java | 24 ++++++++++++------- .../java/subway/service/StationManager.java | 10 ++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/main/java/subway/domain/Station.java b/src/main/java/subway/domain/Station.java index 413c6ddaf..5a57136c7 100644 --- a/src/main/java/subway/domain/Station.java +++ b/src/main/java/subway/domain/Station.java @@ -7,7 +7,7 @@ public class Station { private String name; private List line = new LinkedList(); - private int canDelete=0; + private int canDelete = 0; public Station(final String name) { this.name = name; @@ -16,25 +16,31 @@ public Station(final String name) { public String getName() { return this.name; } - public void addLine(Line lineNode){ + + public void addLine(Line lineNode) { line.add(lineNode); } - public void deleteLine(Line lineNode){ + + public void deleteLine(Line lineNode) { line.remove(lineNode); } - public boolean hasNoLine(){ - if(canDelete==0){ + public List getLine() { + return this.line; + } + + public boolean hasNoLine() { + if (canDelete == 0) { return true; } return false; } - public void canDelete(){ - this.canDelete-=1; + public void canDelete() { + this.canDelete -= 1; } - public void neverDelete(){ - this.canDelete+=1; + public void neverDelete() { + this.canDelete += 1; } } diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index 22455904f..914d56201 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -2,7 +2,11 @@ import subway.config.handler.InputException; import subway.config.handler.SubwayException; +import subway.domain.Line; import subway.domain.Station; + +import java.util.List; + import static subway.service.InitSubwayValues.stationRepo; // 역 관련 기능하는 서비스 @@ -19,6 +23,12 @@ public boolean isEmpty(final String name) { } @Override public boolean delete(final String name) { + Station station = stationRepo.getStationByName(name); + List lines = station.getLine(); + + for(Line line : lines){ + line.deleteStation(name); + } return stationRepo.deleteStation(name); } From fc399b03546e936378fee435ee078ef6c18a5f18 Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 17 May 2024 18:10:51 +0900 Subject: [PATCH 090/104] =?UTF-8?q?fix:=20SectionController=20Register=20?= =?UTF-8?q?=EC=A1=B0=EA=B1=B4=EC=8B=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/subway/config/handler/InputException.java | 2 +- .../subControllers/SectionController.java | 13 +++++++------ src/main/java/subway/domain/Line.java | 7 +++++++ src/main/java/subway/service/SectionManager.java | 11 +++++++++-- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/main/java/subway/config/handler/InputException.java b/src/main/java/subway/config/handler/InputException.java index abfbb8a72..341939d76 100644 --- a/src/main/java/subway/config/handler/InputException.java +++ b/src/main/java/subway/config/handler/InputException.java @@ -24,7 +24,7 @@ public void alreadyCreatedStation(){ public void noCreatedLine(){ StringBuilder sb = new StringBuilder(); - sb.append(DEL_STATION.getError()); + sb.append(DEL_LINE.getError()); System.err.println(sb); } diff --git a/src/main/java/subway/controller/subControllers/SectionController.java b/src/main/java/subway/controller/subControllers/SectionController.java index 6b5b8428b..0b3577ede 100644 --- a/src/main/java/subway/controller/subControllers/SectionController.java +++ b/src/main/java/subway/controller/subControllers/SectionController.java @@ -37,16 +37,17 @@ public boolean sendRequest(final Controller controller, final String command) { @Override public boolean register() { // 노선, 역 이름, 순서 입력 받고 등록 String line = method.getLine(); - if (!sectionManager.isEmpty(Targets.LINE.getTarget(), line)) { + if (sectionManager.isEmpty(Targets.LINE.getTarget(), line)) { inputException.noCreatedLine(); return false; } + String station = method.getStation(); + if(!sectionManager.isValidLine(line, station)) return false; // TODO: 예외처리 + if(sectionManager.isEmpty(Targets.STATION.getTarget(), station)) return false; // TODO: 예외처리 + + int index = method.getIndex(); + sectionManager.insertSection(station, index); - String sName = method.getStation(); - if (!sectionManager.isEmpty(Targets.STATION.getTarget(), sName)) { - int index = method.getIndex(); - sectionManager.insertSection(sName, index); - } response.printInfo("구간이 등록되었습니다."); return true; } diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index dfef43ba7..40b415f2d 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -49,6 +49,13 @@ public boolean deleteStation(final String station) { return false; } + public boolean haveStation(final String name){ + for(Station station:stations){ + if(name.equals(station.getName())) return true; + } + return false; + } + public int getSize(){ return this.stations.size(); } diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index f398bd002..a0681f0f8 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -19,7 +19,7 @@ public boolean isEmpty(final String station) { public boolean isEmpty(final String type, final String name){ if(subwayException.isBack(name) == true) return false; // TODO: 종료시키는 에러? 커스텀? if(type.equals(Targets.STATION.getTarget())) return isStationEmpty(name); - if(type.equals(Targets.LINE.getTarget())) return isLineEmpty(name); + if(type.equals(Targets.LINE.getTarget())) return isValidLine(name); return true; } @@ -27,11 +27,18 @@ boolean isStationEmpty(final String station){ if(stationRepo.getStationByName(station) == null ) return true; return false; } - boolean isLineEmpty(final String line){ + boolean isValidLine(final String line){ if(subwayException.isBack(line) == true) return false; // TODO: 종료시키는 에러? 커스텀? if(lineRepo.getLineByName(line) == null ) return true; return false; } + public boolean isValidLine(final String lName, final String sName){ + Line line = getLine(lName); + if(line.haveStation(sName)){ + return false; + } + return true; + } public void insertSection(final String sName,final int index){ Station station = stationRepo.getStationByName(sName); From 45f20acfa2d0bf292f69e7a0a070f25b6645470e Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 17 May 2024 18:22:49 +0900 Subject: [PATCH 091/104] =?UTF-8?q?fix:=20LineController=20=EB=93=B1?= =?UTF-8?q?=EB=A1=9D=20=EA=B4=80=EB=A0=A8=20Empty=20=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=EC=A1=B0=EA=B1=B4=EB=AC=B8=20=EB=B3=80=EA=B2=BD(getStation=20?= =?UTF-8?q?=EC=97=90=EC=84=9C=EB=8F=84=20=EC=B0=B8=EC=A1=B0=EC=A4=91?= =?UTF-8?q?=EC=9D=B4=EC=97=88=EC=9D=8C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subControllers/LineController.java | 1 + src/main/java/subway/service/LineManager.java | 25 +++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/subway/controller/subControllers/LineController.java b/src/main/java/subway/controller/subControllers/LineController.java index 5be25f652..96715fc4b 100644 --- a/src/main/java/subway/controller/subControllers/LineController.java +++ b/src/main/java/subway/controller/subControllers/LineController.java @@ -30,6 +30,7 @@ public boolean register() { lineManager.setStations(line, upper, bottom); } catch (IllegalArgumentException e) { inputException.noStation(); + return false; } message= makeString.infoMessage(REGISTER, LINE, true); System.out.println(message); diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index e10f30439..968d48e5d 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -1,5 +1,6 @@ package subway.service; +import subway.config.constants.Targets; import subway.domain.Line; import subway.domain.Station; import subway.service.utils.LineMakeString; @@ -11,16 +12,26 @@ public class LineManager implements Managerable { LineMakeString makeString = new LineMakeString(); @Override - public boolean isEmpty(final String name) { - if(subwayException.isBack(name)){ - return false; - } - if(lineRepo.getLineByName(name) == null){ - return true; - } + public boolean isEmpty(final String station) { + if(subwayException.isBack(station) == true) return false; // TODO: 종료시키는 에러? 커스텀? + return isStationEmpty(station); + } + public boolean isEmpty(final String type, final String name){ + if(subwayException.isBack(name) == true) return false; // TODO: 종료시키는 에러? 커스텀? + if(type.equals(Targets.STATION.getTarget())) return isStationEmpty(name); + if(type.equals(Targets.LINE.getTarget())) return isValidLine(name); + return true; + } + boolean isValidLine(final String line){ + if(subwayException.isBack(line) == true) return false; // TODO: 종료시키는 에러? 커스텀? + if(lineRepo.getLineByName(line) == null ) return true; return false; } + boolean isStationEmpty(final String station){ + if(stationRepo.getStationByName(station) == null ) return true; + return false; + } public Line setLine(final String name){ Line node = new Line(name); lineRepo.addLine(node); From 608a1caf1439d4a396ae2ceeaff6caca2c311d9c Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 17 May 2024 18:31:51 +0900 Subject: [PATCH 092/104] =?UTF-8?q?feat:=20=EB=85=B8=EC=84=A0=EC=97=90=20?= =?UTF-8?q?=EC=86=8D=ED=95=9C=20=EC=97=AD=EC=9D=B4=202=EA=B0=9C=20?= =?UTF-8?q?=EC=9D=B4=ED=95=98=EC=9D=BC=20=EB=95=8C=20=EC=97=AD=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EA=B8=88=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/config/constants/views/Errors.java | 1 + src/main/java/subway/config/handler/InputException.java | 6 +++++- src/main/java/subway/config/handler/SubwayException.java | 4 ++++ .../subway/controller/subControllers/StationController.java | 5 ++++- src/main/java/subway/domain/Line.java | 3 +++ src/main/java/subway/service/LineManager.java | 2 +- 6 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/subway/config/constants/views/Errors.java b/src/main/java/subway/config/constants/views/Errors.java index 4b5118571..d79114e4b 100644 --- a/src/main/java/subway/config/constants/views/Errors.java +++ b/src/main/java/subway/config/constants/views/Errors.java @@ -7,6 +7,7 @@ public enum Errors { ADDED("이미 등록된 이름입니다."), DEL_STATION("존재하지 않은 역 이름입니다."), UNEXPECTED("예상치 못한 에러가 발생했습니다."), + UNDER_TWO_STATIONS("속한 역의 개수가 2개 이하인 노선은 삭제할 수 없습니다."), DEL_LINE("존재하지 않는 노선 이름입니다."); private final String message; diff --git a/src/main/java/subway/config/handler/InputException.java b/src/main/java/subway/config/handler/InputException.java index 341939d76..84aa6505f 100644 --- a/src/main/java/subway/config/handler/InputException.java +++ b/src/main/java/subway/config/handler/InputException.java @@ -35,5 +35,9 @@ public void unExpectedError(){ } - + public void underTwoStation() { + StringBuilder sb = new StringBuilder(); + sb.append(UNDER_TWO_STATIONS.getError()); + System.err.println(sb); + } } diff --git a/src/main/java/subway/config/handler/SubwayException.java b/src/main/java/subway/config/handler/SubwayException.java index e826ae3ee..e79adec98 100644 --- a/src/main/java/subway/config/handler/SubwayException.java +++ b/src/main/java/subway/config/handler/SubwayException.java @@ -3,6 +3,7 @@ import subway.config.constants.views.Errors; import subway.config.constants.views.Prefixes; +import static subway.config.constants.views.Errors.UNDER_TWO_STATIONS; import static subway.config.handler.InputExceptionError.ErrorMessage.*; public class SubwayException extends RuntimeException { @@ -50,4 +51,7 @@ public boolean noStation() { public void noLine() { throw new IllegalArgumentException(THERE_IS_NO_SUCH_LINE.getMessage()); } + public void underTwoStation() { + throw new IllegalArgumentException(); + } } diff --git a/src/main/java/subway/controller/subControllers/StationController.java b/src/main/java/subway/controller/subControllers/StationController.java index fe62f8025..c7d8cec5c 100644 --- a/src/main/java/subway/controller/subControllers/StationController.java +++ b/src/main/java/subway/controller/subControllers/StationController.java @@ -39,7 +39,10 @@ public boolean delete() { boolean result=false; try { result = stationManager.delete(command); - } catch (Exception e) { + } catch(IllegalArgumentException e){ + inputException.underTwoStation(); + return false; + }catch (Exception e) { inputException.unExpectedError(); return false; } diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index 40b415f2d..81cc84ee3 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -2,6 +2,8 @@ import java.util.LinkedList; +import static subway.service.Managerable.subwayException; + public class Line { private String name; private LinkedList stations = new LinkedList<>(); @@ -39,6 +41,7 @@ public void addStation(final Station station, final int index) { } public boolean deleteStation(final String station) { + if(stations.size() <= 2) subwayException.underTwoStation(); for (int index = 0; index < stations.size(); index++) { Station s = stations.get(index); if (s.getName().equals(station)) { diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index 968d48e5d..216ec1142 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -11,7 +11,7 @@ public class LineManager implements Managerable { LineMakeString makeString = new LineMakeString(); - @Override + @Override // TODO: Util 화? 할까? Section 이랑도 너무 겹치는데 public boolean isEmpty(final String station) { if(subwayException.isBack(station) == true) return false; // TODO: 종료시키는 에러? 커스텀? return isStationEmpty(station); From ae647f46081e7e4bd03bcc24c6e156745ec1278c Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 17 May 2024 18:56:57 +0900 Subject: [PATCH 093/104] =?UTF-8?q?refactor:=20Verify=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=83=9D=EC=84=B1,=20=EA=B0=9D=EC=B2=B4=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=20=EA=B0=80=EB=8A=A5=20=EC=97=AC=EB=B6=80=20?= =?UTF-8?q?=ED=8C=90=EB=B3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subControllers/StationController.java | 13 ++++-- .../java/subway/controller/utils/Methods.java | 8 ++-- src/main/java/subway/domain/Line.java | 2 +- src/main/java/subway/service/LineManager.java | 25 ++--------- .../java/subway/service/SectionManager.java | 33 ++------------ .../java/subway/service/StationManager.java | 15 +++---- .../service/{ => utils}/Managerable.java | 7 +-- .../java/subway/service/utils/Verify.java | 45 +++++++++++++++++++ 8 files changed, 76 insertions(+), 72 deletions(-) rename src/main/java/subway/service/{ => utils}/Managerable.java (64%) create mode 100644 src/main/java/subway/service/utils/Verify.java diff --git a/src/main/java/subway/controller/subControllers/StationController.java b/src/main/java/subway/controller/subControllers/StationController.java index c7d8cec5c..8bd03ea2e 100644 --- a/src/main/java/subway/controller/subControllers/StationController.java +++ b/src/main/java/subway/controller/subControllers/StationController.java @@ -1,5 +1,6 @@ package subway.controller.subControllers; +import subway.config.constants.Targets; import subway.config.handler.InputException; import subway.config.handler.SubwayException; import subway.controller.utils.ClassifyMethods; @@ -22,11 +23,13 @@ public boolean register() { ask.orderWhere(REGISTER, STATION); boolean result = false; String station = method.getUserInput(); - try { - result = stationManager.isEmpty(station); - } catch (Exception e) { + if(!stationManager.isEmpty(Targets.STATION.getTarget(), station)){ inputException.alreadyCreatedStation(); + return false; } + stationManager.register(station); + result = true; + String message = makeString.infoMessage(REGISTER, STATION, result); response.printInfo(message); return result; @@ -37,6 +40,10 @@ public boolean delete() { ask.orderWhere(DELETE, STATION); String command = method.getUserInput(); boolean result=false; + if(method.isEmpty(command)) { + inputException.noStation(); + return false; + } try { result = stationManager.delete(command); } catch(IllegalArgumentException e){ diff --git a/src/main/java/subway/controller/utils/Methods.java b/src/main/java/subway/controller/utils/Methods.java index 5c1cf7fd6..dfbe448ec 100644 --- a/src/main/java/subway/controller/utils/Methods.java +++ b/src/main/java/subway/controller/utils/Methods.java @@ -1,7 +1,9 @@ package subway.controller.utils; +import subway.config.constants.Targets; import subway.config.handler.InputException; -import subway.service.Managerable; +import subway.service.utils.Managerable; +import subway.service.utils.Verify; import java.io.BufferedReader; import java.io.IOException; @@ -10,7 +12,7 @@ import static subway.controller.utils.ClassifyMethods.checkCommand; import static subway.controller.utils.ClassifyMethods.subwayException; -public class Methods implements Constants { +public class Methods extends Verify implements Constants { private BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); InputException inputException = new InputException(); public Methods() {} @@ -65,7 +67,7 @@ public String getStation() { public String getStation(Managerable manager, String function, String station) { ask.orderWhere(function, station); String node = getUserInput(); - if (manager.isEmpty(node) == true) { + if (manager.isEmpty(Targets.STATION.getTarget(),node) == true) { subwayException.noStation(); } return node; diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index 81cc84ee3..6de66a0bc 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -2,7 +2,7 @@ import java.util.LinkedList; -import static subway.service.Managerable.subwayException; +import static subway.service.utils.Managerable.subwayException; public class Line { private String name; diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index 216ec1142..3029fa302 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -4,34 +4,15 @@ import subway.domain.Line; import subway.domain.Station; import subway.service.utils.LineMakeString; +import subway.service.utils.Managerable; +import subway.service.utils.Verify; import java.util.List; import static subway.service.InitSubwayValues.*; -public class LineManager implements Managerable { +public class LineManager extends Verify implements Managerable { LineMakeString makeString = new LineMakeString(); - @Override // TODO: Util 화? 할까? Section 이랑도 너무 겹치는데 - public boolean isEmpty(final String station) { - if(subwayException.isBack(station) == true) return false; // TODO: 종료시키는 에러? 커스텀? - return isStationEmpty(station); - } - public boolean isEmpty(final String type, final String name){ - if(subwayException.isBack(name) == true) return false; // TODO: 종료시키는 에러? 커스텀? - if(type.equals(Targets.STATION.getTarget())) return isStationEmpty(name); - if(type.equals(Targets.LINE.getTarget())) return isValidLine(name); - return true; - } - boolean isValidLine(final String line){ - if(subwayException.isBack(line) == true) return false; // TODO: 종료시키는 에러? 커스텀? - if(lineRepo.getLineByName(line) == null ) return true; - return false; - } - - boolean isStationEmpty(final String station){ - if(stationRepo.getStationByName(station) == null ) return true; - return false; - } public Line setLine(final String name){ Line node = new Line(name); lineRepo.addLine(node); diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index a0681f0f8..9a1a0cec8 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -1,44 +1,17 @@ package subway.service; -import subway.config.constants.Targets; import subway.config.handler.SubwayException; import subway.domain.Line; import subway.domain.Station; +import subway.service.utils.Managerable; +import subway.service.utils.Verify; import static subway.service.InitSubwayValues.lineRepo; import static subway.service.InitSubwayValues.stationRepo; -public class SectionManager implements Managerable { +public class SectionManager extends Verify implements Managerable { private Line line=null; SubwayException subwayException =new SubwayException(); - @Override - public boolean isEmpty(final String station) { - if(subwayException.isBack(station) == true) return false; // TODO: 종료시키는 에러? 커스텀? - return isStationEmpty(station); - } - public boolean isEmpty(final String type, final String name){ - if(subwayException.isBack(name) == true) return false; // TODO: 종료시키는 에러? 커스텀? - if(type.equals(Targets.STATION.getTarget())) return isStationEmpty(name); - if(type.equals(Targets.LINE.getTarget())) return isValidLine(name); - return true; - } - - boolean isStationEmpty(final String station){ - if(stationRepo.getStationByName(station) == null ) return true; - return false; - } - boolean isValidLine(final String line){ - if(subwayException.isBack(line) == true) return false; // TODO: 종료시키는 에러? 커스텀? - if(lineRepo.getLineByName(line) == null ) return true; - return false; - } - public boolean isValidLine(final String lName, final String sName){ - Line line = getLine(lName); - if(line.haveStation(sName)){ - return false; - } - return true; - } public void insertSection(final String sName,final int index){ Station station = stationRepo.getStationByName(sName); diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index 914d56201..2a01fec5e 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -1,25 +1,20 @@ package subway.service; import subway.config.handler.InputException; -import subway.config.handler.SubwayException; import subway.domain.Line; import subway.domain.Station; +import subway.service.utils.Managerable; +import subway.service.utils.Verify; import java.util.List; import static subway.service.InitSubwayValues.stationRepo; // 역 관련 기능하는 서비스 -public class StationManager implements Managerable { - InputException inputException= new InputException(); - @Override // 삽입 - public boolean isEmpty(final String name) { - if(stationRepo.getStationByName(name)!=null){ - inputException.alreadyCreatedStation(); - return false; - } +public class StationManager extends Verify implements Managerable { + + public void register(String name){ stationRepo.addStation(new Station(name)); - return true; } @Override public boolean delete(final String name) { diff --git a/src/main/java/subway/service/Managerable.java b/src/main/java/subway/service/utils/Managerable.java similarity index 64% rename from src/main/java/subway/service/Managerable.java rename to src/main/java/subway/service/utils/Managerable.java index 55525866e..75e2b4986 100644 --- a/src/main/java/subway/service/Managerable.java +++ b/src/main/java/subway/service/utils/Managerable.java @@ -1,10 +1,11 @@ -package subway.service; +package subway.service.utils; import subway.config.handler.SubwayException; -public interface Managerable { - public boolean isEmpty(final String command); +public interface Managerable{ public boolean delete(final String name); public StringBuilder read(); static final SubwayException subwayException = new SubwayException(); + + public boolean isEmpty(String type,String node); } diff --git a/src/main/java/subway/service/utils/Verify.java b/src/main/java/subway/service/utils/Verify.java new file mode 100644 index 000000000..25b56e809 --- /dev/null +++ b/src/main/java/subway/service/utils/Verify.java @@ -0,0 +1,45 @@ +package subway.service.utils; + +import subway.config.constants.Targets; +import subway.config.handler.SubwayException; +import subway.domain.Line; + +import static subway.service.utils.Constants.lineRepo; +import static subway.service.utils.Constants.stationRepo; + +public class Verify { + SubwayException subwayException = new SubwayException(); + public boolean isEmpty(final String station) { + if(subwayException.isBack(station) == true) return false; // TODO: 종료시키는 에러? 커스텀? + return isStationEmpty(station); + } + public boolean isEmpty(final String type, final String name){ + if(subwayException.isBack(name) == true) return false; // TODO: 종료시키는 에러? 커스텀? + if(type.equals(Targets.STATION.getTarget())) return isStationEmpty(name); + if(type.equals(Targets.LINE.getTarget())) return isValidLine(name); + return true; + } + + boolean isStationEmpty(final String station){ + if(stationRepo.getStationByName(station) == null ) return true; + return false; + } + boolean isValidLine(final String line){ + if(subwayException.isBack(line) == true) return false; // TODO: 종료시키는 에러? 커스텀? + if(lineRepo.getLineByName(line) == null ) return true; + return false; + } + public boolean isValidLine(final String lName, final String sName){ + Line line = getLine(lName); + if(line.haveStation(sName)){ + return false; + } + return true; + } + public Line getLine(String name){ + Line line = lineRepo.getLineByName(name); + if(line == null) subwayException.noLine(); + return line; + } + +} From b51622018e7b7c50ca12d90b6f47f7d2ed2ca7a8 Mon Sep 17 00:00:00 2001 From: aristo Date: Fri, 17 May 2024 19:08:33 +0900 Subject: [PATCH 094/104] =?UTF-8?q?fix:=20=EC=A1=B4=EC=9E=AC=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EC=97=AD=EA=B3=BC=20=EB=8F=99=EC=9D=BC=ED=95=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=EC=9D=98=20=EC=97=AD=20=EC=9D=B4=EB=A6=84=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EB=B6=88=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/config/handler/InputException.java | 6 ++++++ .../subway/controller/subControllers/LineController.java | 7 ++++++- src/main/java/subway/service/utils/Verify.java | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/subway/config/handler/InputException.java b/src/main/java/subway/config/handler/InputException.java index 84aa6505f..9261d5b0c 100644 --- a/src/main/java/subway/config/handler/InputException.java +++ b/src/main/java/subway/config/handler/InputException.java @@ -40,4 +40,10 @@ public void underTwoStation() { sb.append(UNDER_TWO_STATIONS.getError()); System.err.println(sb); } + + public void lineEqualStation(){ + StringBuilder sb = new StringBuilder(); + sb.append(ADD_STATION.getError()); + System.err.println(sb); + } } diff --git a/src/main/java/subway/controller/subControllers/LineController.java b/src/main/java/subway/controller/subControllers/LineController.java index 96715fc4b..682ab9b46 100644 --- a/src/main/java/subway/controller/subControllers/LineController.java +++ b/src/main/java/subway/controller/subControllers/LineController.java @@ -1,5 +1,6 @@ package subway.controller.subControllers; +import subway.config.constants.Targets; import subway.config.handler.InputException; import subway.config.handler.SubwayException; import subway.controller.utils.ClassifyMethods; @@ -21,10 +22,14 @@ public boolean register() { String upper, bottom, message; String line = method.getLine(REGISTER, LINE); try { - if (lineManager.isEmpty(line) != true) { + if (lineManager.isEmpty(Targets.LINE.getTarget(), line) != true) { inputException.alreadyCreatedLine(); return false; } + if(lineManager.lineEqualStation(line)) { + inputException.lineEqualStation(); + return false; + } upper = method.getStation(lineManager, REGISTER, UPPER); bottom = method.getStation(lineManager, REGISTER, BOTTOM); lineManager.setStations(line, upper, bottom); diff --git a/src/main/java/subway/service/utils/Verify.java b/src/main/java/subway/service/utils/Verify.java index 25b56e809..71ff063ae 100644 --- a/src/main/java/subway/service/utils/Verify.java +++ b/src/main/java/subway/service/utils/Verify.java @@ -9,6 +9,7 @@ public class Verify { SubwayException subwayException = new SubwayException(); + public boolean isEmpty(final String station) { if(subwayException.isBack(station) == true) return false; // TODO: 종료시키는 에러? 커스텀? return isStationEmpty(station); @@ -29,6 +30,13 @@ boolean isValidLine(final String line){ if(lineRepo.getLineByName(line) == null ) return true; return false; } + + public boolean lineEqualStation(String line){ + if(stationRepo.getStationByName(line) != null) { + return true; + } + return false; + } public boolean isValidLine(final String lName, final String sName){ Line line = getLine(lName); if(line.haveStation(sName)){ From 9009f7bfe84d9ee7dae1034684bb72c9d764f098 Mon Sep 17 00:00:00 2001 From: aristo Date: Sat, 18 May 2024 14:34:46 +0900 Subject: [PATCH 095/104] =?UTF-8?q?refactor:=20lineController=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20=EC=BD=94=EB=93=9C=20=EB=AA=A8=EB=93=88=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/MainController.java | 4 +- .../subControllers/LineController.java | 39 +++++++++++-------- .../subControllers/SectionController.java | 17 ++++---- .../subControllers/StationController.java | 14 +++---- .../subway/controller/utils/Constants.java | 2 + .../subway/controller/utils/Controller.java | 2 +- .../utils/{Methods.java => GetUserInput.java} | 22 +++++------ .../subway/controller/utils/SetPrint.java | 15 +++++++ src/main/java/subway/service/LineManager.java | 3 ++ .../java/subway/service/utils/Verify.java | 20 ++++++---- src/main/java/subway/view/AskView.java | 6 +-- 11 files changed, 82 insertions(+), 62 deletions(-) rename src/main/java/subway/controller/utils/{Methods.java => GetUserInput.java} (75%) create mode 100644 src/main/java/subway/controller/utils/SetPrint.java diff --git a/src/main/java/subway/controller/MainController.java b/src/main/java/subway/controller/MainController.java index 12e3e3d57..7fb8b2f0b 100644 --- a/src/main/java/subway/controller/MainController.java +++ b/src/main/java/subway/controller/MainController.java @@ -7,7 +7,7 @@ import subway.controller.subControllers.StationController; import subway.controller.utils.CheckCommand; import subway.controller.utils.Constants; -import subway.controller.utils.Methods; +import subway.controller.utils.GetUserInput; import subway.service.InitSubwayValues; import subway.view.AskView; @@ -19,7 +19,7 @@ public class MainController implements Constants { private MapController map; private static SubwayException subwayException; private CheckCommand checkCommand = new CheckCommand(); - private Methods method=new Methods(); + private GetUserInput method=new GetUserInput(); public MainController() { diff --git a/src/main/java/subway/controller/subControllers/LineController.java b/src/main/java/subway/controller/subControllers/LineController.java index 682ab9b46..f1a53d02e 100644 --- a/src/main/java/subway/controller/subControllers/LineController.java +++ b/src/main/java/subway/controller/subControllers/LineController.java @@ -1,6 +1,5 @@ package subway.controller.subControllers; -import subway.config.constants.Targets; import subway.config.handler.InputException; import subway.config.handler.SubwayException; import subway.controller.utils.ClassifyMethods; @@ -19,34 +18,42 @@ public LineController(final InitSubwayValues manager) { @Override public boolean register() { - String upper, bottom, message; String line = method.getLine(REGISTER, LINE); + if(!canRegister(line)) return false; + if(!addSubStations(line)) return false; + return setPrint.printResult(REGISTER, LINE, true); + } + + public boolean addSubStations(String line){ + String upper, bottom; try { - if (lineManager.isEmpty(Targets.LINE.getTarget(), line) != true) { - inputException.alreadyCreatedLine(); - return false; - } - if(lineManager.lineEqualStation(line)) { - inputException.lineEqualStation(); - return false; - } + if(!canRegister(line)) return false; upper = method.getStation(lineManager, REGISTER, UPPER); bottom = method.getStation(lineManager, REGISTER, BOTTOM); lineManager.setStations(line, upper, bottom); + return true; } catch (IllegalArgumentException e) { inputException.noStation(); + } + return false; + } + + public boolean canRegister(String line){ + if (lineManager.isEmpty(LINE, line) != true) { + inputException.alreadyCreatedLine(); + return false; + } + if(lineManager.lineEqualStation(line)) { + inputException.lineEqualStation(); return false; } - message= makeString.infoMessage(REGISTER, LINE, true); - System.out.println(message); return true; } @Override public boolean delete() { - String message; boolean result=false; - ask.orderWhere(DELETE, LINE); + ask.orderIndex(DELETE, LINE); try { String command = method.getUserInput(); result = lineManager.delete(command); @@ -54,9 +61,7 @@ public boolean delete() { subwayException.noLine(); return false; } - message=makeString.infoMessage(DELETE,LINE, result); - System.out.println(message); - return true; + return setPrint.printResult(DELETE, LINE, result); } public boolean read() { diff --git a/src/main/java/subway/controller/subControllers/SectionController.java b/src/main/java/subway/controller/subControllers/SectionController.java index 0b3577ede..b03fc3df3 100644 --- a/src/main/java/subway/controller/subControllers/SectionController.java +++ b/src/main/java/subway/controller/subControllers/SectionController.java @@ -1,6 +1,5 @@ package subway.controller.subControllers; -import subway.config.constants.Targets; import subway.config.constants.views.Errors; import subway.config.handler.InputException; import subway.config.handler.SubwayException; @@ -37,19 +36,18 @@ public boolean sendRequest(final Controller controller, final String command) { @Override public boolean register() { // 노선, 역 이름, 순서 입력 받고 등록 String line = method.getLine(); - if (sectionManager.isEmpty(Targets.LINE.getTarget(), line)) { + if (sectionManager.isEmpty(LINE, line)) { inputException.noCreatedLine(); return false; } String station = method.getStation(); if(!sectionManager.isValidLine(line, station)) return false; // TODO: 예외처리 - if(sectionManager.isEmpty(Targets.STATION.getTarget(), station)) return false; // TODO: 예외처리 + if(sectionManager.isEmpty(STATION, station)) return false; // TODO: 예외처리 int index = method.getIndex(); sectionManager.insertSection(station, index); - response.printInfo("구간이 등록되었습니다."); - return true; + return setPrint.printResult(REGISTER, SECTION, true); } @Override @@ -59,15 +57,14 @@ public boolean delete() { sectionManager.getLine(line); String station = method.getStation(sectionManager, DELETE, "구간의 역"); - if (!sectionManager.isEmpty(Targets.STATION.getTarget(), station)) { // TODO: 존재하지 않는 구간을 삭제하려 할 때 예외 처리 추가 - sectionManager.delete(station); - response.printInfo("구간이 삭제되었습니다."); + if (!sectionManager.isEmpty(STATION, station)) { + return setPrint.printResult(DELETE, STATION, true); } +// response.printInfo("존재하지 않는 역에 접근했습니다."); } catch (IllegalArgumentException e) { System.err.println(Errors.VALID.getError()); - return false; } - return true; + return false; } @Override diff --git a/src/main/java/subway/controller/subControllers/StationController.java b/src/main/java/subway/controller/subControllers/StationController.java index 8bd03ea2e..231f3adda 100644 --- a/src/main/java/subway/controller/subControllers/StationController.java +++ b/src/main/java/subway/controller/subControllers/StationController.java @@ -20,9 +20,10 @@ public StationController(final InitSubwayValues manager) { @Override public boolean register() { - ask.orderWhere(REGISTER, STATION); + ask.orderIndex(REGISTER, STATION); boolean result = false; String station = method.getUserInput(); + // 조건 모듈 if(!stationManager.isEmpty(Targets.STATION.getTarget(), station)){ inputException.alreadyCreatedStation(); return false; @@ -30,14 +31,12 @@ public boolean register() { stationManager.register(station); result = true; - String message = makeString.infoMessage(REGISTER, STATION, result); - response.printInfo(message); - return result; + return setPrint.printResult(REGISTER, STATION, result); } @Override public boolean delete() { - ask.orderWhere(DELETE, STATION); + ask.orderIndex(DELETE, STATION); String command = method.getUserInput(); boolean result=false; if(method.isEmpty(command)) { @@ -53,9 +52,8 @@ public boolean delete() { inputException.unExpectedError(); return false; } - String message = makeString.infoMessage(DELETE, STATION, result); - response.printInfo(message); - return true; + + return setPrint.printResult(DELETE, STATION, result); } public boolean read() { diff --git a/src/main/java/subway/controller/utils/Constants.java b/src/main/java/subway/controller/utils/Constants.java index 11feb8664..590f0a54f 100644 --- a/src/main/java/subway/controller/utils/Constants.java +++ b/src/main/java/subway/controller/utils/Constants.java @@ -29,4 +29,6 @@ public interface Constants { static final AskView ask = new AskView(); static final ResponseView response = new ResponseView(); static final MakeString makeString = new MakeString(); + + static SetPrint setPrint = new SetPrint(); } diff --git a/src/main/java/subway/controller/utils/Controller.java b/src/main/java/subway/controller/utils/Controller.java index 82e0ba4cf..f9927afb2 100644 --- a/src/main/java/subway/controller/utils/Controller.java +++ b/src/main/java/subway/controller/utils/Controller.java @@ -1,7 +1,7 @@ package subway.controller.utils; public interface Controller { - static final Methods method = new Methods(); + static final GetUserInput method = new GetUserInput(); public boolean register(); public boolean delete(); diff --git a/src/main/java/subway/controller/utils/Methods.java b/src/main/java/subway/controller/utils/GetUserInput.java similarity index 75% rename from src/main/java/subway/controller/utils/Methods.java rename to src/main/java/subway/controller/utils/GetUserInput.java index dfbe448ec..e13d0ea42 100644 --- a/src/main/java/subway/controller/utils/Methods.java +++ b/src/main/java/subway/controller/utils/GetUserInput.java @@ -1,7 +1,5 @@ package subway.controller.utils; -import subway.config.constants.Targets; -import subway.config.handler.InputException; import subway.service.utils.Managerable; import subway.service.utils.Verify; @@ -12,10 +10,9 @@ import static subway.controller.utils.ClassifyMethods.checkCommand; import static subway.controller.utils.ClassifyMethods.subwayException; -public class Methods extends Verify implements Constants { +public class GetUserInput extends Verify implements Constants { private BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); - InputException inputException = new InputException(); - public Methods() {} + public GetUserInput() {} public void closeBuffer(){ try{ @@ -35,7 +32,7 @@ public String getUserInput(){ } public int getIndex() { - ask.orderWhere(); + ask.orderIndex(); String input = getUserInput(); if (subwayException.isBack(input) == true) return -1; int index = checkCommand.strToInt(input); @@ -44,10 +41,9 @@ public int getIndex() { } public String getLine() { - ask.orderWhere(LINE); + ask.orderIndex(LINE); try { - String line = getUserInput(); - return line; + return getUserInput(); } catch (Exception e) { subwayException.checkCommand(); } @@ -55,19 +51,19 @@ public String getLine() { } public String getLine(String function, String target) { - ask.orderWhere(function, target); + ask.orderIndex(function, target); return getUserInput(); } public String getStation() { - ask.orderWhere("역 이름"); + ask.orderIndex("역 이름"); return getUserInput(); } public String getStation(Managerable manager, String function, String station) { - ask.orderWhere(function, station); + ask.orderIndex(function, station); String node = getUserInput(); - if (manager.isEmpty(Targets.STATION.getTarget(),node) == true) { + if (manager.isEmpty(STATION,node) == true) { subwayException.noStation(); } return node; diff --git a/src/main/java/subway/controller/utils/SetPrint.java b/src/main/java/subway/controller/utils/SetPrint.java new file mode 100644 index 000000000..31dda5a83 --- /dev/null +++ b/src/main/java/subway/controller/utils/SetPrint.java @@ -0,0 +1,15 @@ +package subway.controller.utils; + +import static subway.controller.utils.Constants.makeString; +import static subway.controller.utils.Constants.response; + +public class SetPrint { + + public boolean printResult(String method, String target, boolean result){ + String message = makeString.infoMessage(method, target, result); + response.printInfo(message); + return true; + } + + +} diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index 3029fa302..c59e7e893 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -1,6 +1,7 @@ package subway.service; import subway.config.constants.Targets; +import subway.config.handler.InputException; import subway.domain.Line; import subway.domain.Station; import subway.service.utils.LineMakeString; @@ -9,10 +10,12 @@ import java.util.List; +import static subway.controller.utils.Constants.LINE; import static subway.service.InitSubwayValues.*; public class LineManager extends Verify implements Managerable { LineMakeString makeString = new LineMakeString(); + InputException inputException = new InputException(); public Line setLine(final String name){ Line node = new Line(name); lineRepo.addLine(node); diff --git a/src/main/java/subway/service/utils/Verify.java b/src/main/java/subway/service/utils/Verify.java index 71ff063ae..e08776023 100644 --- a/src/main/java/subway/service/utils/Verify.java +++ b/src/main/java/subway/service/utils/Verify.java @@ -1,14 +1,17 @@ package subway.service.utils; -import subway.config.constants.Targets; +import subway.config.handler.InputException; import subway.config.handler.SubwayException; import subway.domain.Line; +import static subway.controller.utils.Constants.LINE; +import static subway.controller.utils.Constants.STATION; import static subway.service.utils.Constants.lineRepo; import static subway.service.utils.Constants.stationRepo; public class Verify { SubwayException subwayException = new SubwayException(); + InputException inputException = new InputException(); public boolean isEmpty(final String station) { if(subwayException.isBack(station) == true) return false; // TODO: 종료시키는 에러? 커스텀? @@ -16,8 +19,8 @@ public boolean isEmpty(final String station) { } public boolean isEmpty(final String type, final String name){ if(subwayException.isBack(name) == true) return false; // TODO: 종료시키는 에러? 커스텀? - if(type.equals(Targets.STATION.getTarget())) return isStationEmpty(name); - if(type.equals(Targets.LINE.getTarget())) return isValidLine(name); + if(type.equals(STATION)) return isStationEmpty(name); + if(type.equals(LINE)) return isValidLine(LINE, name); return true; } @@ -25,19 +28,20 @@ boolean isStationEmpty(final String station){ if(stationRepo.getStationByName(station) == null ) return true; return false; } - boolean isValidLine(final String line){ - if(subwayException.isBack(line) == true) return false; // TODO: 종료시키는 에러? 커스텀? - if(lineRepo.getLineByName(line) == null ) return true; - return false; + + public boolean isValidLine(String target, String name){ + if(lineRepo.getLineByName(name) != null ) return false; + return true; } public boolean lineEqualStation(String line){ if(stationRepo.getStationByName(line) != null) { + inputException.lineEqualStation(); return true; } return false; } - public boolean isValidLine(final String lName, final String sName){ + public boolean lineHaveStation(final String lName, final String sName){ Line line = getLine(lName); if(line.haveStation(sName)){ return false; diff --git a/src/main/java/subway/view/AskView.java b/src/main/java/subway/view/AskView.java index 77a5bdc90..afc861a81 100644 --- a/src/main/java/subway/view/AskView.java +++ b/src/main/java/subway/view/AskView.java @@ -22,18 +22,18 @@ public void Function() { } //## 등록/삭제할 역/노선을 입력하세요 - public void orderWhere(final String work, final String name) { + public void orderIndex(final String work, final String name) { String sentence = makeString.askName(work, name); System.out.println(sentence); } // 역/노선을 입력하세요 - public void orderWhere(final String name) { + public void orderIndex(final String name) { String sentence = makeString.askName(name); System.out.println(sentence); } - public void orderWhere() { // 등록 or 삭제 + public void orderIndex() { // 등록 or 삭제 System.out.println(makeString.askSequence()); } From 09bd61687011e4cdc7aa6b243fcc50db3f4ddb09 Mon Sep 17 00:00:00 2001 From: aristo Date: Sat, 18 May 2024 14:38:27 +0900 Subject: [PATCH 096/104] =?UTF-8?q?refacotr:=20=EB=AA=A8=ED=98=B8=ED=95=9C?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=EB=AA=85=ED=99=95=ED=9E=88=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/subControllers/SectionController.java | 2 +- src/main/java/subway/service/utils/Verify.java | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/subway/controller/subControllers/SectionController.java b/src/main/java/subway/controller/subControllers/SectionController.java index b03fc3df3..84c32007c 100644 --- a/src/main/java/subway/controller/subControllers/SectionController.java +++ b/src/main/java/subway/controller/subControllers/SectionController.java @@ -41,7 +41,7 @@ public boolean register() { // 노선, 역 이름, 순서 입력 받고 등록 return false; } String station = method.getStation(); - if(!sectionManager.isValidLine(line, station)) return false; // TODO: 예외처리 + if(sectionManager.lineHaveStation(line, station)) return false; // TODO: 예외처리 if(sectionManager.isEmpty(STATION, station)) return false; // TODO: 예외처리 int index = method.getIndex(); diff --git a/src/main/java/subway/service/utils/Verify.java b/src/main/java/subway/service/utils/Verify.java index e08776023..d0f129ecb 100644 --- a/src/main/java/subway/service/utils/Verify.java +++ b/src/main/java/subway/service/utils/Verify.java @@ -20,7 +20,7 @@ public boolean isEmpty(final String station) { public boolean isEmpty(final String type, final String name){ if(subwayException.isBack(name) == true) return false; // TODO: 종료시키는 에러? 커스텀? if(type.equals(STATION)) return isStationEmpty(name); - if(type.equals(LINE)) return isValidLine(LINE, name); + if(type.equals(LINE)) return isEmptyLine(name); return true; } @@ -29,7 +29,7 @@ boolean isStationEmpty(final String station){ return false; } - public boolean isValidLine(String target, String name){ + public boolean isEmptyLine(String name){ if(lineRepo.getLineByName(name) != null ) return false; return true; } @@ -44,9 +44,9 @@ public boolean lineEqualStation(String line){ public boolean lineHaveStation(final String lName, final String sName){ Line line = getLine(lName); if(line.haveStation(sName)){ - return false; + return true; } - return true; + return false; } public Line getLine(String name){ Line line = lineRepo.getLineByName(name); From fcd006cafbe3138b20d1d85bc1d61154b8ff1c86 Mon Sep 17 00:00:00 2001 From: aristo Date: Sat, 18 May 2024 15:22:56 +0900 Subject: [PATCH 097/104] =?UTF-8?q?refactor:=20SectionCopntroller=20?= =?UTF-8?q?=EC=A4=91=EC=8B=AC=20=EC=BD=94=EB=93=9C=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/config/constants/views/Errors.java | 17 +++++----- .../subway/config/handler/InputException.java | 33 ++++++------------ .../subControllers/LineController.java | 19 ++--------- .../subControllers/SectionController.java | 28 +++++++++++---- .../subControllers/StationController.java | 2 +- src/main/java/subway/domain/Line.java | 3 +- src/main/java/subway/service/LineManager.java | 9 +++-- .../java/subway/service/SectionManager.java | 22 ++++++++++++ .../java/subway/service/StationManager.java | 5 +++ .../java/subway/service/utils/Verify.java | 34 ++++++++++++++++--- .../java/subway/view/util/MakeString.java | 2 +- 11 files changed, 110 insertions(+), 64 deletions(-) diff --git a/src/main/java/subway/config/constants/views/Errors.java b/src/main/java/subway/config/constants/views/Errors.java index d79114e4b..6193c6448 100644 --- a/src/main/java/subway/config/constants/views/Errors.java +++ b/src/main/java/subway/config/constants/views/Errors.java @@ -1,14 +1,15 @@ package subway.config.constants.views; public enum Errors { - VALID("잘못된 명령입니다. 입력값을 확인해주세요."), - ADD_STATION("이미 등록된 역 이름입니다."), - ADD_LINE("이미 등록된 노선 이름입니다."), - ADDED("이미 등록된 이름입니다."), - DEL_STATION("존재하지 않은 역 이름입니다."), - UNEXPECTED("예상치 못한 에러가 발생했습니다."), - UNDER_TWO_STATIONS("속한 역의 개수가 2개 이하인 노선은 삭제할 수 없습니다."), - DEL_LINE("존재하지 않는 노선 이름입니다."); + VALID("잘못된 명령입니다. 입력값을 확인해주세요.\n"), + ADD_STATION("이미 등록된 역 이름입니다.\n"), + ADD_LINE("이미 등록된 노선 이름입니다.\n"), + ADDED("이미 등록된 이름입니다.\n"), + DEL_STATION("존재하지 않은 역 이름입니다.\n"), + ALREADY_WITH_STAION("노선에 해당 역이 이미 포함되어 있습니다.\n"), + UNEXPECTED("예상치 못한 에러가 발생했습니다.\n"), + UNDER_TWO_STATIONS("속한 역의 개수가 2개 이하인 노선은 삭제할 수 없습니다.\n"), + DEL_LINE("존재하지 않는 노선 이름입니다.\n"); private final String message; Errors(String message){ diff --git a/src/main/java/subway/config/handler/InputException.java b/src/main/java/subway/config/handler/InputException.java index 9261d5b0c..3c05a61bf 100644 --- a/src/main/java/subway/config/handler/InputException.java +++ b/src/main/java/subway/config/handler/InputException.java @@ -1,49 +1,38 @@ package subway.config.handler; import static subway.config.constants.views.Errors.*; -import static subway.config.constants.views.Prefixes.ERROR; public class InputException { public void noStation(){ - StringBuilder sb = new StringBuilder(); - sb.append(DEL_STATION.getError()); - System.err.println(sb); + System.err.println(DEL_STATION.getError()); } public void alreadyCreatedLine(){ - StringBuilder sb = new StringBuilder(); - sb.append(ADD_LINE.getError()); - System.err.println(sb); + System.err.println(ADD_LINE.getError()); } public void alreadyCreatedStation(){ - StringBuilder sb = new StringBuilder(); - sb.append(ADD_STATION.getError()); - System.err.println(sb); + System.err.println(ADD_STATION.getError()); } public void noCreatedLine(){ - StringBuilder sb = new StringBuilder(); - sb.append(DEL_LINE.getError()); - System.err.println(sb); + System.err.println(DEL_LINE.getError()); } public void unExpectedError(){ - StringBuilder sb = new StringBuilder(); - sb.append(UNEXPECTED.getError()); - System.err.println(sb); + System.err.println(UNEXPECTED.getError()); } public void underTwoStation() { - StringBuilder sb = new StringBuilder(); - sb.append(UNDER_TWO_STATIONS.getError()); - System.err.println(sb); + System.err.println(UNDER_TWO_STATIONS.getError()); } public void lineEqualStation(){ - StringBuilder sb = new StringBuilder(); - sb.append(ADD_STATION.getError()); - System.err.println(sb); + System.err.println(ADD_STATION.getError()); + } + + public void alreadyWithStation() { + System.err.print(ALREADY_WITH_STAION.getError()); } } diff --git a/src/main/java/subway/controller/subControllers/LineController.java b/src/main/java/subway/controller/subControllers/LineController.java index f1a53d02e..ab2a2cc23 100644 --- a/src/main/java/subway/controller/subControllers/LineController.java +++ b/src/main/java/subway/controller/subControllers/LineController.java @@ -19,7 +19,7 @@ public LineController(final InitSubwayValues manager) { @Override public boolean register() { String line = method.getLine(REGISTER, LINE); - if(!canRegister(line)) return false; + if(!lineManager.haveSameName(line)) return false; if(!addSubStations(line)) return false; return setPrint.printResult(REGISTER, LINE, true); } @@ -27,29 +27,16 @@ public boolean register() { public boolean addSubStations(String line){ String upper, bottom; try { - if(!canRegister(line)) return false; + if(!lineManager.haveSameName(line)) return false; upper = method.getStation(lineManager, REGISTER, UPPER); bottom = method.getStation(lineManager, REGISTER, BOTTOM); - lineManager.setStations(line, upper, bottom); - return true; + return lineManager.setStations(line, upper, bottom); } catch (IllegalArgumentException e) { inputException.noStation(); } return false; } - public boolean canRegister(String line){ - if (lineManager.isEmpty(LINE, line) != true) { - inputException.alreadyCreatedLine(); - return false; - } - if(lineManager.lineEqualStation(line)) { - inputException.lineEqualStation(); - return false; - } - return true; - } - @Override public boolean delete() { boolean result=false; diff --git a/src/main/java/subway/controller/subControllers/SectionController.java b/src/main/java/subway/controller/subControllers/SectionController.java index 84c32007c..0a5efc8da 100644 --- a/src/main/java/subway/controller/subControllers/SectionController.java +++ b/src/main/java/subway/controller/subControllers/SectionController.java @@ -36,20 +36,34 @@ public boolean sendRequest(final Controller controller, final String command) { @Override public boolean register() { // 노선, 역 이름, 순서 입력 받고 등록 String line = method.getLine(); - if (sectionManager.isEmpty(LINE, line)) { - inputException.noCreatedLine(); - return false; - } String station = method.getStation(); - if(sectionManager.lineHaveStation(line, station)) return false; // TODO: 예외처리 - if(sectionManager.isEmpty(STATION, station)) return false; // TODO: 예외처리 - + if(!isValid(line, station))return false; int index = method.getIndex(); sectionManager.insertSection(station, index); return setPrint.printResult(REGISTER, SECTION, true); } + public boolean isValid(String line, String station){ + try{ + if(sectionManager.isEmptySpace(line, station)) { + return false; + } + if (!sectionManager.haveSameName(line)) { + inputException.noCreatedLine(); + return false; + } + if(sectionManager.lineHaveStation(line, station)) { + inputException.alreadyWithStation(); + return false; + }; // TODO: 예외처리 + }catch (Exception e){ + inputException.unExpectedError(); + return false; + } + return true; + } + @Override public boolean delete() { try { diff --git a/src/main/java/subway/controller/subControllers/StationController.java b/src/main/java/subway/controller/subControllers/StationController.java index 231f3adda..3c0242105 100644 --- a/src/main/java/subway/controller/subControllers/StationController.java +++ b/src/main/java/subway/controller/subControllers/StationController.java @@ -24,7 +24,7 @@ public boolean register() { boolean result = false; String station = method.getUserInput(); // 조건 모듈 - if(!stationManager.isEmpty(Targets.STATION.getTarget(), station)){ + if(!stationManager.haveSameName(station)){ inputException.alreadyCreatedStation(); return false; } diff --git a/src/main/java/subway/domain/Line.java b/src/main/java/subway/domain/Line.java index 6de66a0bc..349d891e4 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -23,9 +23,10 @@ public StringBuilder stationList(){ return sb; } - public void setStations(final Station upper, final Station bottom) { + public boolean setStations(final Station upper, final Station bottom) { stations.addFirst(upper); stations.addLast(bottom); + return true; } public void addStation(final Station station, final int index) { diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index c59e7e893..a1c5f8067 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -10,7 +10,6 @@ import java.util.List; -import static subway.controller.utils.Constants.LINE; import static subway.service.InitSubwayValues.*; public class LineManager extends Verify implements Managerable { @@ -22,13 +21,13 @@ public Line setLine(final String name){ return node; } - public void setStations(final String name, final String upperStation, final String bottomStation){ + public boolean setStations(final String name, final String upperStation, final String bottomStation){ Line line = setLine(name); Station upper = stationManager.getByStationName(upperStation); Station bottom = stationManager.getByStationName(bottomStation); upper.addLine(line); bottom.addLine(line); - line.setStations(upper, bottom); + return line.setStations(upper, bottom); } @Override @@ -62,4 +61,8 @@ public StringBuilder getStationLines(){ } return sb; } + + public boolean haveSameName(String line){ + return haveSameNameLine(line); + } } diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 9a1a0cec8..292f96f00 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -1,17 +1,20 @@ package subway.service; +import subway.config.handler.InputException; import subway.config.handler.SubwayException; import subway.domain.Line; import subway.domain.Station; import subway.service.utils.Managerable; import subway.service.utils.Verify; +import static subway.controller.utils.Constants.STATION; import static subway.service.InitSubwayValues.lineRepo; import static subway.service.InitSubwayValues.stationRepo; public class SectionManager extends Verify implements Managerable { private Line line=null; SubwayException subwayException =new SubwayException(); + InputException inputException = new InputException(); public void insertSection(final String sName,final int index){ Station station = stationRepo.getStationByName(sName); @@ -39,4 +42,23 @@ public StringBuilder read() { StringBuilder sb = new StringBuilder(); return sb; } + + public boolean isEmptySpace(String line, String station){ + if(isEmptyStation(station)){ + inputException.noStation(); + return true; + } + if(!isEmptyLine(line)){ + inputException.noCreatedLine(); + return true; + } + return false; + } + + public boolean haveSameName(String line){ + if(haveSameNameLine(line)) { + return true; + } + return false; + } } diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index 2a01fec5e..0af8c32b3 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -43,4 +43,9 @@ public Station getByStationName(final String name){ } return station; } + + public boolean haveSameName(String station){ + if(haveSameNameStation(station)) return true; + return false; + } } diff --git a/src/main/java/subway/service/utils/Verify.java b/src/main/java/subway/service/utils/Verify.java index d0f129ecb..6a507c76b 100644 --- a/src/main/java/subway/service/utils/Verify.java +++ b/src/main/java/subway/service/utils/Verify.java @@ -15,23 +15,23 @@ public class Verify { public boolean isEmpty(final String station) { if(subwayException.isBack(station) == true) return false; // TODO: 종료시키는 에러? 커스텀? - return isStationEmpty(station); + return isEmptyStation(station); } public boolean isEmpty(final String type, final String name){ if(subwayException.isBack(name) == true) return false; // TODO: 종료시키는 에러? 커스텀? - if(type.equals(STATION)) return isStationEmpty(name); + if(type.equals(STATION)) return isEmptyStation(name); if(type.equals(LINE)) return isEmptyLine(name); return true; } - boolean isStationEmpty(final String station){ + public boolean isEmptyStation(final String station){ if(stationRepo.getStationByName(station) == null ) return true; return false; } public boolean isEmptyLine(String name){ - if(lineRepo.getLineByName(name) != null ) return false; - return true; + if(lineRepo.getLineByName(name) != null ) return true; + return false; } public boolean lineEqualStation(String line){ @@ -54,4 +54,28 @@ public Line getLine(String name){ return line; } + public boolean haveSameNameLine(String line){ + if (isEmpty(LINE, line) != true) { + inputException.alreadyCreatedLine(); + return false; + } + if(lineEqualStation(line)) { + inputException.lineEqualStation(); + return false; + } + return true; + } + + public boolean haveSameNameStation(String station){ + if (isEmpty(STATION, station) != true) { + inputException.alreadyCreatedStation(); + return false; + } + if(lineEqualStation(station)) { + inputException.lineEqualStation(); + return false; + } + return true; + } + } diff --git a/src/main/java/subway/view/util/MakeString.java b/src/main/java/subway/view/util/MakeString.java index 7ecb14a41..e207ac6bd 100644 --- a/src/main/java/subway/view/util/MakeString.java +++ b/src/main/java/subway/view/util/MakeString.java @@ -32,7 +32,7 @@ public String infoMessage(final String work, final String target, final boolean sb.append(work).append("되었습니다."); } if(result == false){ - sb.append(work).append("되지 않았습니다. 연결된 노선이나 해당 노선에 속한 역을 확인해주세요."); + sb.append(work).append("되지 않았습니다."); } return sb.toString(); } From 6a0feddec8a49d338664722473c0ae5c3edb5217 Mon Sep 17 00:00:00 2001 From: aristo Date: Sat, 18 May 2024 16:05:26 +0900 Subject: [PATCH 098/104] =?UTF-8?q?refactor:=20=EC=9E=98=EB=AA=BB=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=EB=90=98=EB=8A=94=20=EC=97=90=EB=9F=AC=20?= =?UTF-8?q?=EB=AC=B8=EA=B5=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subControllers/SectionController.java | 4 --- .../java/subway/service/SectionManager.java | 8 +++--- .../java/subway/service/utils/Verify.java | 28 ++++++------------- 3 files changed, 12 insertions(+), 28 deletions(-) diff --git a/src/main/java/subway/controller/subControllers/SectionController.java b/src/main/java/subway/controller/subControllers/SectionController.java index 0a5efc8da..04d537bee 100644 --- a/src/main/java/subway/controller/subControllers/SectionController.java +++ b/src/main/java/subway/controller/subControllers/SectionController.java @@ -49,10 +49,6 @@ public boolean isValid(String line, String station){ if(sectionManager.isEmptySpace(line, station)) { return false; } - if (!sectionManager.haveSameName(line)) { - inputException.noCreatedLine(); - return false; - } if(sectionManager.lineHaveStation(line, station)) { inputException.alreadyWithStation(); return false; diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 292f96f00..eb94d3097 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -7,7 +7,6 @@ import subway.service.utils.Managerable; import subway.service.utils.Verify; -import static subway.controller.utils.Constants.STATION; import static subway.service.InitSubwayValues.lineRepo; import static subway.service.InitSubwayValues.stationRepo; @@ -48,15 +47,16 @@ public boolean isEmptySpace(String line, String station){ inputException.noStation(); return true; } - if(!isEmptyLine(line)){ + if(isEmptyLine(line)){ inputException.noCreatedLine(); return true; } return false; } - public boolean haveSameName(String line){ - if(haveSameNameLine(line)) { + public boolean lineHaveStation(final String lName, final String sName){ + Line line = getLine(lName); + if(line.haveStation(sName)){ return true; } return false; diff --git a/src/main/java/subway/service/utils/Verify.java b/src/main/java/subway/service/utils/Verify.java index 6a507c76b..6455bbf61 100644 --- a/src/main/java/subway/service/utils/Verify.java +++ b/src/main/java/subway/service/utils/Verify.java @@ -13,10 +13,6 @@ public class Verify { SubwayException subwayException = new SubwayException(); InputException inputException = new InputException(); - public boolean isEmpty(final String station) { - if(subwayException.isBack(station) == true) return false; // TODO: 종료시키는 에러? 커스텀? - return isEmptyStation(station); - } public boolean isEmpty(final String type, final String name){ if(subwayException.isBack(name) == true) return false; // TODO: 종료시키는 에러? 커스텀? if(type.equals(STATION)) return isEmptyStation(name); @@ -30,7 +26,7 @@ public boolean isEmptyStation(final String station){ } public boolean isEmptyLine(String name){ - if(lineRepo.getLineByName(name) != null ) return true; + if(lineRepo.getLineByName(name) == null ) return true; return false; } @@ -41,22 +37,8 @@ public boolean lineEqualStation(String line){ } return false; } - public boolean lineHaveStation(final String lName, final String sName){ - Line line = getLine(lName); - if(line.haveStation(sName)){ - return true; - } - return false; - } - public Line getLine(String name){ - Line line = lineRepo.getLineByName(name); - if(line == null) subwayException.noLine(); - return line; - } - public boolean haveSameNameLine(String line){ if (isEmpty(LINE, line) != true) { - inputException.alreadyCreatedLine(); return false; } if(lineEqualStation(line)) { @@ -71,11 +53,17 @@ public boolean haveSameNameStation(String station){ inputException.alreadyCreatedStation(); return false; } - if(lineEqualStation(station)) { + if(isEmptyLine(station) != true) { inputException.lineEqualStation(); return false; } return true; } + public Line getLine(String name){ + Line line = lineRepo.getLineByName(name); + if(line == null) subwayException.noLine(); + return line; + } + } From 27f7523fb46ca21f9859fddb8f4454029de22c35 Mon Sep 17 00:00:00 2001 From: aristo Date: Sat, 18 May 2024 16:15:55 +0900 Subject: [PATCH 099/104] =?UTF-8?q?fix:=20=EC=97=90=EB=9F=AC=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EB=AC=B8=EA=B5=AC=20=EB=B0=8F=20=EC=A0=80=EC=9E=A5?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/config/constants/views/Errors.java | 2 + .../subway/config/handler/InputException.java | 8 +++ .../subControllers/LineController.java | 3 +- .../subControllers/SectionController.java | 50 +++++++++---------- .../subControllers/StationController.java | 27 ++++------ .../java/subway/domain/StationRepository.java | 2 +- src/main/java/subway/service/LineManager.java | 7 ++- .../java/subway/service/SectionManager.java | 11 +++- .../java/subway/service/StationManager.java | 20 ++++++-- .../java/subway/service/utils/Verify.java | 9 ++-- 10 files changed, 81 insertions(+), 58 deletions(-) diff --git a/src/main/java/subway/config/constants/views/Errors.java b/src/main/java/subway/config/constants/views/Errors.java index 6193c6448..7bb7a4a12 100644 --- a/src/main/java/subway/config/constants/views/Errors.java +++ b/src/main/java/subway/config/constants/views/Errors.java @@ -9,6 +9,8 @@ public enum Errors { ALREADY_WITH_STAION("노선에 해당 역이 이미 포함되어 있습니다.\n"), UNEXPECTED("예상치 못한 에러가 발생했습니다.\n"), UNDER_TWO_STATIONS("속한 역의 개수가 2개 이하인 노선은 삭제할 수 없습니다.\n"), + STATIONS_UNDER_LINE("노선에 속한 역은 삭제할 수 없습니다.\n"), + NO_SUCH_LINE("요청하신 노선 정보를 불러올 수 없습니다.\n"), DEL_LINE("존재하지 않는 노선 이름입니다.\n"); private final String message; diff --git a/src/main/java/subway/config/handler/InputException.java b/src/main/java/subway/config/handler/InputException.java index 3c05a61bf..2d104d610 100644 --- a/src/main/java/subway/config/handler/InputException.java +++ b/src/main/java/subway/config/handler/InputException.java @@ -35,4 +35,12 @@ public void lineEqualStation(){ public void alreadyWithStation() { System.err.print(ALREADY_WITH_STAION.getError()); } + + public void haveLines() { + System.err.println(STATIONS_UNDER_LINE.getError()); + } + + public void noSuchLine() { + System.err.println(NO_SUCH_LINE.getError()); + } } diff --git a/src/main/java/subway/controller/subControllers/LineController.java b/src/main/java/subway/controller/subControllers/LineController.java index ab2a2cc23..7ad485e98 100644 --- a/src/main/java/subway/controller/subControllers/LineController.java +++ b/src/main/java/subway/controller/subControllers/LineController.java @@ -19,7 +19,7 @@ public LineController(final InitSubwayValues manager) { @Override public boolean register() { String line = method.getLine(REGISTER, LINE); - if(!lineManager.haveSameName(line)) return false; + if(lineManager.haveSameName(line)) return false; if(!addSubStations(line)) return false; return setPrint.printResult(REGISTER, LINE, true); } @@ -27,7 +27,6 @@ public boolean register() { public boolean addSubStations(String line){ String upper, bottom; try { - if(!lineManager.haveSameName(line)) return false; upper = method.getStation(lineManager, REGISTER, UPPER); bottom = method.getStation(lineManager, REGISTER, BOTTOM); return lineManager.setStations(line, upper, bottom); diff --git a/src/main/java/subway/controller/subControllers/SectionController.java b/src/main/java/subway/controller/subControllers/SectionController.java index 04d537bee..5b30070e4 100644 --- a/src/main/java/subway/controller/subControllers/SectionController.java +++ b/src/main/java/subway/controller/subControllers/SectionController.java @@ -37,44 +37,42 @@ public boolean sendRequest(final Controller controller, final String command) { public boolean register() { // 노선, 역 이름, 순서 입력 받고 등록 String line = method.getLine(); String station = method.getStation(); - if(!isValid(line, station))return false; + if(isEmptySpace(line, station)) return false; + if(lineHaveStation(line, station))return false; int index = method.getIndex(); sectionManager.insertSection(station, index); return setPrint.printResult(REGISTER, SECTION, true); } - public boolean isValid(String line, String station){ - try{ - if(sectionManager.isEmptySpace(line, station)) { - return false; - } - if(sectionManager.lineHaveStation(line, station)) { - inputException.alreadyWithStation(); - return false; - }; // TODO: 예외처리 - }catch (Exception e){ - inputException.unExpectedError(); - return false; - } - return true; + public boolean isEmptySpace(String line, String station){ + return sectionManager.isEmptySpace(line, station); + } + + public boolean lineHaveStation(String line, String station){ + return sectionManager.lineHaveStation(line, station); } @Override public boolean delete() { - try { - String line = method.getLine(DELETE, "구간의 노선"); - sectionManager.getLine(line); - String station = method.getStation(sectionManager, DELETE, "구간의 역"); + String line = method.getLine(DELETE, "구간의 노선"); + sectionManager.getLine(line); + String station = method.getStation(sectionManager, DELETE, "구간의 역"); + + return deleteSection(line, station) == true; + } - if (!sectionManager.isEmpty(STATION, station)) { - return setPrint.printResult(DELETE, STATION, true); - } -// response.printInfo("존재하지 않는 역에 접근했습니다."); - } catch (IllegalArgumentException e) { - System.err.println(Errors.VALID.getError()); + public boolean deleteSection(String line, String station) { + if (isEmptySpace(line, station)) return false; + if (!lineHaveStation(line, station)) { + inputException.noStation(); + return false; } - return false; + if (sectionManager.isDeletable(line)) { + inputException.underTwoStation(); + return false; + } + return setPrint.printResult(DELETE, STATION, true); } @Override diff --git a/src/main/java/subway/controller/subControllers/StationController.java b/src/main/java/subway/controller/subControllers/StationController.java index 3c0242105..a40f2532c 100644 --- a/src/main/java/subway/controller/subControllers/StationController.java +++ b/src/main/java/subway/controller/subControllers/StationController.java @@ -21,39 +21,34 @@ public StationController(final InitSubwayValues manager) { @Override public boolean register() { ask.orderIndex(REGISTER, STATION); - boolean result = false; String station = method.getUserInput(); - // 조건 모듈 - if(!stationManager.haveSameName(station)){ - inputException.alreadyCreatedStation(); - return false; + if(stationManager.register(station)){ + return setPrint.printResult(REGISTER, STATION, true); } - stationManager.register(station); - result = true; - - return setPrint.printResult(REGISTER, STATION, result); + return setPrint.printResult(REGISTER, STATION, false); } @Override public boolean delete() { ask.orderIndex(DELETE, STATION); String command = method.getUserInput(); - boolean result=false; - if(method.isEmpty(command)) { + if(method.isEmpty(STATION, command)) { inputException.noStation(); return false; } + boolean result = deleteStation(command); + return setPrint.printResult(DELETE, STATION, result); + } + + boolean deleteStation(String command){ try { - result = stationManager.delete(command); + return stationManager.delete(command); } catch(IllegalArgumentException e){ inputException.underTwoStation(); - return false; }catch (Exception e) { inputException.unExpectedError(); - return false; } - - return setPrint.printResult(DELETE, STATION, result); + return false; } public boolean read() { diff --git a/src/main/java/subway/domain/StationRepository.java b/src/main/java/subway/domain/StationRepository.java index 7a64e2633..b98407e88 100644 --- a/src/main/java/subway/domain/StationRepository.java +++ b/src/main/java/subway/domain/StationRepository.java @@ -17,7 +17,7 @@ public static void addStation(Station station) { } public static boolean deleteStation(final String name) { - return stations.removeIf(station -> Objects.equals(station.getName(), name) && station.hasNoLine()); + return stations.removeIf(station -> Objects.equals(station.getName(), name)); } public static Station getStationByName(final String name){ for(Station station : stations){ diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index a1c5f8067..28d9289b3 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -1,6 +1,5 @@ package subway.service; -import subway.config.constants.Targets; import subway.config.handler.InputException; import subway.domain.Line; import subway.domain.Station; @@ -63,6 +62,10 @@ public StringBuilder getStationLines(){ } public boolean haveSameName(String line){ - return haveSameNameLine(line); + if(sameNameLine(line) == true) { + inputException.alreadyCreatedLine(); + return true; + } + return false; } } diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index eb94d3097..065ba6200 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -28,6 +28,12 @@ public Line getLine(String name){ return line; } + public boolean isDeletable(String name){ + Line line = lineRepo.getLineByName(name); + if(line.getSize()>2) return true; + return false; + } + @Override public boolean delete(final String station) { if(subwayException.isBack(station) == true) return true; @@ -43,14 +49,15 @@ public StringBuilder read() { } public boolean isEmptySpace(String line, String station){ - if(isEmptyStation(station)){ + if(isEmptyStation(station)){ // 등록 : 참 inputException.noStation(); return true; } - if(isEmptyLine(line)){ + if(isEmptyLine(line)){ // 등록: 참 inputException.noCreatedLine(); return true; } +// inputException.noSuchLine(); return false; } diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index 0af8c32b3..92775dc1d 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -8,22 +8,35 @@ import java.util.List; +import static subway.controller.utils.Constants.STATION; import static subway.service.InitSubwayValues.stationRepo; // 역 관련 기능하는 서비스 public class StationManager extends Verify implements Managerable { + InputException inputException = new InputException(); - public void register(String name){ - stationRepo.addStation(new Station(name)); + public boolean register(String name){ + if(isEmpty(STATION, name)) { + stationRepo.addStation(new Station(name)); + return true ; + } + inputException.alreadyCreatedStation(); + return false; } @Override public boolean delete(final String name) { Station station = stationRepo.getStationByName(name); List lines = station.getLine(); + if(station.hasNoLine() == false) { + inputException.haveLines(); + return false; + } + for(Line line : lines){ line.deleteStation(name); } + return stationRepo.deleteStation(name); } @@ -45,7 +58,6 @@ public Station getByStationName(final String name){ } public boolean haveSameName(String station){ - if(haveSameNameStation(station)) return true; - return false; + return haveSameNameStation(station) ; } } diff --git a/src/main/java/subway/service/utils/Verify.java b/src/main/java/subway/service/utils/Verify.java index 6455bbf61..05a08d217 100644 --- a/src/main/java/subway/service/utils/Verify.java +++ b/src/main/java/subway/service/utils/Verify.java @@ -37,23 +37,22 @@ public boolean lineEqualStation(String line){ } return false; } - public boolean haveSameNameLine(String line){ - if (isEmpty(LINE, line) != true) { + public boolean sameNameLine(String line){ + if (isEmpty(LINE, line) == true) { return false; } if(lineEqualStation(line)) { - inputException.lineEqualStation(); return false; } return true; } public boolean haveSameNameStation(String station){ - if (isEmpty(STATION, station) != true) { + if (isEmpty(STATION, station) == false) { inputException.alreadyCreatedStation(); return false; } - if(isEmptyLine(station) != true) { + if(isEmptyLine(station) == false) { inputException.lineEqualStation(); return false; } From ff585971ef7aa3db4533f5f05ce6d48a8e9e4605 Mon Sep 17 00:00:00 2001 From: aristo Date: Sat, 18 May 2024 21:11:25 +0900 Subject: [PATCH 100/104] =?UTF-8?q?fix:=20=EC=82=AD=EC=A0=9C=EB=90=9C=20?= =?UTF-8?q?=EA=B5=AC=EA=B0=84=20=EC=82=AD=EC=A0=9C=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=B3=B5=EA=B5=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/subControllers/SectionController.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/subway/controller/subControllers/SectionController.java b/src/main/java/subway/controller/subControllers/SectionController.java index 5b30070e4..6cd0c6cc5 100644 --- a/src/main/java/subway/controller/subControllers/SectionController.java +++ b/src/main/java/subway/controller/subControllers/SectionController.java @@ -68,10 +68,11 @@ public boolean deleteSection(String line, String station) { inputException.noStation(); return false; } - if (sectionManager.isDeletable(line)) { + if (!sectionManager.isDeletable(line)) { inputException.underTwoStation(); return false; } + sectionManager.delete(station); return setPrint.printResult(DELETE, STATION, true); } From 8e47bf293ed7a2ecdc4e1a8553f43af1425dba60 Mon Sep 17 00:00:00 2001 From: aristo Date: Sat, 18 May 2024 21:13:49 +0900 Subject: [PATCH 101/104] =?UTF-8?q?fix:=20sectionController=20=EA=B5=AC?= =?UTF-8?q?=EA=B0=84=20=EB=93=B1=EB=A1=9D=20=EC=8B=9C=20=EC=97=AD=20?= =?UTF-8?q?=EC=A4=91=EB=B3=B5=20=EB=B6=88=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subway/controller/subControllers/SectionController.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/subway/controller/subControllers/SectionController.java b/src/main/java/subway/controller/subControllers/SectionController.java index 6cd0c6cc5..835d8f090 100644 --- a/src/main/java/subway/controller/subControllers/SectionController.java +++ b/src/main/java/subway/controller/subControllers/SectionController.java @@ -38,7 +38,10 @@ public boolean register() { // 노선, 역 이름, 순서 입력 받고 등록 String line = method.getLine(); String station = method.getStation(); if(isEmptySpace(line, station)) return false; - if(lineHaveStation(line, station))return false; + if(lineHaveStation(line, station)){ + inputException.alreadyWithStation(); + return false; + } int index = method.getIndex(); sectionManager.insertSection(station, index); From 5d34e873409adfb78860ed90f9046678dffe5b51 Mon Sep 17 00:00:00 2001 From: aristo Date: Sat, 18 May 2024 22:54:35 +0900 Subject: [PATCH 102/104] =?UTF-8?q?refactor:=20=EB=B3=80=EC=88=98=EB=AA=85?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/subway/config/handler/InputExceptionError.java | 1 - src/main/java/subway/config/handler/SubwayException.java | 1 - src/main/java/subway/service/LineManager.java | 4 ++-- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/subway/config/handler/InputExceptionError.java b/src/main/java/subway/config/handler/InputExceptionError.java index 3bb8ecfdc..bb5d856e8 100644 --- a/src/main/java/subway/config/handler/InputExceptionError.java +++ b/src/main/java/subway/config/handler/InputExceptionError.java @@ -7,7 +7,6 @@ public enum ErrorMessage{ PUT_VAILD_VALUE("유효한 범위 내의 명령어를 입력해주세요."), PUT_ONLY_VAILD_NUMBER("자연수만 입력 가능합니다."), PUT_UNDER_4_OR_Q_VALUE("4 이하 자연수나 Q만 입력 가능합니다."), - CAN_NOT_CHOICE_THIS_FUNCTION("선택할 수 없는 기능입니다."), THERE_IS_NO_SUCH_STATION("존재하지 않은 역입니다."), THERE_IS_NO_SUCH_LINE("존재하지 않은 노선입니다."), THERE_IS_AREADY_THE_SAME_STATION("이미 생성된 역입니다."), diff --git a/src/main/java/subway/config/handler/SubwayException.java b/src/main/java/subway/config/handler/SubwayException.java index e79adec98..f4a8e695c 100644 --- a/src/main/java/subway/config/handler/SubwayException.java +++ b/src/main/java/subway/config/handler/SubwayException.java @@ -3,7 +3,6 @@ import subway.config.constants.views.Errors; import subway.config.constants.views.Prefixes; -import static subway.config.constants.views.Errors.UNDER_TWO_STATIONS; import static subway.config.handler.InputExceptionError.ErrorMessage.*; public class SubwayException extends RuntimeException { diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index 28d9289b3..e54cbd0a6 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -33,8 +33,8 @@ public boolean setStations(final String name, final String upperStation, final S public boolean delete(final String name) { Line line = lineRepo.getLineByName(name); for(int i=0;i Date: Sat, 18 May 2024 23:07:57 +0900 Subject: [PATCH 103/104] =?UTF-8?q?refactor:=20=EC=9D=B4=EB=A6=84=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=A0=95?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../subControllers/LineController.java | 2 +- src/main/java/subway/service/LineManager.java | 8 ---- .../java/subway/service/StationManager.java | 4 -- .../java/subway/service/utils/Verify.java | 41 ++++++------------- 4 files changed, 13 insertions(+), 42 deletions(-) diff --git a/src/main/java/subway/controller/subControllers/LineController.java b/src/main/java/subway/controller/subControllers/LineController.java index 7ad485e98..77dbe034c 100644 --- a/src/main/java/subway/controller/subControllers/LineController.java +++ b/src/main/java/subway/controller/subControllers/LineController.java @@ -19,7 +19,7 @@ public LineController(final InitSubwayValues manager) { @Override public boolean register() { String line = method.getLine(REGISTER, LINE); - if(lineManager.haveSameName(line)) return false; + if(!lineManager.isEmptyName(line)) return false; if(!addSubStations(line)) return false; return setPrint.printResult(REGISTER, LINE, true); } diff --git a/src/main/java/subway/service/LineManager.java b/src/main/java/subway/service/LineManager.java index e54cbd0a6..8bd9e8000 100644 --- a/src/main/java/subway/service/LineManager.java +++ b/src/main/java/subway/service/LineManager.java @@ -60,12 +60,4 @@ public StringBuilder getStationLines(){ } return sb; } - - public boolean haveSameName(String line){ - if(sameNameLine(line) == true) { - inputException.alreadyCreatedLine(); - return true; - } - return false; - } } diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java index 92775dc1d..ea523cb1f 100644 --- a/src/main/java/subway/service/StationManager.java +++ b/src/main/java/subway/service/StationManager.java @@ -56,8 +56,4 @@ public Station getByStationName(final String name){ } return station; } - - public boolean haveSameName(String station){ - return haveSameNameStation(station) ; - } } diff --git a/src/main/java/subway/service/utils/Verify.java b/src/main/java/subway/service/utils/Verify.java index 05a08d217..30ccc7460 100644 --- a/src/main/java/subway/service/utils/Verify.java +++ b/src/main/java/subway/service/utils/Verify.java @@ -13,6 +13,18 @@ public class Verify { SubwayException subwayException = new SubwayException(); InputException inputException = new InputException(); + public boolean isEmptyName(final String name){ + if(isEmpty(LINE, name) == false) { + inputException.alreadyCreatedLine(); + return false; + } + if(isEmpty(STATION, name) == false) { + inputException.alreadyCreatedStation(); + return false; + } + return true; + } + public boolean isEmpty(final String type, final String name){ if(subwayException.isBack(name) == true) return false; // TODO: 종료시키는 에러? 커스텀? if(type.equals(STATION)) return isEmptyStation(name); @@ -30,35 +42,6 @@ public boolean isEmptyLine(String name){ return false; } - public boolean lineEqualStation(String line){ - if(stationRepo.getStationByName(line) != null) { - inputException.lineEqualStation(); - return true; - } - return false; - } - public boolean sameNameLine(String line){ - if (isEmpty(LINE, line) == true) { - return false; - } - if(lineEqualStation(line)) { - return false; - } - return true; - } - - public boolean haveSameNameStation(String station){ - if (isEmpty(STATION, station) == false) { - inputException.alreadyCreatedStation(); - return false; - } - if(isEmptyLine(station) == false) { - inputException.lineEqualStation(); - return false; - } - return true; - } - public Line getLine(String name){ Line line = lineRepo.getLineByName(name); if(line == null) subwayException.noLine(); From 82353448c65ee97e8c77969359d98dede920e9f4 Mon Sep 17 00:00:00 2001 From: aristo Date: Sat, 18 May 2024 23:31:57 +0900 Subject: [PATCH 104/104] mend --- src/main/java/subway/service/SectionManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/subway/service/SectionManager.java b/src/main/java/subway/service/SectionManager.java index 065ba6200..e027c6e90 100644 --- a/src/main/java/subway/service/SectionManager.java +++ b/src/main/java/subway/service/SectionManager.java @@ -18,7 +18,7 @@ public class SectionManager extends Verify implements Managerable { public void insertSection(final String sName,final int index){ Station station = stationRepo.getStationByName(sName); if(station == null) subwayException.noStation(); - lineRepo.addLine(line, station, index); + lineRepo.addLine(line, station, index-1); } public Line getLine(String name){