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("|"); + } +} 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(); }