From 42a58991d18eb9e1d5c87c979baf3fab8ad60b7b Mon Sep 17 00:00:00 2001 From: jeounpar Date: Tue, 28 Nov 2023 22:23:53 +0900 Subject: [PATCH 1/6] =?UTF-8?q?refactor=20:=20step1=20=EB=A6=AC=EB=B7=B0?= =?UTF-8?q?=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/fp/Conditional.java | 7 +++++++ src/main/java/nextstep/fp/Lambda.java | 17 ++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 src/main/java/nextstep/fp/Conditional.java diff --git a/src/main/java/nextstep/fp/Conditional.java b/src/main/java/nextstep/fp/Conditional.java new file mode 100644 index 0000000000..d9238106e2 --- /dev/null +++ b/src/main/java/nextstep/fp/Conditional.java @@ -0,0 +1,7 @@ +package nextstep.fp; + +@FunctionalInterface +public interface Conditional { + + boolean test(Integer number); +} diff --git a/src/main/java/nextstep/fp/Lambda.java b/src/main/java/nextstep/fp/Lambda.java index 2036517d45..6b7b833378 100644 --- a/src/main/java/nextstep/fp/Lambda.java +++ b/src/main/java/nextstep/fp/Lambda.java @@ -28,22 +28,21 @@ public void run() { } public static int sumAll(List numbers) { - return numbers.stream() - .mapToInt(e -> e) - .sum(); + return sum(numbers, (number) -> true); } public static int sumAllEven(List numbers) { - return numbers.stream() - .filter(e -> e % 2 == 0) - .mapToInt(e -> e) - .sum(); + return sum(numbers, (number) -> number % 2 == 0); } public static int sumAllOverThree(List numbers) { + return sum(numbers, (number) -> number > 3); + } + + private static int sum(List numbers, Conditional conditional) { return numbers.stream() - .filter(e -> e > 3) - .mapToInt(e -> e) + .filter(conditional::test) + .mapToInt(number -> number) .sum(); } } From 2942a15407b9386e9b329551c9d479077302b108 Mon Sep 17 00:00:00 2001 From: jeounpar Date: Tue, 28 Nov 2023 22:59:43 +0900 Subject: [PATCH 2/6] =?UTF-8?q?feat=20:=20model,=20view,=20controller=20?= =?UTF-8?q?=EC=B4=88=EA=B8=B0=20=EC=BD=94=EB=93=9C=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 17 +++++++++++++++-- .../ladder/controller/MainController.java | 14 ++++++++++++++ src/main/java/nextstep/ladder/model/Line.java | 13 +++++++++++++ .../java/nextstep/ladder/view/InputView.java | 19 +++++++++++++++++++ .../java/nextstep/ladder/view/OutputView.java | 5 +++++ 5 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/main/java/nextstep/ladder/controller/MainController.java create mode 100644 src/main/java/nextstep/ladder/model/Line.java create mode 100644 src/main/java/nextstep/ladder/view/InputView.java create mode 100644 src/main/java/nextstep/ladder/view/OutputView.java diff --git a/README.md b/README.md index 70e61dd573..db94b7d384 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# STEP-01 +## STEP-01 * [X] 람다 실습 1 - 익명 클래스를 람다로 전환 * [X] 람다 실습 2 - 람다를 활용해 중복 제거 @@ -6,4 +6,17 @@ * [X] map, reduce, filter 실습 2 - printLongestWordTop100() * [X] 요구사항 1 - Optional을 활용해 조건에 따른 반환 * [X] 요구사항 2 - Optional에서 값을 반환 -* [X] 요구사항 3 - Optional에서 exception 처리 \ No newline at end of file +* [X] 요구사항 3 - Optional에서 exception 처리 + +## STEP-02 + +* [ ] 사용자 입력 + * [ ] 사람의 이름은 최대 5글자이다 + * 쉼표(,) 를 기준으로 이름을 구분한다 + + +* [ ] 출력 + * [ ] 5자 기준으로 사용자 이름을 출력한다 + +* [ ] 기능 + * [ ] 사다리 생성 \ No newline at end of file diff --git a/src/main/java/nextstep/ladder/controller/MainController.java b/src/main/java/nextstep/ladder/controller/MainController.java new file mode 100644 index 0000000000..141cd87b3e --- /dev/null +++ b/src/main/java/nextstep/ladder/controller/MainController.java @@ -0,0 +1,14 @@ +package nextstep.ladder.controller; + +import nextstep.ladder.view.InputView; + +public class MainController { + + public static void main(String[] args) { + InputView inputView = new InputView(); + + String names = inputView.inputUserNames(); + + } + +} diff --git a/src/main/java/nextstep/ladder/model/Line.java b/src/main/java/nextstep/ladder/model/Line.java new file mode 100644 index 0000000000..796291bb2c --- /dev/null +++ b/src/main/java/nextstep/ladder/model/Line.java @@ -0,0 +1,13 @@ +package nextstep.ladder.model; + +import java.util.ArrayList; +import java.util.List; + +public class Line { + + private final List points; + + public Line(int countOfPerson) { + this.points = new ArrayList<>(); + } +} diff --git a/src/main/java/nextstep/ladder/view/InputView.java b/src/main/java/nextstep/ladder/view/InputView.java new file mode 100644 index 0000000000..d42a4fac19 --- /dev/null +++ b/src/main/java/nextstep/ladder/view/InputView.java @@ -0,0 +1,19 @@ +package nextstep.ladder.view; + +import java.util.Scanner; + +public class InputView { + + private static final Scanner SCANNER = new Scanner(System.in); + + public String inputUserNames() { + System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); + return SCANNER.nextLine(); + } + + public String inputLadderMaxHeight() { + System.out.println("\n최대 사다리 높이는 몇 개인가요?"); + return SCANNER.nextLine(); + } + +} diff --git a/src/main/java/nextstep/ladder/view/OutputView.java b/src/main/java/nextstep/ladder/view/OutputView.java new file mode 100644 index 0000000000..5253cb58da --- /dev/null +++ b/src/main/java/nextstep/ladder/view/OutputView.java @@ -0,0 +1,5 @@ +package nextstep.ladder.view; + +public class OutputView { + +} From 05cd1cabd288f3791704f55461644a7e64fe9c80 Mon Sep 17 00:00:00 2001 From: jeounpar Date: Wed, 29 Nov 2023 12:19:31 +0900 Subject: [PATCH 3/6] =?UTF-8?q?feat=20:=20=EC=82=AC=EB=8B=A4=EB=A6=AC=20?= =?UTF-8?q?=ED=83=80=EA=B8=B0=EA=B0=80=20=EC=A0=95=EC=83=81=EC=A0=81?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=8F=99=EC=9E=91=ED=95=98=EA=B8=B0=20?= =?UTF-8?q?=EC=9C=84=ED=95=B4=20=EB=9D=BC=EC=9D=B8=EC=9D=B4=20=EA=B2=B9?= =?UTF-8?q?=EC=B9=98=EC=A7=80=20=EC=95=8A=EB=8F=84=EB=A1=9D=20=ED=95=9C?= =?UTF-8?q?=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 32 +++++++++++-------- src/main/java/nextstep/ladder/model/Line.java | 31 +++++++++++++++++- .../ladder/model/RandomTrueOrFalse.java | 11 +++++++ .../java/nextstep/ladder/view/OutputView.java | 3 ++ .../java/nextstep/ladder/model/LineTest.java | 28 ++++++++++++++++ 5 files changed, 90 insertions(+), 15 deletions(-) create mode 100644 src/main/java/nextstep/ladder/model/RandomTrueOrFalse.java create mode 100644 src/test/java/nextstep/ladder/model/LineTest.java diff --git a/README.md b/README.md index db94b7d384..e46b8121c2 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,20 @@ +## STEP-02 + +### 입력 + +* [ ] 사람의 이름은 최대 5글자이다 +* [ ] 쉼표(,) 를 기준으로 이름을 구분한다 + +### 출력 + +* [ ] 5자 기준으로 사용자 이름을 출력한다 +* [ ] 사다리를 출력 한다 + +### 기능 + +* [X] 사다리 타기가 정상적으로 동작하기 위해 라인이 겹치지 않도록 한다 +* [ ] 높이에 맞는 사다리 생성한다 + ## STEP-01 * [X] 람다 실습 1 - 익명 클래스를 람다로 전환 @@ -6,17 +23,4 @@ * [X] map, reduce, filter 실습 2 - printLongestWordTop100() * [X] 요구사항 1 - Optional을 활용해 조건에 따른 반환 * [X] 요구사항 2 - Optional에서 값을 반환 -* [X] 요구사항 3 - Optional에서 exception 처리 - -## STEP-02 - -* [ ] 사용자 입력 - * [ ] 사람의 이름은 최대 5글자이다 - * 쉼표(,) 를 기준으로 이름을 구분한다 - - -* [ ] 출력 - * [ ] 5자 기준으로 사용자 이름을 출력한다 - -* [ ] 기능 - * [ ] 사다리 생성 \ No newline at end of file +* [X] 요구사항 3 - Optional에서 exception 처리 \ No newline at end of file diff --git a/src/main/java/nextstep/ladder/model/Line.java b/src/main/java/nextstep/ladder/model/Line.java index 796291bb2c..791f3dd609 100644 --- a/src/main/java/nextstep/ladder/model/Line.java +++ b/src/main/java/nextstep/ladder/model/Line.java @@ -2,12 +2,41 @@ import java.util.ArrayList; import java.util.List; +import java.util.Random; +import java.util.stream.IntStream; public class Line { private final List points; - public Line(int countOfPerson) { + public Line(int countOfPerson, Random random) { this.points = new ArrayList<>(); + this.points.add(random.nextBoolean()); + IntStream.range(0, countOfPerson - 2) + .forEach(index -> initPoint(index, random.nextBoolean())); + validateLine(countOfPerson); + } + + private void validateLine(int countOfPerson) { + IntStream.range(0, countOfPerson - 2) + .filter(index -> points.get(index) && points.get(index + 1)) + .forEach(point -> { + throw new IllegalArgumentException("사다리 라인이 겹칩니다"); + }); + } + + private void initPoint(int index, boolean nextBoolean) { + if (this.points.get(index) && nextBoolean || this.points.get(index) && !nextBoolean + || !this.points.get(index) && !nextBoolean) { + this.points.add(false); + return; + } + if (!this.points.get(index) && nextBoolean) { + this.points.add(true); + } + } + + public List getPoints() { + return this.points; } } diff --git a/src/main/java/nextstep/ladder/model/RandomTrueOrFalse.java b/src/main/java/nextstep/ladder/model/RandomTrueOrFalse.java new file mode 100644 index 0000000000..5f4668e3a4 --- /dev/null +++ b/src/main/java/nextstep/ladder/model/RandomTrueOrFalse.java @@ -0,0 +1,11 @@ +package nextstep.ladder.model; + +import java.util.Random; + +public class RandomTrueOrFalse extends Random { + + @Override + public boolean nextBoolean() { + return super.nextBoolean(); + } +} diff --git a/src/main/java/nextstep/ladder/view/OutputView.java b/src/main/java/nextstep/ladder/view/OutputView.java index 5253cb58da..73b4a642d6 100644 --- a/src/main/java/nextstep/ladder/view/OutputView.java +++ b/src/main/java/nextstep/ladder/view/OutputView.java @@ -2,4 +2,7 @@ public class OutputView { + public OutputView() { + System.out.println("실행결과\n"); + } } diff --git a/src/test/java/nextstep/ladder/model/LineTest.java b/src/test/java/nextstep/ladder/model/LineTest.java new file mode 100644 index 0000000000..194de052c2 --- /dev/null +++ b/src/test/java/nextstep/ladder/model/LineTest.java @@ -0,0 +1,28 @@ +package nextstep.ladder.model; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Random; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class LineTest { + + static class AlwaysReturnTrue extends Random { + + @Override + public boolean nextBoolean() { + return true; + } + } + + @Test + @DisplayName("사다리의 라인 하나를 생성한다") + void line() { + int countOfPerson = 5; + Line line = new Line(countOfPerson, new AlwaysReturnTrue()); + + assertThat(line.getPoints()).hasSize(countOfPerson - 1) + .containsExactly(true, false, true, false); + } +} \ No newline at end of file From a613e8384c67b7a29d9035926ed82b2402523fca Mon Sep 17 00:00:00 2001 From: jeounpar Date: Wed, 29 Nov 2023 13:37:32 +0900 Subject: [PATCH 4/6] =?UTF-8?q?feat=20:=20=EC=82=AC=EB=8B=A4=EB=A6=AC?= =?UTF-8?q?=EB=A5=BC=20=EC=B6=9C=EB=A0=A5=20=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../java/nextstep/ladder/model/Ladder.java | 22 +++++++++++++++++ src/main/java/nextstep/ladder/model/Line.java | 5 ++++ .../java/nextstep/ladder/view/OutputView.java | 22 +++++++++++++++++ .../nextstep/ladder/model/LadderTest.java | 24 +++++++++++++++++++ .../nextstep/ladder/view/OutputViewTest.java | 17 +++++++++++++ 6 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/main/java/nextstep/ladder/model/Ladder.java create mode 100644 src/test/java/nextstep/ladder/model/LadderTest.java create mode 100644 src/test/java/nextstep/ladder/view/OutputViewTest.java diff --git a/README.md b/README.md index e46b8121c2..d8c66acc4c 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ ### 출력 * [ ] 5자 기준으로 사용자 이름을 출력한다 -* [ ] 사다리를 출력 한다 +* [X] 사다리를 출력 한다 ### 기능 diff --git a/src/main/java/nextstep/ladder/model/Ladder.java b/src/main/java/nextstep/ladder/model/Ladder.java new file mode 100644 index 0000000000..4e196d584d --- /dev/null +++ b/src/main/java/nextstep/ladder/model/Ladder.java @@ -0,0 +1,22 @@ +package nextstep.ladder.model; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.stream.IntStream; + +public class Ladder { + + private final List lines; + + public Ladder(int ladderHeight, int countOfPerson, Random random) { + this.lines = new ArrayList<>(); + IntStream.range(0, ladderHeight) + .mapToObj(line -> new Line(countOfPerson, random)) + .forEach(this.lines::add); + } + + public List getLines() { + return lines; + } +} diff --git a/src/main/java/nextstep/ladder/model/Line.java b/src/main/java/nextstep/ladder/model/Line.java index 791f3dd609..fe3c8da20e 100644 --- a/src/main/java/nextstep/ladder/model/Line.java +++ b/src/main/java/nextstep/ladder/model/Line.java @@ -39,4 +39,9 @@ private void initPoint(int index, boolean nextBoolean) { public List getPoints() { return this.points; } + + @Override + public String toString() { + return this.points.toString(); + } } diff --git a/src/main/java/nextstep/ladder/view/OutputView.java b/src/main/java/nextstep/ladder/view/OutputView.java index 73b4a642d6..3feab352ad 100644 --- a/src/main/java/nextstep/ladder/view/OutputView.java +++ b/src/main/java/nextstep/ladder/view/OutputView.java @@ -1,8 +1,30 @@ package nextstep.ladder.view; +import java.util.stream.Collectors; +import nextstep.ladder.model.Ladder; + public class OutputView { + private static final String FOUR_SPACE = " "; + private static final String LADDER = "|"; + private static final String LINE = "----"; + public OutputView() { System.out.println("실행결과\n"); } + + public void printLadder(Ladder ladder) { + StringBuilder stringBuilder = new StringBuilder(); + ladder.getLines().forEach(line -> { + stringBuilder.append(FOUR_SPACE); + stringBuilder.append(line.getPoints() + .stream() + .map(point -> point ? LADDER + LINE : LADDER + FOUR_SPACE) + .collect(Collectors.joining())); + stringBuilder.append(LADDER) + .append("\n"); + }); + System.out.println(stringBuilder); + } } + diff --git a/src/test/java/nextstep/ladder/model/LadderTest.java b/src/test/java/nextstep/ladder/model/LadderTest.java new file mode 100644 index 0000000000..5d0fa8bbf6 --- /dev/null +++ b/src/test/java/nextstep/ladder/model/LadderTest.java @@ -0,0 +1,24 @@ +package nextstep.ladder.model; + +import java.util.Random; +import org.junit.jupiter.api.Test; + +class LadderTest { + + static class AlwaysReturnTrue extends Random { + + @Override + public boolean nextBoolean() { + return true; + } + } + + @Test + void ladder() { + Ladder ladder = new Ladder(5, 4, new AlwaysReturnTrue()); + + ladder.getLines() + .forEach(System.out::println); + } + +} \ No newline at end of file diff --git a/src/test/java/nextstep/ladder/view/OutputViewTest.java b/src/test/java/nextstep/ladder/view/OutputViewTest.java new file mode 100644 index 0000000000..6a79526b7d --- /dev/null +++ b/src/test/java/nextstep/ladder/view/OutputViewTest.java @@ -0,0 +1,17 @@ +package nextstep.ladder.view; + +import nextstep.ladder.model.Ladder; +import nextstep.ladder.model.RandomTrueOrFalse; +import org.junit.jupiter.api.Test; + +class OutputViewTest { + + @Test + void print_ladder() { + Ladder ladder = new Ladder(10, 5, new RandomTrueOrFalse()); + OutputView outputView = new OutputView(); + outputView.printLadder(ladder); + + } + +} \ No newline at end of file From 0e440fd4a72fb8f21b6044452fde0c2f129b5221 Mon Sep 17 00:00:00 2001 From: jeounpar Date: Wed, 29 Nov 2023 14:58:23 +0900 Subject: [PATCH 5/6] =?UTF-8?q?feat=20:=20LadderHeight,=20PersonCount=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ladder/controller/MainController.java | 21 +++++++++++++++- .../java/nextstep/ladder/model/Ladder.java | 6 ++--- .../nextstep/ladder/model/LadderHeight.java | 21 ++++++++++++++++ .../nextstep/ladder/model/LadderService.java | 15 ++++++++++++ src/main/java/nextstep/ladder/model/Name.java | 17 +++++++++++++ .../nextstep/ladder/model/PersonCount.java | 21 ++++++++++++++++ .../java/nextstep/ladder/view/InputView.java | 2 +- .../ladder/model/LadderHeightTest.java | 24 +++++++++++++++++++ .../nextstep/ladder/model/LadderTest.java | 5 +--- .../java/nextstep/ladder/model/NameTest.java | 24 +++++++++++++++++++ .../nextstep/ladder/view/OutputViewTest.java | 5 +++- 11 files changed, 151 insertions(+), 10 deletions(-) create mode 100644 src/main/java/nextstep/ladder/model/LadderHeight.java create mode 100644 src/main/java/nextstep/ladder/model/LadderService.java create mode 100644 src/main/java/nextstep/ladder/model/Name.java create mode 100644 src/main/java/nextstep/ladder/model/PersonCount.java create mode 100644 src/test/java/nextstep/ladder/model/LadderHeightTest.java create mode 100644 src/test/java/nextstep/ladder/model/NameTest.java diff --git a/src/main/java/nextstep/ladder/controller/MainController.java b/src/main/java/nextstep/ladder/controller/MainController.java index 141cd87b3e..0f74283a2f 100644 --- a/src/main/java/nextstep/ladder/controller/MainController.java +++ b/src/main/java/nextstep/ladder/controller/MainController.java @@ -1,5 +1,13 @@ package nextstep.ladder.controller; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import nextstep.ladder.model.Ladder; +import nextstep.ladder.model.LadderHeight; +import nextstep.ladder.model.Name; +import nextstep.ladder.model.PersonCount; +import nextstep.ladder.model.RandomTrueOrFalse; import nextstep.ladder.view.InputView; public class MainController { @@ -7,7 +15,18 @@ public class MainController { public static void main(String[] args) { InputView inputView = new InputView(); - String names = inputView.inputUserNames(); + String namesText = inputView.inputUserNames(); + List names = Arrays.stream(namesText.split(",")) + .map(Name::new) + .collect(Collectors.toList()); + + PersonCount countOfPerson = new PersonCount(names.size()); + + String heightOfLadderText = inputView.inputHeightOfLadder(); + LadderHeight heightOfLadder = new LadderHeight(Integer.parseInt(heightOfLadderText)); + + Ladder ladder = new Ladder(heightOfLadder, countOfPerson, new RandomTrueOrFalse()); + } diff --git a/src/main/java/nextstep/ladder/model/Ladder.java b/src/main/java/nextstep/ladder/model/Ladder.java index 4e196d584d..4062ee514e 100644 --- a/src/main/java/nextstep/ladder/model/Ladder.java +++ b/src/main/java/nextstep/ladder/model/Ladder.java @@ -9,10 +9,10 @@ public class Ladder { private final List lines; - public Ladder(int ladderHeight, int countOfPerson, Random random) { + public Ladder(LadderHeight heightOfLadder, PersonCount countOfPerson, Random random) { this.lines = new ArrayList<>(); - IntStream.range(0, ladderHeight) - .mapToObj(line -> new Line(countOfPerson, random)) + IntStream.range(0, heightOfLadder.getHeightOfLadder()) + .mapToObj(line -> new Line(countOfPerson.getCountOfPerson(), random)) .forEach(this.lines::add); } diff --git a/src/main/java/nextstep/ladder/model/LadderHeight.java b/src/main/java/nextstep/ladder/model/LadderHeight.java new file mode 100644 index 0000000000..53c6364ecd --- /dev/null +++ b/src/main/java/nextstep/ladder/model/LadderHeight.java @@ -0,0 +1,21 @@ +package nextstep.ladder.model; + +public class LadderHeight { + + private final Integer heightOfLadder; + + public LadderHeight(Integer heightOfLadder) { + validateHeight(heightOfLadder); + this.heightOfLadder = heightOfLadder; + } + + private void validateHeight(Integer heightOfLadder) { + if (heightOfLadder <= 0) { + throw new IllegalArgumentException("높이는 1 이상 이어야 합니다."); + } + } + + public Integer getHeightOfLadder() { + return heightOfLadder; + } +} diff --git a/src/main/java/nextstep/ladder/model/LadderService.java b/src/main/java/nextstep/ladder/model/LadderService.java new file mode 100644 index 0000000000..58fa3511f2 --- /dev/null +++ b/src/main/java/nextstep/ladder/model/LadderService.java @@ -0,0 +1,15 @@ +package nextstep.ladder.model; + +import java.util.List; +import java.util.Random; + +public class LadderService { + + private final List names; + private final Ladder ladder; + + public LadderService(LadderHeight heightOfLadder, PersonCount countOfPerson, List names, Random random) { + ladder = new Ladder(heightOfLadder, countOfPerson, random); + this.names = names; + } +} diff --git a/src/main/java/nextstep/ladder/model/Name.java b/src/main/java/nextstep/ladder/model/Name.java new file mode 100644 index 0000000000..9ca8972fb5 --- /dev/null +++ b/src/main/java/nextstep/ladder/model/Name.java @@ -0,0 +1,17 @@ +package nextstep.ladder.model; + +public class Name { + + private final String name; + + public Name(String name) { + validateNameLength(name); + this.name = name; + } + + private void validateNameLength(String name) { + if (name.length() < 1 || name.length() > 5) { + throw new IllegalArgumentException("이름의 길이는 1자 ~ 5자 입니다"); + } + } +} diff --git a/src/main/java/nextstep/ladder/model/PersonCount.java b/src/main/java/nextstep/ladder/model/PersonCount.java new file mode 100644 index 0000000000..4f966cb7ee --- /dev/null +++ b/src/main/java/nextstep/ladder/model/PersonCount.java @@ -0,0 +1,21 @@ +package nextstep.ladder.model; + +public class PersonCount { + + private final Integer countOfPerson; + + public PersonCount(Integer countOfPerson) { + validateCountOfPerson(countOfPerson); + this.countOfPerson = countOfPerson; + } + + private void validateCountOfPerson(Integer countOfPerson) { + if (countOfPerson < 0) { + throw new IllegalArgumentException("사람의 수는 1 이상 이어야 합니다."); + } + } + + public Integer getCountOfPerson() { + return countOfPerson; + } +} diff --git a/src/main/java/nextstep/ladder/view/InputView.java b/src/main/java/nextstep/ladder/view/InputView.java index d42a4fac19..3c045119d3 100644 --- a/src/main/java/nextstep/ladder/view/InputView.java +++ b/src/main/java/nextstep/ladder/view/InputView.java @@ -11,7 +11,7 @@ public String inputUserNames() { return SCANNER.nextLine(); } - public String inputLadderMaxHeight() { + public String inputHeightOfLadder() { System.out.println("\n최대 사다리 높이는 몇 개인가요?"); return SCANNER.nextLine(); } diff --git a/src/test/java/nextstep/ladder/model/LadderHeightTest.java b/src/test/java/nextstep/ladder/model/LadderHeightTest.java new file mode 100644 index 0000000000..91eba189b3 --- /dev/null +++ b/src/test/java/nextstep/ladder/model/LadderHeightTest.java @@ -0,0 +1,24 @@ +package nextstep.ladder.model; + +import static org.junit.jupiter.api.Assertions.*; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class LadderHeightTest { + + @Test + @DisplayName("사다리의 높의는 1 이상이다") + void ladder_height() { + Assertions.assertThatThrownBy(() -> { + new LadderHeight(0); + }).isInstanceOf(IllegalArgumentException.class) + .hasMessage("높이는 1 이상 이어야 합니다."); + + Assertions.assertThatThrownBy(() -> { + new LadderHeight(-1); + }).isInstanceOf(IllegalArgumentException.class) + .hasMessage("높이는 1 이상 이어야 합니다."); + } +} \ No newline at end of file diff --git a/src/test/java/nextstep/ladder/model/LadderTest.java b/src/test/java/nextstep/ladder/model/LadderTest.java index 5d0fa8bbf6..ffc3b0cd69 100644 --- a/src/test/java/nextstep/ladder/model/LadderTest.java +++ b/src/test/java/nextstep/ladder/model/LadderTest.java @@ -15,10 +15,7 @@ public boolean nextBoolean() { @Test void ladder() { - Ladder ladder = new Ladder(5, 4, new AlwaysReturnTrue()); - - ladder.getLines() - .forEach(System.out::println); + Ladder ladder = new Ladder(new LadderHeight(5), new PersonCount(4), new AlwaysReturnTrue()); } } \ No newline at end of file diff --git a/src/test/java/nextstep/ladder/model/NameTest.java b/src/test/java/nextstep/ladder/model/NameTest.java new file mode 100644 index 0000000000..a1018732f2 --- /dev/null +++ b/src/test/java/nextstep/ladder/model/NameTest.java @@ -0,0 +1,24 @@ +package nextstep.ladder.model; + +import static org.junit.jupiter.api.Assertions.*; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class NameTest { + + @Test + @DisplayName("이름의 길이는 1자 ~ 5자 이다") + void name_length() { + Assertions.assertThatThrownBy(() -> { + new Name("123456"); + }).isInstanceOf(IllegalArgumentException.class) + .hasMessage("이름의 길이는 1자 ~ 5자 입니다"); + + Assertions.assertThatThrownBy(() -> { + new Name(""); + }).isInstanceOf(IllegalArgumentException.class) + .hasMessage("이름의 길이는 1자 ~ 5자 입니다"); + } +} \ No newline at end of file diff --git a/src/test/java/nextstep/ladder/view/OutputViewTest.java b/src/test/java/nextstep/ladder/view/OutputViewTest.java index 6a79526b7d..ba16737ad8 100644 --- a/src/test/java/nextstep/ladder/view/OutputViewTest.java +++ b/src/test/java/nextstep/ladder/view/OutputViewTest.java @@ -1,6 +1,8 @@ package nextstep.ladder.view; import nextstep.ladder.model.Ladder; +import nextstep.ladder.model.LadderHeight; +import nextstep.ladder.model.PersonCount; import nextstep.ladder.model.RandomTrueOrFalse; import org.junit.jupiter.api.Test; @@ -8,7 +10,8 @@ class OutputViewTest { @Test void print_ladder() { - Ladder ladder = new Ladder(10, 5, new RandomTrueOrFalse()); + Ladder ladder = new Ladder(new LadderHeight(10), new PersonCount(5), + new RandomTrueOrFalse()); OutputView outputView = new OutputView(); outputView.printLadder(ladder); From 684953b3901b92cc7f2b4de5c37017cf434ffe0e Mon Sep 17 00:00:00 2001 From: jeounpar Date: Wed, 29 Nov 2023 15:15:25 +0900 Subject: [PATCH 6/6] =?UTF-8?q?feat=20:=20TODO=20=EB=AA=A8=EB=93=A0=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 +++---- .../ladder/controller/MainController.java | 4 +++- .../nextstep/ladder/model/LadderHeight.java | 4 +++- src/main/java/nextstep/ladder/model/Name.java | 15 +++++++++++- .../nextstep/ladder/model/PersonCount.java | 3 ++- .../java/nextstep/ladder/view/OutputView.java | 18 +++++++++++++-- .../ladder/model/PersonCountTest.java | 23 +++++++++++++++++++ .../nextstep/ladder/view/OutputViewTest.java | 2 +- 8 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 src/test/java/nextstep/ladder/model/PersonCountTest.java diff --git a/README.md b/README.md index d8c66acc4c..87c7a36170 100644 --- a/README.md +++ b/README.md @@ -2,18 +2,18 @@ ### 입력 -* [ ] 사람의 이름은 최대 5글자이다 -* [ ] 쉼표(,) 를 기준으로 이름을 구분한다 +* [X] 사람의 이름은 최대 5글자이다 +* [X] 쉼표(,) 를 기준으로 이름을 구분한다 ### 출력 -* [ ] 5자 기준으로 사용자 이름을 출력한다 +* [X] 5자 기준으로 사용자 이름을 출력한다 * [X] 사다리를 출력 한다 ### 기능 * [X] 사다리 타기가 정상적으로 동작하기 위해 라인이 겹치지 않도록 한다 -* [ ] 높이에 맞는 사다리 생성한다 +* [X] 높이에 맞는 사다리 생성한다 ## STEP-01 diff --git a/src/main/java/nextstep/ladder/controller/MainController.java b/src/main/java/nextstep/ladder/controller/MainController.java index 0f74283a2f..4ed61d7805 100644 --- a/src/main/java/nextstep/ladder/controller/MainController.java +++ b/src/main/java/nextstep/ladder/controller/MainController.java @@ -9,6 +9,7 @@ import nextstep.ladder.model.PersonCount; import nextstep.ladder.model.RandomTrueOrFalse; import nextstep.ladder.view.InputView; +import nextstep.ladder.view.OutputView; public class MainController { @@ -27,7 +28,8 @@ public static void main(String[] args) { Ladder ladder = new Ladder(heightOfLadder, countOfPerson, new RandomTrueOrFalse()); - + OutputView outputView = new OutputView(); + outputView.print(ladder, names); } } diff --git a/src/main/java/nextstep/ladder/model/LadderHeight.java b/src/main/java/nextstep/ladder/model/LadderHeight.java index 53c6364ecd..5f0119314f 100644 --- a/src/main/java/nextstep/ladder/model/LadderHeight.java +++ b/src/main/java/nextstep/ladder/model/LadderHeight.java @@ -2,6 +2,8 @@ public class LadderHeight { + private static final int MIN_HEIGHT = 1; + private final Integer heightOfLadder; public LadderHeight(Integer heightOfLadder) { @@ -10,7 +12,7 @@ public LadderHeight(Integer heightOfLadder) { } private void validateHeight(Integer heightOfLadder) { - if (heightOfLadder <= 0) { + if (heightOfLadder < MIN_HEIGHT) { throw new IllegalArgumentException("높이는 1 이상 이어야 합니다."); } } diff --git a/src/main/java/nextstep/ladder/model/Name.java b/src/main/java/nextstep/ladder/model/Name.java index 9ca8972fb5..7d1792cfe6 100644 --- a/src/main/java/nextstep/ladder/model/Name.java +++ b/src/main/java/nextstep/ladder/model/Name.java @@ -1,7 +1,13 @@ package nextstep.ladder.model; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + public class Name { + private static final int MIN_LENGTH = 1; + private static final int MAX_LENGTH = 5; + private static final String SPACE = " "; private final String name; public Name(String name) { @@ -10,8 +16,15 @@ public Name(String name) { } private void validateNameLength(String name) { - if (name.length() < 1 || name.length() > 5) { + if (name.length() < MIN_LENGTH || name.length() > MAX_LENGTH) { throw new IllegalArgumentException("이름의 길이는 1자 ~ 5자 입니다"); } } + + @Override + public String toString() { + return IntStream.range(0, MAX_LENGTH - name.length()) + .mapToObj(i -> SPACE) + .collect(Collectors.joining("", name, "")); + } } diff --git a/src/main/java/nextstep/ladder/model/PersonCount.java b/src/main/java/nextstep/ladder/model/PersonCount.java index 4f966cb7ee..da5172d422 100644 --- a/src/main/java/nextstep/ladder/model/PersonCount.java +++ b/src/main/java/nextstep/ladder/model/PersonCount.java @@ -2,6 +2,7 @@ public class PersonCount { + private static final int MIN_COUNT = 1; private final Integer countOfPerson; public PersonCount(Integer countOfPerson) { @@ -10,7 +11,7 @@ public PersonCount(Integer countOfPerson) { } private void validateCountOfPerson(Integer countOfPerson) { - if (countOfPerson < 0) { + if (countOfPerson < MIN_COUNT) { throw new IllegalArgumentException("사람의 수는 1 이상 이어야 합니다."); } } diff --git a/src/main/java/nextstep/ladder/view/OutputView.java b/src/main/java/nextstep/ladder/view/OutputView.java index 3feab352ad..eb90ef186b 100644 --- a/src/main/java/nextstep/ladder/view/OutputView.java +++ b/src/main/java/nextstep/ladder/view/OutputView.java @@ -1,7 +1,9 @@ package nextstep.ladder.view; +import java.util.List; import java.util.stream.Collectors; import nextstep.ladder.model.Ladder; +import nextstep.ladder.model.Name; public class OutputView { @@ -10,10 +12,15 @@ public class OutputView { private static final String LINE = "----"; public OutputView() { - System.out.println("실행결과\n"); + System.out.println("\n실행결과\n"); } - public void printLadder(Ladder ladder) { + public void print(Ladder ladder, List names) { + printNames(names); + printLadder(ladder); + } + + private void printLadder(Ladder ladder) { StringBuilder stringBuilder = new StringBuilder(); ladder.getLines().forEach(line -> { stringBuilder.append(FOUR_SPACE); @@ -26,5 +33,12 @@ public void printLadder(Ladder ladder) { }); System.out.println(stringBuilder); } + + private void printNames(List names) { + names.stream() + .map(name -> name + " ") + .forEach(System.out::print); + System.out.println(); + } } diff --git a/src/test/java/nextstep/ladder/model/PersonCountTest.java b/src/test/java/nextstep/ladder/model/PersonCountTest.java new file mode 100644 index 0000000000..90b0022100 --- /dev/null +++ b/src/test/java/nextstep/ladder/model/PersonCountTest.java @@ -0,0 +1,23 @@ +package nextstep.ladder.model; + + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class PersonCountTest { + + @Test + @DisplayName("사람의 수는 1 이상 이어야 합니다.") + void person_count() { + Assertions.assertThatThrownBy(() -> { + new PersonCount(0); + }).isInstanceOf(IllegalArgumentException.class) + .hasMessage("사람의 수는 1 이상 이어야 합니다."); + + Assertions.assertThatThrownBy(() -> { + new PersonCount(-1); + }).isInstanceOf(IllegalArgumentException.class) + .hasMessage("사람의 수는 1 이상 이어야 합니다."); + } +} \ No newline at end of file diff --git a/src/test/java/nextstep/ladder/view/OutputViewTest.java b/src/test/java/nextstep/ladder/view/OutputViewTest.java index ba16737ad8..8abe9d0ad3 100644 --- a/src/test/java/nextstep/ladder/view/OutputViewTest.java +++ b/src/test/java/nextstep/ladder/view/OutputViewTest.java @@ -13,7 +13,7 @@ void print_ladder() { Ladder ladder = new Ladder(new LadderHeight(10), new PersonCount(5), new RandomTrueOrFalse()); OutputView outputView = new OutputView(); - outputView.printLadder(ladder); +// outputView.printLadder(ladder); }