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

πŸš€ 2단계 - 사닀리(생성) #1963

Open
wants to merge 6 commits into
base: jeounpar
Choose a base branch
from
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
# STEP-01
## STEP-02

### μž…λ ₯

* [X] μ‚¬λžŒμ˜ 이름은 μ΅œλŒ€ 5κΈ€μžμ΄λ‹€
* [X] μ‰Όν‘œ(,) λ₯Ό κΈ°μ€€μœΌλ‘œ 이름을 κ΅¬λΆ„ν•œλ‹€

### 좜λ ₯

* [X] 5자 κΈ°μ€€μœΌλ‘œ μ‚¬μš©μž 이름을 좜λ ₯ν•œλ‹€
* [X] 사닀리λ₯Ό 좜λ ₯ ν•œλ‹€

### κΈ°λŠ₯

* [X] 사닀리 타기가 μ •μƒμ μœΌλ‘œ λ™μž‘ν•˜κΈ° μœ„ν•΄ 라인이 κ²ΉμΉ˜μ§€ μ•Šλ„λ‘ ν•œλ‹€
* [X] 높이에 λ§žλŠ” 사닀리 μƒμ„±ν•œλ‹€

## STEP-01

* [X] λžŒλ‹€ μ‹€μŠ΅ 1 - 읡λͺ… 클래슀λ₯Ό λžŒλ‹€λ‘œ μ „ν™˜
* [X] λžŒλ‹€ μ‹€μŠ΅ 2 - λžŒλ‹€λ₯Ό ν™œμš©ν•΄ 쀑볡 제거
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/nextstep/fp/Conditional.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nextstep.fp;

@FunctionalInterface
public interface Conditional {

boolean test(Integer number);
}
17 changes: 8 additions & 9 deletions src/main/java/nextstep/fp/Lambda.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,21 @@ public void run() {
}

public static int sumAll(List<Integer> numbers) {
return numbers.stream()
.mapToInt(e -> e)
.sum();
return sum(numbers, (number) -> true);
}

public static int sumAllEven(List<Integer> 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<Integer> numbers) {
return sum(numbers, (number) -> number > 3);
}

private static int sum(List<Integer> numbers, Conditional conditional) {
return numbers.stream()
.filter(e -> e > 3)
.mapToInt(e -> e)
.filter(conditional::test)
.mapToInt(number -> number)
.sum();
}
}
35 changes: 35 additions & 0 deletions src/main/java/nextstep/ladder/controller/MainController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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;
import nextstep.ladder.view.OutputView;

public class MainController {

public static void main(String[] args) {
InputView inputView = new InputView();

String namesText = inputView.inputUserNames();
List<Name> 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());

OutputView outputView = new OutputView();
outputView.print(ladder, names);
}

}
22 changes: 22 additions & 0 deletions src/main/java/nextstep/ladder/model/Ladder.java
Original file line number Diff line number Diff line change
@@ -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<Line> lines;

public Ladder(LadderHeight heightOfLadder, PersonCount countOfPerson, Random random) {
this.lines = new ArrayList<>();
IntStream.range(0, heightOfLadder.getHeightOfLadder())
.mapToObj(line -> new Line(countOfPerson.getCountOfPerson(), random))
.forEach(this.lines::add);
}
Comment on lines +12 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μƒμ„±μžμ— 둜직이 ν¬ν•¨λ˜μ—ˆλ„€μš”
링크λ₯Ό μ°Έκ³ ν•΄ λ³΄μ‹œλ©΄ 쒋을 것 κ°™μŠ΅λ‹ˆλ‹€

IntStream 은 for문으둜 λŒ€μ²΄ν•  수 μžˆμ„ 것 κ°™μ€λ°μš”
IntStream 으둜 Line 객체듀을 μƒμ„±ν•˜μ‹  μ΄μœ κ°€ κΆκΈˆν•©λ‹ˆλ‹€

Random을 인자둜 λ°›μœΌλ©΄ 멱등성이 μ§€μΌœμ§€μ§€ μ•ŠλŠ” 것 κ°™μ•„μš”
λžœλ€μ„ μ‚¬μš©ν•˜λ”λΌλ„ μΈν„°νŽ˜μ΄μŠ€λ₯Ό λ§Œλ“€μ–΄μ„œ LadderλŠ” μΈν„°νŽ˜μ΄μŠ€μ— μ˜μ‘΄ν•˜λ„λ‘ λ³€κ²½ν•˜λ©΄ μ–΄λ–¨κΉŒμš”?


public List<Line> getLines() {
return lines;
}
}
23 changes: 23 additions & 0 deletions src/main/java/nextstep/ladder/model/LadderHeight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package nextstep.ladder.model;

public class LadderHeight {

private static final int MIN_HEIGHT = 1;

private final Integer heightOfLadder;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrapper νƒ€μž…μ€ NPEκ°€ λ°œμƒν•  수 μžˆμŠ΅λ‹ˆλ‹€
κΌ­ μ¨μ•Όν•˜λŠ” μ΄μœ κ°€ μ•„λ‹ˆλΌλ©΄ μ›μ‹œ νƒ€μž…μ„ μ‚¬μš©ν•˜λ©΄ μ–΄λ–¨κΉŒμš”?


public LadderHeight(Integer heightOfLadder) {
validateHeight(heightOfLadder);
this.heightOfLadder = heightOfLadder;
}

private void validateHeight(Integer heightOfLadder) {
if (heightOfLadder < MIN_HEIGHT) {
throw new IllegalArgumentException("λ†’μ΄λŠ” 1 이상 이어야 ν•©λ‹ˆλ‹€.");
}
}

public Integer getHeightOfLadder() {
return heightOfLadder;
}
}
15 changes: 15 additions & 0 deletions src/main/java/nextstep/ladder/model/LadderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package nextstep.ladder.model;

import java.util.List;
import java.util.Random;

public class LadderService {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ‚¬μš©ν•˜μ§€ μ•ŠλŠ” 주석, μ½”λ“œ(import 포함)은 μ œκ±°ν•΄ μ£Όμ„Έμš”


private final List<Name> names;
private final Ladder ladder;

public LadderService(LadderHeight heightOfLadder, PersonCount countOfPerson, List<Name> names, Random random) {
ladder = new Ladder(heightOfLadder, countOfPerson, random);
this.names = names;
}
}
47 changes: 47 additions & 0 deletions src/main/java/nextstep/ladder/model/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package nextstep.ladder.model;

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

public class Line {

private final List<Boolean> points;

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);
}
Comment on lines +12 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도 μƒμ„±μž 내뢀에 둜직이 μžˆλ„€μš”
인자 countOfPerson 은 PersonCount νƒ€μž…μ„ ν™œμš©ν•˜λ©΄ μ–΄λ–¨κΉŒμš”?

κ°€λ‘œλΌμΈμ€ κ²ΉμΉ  수 μ—†λ‹€λŠ” μš”κ΅¬μ‚¬ν•­μ€ Line의 μ±…μž„μΌ 것 κ°™μ€λ°μš”
μƒμ„±μžμ— 둜직이 μžˆμ–΄μ„œ μš”κ΅¬μ‚¬ν•­μ„ 검증할 수 없을 것 κ°™μ•„μš”
μš”κ΅¬μ‚¬ν•­μ„ 검증할 수 μžˆλŠ” ꡬ쑰λ₯Ό κ³ λ―Όν•΄ λ³΄μ‹œλ©΄ 쒋을 것 κ°™μ•„μš”


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);
}
Comment on lines +29 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getterκ°€ λ„ˆλ¬΄ 많이 μ‚¬μš©λ˜κ³  μžˆλŠ” 것 κ°™μ•„μš”
λ©”μ‹œμ§€λ₯Ό λ³΄λ‚΄μ„œ ν•΄κ²°ν•  수 μ—†λŠ”μ§€, λ©”μ‹œμ§€λ₯Ό 보낼 객체λ₯Ό λ„μΆœν•  수 μ—†λŠ”μ§€ κ³ λ―Όν•΄ λ³΄μ‹œλ©΄ 쒋을 것 κ°™μ•„μš”

}

public List<Boolean> getPoints() {
return this.points;
}

@Override
public String toString() {
return this.points.toString();
}
}
30 changes: 30 additions & 0 deletions src/main/java/nextstep/ladder/model/Name.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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) {
validateNameLength(name);
this.name = name;
}

private void validateNameLength(String name) {
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, ""));
}
Comment on lines +24 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좜λ ₯을 μœ„ν•΄μ„œ 곡백을 μΆ”κ°€ν•˜λŠ” λ‘œμ§μ„ 좜λ ₯의 μ±…μž„ μ•„λ‹κΉŒμš”?
μ°Έκ°€μžκ°€ λ§Žμ€ 경우 μ‚¬λ‹€λ¦¬μ˜ μ‹œμž‘ μœ„μΉ˜μ— 이름이 좜λ ₯되고 μžˆμ§€ μ•Šμ•„μš”
image

}
22 changes: 22 additions & 0 deletions src/main/java/nextstep/ladder/model/PersonCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package nextstep.ladder.model;

public class PersonCount {

private static final int MIN_COUNT = 1;
private final Integer countOfPerson;

public PersonCount(Integer countOfPerson) {
validateCountOfPerson(countOfPerson);
this.countOfPerson = countOfPerson;
}

private void validateCountOfPerson(Integer countOfPerson) {
if (countOfPerson < MIN_COUNT) {
throw new IllegalArgumentException("μ‚¬λžŒμ˜ μˆ˜λŠ” 1 이상 이어야 ν•©λ‹ˆλ‹€.");
}
}

public Integer getCountOfPerson() {
return countOfPerson;
}
}
11 changes: 11 additions & 0 deletions src/main/java/nextstep/ladder/model/RandomTrueOrFalse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package nextstep.ladder.model;

import java.util.Random;

public class RandomTrueOrFalse extends Random {

@Override
public boolean nextBoolean() {
return super.nextBoolean();
}
}
19 changes: 19 additions & 0 deletions src/main/java/nextstep/ladder/view/InputView.java
Original file line number Diff line number Diff line change
@@ -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 inputHeightOfLadder() {
System.out.println("\nμ΅œλŒ€ 사닀리 λ†’μ΄λŠ” λͺ‡ κ°œμΈκ°€μš”?");
return SCANNER.nextLine();
}

}
44 changes: 44 additions & 0 deletions src/main/java/nextstep/ladder/view/OutputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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 {

private static final String FOUR_SPACE = " ";
private static final String LADDER = "|";
private static final String LINE = "----";

public OutputView() {
System.out.println("\nμ‹€ν–‰κ²°κ³Ό\n");
}

public void print(Ladder ladder, List<Name> names) {
printNames(names);
printLadder(ladder);
}

private 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);
}

private void printNames(List<Name> names) {
names.stream()
.map(name -> name + " ")
.forEach(System.out::print);
System.out.println();
}
}

24 changes: 24 additions & 0 deletions src/test/java/nextstep/ladder/model/LadderHeightTest.java
Original file line number Diff line number Diff line change
@@ -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 이상 이어야 ν•©λ‹ˆλ‹€.");
}
Comment on lines +11 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

경계값인 0으둜 κ²€μ¦ν–ˆλŠ”λ° μΆ”κ°€λ‘œ -1도 κ²€μ¦ν•˜μ‹  μ΄μœ κ°€ μžˆμœΌμ‹ κ°€μš”?

}
21 changes: 21 additions & 0 deletions src/test/java/nextstep/ladder/model/LadderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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(new LadderHeight(5), new PersonCount(4), new AlwaysReturnTrue());
}
Comment on lines +16 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λ‹¨μœ„ ν…ŒμŠ€νŠΈ μž‘μ„±μ΄ λˆ„λ½λ˜μ—ˆλ„€μš”


}
Loading