diff --git a/src/main/java/nextstep/ladder/LadderMain.java b/src/main/java/nextstep/ladder/LadderMain.java index a7db0011b0..6a66b877bc 100644 --- a/src/main/java/nextstep/ladder/LadderMain.java +++ b/src/main/java/nextstep/ladder/LadderMain.java @@ -13,15 +13,14 @@ public class LadderMain { public static void main(String[] args){ InputView inputView = new InputView(); OutputView outputView = new OutputView(); + final Names names = new Names(inputView.inputNames()); final LadderResult ladderResult = new LadderResult(inputView.inputResult()); - final Height height = new Height(inputView.inputLadderHeight()); - final Ladder ladder = new Ladder(names.getNumberOfNames(), height); + final Ladder ladder = new Ladder(names.getNumberOfNames(), inputView.inputLadderHeight()); outputView.printLadder(names, ladder); final LadderResultDeterminer determiner = new LadderResultDeterminer(ladderResult, names); - final LadderResultManager ladderResultManager = new LadderResultManager(names); - ladderResultManager.update(ladder, determiner); + final LadderResultManager ladderResultManager = new LadderResultManager(names, ladder, determiner); outputView.printResult(inputView.inputName(), ladderResultManager); } } diff --git a/src/main/java/nextstep/ladder/domain/Ladder.java b/src/main/java/nextstep/ladder/domain/Ladder.java index e161519214..698e9a5c65 100644 --- a/src/main/java/nextstep/ladder/domain/Ladder.java +++ b/src/main/java/nextstep/ladder/domain/Ladder.java @@ -7,9 +7,9 @@ public class Ladder { private final List lines; - public Ladder(final int numberOfPeople, final Height height) { + public Ladder(final int numberOfPeople, final int height) { lines = Stream.generate(() -> new Line(new RandomGenerator(), numberOfPeople)) - .limit(height.getValue()) + .limit(height) .collect(Collectors.toList()); } public List getLines() { diff --git a/src/main/java/nextstep/ladder/domain/LadderResultManager.java b/src/main/java/nextstep/ladder/domain/LadderResultManager.java index c203587b5e..e45afb1184 100644 --- a/src/main/java/nextstep/ladder/domain/LadderResultManager.java +++ b/src/main/java/nextstep/ladder/domain/LadderResultManager.java @@ -7,8 +7,9 @@ public class LadderResultManager { private final Map results; - public LadderResultManager(final Names names) { + public LadderResultManager(final Names names, final Ladder ladder, final LadderResultDeterminer determiner) { this.results = names.toResult(); + update(ladder, determiner); } public void update(final Ladder ladder, final LadderResultDeterminer determiner) { diff --git a/src/main/java/nextstep/ladder/domain/Line.java b/src/main/java/nextstep/ladder/domain/Line.java index 2095b7350a..de2826ae42 100644 --- a/src/main/java/nextstep/ladder/domain/Line.java +++ b/src/main/java/nextstep/ladder/domain/Line.java @@ -58,12 +58,16 @@ public List getBridges() { } private Bridge getBridgeAtIndex(final int index) { - if (index >= 0 && index < bridges.size()) { + if (isIndexWithinBounds(index)) { return bridges.get(index); } return Bridge.NON_BRIDGE; } + private boolean isIndexWithinBounds(int index) { + return index >= 0 && index < bridges.size(); + } + @Override public boolean equals(Object o) { if (this == o) diff --git a/src/test/java/nextstep/ladder/LadderTest.java b/src/test/java/nextstep/ladder/LadderTest.java index 507a8c6cd3..c1bd8ff84e 100644 --- a/src/test/java/nextstep/ladder/LadderTest.java +++ b/src/test/java/nextstep/ladder/LadderTest.java @@ -12,8 +12,7 @@ public class LadderTest { @Test @DisplayName("입력된 height 만큼 사다리 길이를 만든다") void 사다리를_생성한다() { - final Height height = new Height(5); - final Ladder ladder = new Ladder(4, height); + final Ladder ladder = new Ladder(4, 5); assertThat(ladder.getLines()).hasSize(5); } @@ -21,7 +20,7 @@ public class LadderTest { @Test @DisplayName("height가 음수일 경우 사다리 생성 실패") void 사다리를_생성을_실패한다() { - assertThatThrownBy(() -> new Ladder(4, new Height(-1))) + assertThatThrownBy(() -> new Ladder(4, -1)) .isInstanceOf(IllegalArgumentException.class); }