diff --git a/src/main/java/LadderApplication.java b/src/main/java/LadderApplication.java index 9e6b41f..d1b2089 100644 --- a/src/main/java/LadderApplication.java +++ b/src/main/java/LadderApplication.java @@ -1,10 +1,13 @@ import controller.LadderController; import domain.Ladder; +import domain.Participants; public class LadderApplication { public static void main(String[] args) { final LadderController ladderController = new LadderController(); Ladder ladder = ladderController.createLadder(); - ladderController.printResult(ladder); + Participants participants = ladderController.createParticipants(ladder.getWidth()); + ladderController.getResult(ladder, participants); + ladderController.printResult(ladder, participants); } } diff --git a/src/main/java/controller/LadderController.java b/src/main/java/controller/LadderController.java index 93dbd28..afcbceb 100644 --- a/src/main/java/controller/LadderController.java +++ b/src/main/java/controller/LadderController.java @@ -1,22 +1,36 @@ package controller; import domain.Ladder; +import domain.Participant; +import domain.Participants; import domain.Size; +import java.util.List; +import service.LadderService; import view.InputView; import view.OutputView; public class LadderController { private final OutputView outputView = new OutputView(); private final InputView inputView = new InputView(); + private final LadderService ladderService = new LadderService(); public Ladder createLadder() { - Size width = inputView.getLadderWidth(); - Size height = inputView.getLadderHeight(); - return new Ladder(height, width); + int width = inputView.getLadderWidth(); + int height = inputView.getLadderHeight(); + return ladderService.createLadder(height, width); } - public void printResult(Ladder ladder){ - outputView.printResult(); + public Participants createParticipants(int width) { + return ladderService.createParticipants(width); + } + + public void getResult(Ladder ladder, Participants participants) { + ladderService.getResult(ladder, participants); + } + + public void printResult(Ladder ladder, Participants participants){ + outputView.printResultText(); outputView.printLadder(ladder); + outputView.printResult(participants); } } diff --git a/src/main/java/domain/Ladder.java b/src/main/java/domain/Ladder.java index 100ee46..abb785d 100644 --- a/src/main/java/domain/Ladder.java +++ b/src/main/java/domain/Ladder.java @@ -10,6 +10,10 @@ public Ladder(Size ladderSize, Size lineSize) { this.lines = generateLines(ladderSize, lineSize); } + public Ladder(List lines) { + this.lines = lines; + } + private List generateLines(Size ladderSize, Size lineSize) { List lines = new ArrayList<>(); for (int i = 0; i < ladderSize.getSize(); i++) { @@ -18,7 +22,36 @@ private List generateLines(Size ladderSize, Size lineSize) { return lines; } + public void getResult(Participants participants){ + for (Line line : lines) { + changeByLine(line, participants.getParticipants()); + } + } + + private void changeByLine(Line line, List participants){ + for(int i = 0; i< line.getPoints().size() ; i++){ + changeByPoint(i,line.getPoints().get(i), participants); + } + } + + private void changeByPoint(int idx, Point point, List participants){ + if(point.isConnected()){ + move(idx, participants); + } + } + + private void move(int idx, List participants){ + int temp = participants.get(idx).getEnd(); + participants.get(idx).changeEnd(participants.get(idx+1).getEnd()); + participants.get(idx+1).changeEnd(temp); + } + public List getLines() { return lines; } + + public int getWidth() { + return lines.get(0).getPoints().size()+1; + } + } diff --git a/src/main/java/domain/Participant.java b/src/main/java/domain/Participant.java new file mode 100644 index 0000000..ef70a0f --- /dev/null +++ b/src/main/java/domain/Participant.java @@ -0,0 +1,23 @@ +package domain; + +public class Participant { + private final int start; + private int end; + + public Participant(int start) { + this.start = start; + this.end = start; + } + + public int getStart() { + return start; + } + + public int getEnd() { + return end; + } + + public void changeEnd(int end) { + this.end = end; + } +} diff --git a/src/main/java/domain/Participants.java b/src/main/java/domain/Participants.java new file mode 100644 index 0000000..9e651a4 --- /dev/null +++ b/src/main/java/domain/Participants.java @@ -0,0 +1,15 @@ +package domain; + +import java.util.List; + +public class Participants { + private final List participants; + + public Participants(List participants) { + this.participants = participants; + } + + public List getParticipants() { + return participants; + } +} diff --git a/src/main/java/service/LadderService.java b/src/main/java/service/LadderService.java new file mode 100644 index 0000000..9f6d500 --- /dev/null +++ b/src/main/java/service/LadderService.java @@ -0,0 +1,29 @@ +package service; + +import domain.Ladder; +import domain.Participant; +import domain.Participants; +import domain.Size; +import java.util.ArrayList; +import java.util.List; + +public class LadderService { + + public Ladder createLadder(int height, int width) { + Size heightSize = new Size(height); + Size widthSize = new Size(width); + return new Ladder(heightSize, widthSize); + } + + public Participants createParticipants(int width) { + List participants = new ArrayList<>(); + for(int i = 0 ; i< width; i++){ + participants.add(new Participant(i)); + } + return new Participants(participants); + } + + public void getResult(Ladder ladder, Participants participants) { + ladder.getResult(participants); + } +} diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index ef183b5..963088c 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -1,24 +1,23 @@ package view; -import domain.Size; import java.util.Scanner; public class InputView { private Scanner scanner = new Scanner(System.in); - public Size getLadderWidth() { + public int getLadderWidth() { System.out.println("사다리의 넓이는 몇 개인가요?"); int width = scanner.nextInt(); scanner.nextLine(); System.out.println(); - return new Size(width); + return width; } - public Size getLadderHeight() { + public int getLadderHeight() { System.out.println("사다리의 높이는 몇 개인가요?"); int height = scanner.nextInt(); scanner.nextLine(); System.out.println(); - return new Size(height); + return height; } } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 4d411da..f484d50 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -2,11 +2,14 @@ import domain.Ladder; import domain.Line; +import domain.Participant; +import domain.Participants; import domain.Point; +import java.util.List; public class OutputView { - public void printResult(){ + public void printResultText(){ System.out.println("실행결과"); } @@ -25,4 +28,9 @@ public void printLine(Line line){ System.out.print("|"); } + public void printResult(Participants participants){ + for(Participant participant : participants.getParticipants()){ + System.out.println(participant.getStart() + " -> " + participant.getEnd()); + } + } } diff --git a/src/test/java/domain/LadderTest.java b/src/test/java/domain/LadderTest.java index 85a3660..b6cbec6 100644 --- a/src/test/java/domain/LadderTest.java +++ b/src/test/java/domain/LadderTest.java @@ -2,6 +2,8 @@ import static org.assertj.core.api.Assertions.assertThat; +import java.util.ArrayList; +import java.util.List; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -12,4 +14,23 @@ public void creationTest() { Ladder ladder = new Ladder(new Size(4),new Size(4)); assertThat(ladder.getLines().size()).isEqualTo(4); } + + @DisplayName("사다리 연결되었을 경우, 이웃 참가자 결과 swap 수행") + @Test + public void getResultTest() { + List participantList = new ArrayList(); + for (int i = 0; i < 4; i++) { + participantList.add(new Participant(i)); + } + Participants participants = new Participants(participantList); + List points = List.of(new Point(true), new Point(false), new Point(true)); + Line line = new Line(points); + Ladder ladder = new Ladder(List.of(line)); + ladder.getResult(participants); + + assertThat(participants.getParticipants().get(0).getEnd()).isEqualTo(1); + assertThat(participants.getParticipants().get(1).getEnd()).isEqualTo(0); + assertThat(participants.getParticipants().get(2).getEnd()).isEqualTo(3); + assertThat(participants.getParticipants().get(3).getEnd()).isEqualTo(2); + } }