-
Notifications
You must be signed in to change notification settings - Fork 709
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
base: jeounpar
Are you sure you want to change the base?
Changes from all commits
42a5899
2942a15
05cd1ca
a613e83
0e440fd
684953b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} |
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); | ||
} | ||
|
||
} |
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); | ||
} | ||
|
||
public List<Line> getLines() { | ||
return lines; | ||
} | ||
} |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ¬κΈ°λ μμ±μ λ΄λΆμ λ‘μ§μ΄ μλ€μ κ°λ‘λΌμΈμ κ²ΉμΉ μ μλ€λ μꡬμ¬νμ 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} |
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; | ||
} | ||
} |
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(); | ||
} | ||
} |
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(); | ||
} | ||
|
||
} |
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(); | ||
} | ||
} | ||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. κ²½κ³κ°μΈ 0μΌλ‘ κ²μ¦νλλ° μΆκ°λ‘ -1λ κ²μ¦νμ μ΄μ κ° μμΌμ κ°μ? |
||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. λ¨μ ν μ€νΈ μμ±μ΄ λλ½λμλ€μ |
||
|
||
} |
There was a problem hiding this comment.
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λ μΈν°νμ΄μ€μ μμ‘΄νλλ‘ λ³κ²½νλ©΄ μ΄λ¨κΉμ?