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

Step2 사다리(생성) #2013

Open
wants to merge 2 commits into
base: kyopread
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,19 @@
* 모든 피드백을 완료하면 다음 단계를 도전하고 앞의 과정을 반복한다.

## 온라인 코드 리뷰 과정
* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/nextstep-step/nextstep-docs/tree/master/codereview)
* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/nextstep-step/nextstep-docs/tree/master/codereview)

## 기능 요구사항
- 사다리 게임에 참여하는 사람에 이름을 부여할 수 있다.
- 이름은 최대 5글자로 제한된다.
- 사람의 이름은 띄어쓰기가 존재하지 않는 쉼표로 구분한다.
- 사다리의 모양은 |-----|으로 출력된다.
- 사다리를 출력할 때 사람 이름도 같이 출력한다.
- 참여하는 사람의 숫자에 따라 사다리의 폭과 사다리의 개수가 달라진다.
- |-----|-----| 같이 연속적인 사다리는 가능하지 않다.
- 하나의 라인에는 최소 하나 이상이 사다리가 존재하며 사다리의 개수는 랜덤하다.

## 프로그래밍 요구사항
- 최대한 자바 8의 스트림과 람다를 이용한다.
- 모든 엔티티를 작게 유지한다.
- 최대한 TDD 기반으로 진행한다.
24 changes: 24 additions & 0 deletions src/main/java/ladder/LadderApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ladder;

import ladder.dto.LadderRequest;
import ladder.dto.LadderResponse;
import ladder.service.LadderService;
import ladder.view.InputView;
import ladder.view.LinePrinter;
import ladder.view.ResultView;

public class LadderApplication {
public static void main(String[] args) {
InputView inputView = new InputView();
String participant = inputView.inputParticipant();
String[] splitParticipants = inputView.splitParticipants(participant);
int ladderLimitCount = inputView.inputLadderLimitCount();

LadderService ladderService = new LadderService();
LadderResponse response = ladderService.createLadder(new LadderRequest(splitParticipants, ladderLimitCount));

LinePrinter printer = new LinePrinter(ladderLimitCount);
ResultView resultView = new ResultView(printer, splitParticipants, response.getLines());
resultView.showResult();
}
}
15 changes: 15 additions & 0 deletions src/main/java/ladder/domain/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ladder.domain;

public class Line {
private RowLinePositions rowLinePositions;

public Line(String[] participants) {
int participantNumber = participants.length;
int rowLineNumber = participantNumber - 1;
rowLinePositions = new RowLinePositions(rowLineNumber);
}

public boolean isPosition(int index) {
return rowLinePositions.isPosition(index);
}
}
22 changes: 22 additions & 0 deletions src/main/java/ladder/domain/Lines.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ladder.domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Lines {
private List<Line> lineList = new ArrayList<>();
private int limitCount;

public Lines(int limitCount) {
this.limitCount = limitCount;
}

public void addLine(Line line) {
this.lineList.add(line);
}

public final List<Line> getLines() {
return Collections.unmodifiableList(lineList);
}
}
45 changes: 45 additions & 0 deletions src/main/java/ladder/domain/RowLinePositions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ladder.domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;

public class RowLinePositions {
private List<Boolean> positionList = new ArrayList<>();
private int rowLineCount;
private Random random = new Random();

public RowLinePositions(int rowLineCount) {
this.rowLineCount = rowLineCount;
initializePositionList();
}

private void initializePositionList() {
positionList.add(random.nextBoolean());
IntStream.range(1, rowLineCount)
.forEach(index -> addRandomBoolean(index));
addTrueIfAllFalse();
}

private void addRandomBoolean(int index) {
if (positionList.get(index - 1)) {
positionList.add(false);
return;
}
positionList.add(random.nextBoolean());
}

private void addTrueIfAllFalse() {
boolean isAllFalse = positionList.stream()
.allMatch(e -> e.equals(Boolean.FALSE));
if (isAllFalse) {
positionList.set(random.nextInt(rowLineCount), Boolean.TRUE);
}
}

public boolean isPosition(int index) {
return positionList.get(index);
}
}
19 changes: 19 additions & 0 deletions src/main/java/ladder/dto/LadderRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ladder.dto;

public class LadderRequest {
private String[] participants;
private int limitCount;

public LadderRequest(String[] participants, int highCount) {
this.participants = participants;
this.limitCount = highCount;
}

public String[] getParticipants() {
return participants;
}

public int getLimitCount() {
return limitCount;
}
}
15 changes: 15 additions & 0 deletions src/main/java/ladder/dto/LadderResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ladder.dto;

import ladder.domain.Lines;

public class LadderResponse {
private final Lines lines;

public LadderResponse(Lines lines) {
this.lines = lines;
}

public Lines getLines() {
return lines;
}
}
18 changes: 18 additions & 0 deletions src/main/java/ladder/service/LadderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ladder.service;

import ladder.domain.Line;
import ladder.domain.Lines;
import ladder.dto.LadderRequest;
import ladder.dto.LadderResponse;

import java.util.stream.IntStream;

public class LadderService {
public LadderResponse createLadder(LadderRequest request) {
Lines lines = new Lines(request.getLimitCount());
IntStream.range(0, request.getLimitCount())
.forEach(i -> lines.addLine(new Line(request.getParticipants())));

return new LadderResponse(lines);
}
}
26 changes: 26 additions & 0 deletions src/main/java/ladder/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ladder.view;

import java.util.Scanner;

public class InputView {
private static final Scanner scanner = new Scanner(System.in);

public String inputParticipant() {
System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표 ','로 구분하세요.)");
return scanner.nextLine();
}

public int inputLadderLimitCount() {
printBlank();
System.out.println("최대 사다리 높이는 몇 개인가요?");
return scanner.nextInt();
}

public String[] splitParticipants(String participant) {
return participant.split(",");
}

private void printBlank() {
System.out.println("");
}
}
23 changes: 23 additions & 0 deletions src/main/java/ladder/view/LinePrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ladder.view;

public class LinePrinter {
private final String COLUMN_SIGN = "|";
private final String ROW_SIGN = "-";
private final String EMPTY_ROW_SIGN = " ";
private int repeatNumber;

/**
* repeatNumber -> highCount
*/
public LinePrinter(int repeatNumber) {
this.repeatNumber = repeatNumber;
}

public void printRefactoring(boolean isTruePosition) {
if (isTruePosition) {
System.out.print(ROW_SIGN.repeat(repeatNumber) + COLUMN_SIGN);
return;
}
System.out.print(EMPTY_ROW_SIGN.repeat(repeatNumber) + COLUMN_SIGN);
}
}
46 changes: 46 additions & 0 deletions src/main/java/ladder/view/ResultView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ladder.view;

import ladder.domain.Line;
import ladder.domain.Lines;

import java.util.Arrays;

public class ResultView {
private LinePrinter printer;
private final String[] participants;
private final Lines lines;

public ResultView(LinePrinter printer, String[] participants, Lines lines) {
this.printer = printer;
this.participants = participants;
this.lines = lines;
}

public void showResult() {
System.out.println("");
System.out.println("실행 결과");
printParticipants();
printResult();
}

private void printParticipants() {
Arrays.stream(participants)
.forEach(name -> System.out.print(name + " "));
System.out.println("");
}

private void printResult() {
for (Line line : lines.getLines()) {
printLadderByOneLine(line);
System.out.println("");
}
}

private void printLadderByOneLine(Line line) {
int rowLineNumber = participants.length - 1;
System.out.print("|");
for (int index = 0; index < rowLineNumber; index++) {
printer.printRefactoring(line.isPosition(index));
}
}
}
16 changes: 9 additions & 7 deletions src/main/java/nextstep/fp/StreamStudy.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ public static void printLongestWordTop100() throws IOException {
String contents = new String(Files.readAllBytes(Paths
.get("src/main/resources/fp/war-and-peace.txt")), StandardCharsets.UTF_8);
List<String> words = Arrays.asList(contents.split("[\\P{L}]+"));
words.stream()
.filter(word -> word.length() > 12)
.distinct()
.sorted((p1, p2) -> Integer.compare(p2.length(), p1.length()))
.limit(100)
.map(String::toLowerCase)
.forEach(System.out::println);
String joinWords = words.stream()
.filter(word -> word.length() > 12)
.distinct()
.sorted((p1, p2) -> Integer.compare(p2.length(), p1.length()))
.limit(100)
.map(String::toLowerCase)
.collect(Collectors.joining("\n"));

System.out.println(joinWords);
}

public static List<Integer> doubleNumbers(List<Integer> numbers) {
Expand Down