diff --git a/src/main/java/engine/LinesCreator.java b/src/main/java/engine/LinesCreator.java new file mode 100644 index 0000000000..0171a359fa --- /dev/null +++ b/src/main/java/engine/LinesCreator.java @@ -0,0 +1,12 @@ +package engine; + +import nextstep.ladder.domain.Line; +import nextstep.ladder.strategy.LineStrategy; + +import java.util.List; + + +public interface LinesCreator { + void generateLine(int participantCnt, int maxLadder, LineStrategy lineStrategy); + List getLines(); +} diff --git a/src/main/java/factory/LinesFactoryBean.java b/src/main/java/factory/LinesFactoryBean.java new file mode 100644 index 0000000000..f08d38d3c6 --- /dev/null +++ b/src/main/java/factory/LinesFactoryBean.java @@ -0,0 +1,15 @@ +package factory; + +import engine.LinesCreator; +import nextstep.ladder.domain.NextStepLines; +import nextstep.ladder.strategy.LineStrategy; + +public class LinesFactoryBean { + + public static LinesCreator createNextStepLadderFactory(int size, int maxLadder, LineStrategy lineStrategy) { + NextStepLines nextStepLines = new NextStepLines(); + nextStepLines.generateLine(size, maxLadder, lineStrategy); + return nextStepLines; + } + +} diff --git a/src/main/java/nextstep/ladder/controller/LadderController.java b/src/main/java/nextstep/ladder/controller/LadderController.java index 862f7cd05a..1e216e934c 100644 --- a/src/main/java/nextstep/ladder/controller/LadderController.java +++ b/src/main/java/nextstep/ladder/controller/LadderController.java @@ -1,12 +1,13 @@ package nextstep.ladder.controller; +import engine.LinesCreator; +import factory.LinesFactoryBean; import nextstep.ladder.domain.LadderExecutor; -import nextstep.ladder.domain.LadderFactory; import nextstep.ladder.domain.ExecuteResult; import nextstep.ladder.domain.LadderResult; -import nextstep.ladder.domain.Lines; import nextstep.ladder.domain.MachingResult; import nextstep.ladder.domain.Participants; +import nextstep.ladder.strategy.LadderLineStrategy; import nextstep.ladder.view.InputView; import nextstep.ladder.view.OutputView; @@ -28,23 +29,33 @@ public void startLadder() { ExecuteResult executeResult = new ExecuteResult(participants.size(), executeResultStr); - Lines lines = LadderFactory.createLadder(participants, maxLadder); + LinesCreator linesCreator = LinesFactoryBean.createNextStepLadderFactory(participants.size(), maxLadder, new LadderLineStrategy()); - LadderExecutor ladderExecutor = new LadderExecutor(lines, participants); + LadderExecutor ladderExecutor = new LadderExecutor(linesCreator, participants); MachingResult machingResult = ladderExecutor.play(); LadderResult ladderResult = machingResult.map(participants, executeResult); outputView.outputParticipants(participants); - outputView.outputResult(executeResult, lines, ladderResult); + outputView.outputResult(executeResult, linesCreator, ladderResult); - String participant = inputView.inputParticipantResult(); - outputView.outputParticipantResult(ladderResult.getReward(participant)); - - participant = inputView.inputParticipantResult(); - outputView.outputParticipantResult(ladderResult.getReward(participant)); + inputOutputFindResult(ladderResult); outputView.outputParticipantAllResult(ladderResult); } + private void inputOutputFindResult(LadderResult ladderResult) { + String participant = inputOutputResult(ladderResult); + + if (!"all".equals(participant)) { + inputOutputResult(ladderResult); + } + } + + private String inputOutputResult(LadderResult ladderResult) { + String participant = inputView.inputParticipantResult(); + outputView.outputParticipantResult(ladderResult.getReward(participant)); + return participant; + } + } diff --git a/src/main/java/nextstep/ladder/domain/LadderExecutor.java b/src/main/java/nextstep/ladder/domain/LadderExecutor.java index aa8a6e0640..b72d1e8416 100644 --- a/src/main/java/nextstep/ladder/domain/LadderExecutor.java +++ b/src/main/java/nextstep/ladder/domain/LadderExecutor.java @@ -1,14 +1,16 @@ package nextstep.ladder.domain; +import engine.LinesCreator; + import java.util.Collections; public class LadderExecutor { - private Lines lines; + private LinesCreator linesCreator; private Participants participants; - public LadderExecutor(Lines lines, Participants participants) { - this.lines = lines; + public LadderExecutor(LinesCreator linesCreator, Participants participants) { + this.linesCreator = linesCreator; this.participants = participants; } @@ -22,7 +24,7 @@ public MachingResult play() { } private void moveAndCollectResult(MachingResult results, Position position) { - for (Line line : lines.getLines()) { + for (Line line : linesCreator.getLines()) { Point point = PointFactory.generatePoint(position.getX(), line); position.move(point.getDirection()); } diff --git a/src/main/java/nextstep/ladder/domain/LadderFactory.java b/src/main/java/nextstep/ladder/domain/LadderFactory.java deleted file mode 100644 index e084cb6836..0000000000 --- a/src/main/java/nextstep/ladder/domain/LadderFactory.java +++ /dev/null @@ -1,13 +0,0 @@ -package nextstep.ladder.domain; - -import nextstep.ladder.strategy.LadderLineStrategy; - -public class LadderFactory { - - public static Lines createLadder(Participants participants, int maxLadder) { - Lines lines = new Lines(); - lines.generateLine(participants.size(), maxLadder, new LadderLineStrategy()); - return lines; - } - -} diff --git a/src/main/java/nextstep/ladder/domain/LadderResult.java b/src/main/java/nextstep/ladder/domain/LadderResult.java index cd0bc1e1f6..7ef9cd2b66 100644 --- a/src/main/java/nextstep/ladder/domain/LadderResult.java +++ b/src/main/java/nextstep/ladder/domain/LadderResult.java @@ -25,6 +25,9 @@ public Map getPlayerToReward() { } public String getReward(String name) { + if (name.equals("all")) { + return ""; + } return playerToReward.get(new Participant(name)); } } diff --git a/src/main/java/nextstep/ladder/domain/Lines.java b/src/main/java/nextstep/ladder/domain/NextStepLines.java similarity index 86% rename from src/main/java/nextstep/ladder/domain/Lines.java rename to src/main/java/nextstep/ladder/domain/NextStepLines.java index 4a931f848f..0047a3febc 100644 --- a/src/main/java/nextstep/ladder/domain/Lines.java +++ b/src/main/java/nextstep/ladder/domain/NextStepLines.java @@ -1,18 +1,19 @@ package nextstep.ladder.domain; +import engine.LinesCreator; import nextstep.ladder.strategy.LineStrategy; import java.util.ArrayList; import java.util.List; -public class Lines { +public class NextStepLines implements LinesCreator { private final List lines; - public Lines() { + public NextStepLines() { this(new ArrayList()); } - public Lines(ArrayList lines) { + public NextStepLines(ArrayList lines) { this.lines = new ArrayList<>(lines); } @@ -27,10 +28,6 @@ private void generateLine(int participantCnt, LineStrategy lineStrategy) { lines.add(line); } - public int size() { - return lines.size(); - } - public List getLines() { return lines; } @@ -42,4 +39,8 @@ public int getLineSize(int linesIdx) { public boolean getPoint(int linesIdx, int lineIdx) { return lines.get(linesIdx).getLine(lineIdx); } + + public int size() { + return lines.size(); + } } diff --git a/src/main/java/nextstep/ladder/view/OutputView.java b/src/main/java/nextstep/ladder/view/OutputView.java index 70692a33a7..2cb40ca201 100644 --- a/src/main/java/nextstep/ladder/view/OutputView.java +++ b/src/main/java/nextstep/ladder/view/OutputView.java @@ -1,8 +1,9 @@ package nextstep.ladder.view; +import engine.LinesCreator; import nextstep.ladder.domain.ExecuteResult; import nextstep.ladder.domain.LadderResult; -import nextstep.ladder.domain.Lines; +import nextstep.ladder.domain.NextStepLines; import nextstep.ladder.domain.Participant; import nextstep.ladder.domain.Participants; @@ -18,16 +19,16 @@ public class OutputView { POINTS_LINE.put(false, outputLineFalse()); } - public void outputResult(ExecuteResult executeResult, Lines lines, LadderResult ladderResult) { + public void outputResult(ExecuteResult executeResult, LinesCreator linesCreator, LadderResult ladderResult) { System.out.println(); - outputLadder(lines); + outputLadder(linesCreator); ouputExecuteResult(executeResult); } - private static void outputLadder(Lines lines) { - lines.getLines().forEach(line -> { + private static void outputLadder(LinesCreator linesCreator) { + ((NextStepLines) linesCreator).getLines().forEach(line -> { line.getPoints().forEach(points -> System.out.print(POINTS_LINE.get(points))); System.out.println("|"); }); diff --git a/src/test/java/engine/LinesFactoryBeanTest.java b/src/test/java/engine/LinesFactoryBeanTest.java new file mode 100644 index 0000000000..3a9337f7b6 --- /dev/null +++ b/src/test/java/engine/LinesFactoryBeanTest.java @@ -0,0 +1,23 @@ +package engine; + +import factory.LinesFactoryBean; +import nextstep.ladder.domain.LadderExecutor; +import nextstep.ladder.domain.MachingResult; +import nextstep.ladder.domain.Participants; +import nextstep.ladder.strategy.TrueLineStrategy; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LinesFactoryBeanTest { + + @Test + void createNextStepLadderFactory() { + LinesCreator linesCreator = LinesFactoryBean.createNextStepLadderFactory(3, 5, new TrueLineStrategy()); + Participants participants = new Participants("a,b,c"); + LadderExecutor ladderExecutor = new LadderExecutor(linesCreator, participants); + MachingResult machingResult = ladderExecutor.play(); + + assertThat(machingResult.getMachingResultMap()).hasSize(3); + } +} diff --git a/src/test/java/nextstep/ladder/domain/LadderExecutorTest.java b/src/test/java/nextstep/ladder/domain/LadderExecutorTest.java index 73f514ea62..a56621ea66 100644 --- a/src/test/java/nextstep/ladder/domain/LadderExecutorTest.java +++ b/src/test/java/nextstep/ladder/domain/LadderExecutorTest.java @@ -18,7 +18,7 @@ void setUp() { @Test @DisplayName("사다리를 실행하고 결과를 수집") void moveAndCollectResult() { - LadderExecutor ladderExecutor = new LadderExecutor(LinesTest.lines(), participants); + LadderExecutor ladderExecutor = new LadderExecutor(NextStepLinesTest.lines(), participants); MachingResult machingResult = ladderExecutor.play(); assertThat(machingResult.getMachingResultMap().get(0)).isEqualTo(1); diff --git a/src/test/java/nextstep/ladder/domain/LinesTest.java b/src/test/java/nextstep/ladder/domain/LinesTest.java deleted file mode 100644 index 85dc1253e4..0000000000 --- a/src/test/java/nextstep/ladder/domain/LinesTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package nextstep.ladder.domain; - -import nextstep.ladder.strategy.LineStrategy; -import nextstep.ladder.strategy.TrueLineStrategy; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -class LinesTest { - - public static Lines lines() { - LineStrategy lineStrategy = new TrueLineStrategy(); - Lines lines = new Lines(); - lines.generateLine(4, 3, lineStrategy); - return lines; - } - - @Test - void 세명의참가자만큼_네줄로_사다리_생성() { - LineStrategy lineStrategy = new TrueLineStrategy(); - Lines lines = new Lines(); - lines.generateLine(3, 4, lineStrategy); - - assertThat(lines.size()).isEqualTo(4); - for (int i = 0; i < lines.size(); i++) { - assertThatLineSize(lines, i); - assertThatLinePatternIsCorrect(lines, i); - } - } - - private static void assertThatLinePatternIsCorrect(Lines lines, int linesIdx) { - for (int j = 0; j < lines.getLineSize(linesIdx); j++) { - assertThat(lines.getPoint(linesIdx, j)).isEqualTo(j % 2 == 0); - } - } - - private static void assertThatLineSize(Lines lines, int linesIdx) { - assertThat(lines.getLineSize(linesIdx)).isEqualTo(2); - } -} diff --git a/src/test/java/nextstep/ladder/domain/NextStepLinesTest.java b/src/test/java/nextstep/ladder/domain/NextStepLinesTest.java new file mode 100644 index 0000000000..c7b46849ad --- /dev/null +++ b/src/test/java/nextstep/ladder/domain/NextStepLinesTest.java @@ -0,0 +1,40 @@ +package nextstep.ladder.domain; + +import nextstep.ladder.strategy.LineStrategy; +import nextstep.ladder.strategy.TrueLineStrategy; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class NextStepLinesTest { + + public static NextStepLines lines() { + LineStrategy lineStrategy = new TrueLineStrategy(); + NextStepLines nextStepLines = new NextStepLines(); + nextStepLines.generateLine(4, 3, lineStrategy); + return nextStepLines; + } + + @Test + void 세명의참가자만큼_네줄로_사다리_생성() { + LineStrategy lineStrategy = new TrueLineStrategy(); + NextStepLines nextStepLines = new NextStepLines(); + nextStepLines.generateLine(3, 4, lineStrategy); + + assertThat(nextStepLines.size()).isEqualTo(4); + for (int i = 0; i < nextStepLines.size(); i++) { + assertThatLineSize(nextStepLines, i); + assertThatLinePatternIsCorrect(nextStepLines, i); + } + } + + private static void assertThatLinePatternIsCorrect(NextStepLines nextStepLines, int linesIdx) { + for (int j = 0; j < nextStepLines.getLineSize(linesIdx); j++) { + assertThat(nextStepLines.getPoint(linesIdx, j)).isEqualTo(j % 2 == 0); + } + } + + private static void assertThatLineSize(NextStepLines nextStepLines, int linesIdx) { + assertThat(nextStepLines.getLineSize(linesIdx)).isEqualTo(2); + } +}