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

Step4 #2293

Open
wants to merge 7 commits into
base: kkksusu161
Choose a base branch
from
12 changes: 12 additions & 0 deletions src/main/java/engine/LinesCreator.java
Original file line number Diff line number Diff line change
@@ -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<Line> getLines();
}
15 changes: 15 additions & 0 deletions src/main/java/factory/LinesFactoryBean.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
31 changes: 21 additions & 10 deletions src/main/java/nextstep/ladder/controller/LadderController.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}

}
10 changes: 6 additions & 4 deletions src/main/java/nextstep/ladder/domain/LadderExecutor.java
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -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());
}
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/nextstep/ladder/domain/LadderFactory.java

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/java/nextstep/ladder/domain/LadderResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public Map<Participant, String> getPlayerToReward() {
}

public String getReward(String name) {
if (name.equals("all")) {
return "";
}
return playerToReward.get(new Participant(name));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Line> lines;

public Lines() {
public NextStepLines() {
this(new ArrayList<Line>());
}

public Lines(ArrayList<Line> lines) {
public NextStepLines(ArrayList<Line> lines) {
this.lines = new ArrayList<>(lines);
}

Expand All @@ -27,10 +28,6 @@ private void generateLine(int participantCnt, LineStrategy lineStrategy) {
lines.add(line);
}

public int size() {
return lines.size();
}

public List<Line> getLines() {
return lines;
}
Expand All @@ -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();
}
}
11 changes: 6 additions & 5 deletions src/main/java/nextstep/ladder/view/OutputView.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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("|");
});
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/engine/LinesFactoryBeanTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
40 changes: 0 additions & 40 deletions src/test/java/nextstep/ladder/domain/LinesTest.java

This file was deleted.

40 changes: 40 additions & 0 deletions src/test/java/nextstep/ladder/domain/NextStepLinesTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}