From f033e733a26ad2776f6f13f43a105ea99912f7cd Mon Sep 17 00:00:00 2001 From: 0319easy <0319easy@gmail.com> Date: Thu, 19 May 2022 02:04:48 +0900 Subject: [PATCH 1/2] =?UTF-8?q?refactor=20User=20=EC=97=90=EC=84=9C=20map(?= =?UTF-8?q?)=20=EC=82=AC=EC=9A=A9=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/optional/User.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/nextstep/optional/User.java b/src/main/java/nextstep/optional/User.java index 51e9e4567b..93b2c9447c 100644 --- a/src/main/java/nextstep/optional/User.java +++ b/src/main/java/nextstep/optional/User.java @@ -36,9 +36,10 @@ public static boolean ageIsInRange1(User user) { public static boolean ageIsInRange2(User user) { return Optional.ofNullable(user) - .filter(o -> o.age != null) - .filter(o -> o.age >= 30) - .filter(o -> o.age <= 45) + .map(User::getAge) + .filter(o -> o != null) + .filter(o -> o >= 30) + .filter(o -> o <= 45) .isPresent(); } From aa9f7e3ee52721a312b3306a3251395890a520cd Mon Sep 17 00:00:00 2001 From: 0319easy <0319easy@gmail.com> Date: Wed, 1 Jun 2022 02:24:48 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat=20=EC=82=AC=EB=8B=A4=EB=A6=AC=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 +++++- src/main/java/ladder/LadderMain.java | 18 ++++++++++++ src/main/java/ladder/domain/Ladder.java | 18 ++++++++++++ src/main/java/ladder/domain/Line.java | 27 +++++++++++++++++ src/main/java/ladder/domain/Point.java | 27 +++++++++++++++++ src/main/java/ladder/ui/InputView.java | 11 +++++++ src/main/java/ladder/ui/LadderScanner.java | 17 +++++++++++ src/main/java/ladder/ui/ResultView.java | 34 ++++++++++++++++++++++ 8 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ladder/LadderMain.java create mode 100644 src/main/java/ladder/domain/Ladder.java create mode 100644 src/main/java/ladder/domain/Line.java create mode 100644 src/main/java/ladder/domain/Point.java create mode 100644 src/main/java/ladder/ui/InputView.java create mode 100644 src/main/java/ladder/ui/LadderScanner.java create mode 100644 src/main/java/ladder/ui/ResultView.java diff --git a/README.md b/README.md index b853592f59..32359ea73d 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,11 @@ * 모든 피드백을 완료하면 다음 단계를 도전하고 앞의 과정을 반복한다. ## 온라인 코드 리뷰 과정 -* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/nextstep-step/nextstep-docs/tree/master/codereview) \ No newline at end of file +* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/nextstep-step/nextstep-docs/tree/master/codereview) + +## step1 - 기능요구사항 +* 사다리 게임에 참여하는 사람에 이름을 최대5글자까지 부여할 수 있다. 사다리를 출력할 때 사람 이름도 같이 출력한다. +* 사람 이름은 쉼표(,)를 기준으로 구분한다. +* 사람 이름을 5자 기준으로 출력하기 때문에 사다리 폭도 넓어져야 한다. +* 사다리 타기가 정상적으로 동작하려면 라인이 겹치지 않도록 해야 한다. + * |-----|-----| 모양과 같이 가로 라인이 겹치는 경우 어느 방향으로 이동할지 결정할 수 없다. diff --git a/src/main/java/ladder/LadderMain.java b/src/main/java/ladder/LadderMain.java new file mode 100644 index 0000000000..7827c1c6d3 --- /dev/null +++ b/src/main/java/ladder/LadderMain.java @@ -0,0 +1,18 @@ +package ladder; + +import java.util.List; +import ladder.domain.Ladder; +import ladder.ui.LadderScanner; +import ladder.ui.ResultView; + +public class LadderMain { + public static void main(String[] args) { + String input = LadderScanner.insertParticipant(); + List participants = List.of(input.split(",")); + int height = LadderScanner.insertLadderHeight(); + + Ladder ladder = new Ladder(participants.size() - 1, height); + + ResultView.printResult(participants, ladder); + } +} diff --git a/src/main/java/ladder/domain/Ladder.java b/src/main/java/ladder/domain/Ladder.java new file mode 100644 index 0000000000..b8dff85f4d --- /dev/null +++ b/src/main/java/ladder/domain/Ladder.java @@ -0,0 +1,18 @@ +package ladder.domain; + +import java.util.ArrayList; +import java.util.List; + +public class Ladder { + private List lines = new ArrayList<>(); + + public Ladder(int width, int height) { + for (int i = 0; i < height; ++i) { + lines.add(Line.of(width)); + } + } + + public List getLines() { + return lines; + } +} diff --git a/src/main/java/ladder/domain/Line.java b/src/main/java/ladder/domain/Line.java new file mode 100644 index 0000000000..ae819ff450 --- /dev/null +++ b/src/main/java/ladder/domain/Line.java @@ -0,0 +1,27 @@ +package ladder.domain; + +import java.util.ArrayList; +import java.util.List; + +public class Line { + private List points; + + Line(List points) { + this.points = points; + } + + static Line of(int size) { + List points = new ArrayList<>(); + + points.add(new Point()); + for (int i = 1; i < size; i++) { + points.add(points.get(i - 1).nextPoint()); + } + + return new Line(points); + } + + public List getPoints() { + return points; + } +} diff --git a/src/main/java/ladder/domain/Point.java b/src/main/java/ladder/domain/Point.java new file mode 100644 index 0000000000..90faf27b02 --- /dev/null +++ b/src/main/java/ladder/domain/Point.java @@ -0,0 +1,27 @@ +package ladder.domain; + +import java.util.Random; + +public class Point { + private final boolean exist; + + Point() { + this(new Random().nextBoolean()); + } + + Point(boolean exist) { + this.exist = exist; + } + + public boolean isExist() { + return exist; + } + + Point nextPoint() { + if (isExist()) { + return new Point(false); + } + + return new Point(); + } +} diff --git a/src/main/java/ladder/ui/InputView.java b/src/main/java/ladder/ui/InputView.java new file mode 100644 index 0000000000..774a5f4462 --- /dev/null +++ b/src/main/java/ladder/ui/InputView.java @@ -0,0 +1,11 @@ +package ladder.ui; + +public class InputView { + public static void printInsertParticipantPhrase() { + System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); + } + + public static void printInsertLadderHeightPhrase() { + System.out.println("최대 사다리 높이는 몇 개인가요?"); + } +} diff --git a/src/main/java/ladder/ui/LadderScanner.java b/src/main/java/ladder/ui/LadderScanner.java new file mode 100644 index 0000000000..08a8687f83 --- /dev/null +++ b/src/main/java/ladder/ui/LadderScanner.java @@ -0,0 +1,17 @@ +package ladder.ui; + +import java.util.Scanner; + +public class LadderScanner { + public static Scanner scanner = new Scanner(System.in); + + public static String insertParticipant() { + InputView.printInsertParticipantPhrase(); + return scanner.nextLine(); + } + + public static int insertLadderHeight() { + InputView.printInsertLadderHeightPhrase(); + return Integer.parseInt(scanner.nextLine()); + } +} \ No newline at end of file diff --git a/src/main/java/ladder/ui/ResultView.java b/src/main/java/ladder/ui/ResultView.java new file mode 100644 index 0000000000..d43ae23725 --- /dev/null +++ b/src/main/java/ladder/ui/ResultView.java @@ -0,0 +1,34 @@ +package ladder.ui; + +import java.util.List; +import ladder.domain.Ladder; +import ladder.domain.Line; +import ladder.domain.Point; + +public class ResultView { + public static void printResult(List participants, Ladder ladder) { + participants.forEach(it -> { + it = " " + it; + it = it.substring(it.length() - 6); + System.out.print(it); + }); + System.out.println(); + + List lines = ladder.getLines(); + + lines.forEach(it -> printLine(it.getPoints())); + } + + private static void printLine(List points) { + System.out.print(" "); + points.forEach(it -> { + if (it.isExist()) { + System.out.print("|-----"); + return; + } + + System.out.print("| "); + }); + System.out.println("|"); + } +}