Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[지하철 노선도] 김주언 제출합니다. #4

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/main/java/subway/Application.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package subway;

import com.sun.tools.javac.Main;
import subway.controller.MainController;
import subway.domain.Station;
import subway.domain.StationRepository;

import java.util.List;
import java.util.Scanner;


public class Application {
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
// TODO: 프로그램 구현
MainController mainController = new MainController();
mainController.run();
}
}
53 changes: 53 additions & 0 deletions src/main/java/subway/controller/MainController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package subway.controller;

import subway.domain.*;
import subway.view.InputView;
import subway.view.OutputView;

import java.util.List;
import java.util.Map;

public class MainController {
private final OutputView outputView;
private final InputView inputView;
private final StationService stationService;
private final LineService lineService;
private final SectionService sectionService;

public MainController() {
outputView = new OutputView();
inputView = new InputView();
stationService = new StationService(outputView, inputView);
lineService = new LineService(outputView, inputView);
sectionService = new SectionService(outputView, inputView);
}

public void run(){
while (true){
outputView.mainPage();
String input = inputView.selectFunction();

if(input.equals("1")){
stationService.manage();
continue;
}
if(input.equals("2")){
lineService.manage();
continue;
}
if(input.equals("3")){
sectionService.manage();
continue;
}
if(input.equals("4")){
lineService.listALl();
continue;
}
if(input.equals("Q")) return;


}
}


}
5 changes: 5 additions & 0 deletions src/main/java/subway/domain/LineRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public static void addLine(Line line) {
lines.add(line);
}

public static boolean isExist(String name) {
return lines().stream()
.anyMatch(station -> station.getName().equals(name));
}

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

import subway.view.InputView;
import subway.view.OutputView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LineService {
private final OutputView outputView;
private final InputView inputView;

public LineService(OutputView outputView, InputView inputView) {
this.outputView = outputView;
this.inputView = inputView;
}

public void manage(){
outputView.lineManagementPage();
String input = inputView.selectFunction();

if(input.equals("1")){
registerLine();
return;
}
if(input.equals("2")){
deleteLine();
return;
}
if(input.equals("3")){
listLines();
return;
}
if(input.equals("B")) return;
}

private void registerLine(){
String lineName = inputView.inputRegisterLine();
String startStationName = inputView.inputStartStation();
String endStationName = inputView.inputEndStation();

if(lineName.length() < 2){
System.out.println("두 글자 이상");
return;
}
Line line = new Line(lineName);

if(Subway.lines.containsKey(line)){
System.out.println("이미 존재하는 노선");
}
if(!StationRepository.isExist(startStationName)){
System.out.println("존재하지 않는 역");
return;
}
if(!StationRepository.isExist(endStationName)){
System.out.println("존재하지 않는 역");
return;
}

LineRepository.addLine(line);

List<Station> stations = new ArrayList<>();
stations.add(new Station(startStationName));
stations.add(new Station(endStationName));

Subway.lines.put(line, stations);
outputView.registeredLine();
}

public void deleteLine(){
String stationName = inputView.inputDeleteLine();
if(!LineRepository.deleteLineByName(stationName)){
System.out.println("존재하지 않는 노선");
return;
}
Line line = new Line(stationName);
Subway.lines.remove(line);
outputView.deletedLine();
}

public void listLines(){
LineRepository.lines().stream()
.map(Line::getName)
.forEach(name -> System.out.println("[INFO} " + name));
}

public void listALl(){
for (Map.Entry<Line, List<Station>> entry : Subway.lines.entrySet()) {
Line line = entry.getKey();
List<Station> stations = entry.getValue();

System.out.println("[INFO] " + line.getName());
for (Station station : stations) {
System.out.println("[INFO}" + station.getName());
}

System.out.println();
}
}
}
72 changes: 72 additions & 0 deletions src/main/java/subway/domain/SectionService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package subway.domain;

import subway.view.InputView;
import subway.view.OutputView;

import java.util.List;


public class SectionService {
private final OutputView outputView;
private final InputView inputView;

public SectionService(OutputView outputView, InputView inputView) {
this.outputView = outputView;
this.inputView = inputView;
}

public void manage(){
outputView.sectionManagementPage();
String input = inputView.selectFunction();

if(input.equals("1")){
registerSection();
return;
}
if(input.equals("2")){
deleteSection();
}
if(input.equals("B")) return;
}

private void registerSection(){
String line = inputView.inputLineToRegisterSection();
String station = inputView.inputStationToRegisterSection();
int order = Integer.parseInt(inputView.inputOrder());

if(!LineRepository.isExist(line)){
System.out.println("[ERROR] 존재하지 않는 노선입니다.");
return;
}
if(!StationRepository.isExist(station)){
System.out.println("[ERROR] 존재하지 않는 역입니다.");
return;
}

List<Station> stations = Subway.lines.get(line);
if(stations.contains(new Station(station))){
System.out.println("[ERROR] 이미 존재하는 역입니다.");
}

stations.add(order, new Station(station));
outputView.registeredSection();
}

private void deleteSection(){
String line = inputView.inputDeleteLineOfSection();
String station = inputView.inputDeleteStationOfSection();

if(!LineRepository.isExist(line)){
System.out.println("[ERROR] 존재하지 않는 노선입니다.");
return;
}
if(!StationRepository.isExist(station)){
System.out.println("[ERROR] 존재하지 않는 역입니다.");
return;
}

List<Station> stations = Subway.lines.get(line);
stations.remove(new Station(station));
outputView.deletedStation();
}
}
5 changes: 5 additions & 0 deletions src/main/java/subway/domain/StationRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public static void addStation(Station station) {
public static boolean deleteStation(String name) {
return stations.removeIf(station -> Objects.equals(station.getName(), name));
}

public static boolean isExist(String name) {
return stations.stream()
.anyMatch(station -> station.getName().equals(name));
}
}
63 changes: 63 additions & 0 deletions src/main/java/subway/domain/StationService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package subway.domain;

import subway.view.InputView;
import subway.view.OutputView;

public class StationService {
private final OutputView outputView;
private final InputView inputView;

public StationService(OutputView outputView, InputView inputView) {
this.outputView = outputView;
this.inputView = inputView;
}

public void manage(){
outputView.stationManagementPage();
String input = inputView.selectFunction();

if(input.equals("1")){
registerStation();
return;
}
if(input.equals("2")){
deleteStation();
return;
}
if(input.equals("3")){
listStation();
return;
}
if(input.equals("B")) return;
}

private void registerStation(){
String stationName = inputView.inputRegisterStation();

if(stationName.length() < 2){
System.out.println("[ERROR] 두 글자 이상 입력해야합니다.");
return;
}
if(StationRepository.isExist(stationName)){
System.out.println("[ERROR] 존재하는 역입니다.");
return;
}

StationRepository.addStation(new Station(stationName));
outputView.registeredStation();
}

private void deleteStation(){
String stationName = inputView.inputDeleteStation();
if(!StationRepository.deleteStation(stationName)){
System.out.println("[ERROR] 역 삭제에 실패했습니다.");
}
outputView.deletedStation();
}

private void listStation(){
StationRepository.stations().stream()
.map(Station::getName)
.forEach(name -> System.out.println("[INFO] " + name));
}
}
10 changes: 10 additions & 0 deletions src/main/java/subway/domain/Subway.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package subway.domain;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Subway {
public static Map<Line, List<Station>> lines = new HashMap<>();

}
Loading