diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..979905fc3 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,83 @@ +# 서비스 파악 및 구현 계획 +> **지하철 역과 노선을 관리하는 지하철 노선도 기능을 구현한 코드** + +### 사전등록정보 +- 교대역, 강남역, 역삼역, 남부터미널역, 양재역, 양재시민의 숲역, 매봉역 + - 하행 - 상행 +- 2호선: 교대역 - 강남역 - 역삼역 +- 3호선: 교대역 - 남부터미널역 - 양재역 - 매봉역 +- 신분당선: 강남역 - 양재역 - 양재시민의숲역 + +# Controller +- 사용자 입력과 Service 코드 매칭 +- 유효한 입력인지 확인 ```(Exception 1)``` + +# Service +### Managerable +- 공통 기능 추출 후 추상화 + - 역 관련 기능/지하철 노선 등록/노선 구간 추가 기능 service 코드가 확장 +- 등록/삭제/조회/(메인화면으로)돌아가기 +```aidl +public abstract class Managerable{} +``` +> interface 로 초기 기획하다가 추상 클래스로 변경함. +> **되돌아가기**을 공통적으로 갖고 있으며 기능 수행 후 **출력하는 내용**이 거의 비슷하니 확장하는 방향으로 변경. +> 맞는 선택인지 구현하면서 계속 고민해볼 것 + +### StationManagerImpl +> 역 관련 기능 +- 지하철 역 등록 + - 등록 이후 노선에 등록하지 않기 + - 노선에 등록되어 있으면 중복 등록 불가 ```(SubwayConstraintHandler.2)``` +- 지하철 역 삭제 + - 노선에 등록된 역은 삭제할 수 없다. ```(SubwayConstraintHandler.2)``` +- 지하철 역의 목록 조회 + - 지하철 역 가나다순 나열하기 + - 역 이름 앞에 **'[INFO]'** 붙이기 ```(view )``` + - 한줄씩 출력 + +- View 코드에서 출력 포맷 활용하여 구현하고 호출할 때 매개변수로 구문이 다르게 나가도록 + - 출력 포맷 적극 활용할 것 + +### LineManagerImpl +> 지하철 노선 등록 +- 노선 등록 + 1. 노선 이름 입력 + 2. 상행 종점 이름 입력 + 3. 하행 종점 이름 입력 + 4. **'[INFO] 지하철 노선이 등록되었습니다.'** ```(view)``` +- 지하철 노선 삭제 +- 지하철 노선 목록 조회 + - **'[INFO] n 호선/신분당선'** 나열 ```(view)``` + +### SectionManagerImpl +> 노선에 구간 추가 +- 구간 등록 + 1. 등록한 노선 입력 ```(SubwayConstraintHandler.2 : true-continue, false - retry)``` + 2. 등록할 역 이름 입력 + 3. 노선에서 차지할 순서 입력 + 4. **'[INFO] 지하철 구간이 등록되었습니다.'** ```(view)``` + +### MapServcie +> 지하철 노선에 등록된 역 조회 +- 노선의 상행 종점부터 하행 종점까지 연결된 순으로 역 목록 조회 + + + +# config +> 1. 에러나 제약 사항 체크 +> 2. 상수값 모음 + +위반 시 **[ERROR]** 를 반드시 앞에 붙인다. + +### SubwayException +Controller 에서 주로 사용되는 예외 +1. 제공되는 서비스 외 문자 입력 및 선택 시 발생 + +### SubwayConstraintHandler +Service 에서 주로 사용되는 예외 +1. 지하철 역/노선 이름은 2글자 이상인지 확인 + - 아니면 다시 입력 받도록 +2. 노선에 등록되어있는지 확인(수정 및 업데이트 시) + - 있으면 true 없으면 false +3. 노선에 등록되어있는지 확인(조회 시) 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 diff --git a/src/main/java/subway/Application.java b/src/main/java/subway/Application.java index 0bcf786cc..a464040bb 100644 --- a/src/main/java/subway/Application.java +++ b/src/main/java/subway/Application.java @@ -1,10 +1,15 @@ package subway; -import java.util.Scanner; - +import subway.controller.MainController; public class Application { public static void main(String[] args) { - final Scanner scanner = new Scanner(System.in); - // TODO: 프로그램 구현 + try{ + MainController main = new MainController(); + main.startService(0); + main.closeService(); + System.out.println(" 안녕히 가세요. "); + } catch (Exception e){ + System.err.println("에러가 발생했습니다. 관리자에게 문의해주세요."); + } } } diff --git a/src/main/java/subway/config/constants/Targets.java b/src/main/java/subway/config/constants/Targets.java new file mode 100644 index 000000000..338e8e7fd --- /dev/null +++ b/src/main/java/subway/config/constants/Targets.java @@ -0,0 +1,25 @@ +package subway.config.constants; + +public enum Targets { + STATION("역", 1), + LINE("노선",2), + SECTION("구간", 3), + MAP("지하철 노선도", 4), + UPPER("상행 종점역", 5), + BOTTOM("하행 종점역", 6); + + private final 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/config/constants/initValues/Lines.java b/src/main/java/subway/config/constants/initValues/Lines.java new file mode 100644 index 000000000..e4314f6c7 --- /dev/null +++ b/src/main/java/subway/config/constants/initValues/Lines.java @@ -0,0 +1,24 @@ +package subway.config.constants.initValues; + +import java.util.Arrays; +import java.util.List; + +public enum Lines { // TODO: 해시맵 생각해보기 + 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/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/views/Errors.java b/src/main/java/subway/config/constants/views/Errors.java new file mode 100644 index 000000000..4b5118571 --- /dev/null +++ b/src/main/java/subway/config/constants/views/Errors.java @@ -0,0 +1,19 @@ +package subway.config.constants.views; + +public enum Errors { + VALID("잘못된 명령입니다. 입력값을 확인해주세요."), + ADD_STATION("이미 등록된 역 이름입니다."), + ADD_LINE("이미 등록된 노선 이름입니다."), + ADDED("이미 등록된 이름입니다."), + 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/constants/views/Indexes.java b/src/main/java/subway/config/constants/views/Indexes.java new file mode 100644 index 000000000..209faf549 --- /dev/null +++ b/src/main/java/subway/config/constants/views/Indexes.java @@ -0,0 +1,46 @@ +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/Methods.java b/src/main/java/subway/config/constants/views/Methods.java new file mode 100644 index 000000000..9fa3c28ee --- /dev/null +++ b/src/main/java/subway/config/constants/views/Methods.java @@ -0,0 +1,18 @@ +package subway.config.constants.views; + +public enum Methods { + 등록("1"), + 삭제("2"), + 조회("3"), + 출력("4"), + 종료("Q"), + 돌아가기("B"); + 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..a47710333 --- /dev/null +++ b/src/main/java/subway/config/constants/views/Prefixes.java @@ -0,0 +1,14 @@ +package subway.config.constants.views; + +public enum Prefixes { + SHARP("## "), + INFO("[INFO] "), + ERROR("[ERROR] "); + private final String prefix; + 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 new file mode 100644 index 000000000..5fac64e59 --- /dev/null +++ b/src/main/java/subway/config/constants/views/Questions.java @@ -0,0 +1,32 @@ +package subway.config.constants.views; + +public enum Questions { + 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(){ + 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(String work, String target) { + String message = work + "할 " + target + this.question; + return message; + } +} 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..abfbb8a72 --- /dev/null +++ b/src/main/java/subway/config/handler/InputException.java @@ -0,0 +1,39 @@ +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); + } + + public void alreadyCreatedLine(){ + StringBuilder sb = new StringBuilder(); + sb.append(ADD_LINE.getError()); + System.err.println(sb); + } + + public void alreadyCreatedStation(){ + StringBuilder sb = new StringBuilder(); + sb.append(ADD_STATION.getError()); + System.err.println(sb); + } + + public void noCreatedLine(){ + StringBuilder sb = new StringBuilder(); + sb.append(DEL_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/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 new file mode 100644 index 000000000..e826ae3ee --- /dev/null +++ b/src/main/java/subway/config/handler/SubwayException.java @@ -0,0 +1,53 @@ +package subway.config.handler; + +import subway.config.constants.views.Errors; +import subway.config.constants.views.Prefixes; + +import static subway.config.handler.InputExceptionError.ErrorMessage.*; + +public class SubwayException extends RuntimeException { + private static String ERROR = Prefixes.ERROR.getPrefix(); + + public boolean isNotNumber(){ + throw new NumberFormatException(PUT_ONLY_VAILD_NUMBER.getMessage()); + } + + 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()); + } + public boolean isBack(String command){ + if(command.equals("B")){ + return true; + } + return false; + } + + public void checkCommand() { + throw new IllegalArgumentException(ERROR+ Errors.VALID.getError()); + } + + public boolean alreadyCreatedLine(){ + throw new IllegalArgumentException(THERE_IS_AREADY_THE_SAME_LINE.getMessage()); + } + 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/MainController.java b/src/main/java/subway/controller/MainController.java new file mode 100644 index 000000000..12e3e3d57 --- /dev/null +++ b/src/main/java/subway/controller/MainController.java @@ -0,0 +1,98 @@ +package subway.controller; + +import subway.config.handler.SubwayException; +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.controller.utils.Methods; +import subway.service.InitSubwayValues; +import subway.view.AskView; + +public class MainController implements Constants { + private AskView ask; + 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(); + + + public MainController() { + ask = new AskView(); + InitSubwayValues manager = new InitSubwayValues(); + createSubControllers(manager); + subwayException = manager.getSubwayException(); + } + + public void createSubControllers(InitSubwayValues manager) { + station = new StationController(manager); + line = new LineController(manager); + section = new SectionController(manager); + map = new MapController(manager); + } + + public void startService(int tryCount) { + if (tryCount == 3) return; + ask.manageTarget(); + String node = method.getUserInput(); + if(checkCommand.isValidInteger(node,false)){ + node = repeatService(node); + } + if (!checkCommand.isQ(node)) reService(tryCount); + } + + 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(int node) { + try { + commandMapping(node); + return true; + } catch (SubwayException e) { + subwayException.isNotUnder4OrQ(); // 에러 메시지 변경하기, while 조건문에서 검증 마침 + } + return false; + } + + public void reService(int tryCount) { + StringBuilder sb = new StringBuilder(); + sb.append("오류가 발생했습니다.") // view 랑 연결해도 될 것 같은데 + .append("\n남은 시도 횟수 : ") + .append(2-tryCount).append("회"); + System.out.println(sb); + startService(tryCount + 1); + } + + public boolean commandMapping(int target) { + if (target == STATION_COMMAND) { + return station.work(station, STATION, false); + } + if (target == LINE_COMMAND) { + return line.work(line, LINE, false); + } + if (target == SECTION_COMMAND) { + return section.work(section, SECTION, true); + } + if (target == MAP_COMMAND) { + return map.work(); + } + return false; + } + + public void closeService(){ + method.closeBuffer(); + } +} diff --git a/src/main/java/subway/controller/subControllers/LineController.java b/src/main/java/subway/controller/subControllers/LineController.java new file mode 100644 index 000000000..5be25f652 --- /dev/null +++ b/src/main/java/subway/controller/subControllers/LineController.java @@ -0,0 +1,62 @@ +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.LineManager; + +public class LineController extends ClassifyMethods { + static LineManager lineManager; + InputException inputException = new InputException(); + private static SubwayException subwayException; + + public LineController(final InitSubwayValues manager) { + lineManager = manager.getLineManager(); + subwayException = manager.getSubwayException(); + } + + @Override + public boolean register() { + String upper, bottom, message; + String line = method.getLine(REGISTER, LINE); + try { + if (lineManager.isEmpty(line) != true) { + inputException.alreadyCreatedLine(); + return false; + } + upper = method.getStation(lineManager, REGISTER, UPPER); + bottom = method.getStation(lineManager, REGISTER, BOTTOM); + lineManager.setStations(line, upper, bottom); + } catch (IllegalArgumentException e) { + inputException.noStation(); + } + 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); + try { + String command = method.getUserInput(); + result = lineManager.delete(command); + } catch (Exception e) { + subwayException.noLine(); + return false; + } + message=makeString.infoMessage(DELETE,LINE, result); + System.out.println(message); + return true; + } + + public boolean read() { + response.printTitle("지하철 노선도"); + StringBuilder list = lineManager.read(); + response.printList(list); + return true; + } +} diff --git a/src/main/java/subway/controller/subControllers/MapController.java b/src/main/java/subway/controller/subControllers/MapController.java new file mode 100644 index 000000000..eee5a3016 --- /dev/null +++ b/src/main/java/subway/controller/subControllers/MapController.java @@ -0,0 +1,18 @@ +package subway.controller.subControllers; + +import subway.service.InitSubwayValues; +import subway.service.LineManager; + +public class MapController { + static LineManager lineManager; + public MapController(InitSubwayValues manager){ + lineManager = manager.getLineManager(); + } + public boolean work(){ + printMap(); + return true; + } + public void printMap(){ + System.out.println(lineManager.getStationLines()); + } +} diff --git a/src/main/java/subway/controller/subControllers/SectionController.java b/src/main/java/subway/controller/subControllers/SectionController.java new file mode 100644 index 000000000..6b5b8428b --- /dev/null +++ b/src/main/java/subway/controller/subControllers/SectionController.java @@ -0,0 +1,76 @@ +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; +import subway.controller.utils.Controller; +import subway.service.InitSubwayValues; +import subway.service.SectionManager; + +public class SectionController extends ClassifyMethods { + private static SectionManager sectionManager; + static SubwayException subwayException; + InputException inputException = new InputException(); + + public SectionController(final InitSubwayValues manager) { + sectionManager = manager.getSectionManager(); + subwayException = manager.getSubwayException(); + } + + @Override // 구간 조회 없 + public boolean sendRequest(final Controller controller, final String command) { + if (command.equals(REGISTER_COMMAND)) { // 등록 + return controller.register(); + } + if (command.equals(DELETE_COMMAND)) { // 삭제 + return controller.delete(); + } + if (command.equals(BACK_COMMAND)) { // 되돌아가기 + return true; + } + subwayException.notValidCommand(); + return false; + } + + @Override + public boolean register() { // 노선, 역 이름, 순서 입력 받고 등록 + String line = method.getLine(); + if (!sectionManager.isEmpty(Targets.LINE.getTarget(), line)) { + inputException.noCreatedLine(); + return false; + } + + String sName = method.getStation(); + if (!sectionManager.isEmpty(Targets.STATION.getTarget(), sName)) { + int index = method.getIndex(); + sectionManager.insertSection(sName, index); + } + response.printInfo("구간이 등록되었습니다."); + return true; + } + + @Override + public boolean delete() { + try { + String line = method.getLine(DELETE, "구간의 노선"); + sectionManager.getLine(line); + String station = method.getStation(sectionManager, DELETE, "구간의 역"); + + if (!sectionManager.isEmpty(Targets.STATION.getTarget(), station)) { // TODO: 존재하지 않는 구간을 삭제하려 할 때 예외 처리 추가 + sectionManager.delete(station); + response.printInfo("구간이 삭제되었습니다."); + } + } catch (IllegalArgumentException e) { + System.err.println(Errors.VALID.getError()); + return false; + } + return true; + } + + @Override + public boolean read() { + return false; + } +} diff --git a/src/main/java/subway/controller/subControllers/StationController.java b/src/main/java/subway/controller/subControllers/StationController.java new file mode 100644 index 000000000..fe62f8025 --- /dev/null +++ b/src/main/java/subway/controller/subControllers/StationController.java @@ -0,0 +1,57 @@ +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; + + +// 역을 관리하는 컨트롤러 +public class StationController extends ClassifyMethods { + static StationManager stationManager; + SubwayException subwayException; + InputException inputException = new InputException(); + public StationController(final InitSubwayValues manager) { + stationManager = manager.getStationManager(); + subwayException = manager.getSubwayException(); + } + + @Override + public boolean register() { + ask.orderWhere(REGISTER, STATION); + boolean result = false; + String station = method.getUserInput(); + try { + result = stationManager.isEmpty(station); + } catch (Exception e) { + inputException.alreadyCreatedStation(); + } + String message = makeString.infoMessage(REGISTER, STATION, result); + response.printInfo(message); + return result; + } + + @Override + public boolean delete() { + ask.orderWhere(DELETE, STATION); + String command = method.getUserInput(); + boolean result=false; + try { + 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() { + response.printTitle("역 목록"); + StringBuilder list = stationManager.read(); + response.printList(list); + return true; + } +} 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..064653119 --- /dev/null +++ b/src/main/java/subway/controller/utils/CheckCommand.java @@ -0,0 +1,47 @@ +package subway.controller.utils; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +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; + } + return false; + } + + public boolean isValidInteger(String node, boolean isSection){ + int command = strToInt(node); + if(isSection) + return isSectionCommand(command); + if(command <= 0) + return false; + if(command > 4) + return false; + return true; + } + + public boolean isSectionCommand(int command){ + 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 { + return Integer.parseInt(node); + } catch (NumberFormatException e) { + 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 new file mode 100644 index 000000000..d4646e445 --- /dev/null +++ b/src/main/java/subway/controller/utils/ClassifyMethods.java @@ -0,0 +1,41 @@ +package subway.controller.utils; + +import subway.config.handler.SubwayException; + + +public abstract class ClassifyMethods implements Controller, Constants { + static SubwayException subwayException = new SubwayException(); + static CheckCommand checkCommand =new CheckCommand(); + + public boolean work(final Controller controller, final String target, boolean isSection) { + ask.WhatToManage(target); + ask.Function(); + String command = method.getUserInput(); + if (subwayException.isBack(command)) return true; + + if(checkCommand.isValidInteger(command, isSection)){ + sendRequest(controller, command); + return true; + } + //에러 발생하는 부분 : 유효하지 않은 입력값 + return false; + } + + // 등록하기 + public boolean sendRequest(final Controller controller, final String command) { + if (command.equals(REGISTER_COMMAND)) { + return controller.register(); + } + if (command.equals(DELETE_COMMAND)) { + return controller.delete(); + } + if (command.equals(READ_COMMAND)) { + return controller.read(); + } + if (command.equals(BACK_COMMAND)) { + return true; + } +// subwayException.notValidCommand(); + return false; + } +} diff --git a/src/main/java/subway/controller/utils/Constants.java b/src/main/java/subway/controller/utils/Constants.java new file mode 100644 index 000000000..11feb8664 --- /dev/null +++ b/src/main/java/subway/controller/utils/Constants.java @@ -0,0 +1,32 @@ +package subway.controller.utils; + +import subway.config.constants.views.Methods; +import subway.config.constants.Targets; +import subway.view.AskView; +import subway.view.ResponseView; +import subway.view.util.MakeString; + +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 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(); + + 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 new file mode 100644 index 000000000..82e0ba4cf --- /dev/null +++ b/src/main/java/subway/controller/utils/Controller.java @@ -0,0 +1,9 @@ +package subway.controller.utils; + +public interface Controller { + static final Methods method = new Methods(); + + 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..5c1cf7fd6 --- /dev/null +++ b/src/main/java/subway/controller/utils/Methods.java @@ -0,0 +1,73 @@ +package subway.controller.utils; + +import subway.config.handler.InputException; +import subway.service.Managerable; + +import java.io.BufferedReader; +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 { + private BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); + InputException inputException = new InputException(); + public Methods() {} + + public void closeBuffer(){ + try{ + br.close(); + }catch (IOException e){ + System.err.println("[ERROR] BufferedReader를 닫는 중 에러가 발생했습니다."); + } + } + + public String getUserInput(){ + try{ + return br.readLine(); + }catch (IOException e){ + System.err.println("[ERROR] 사용자 입력을 받는 중 에러가 발생했습니다."); + } + return null; + } + + public int getIndex() { + ask.orderWhere(); + String input = getUserInput(); + if (subwayException.isBack(input) == true) return -1; + int index = checkCommand.strToInt(input); + if(index <= 0) subwayException.checkCommand(); + return index; + } + + public String getLine() { + ask.orderWhere(LINE); + try { + String line = getUserInput(); + return line; + } catch (Exception e) { + subwayException.checkCommand(); + } + return null; + } + + public String getLine(String function, String target) { + ask.orderWhere(function, target); + return getUserInput(); + } + + public String getStation() { + ask.orderWhere("역 이름"); + return getUserInput(); + } + + public String getStation(Managerable manager, String function, String station) { + ask.orderWhere(function, station); + String node = getUserInput(); + 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 f4d738d5a..dfef43ba7 100644 --- a/src/main/java/subway/domain/Line.java +++ b/src/main/java/subway/domain/Line.java @@ -1,15 +1,59 @@ package subway.domain; +import java.util.LinkedList; + public class Line { private String name; + private LinkedList stations = new LinkedList<>(); - public Line(String name) { + public Line(final String name) { this.name = 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(final Station upper, final Station bottom) { + stations.addFirst(upper); + stations.addLast(bottom); + } + + public void addStation(final Station station, final int index) { + if(index==0){ + stations.addFirst(station); + return; + } + if(index==stations.size()){ + stations.addLast(station); + return; + } + stations.add(index-1, station); + } - // 추가 기능 구현 + 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(s); + return true; + } + } + return false; + } + + public int getSize(){ + return this.stations.size(); + } + + public Station getStation(int index){ + return this.stations.get(index); + } } diff --git a/src/main/java/subway/domain/LineRepository.java b/src/main/java/subway/domain/LineRepository.java index 49132ddb6..727b4ada3 100644 --- a/src/main/java/subway/domain/LineRepository.java +++ b/src/main/java/subway/domain/LineRepository.java @@ -12,11 +12,24 @@ 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 boolean deleteLineByName(String name) { - return lines.removeIf(line -> Objects.equals(line.getName(), name)); + public static void addLine(final Line line, final Station station, final int index) { + line.addStation(station, index); + } + + public static boolean deleteLineByName(final String name) { + return lines.removeIf(line -> Objects.equals(line.getName(), name) && line.getSize()>2); + } + + public static Line getLineByName(final String name) { + for (Line line : lines) { + if (line.getName().equals(name)) { + return line; + } + } + return null; } } diff --git a/src/main/java/subway/domain/Station.java b/src/main/java/subway/domain/Station.java index bdb142590..413c6ddaf 100644 --- a/src/main/java/subway/domain/Station.java +++ b/src/main/java/subway/domain/Station.java @@ -1,15 +1,40 @@ package subway.domain; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + public class Station { private String name; + private List line = new LinkedList(); + private int canDelete=0; - public Station(String name) { + public Station(final String name) { this.name = name; } public String getName() { - return name; + return this.name; + } + public void addLine(Line lineNode){ + line.add(lineNode); + } + public void deleteLine(Line lineNode){ + line.remove(lineNode); + } + + public boolean hasNoLine(){ + 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/domain/StationRepository.java b/src/main/java/subway/domain/StationRepository.java index b7245c0f3..7a64e2633 100644 --- a/src/main/java/subway/domain/StationRepository.java +++ b/src/main/java/subway/domain/StationRepository.java @@ -16,7 +16,15 @@ public static void addStation(Station station) { stations.add(station); } - public static boolean deleteStation(String name) { - return stations.removeIf(station -> Objects.equals(station.getName(), name)); + public static boolean deleteStation(final String name) { + return stations.removeIf(station -> Objects.equals(station.getName(), name) && station.hasNoLine()); + } + public static Station getStationByName(final String name){ + for(Station station : stations){ + if(station.getName().equals(name)){ + return station; + } + } + return null; } } diff --git a/src/main/java/subway/service/InitSubwayValues.java b/src/main/java/subway/service/InitSubwayValues.java new file mode 100644 index 000000000..19535c153 --- /dev/null +++ b/src/main/java/subway/service/InitSubwayValues.java @@ -0,0 +1,73 @@ +package subway.service; + +import subway.config.constants.initValues.*; +import subway.config.handler.SubwayException; +import subway.domain.Line; +import subway.domain.Station; +import subway.service.utils.Constants; + +import java.util.List; + + +public class InitSubwayValues implements Constants { + static final 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 InitSubwayValues(){ + // 초기값 삽입 + 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); + 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/service/LineManager.java b/src/main/java/subway/service/LineManager.java new file mode 100644 index 000000000..e10f30439 --- /dev/null +++ b/src/main/java/subway/service/LineManager.java @@ -0,0 +1,70 @@ +package subway.service; + +import subway.domain.Line; +import subway.domain.Station; +import subway.service.utils.LineMakeString; + +import java.util.List; + +import static subway.service.InitSubwayValues.*; + +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; + } + return false; + } + + 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 = setLine(name); + Station upper = stationManager.getByStationName(upperStation); + Station bottom = stationManager.getByStationName(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 lineList = lineRepo.lines(); + for(Line line:lineList){ + 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/Managerable.java b/src/main/java/subway/service/Managerable.java new file mode 100644 index 000000000..55525866e --- /dev/null +++ b/src/main/java/subway/service/Managerable.java @@ -0,0 +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 new file mode 100644 index 000000000..179d943a5 --- /dev/null +++ b/src/main/java/subway/service/SectionManager.java @@ -0,0 +1,63 @@ +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=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 isLineEmpty(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 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 station) { + if(subwayException.isBack(station) == true) return true; + if(line.deleteStation(station)) return true; + subwayException.checkCommand(); + return false; + } + + @Override + public StringBuilder read() { + StringBuilder sb = new StringBuilder(); + return sb; + } +} diff --git a/src/main/java/subway/service/StationManager.java b/src/main/java/subway/service/StationManager.java new file mode 100644 index 000000000..22455904f --- /dev/null +++ b/src/main/java/subway/service/StationManager.java @@ -0,0 +1,41 @@ +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){ + inputException.alreadyCreatedStation(); + return false; + } + stationRepo.addStation(new Station(name)); + return true; + } + @Override + public boolean delete(final String name) { + return stationRepo.deleteStation(name); + } + + @Override + public StringBuilder read() { + StringBuilder sb = new StringBuilder(); + for(Station station : stationRepo.stations()){ + sb.append("[INFO] " + station.getName()+"\n"); + } + return sb; + } + + public Station getByStationName(final String name){ + Station station =stationRepo.getStationByName(name); + if(station == null){ + subwayException.noStation(); + } + return station; + } +} diff --git a/src/main/java/subway/service/utils/Constants.java b/src/main/java/subway/service/utils/Constants.java new file mode 100644 index 000000000..5aba15a48 --- /dev/null +++ b/src/main/java/subway/service/utils/Constants.java @@ -0,0 +1,16 @@ +package subway.service.utils; + +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(); + static final StationRepository stationRepo = new StationRepository(); + + static final StationManager stationManager = new StationManager(); + static final SectionManager sectionManager = new SectionManager(); + static final LineManager lineManager = new LineManager(); +} diff --git a/src/main/java/subway/service/utils/LineMakeString.java b/src/main/java/subway/service/utils/LineMakeString.java new file mode 100644 index 000000000..469d0fd67 --- /dev/null +++ b/src/main/java/subway/service/utils/LineMakeString.java @@ -0,0 +1,20 @@ +package subway.service.utils; + +import subway.config.constants.views.Prefixes; + +public class LineMakeString { + String INFO = Prefixes.INFO.getPrefix(); + public String title(final String rail){ + StringBuilder sb= new StringBuilder(); + sb.append(INFO).append(rail).append("\n"); + sb.append(INFO).append("---").append("\n"); + return sb.toString(); + } + + public String lines(final String name){ + StringBuilder sb = new StringBuilder(); + 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 new file mode 100644 index 000000000..77a5bdc90 --- /dev/null +++ b/src/main/java/subway/view/AskView.java @@ -0,0 +1,40 @@ +package subway.view; + +import subway.view.util.Constants; +import subway.view.util.MakeString; + +public class AskView implements Constants { // TODO: (고민) view 가 너무 중구난방인 거 같은데 controller, service 처럼 분할할까. + static final MakeString makeString = new MakeString(); + + public void manageTarget() { + System.out.println(makeString.showTitle(MAIN)); + System.out.println(makeString.showIndex(MAIN)); + } + + public void WhatToManage(final String target) { + System.out.println(makeString.showTitle(target)); + System.out.println(makeString.showIndex(target)); + } + + //"원하는 기능을 선택하세요." + public void Function() { + System.out.println(makeString.askFunction()); + } + + //## 등록/삭제할 역/노선을 입력하세요 + public void orderWhere(final String work, final String name) { + String sentence = makeString.askName(work, name); + System.out.println(sentence); + } + + // 역/노선을 입력하세요 + public void orderWhere(final String name) { + String sentence = makeString.askName(name); + System.out.println(sentence); + } + + public void orderWhere() { // 등록 or 삭제 + System.out.println(makeString.askSequence()); + } + +} diff --git a/src/main/java/subway/view/ResponseView.java b/src/main/java/subway/view/ResponseView.java new file mode 100644 index 000000000..182e32c5a --- /dev/null +++ b/src/main/java/subway/view/ResponseView.java @@ -0,0 +1,22 @@ +package subway.view; + +import subway.view.util.Constants; + +public class ResponseView implements Constants { + public void printInfo(final String message){ + StringBuilder sb = new StringBuilder(); + sb.append(INFO).append(message); + System.out.println(sb); + sb.setLength(0); + } + + public void printTitle(final String title){ + StringBuilder sb = new StringBuilder(); + sb.append(SHARP).append(title); + System.out.println(sb); + sb.setLength(0); + } + public void printList(final StringBuilder message){ + System.out.println(message); + } +} 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 new file mode 100644 index 000000000..7ecb14a41 --- /dev/null +++ b/src/main/java/subway/view/util/MakeString.java @@ -0,0 +1,57 @@ +package subway.view.util; + +public class MakeString implements Constants{ + // ## ~~ 화면 + public String showTitle(final String title){ + return getTitle(MAIN, 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(" 화면"); + return sb.toString(); + } + sb.append(SHARP).append(title); + sb.append("관리 화면"); + return sb.toString(); + } + + public String showIndex(final String target){ + if(target.equals(MAIN)){ + return MAIN_INDEX; + } + + return METHOD.getIndex(target); + } + public String infoMessage(final String work, final String target, final boolean result){ + StringBuilder sb = new StringBuilder(); + sb.append("지하철 ").append(target).append("이 "); + if(result == true){ + sb.append(work).append("되었습니다."); + } + if(result == false){ + sb.append(work).append("되지 않았습니다. 연결된 노선이나 해당 노선에 속한 역을 확인해주세요."); + } + return sb.toString(); + } + + // 노선/역이름 을 입력하세요. + 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(); + } +}