Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
cef9213
Refact : CarTest 리팩토링
this-is-spear Aug 15, 2022
a9888c3
Refact : Lambda 리팩토링
this-is-spear Aug 15, 2022
6eac39f
Refact : StreamStudy 요구사항 1번 해결
this-is-spear Aug 15, 2022
bcd9140
StreamStudy 요구사항 2번 해결
this-is-spear Aug 15, 2022
d57d1ac
Refct : Optional 1번 요구사항 해결
this-is-spear Aug 15, 2022
cac2d45
Refct : Optional 2번 요구사항 해결
this-is-spear Aug 15, 2022
4434455
Refct : Optional 3번 요구사항 해결
this-is-spear Aug 15, 2022
5e0a84b
Refact : Lambda.java 코드 리팩토링
this-is-spear Aug 31, 2022
083322b
Refact : StreamStudy.java 코드 리팩토링
this-is-spear Aug 31, 2022
4096487
Refact: User 리팩토링
this-is-spear Aug 31, 2022
6ca54c8
Refact : Computer 리팩토링
this-is-spear Aug 31, 2022
486d574
Refact : User 리팩토링
this-is-spear Aug 31, 2022
7d783ea
Refact : ComputerStore 리팩토링
this-is-spear Aug 31, 2022
eae04b8
Docs : 블랙잭 모델링
this-is-spear Aug 31, 2022
c675df9
Feat : Card 구현 및 테스트
this-is-spear Aug 31, 2022
fac6f7a
Feat : OneCards 기능 구현 및 테스트
this-is-spear Aug 31, 2022
16ddf56
Feat : 참가자 구현 및 테스트
this-is-spear Aug 31, 2022
ab45912
Feat : Cards 구현 및 테스트
this-is-spear Aug 31, 2022
078122a
Refact : Cards 리팩토링
this-is-spear Aug 31, 2022
1d26d8f
Feat : 딜러 구현 및 테스트
this-is-spear Sep 1, 2022
a4e2280
Refact : OneCards 코드 리팩토링
this-is-spear Sep 1, 2022
eca06c8
Refact : ParticipantsTest -> ParticipantTest 이름 수정
this-is-spear Sep 1, 2022
2b39e03
Feat : Participants 구현 및 테스트
this-is-spear Sep 1, 2022
656e032
Refact : 카드 패키지 응집
this-is-spear Sep 1, 2022
ce9ffa2
Refact : 참여자와 딜러 패키지 이름 수정
this-is-spear Sep 1, 2022
b72fba9
Refact : Cards 생성자 리팩토링
this-is-spear Sep 1, 2022
4ff7092
Refact : Participant 리팩토링
this-is-spear Sep 1, 2022
3c1fc7b
Feat : IO, Main 클래스 구현
this-is-spear Sep 1, 2022
bce2d18
Refact : Cards 리팩토링
this-is-spear Sep 12, 2022
b045fed
Fix : 입력 수정
this-is-spear Sep 12, 2022
9c1b9af
Refact : BlackjackGame 코드 분리
this-is-spear Sep 12, 2022
d3483ac
Docs : Dealer 주석 작성
this-is-spear Sep 12, 2022
e26737b
Refact : OneCards 의존성 수정
this-is-spear Sep 12, 2022
f5942ef
Refact : BlackjackInputService -> BlackjackGameService 이름 수정
this-is-spear Sep 12, 2022
d7155f0
Feat : BlackjackOutputService 구현 및 테스트
this-is-spear Sep 12, 2022
9d94f14
Refact : BlackjackOutputService 의존성 수정
this-is-spear Sep 12, 2022
a2cdc07
Refact : 코드 리팩토링
this-is-spear Sep 12, 2022
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
5 changes: 5 additions & 0 deletions src/main/java/nextstep/fp/Condition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package nextstep.fp;

public interface Condition {
boolean condition(Integer number);
}
62 changes: 30 additions & 32 deletions src/main/java/nextstep/fp/Lambda.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,51 @@

public class Lambda {
public static void printAllOld(List<Integer> numbers) {
System.out.println("printAllOld");

for (int number : numbers) {
System.out.println(number);
}
print("printAllOld");
numbers.forEach(System.out::println);
}

public static void printAllLambda(List<Integer> numbers) {
System.out.println("printAllLambda");

print("printAllLambda");
numbers.forEach(System.out::println);
}

public static void runThread() {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Hello from thread");
}
}).start();
new Thread(() -> print("Hello from thread")).start();
}


public static int sumAll(List<Integer> numbers) {
int total = 0;
for (int number : numbers) {
total += number;
}
return total;
return sumAll(numbers, Lambda::getAll);
}

public static int sumAllEven(List<Integer> numbers) {
int total = 0;
for (int number : numbers) {
if (number % 2 == 0) {
total += number;
}
}
return total;
return sumAll(numbers, Lambda::getEven);
}

public static int sumAllOverThree(List<Integer> numbers) {
int total = 0;
for (int number : numbers) {
if (number > 3) {
total += number;
}
}
return total;
return sumAll(numbers, Lambda::getOverThree);
}

private static int sumAll(List<Integer> numbers, Condition condition) {
Copy link
Member

Choose a reason for hiding this comment

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

condition을 직접 주지 않고 메소드로 분리해 사용하셨군요! 분리하니 확장에 유리한 코드가 된 것 같습니다. 저도 참고하겠습니다 :)

return numbers.stream()
.filter(condition::condition)
.reduce(Integer::sum).orElse(0);
}

private static void print(String Hello_from_thread) {
Copy link
Member

Choose a reason for hiding this comment

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

print 메소드를 따로 구현한 이유가 궁금합니다. 가독성을 위한 건가요?!

Copy link
Author

Choose a reason for hiding this comment

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

이 부분은 제가 추가하지 않고 기존 코드에 존재한 코드이다 보니 리팩토링을 따로 진행하지 않았던 것 같아요..!! 사용하지 않는다면 삭제하겠습니다! ☺️

System.out.println(Hello_from_thread);
}

private static boolean getAll(Integer integer) {
return true;
}

private static boolean getEven(Integer integer) {
return integer % 2 == 0;
}

private static boolean getOverThree(Integer integer) {
return integer > 3;
}
}
36 changes: 30 additions & 6 deletions src/main/java/nextstep/fp/StreamStudy.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class StreamStudy {

public static long countWords() throws IOException {
String contents = new String(Files.readAllBytes(Paths
.get("src/main/resources/fp/war-and-peace.txt")), StandardCharsets.UTF_8);
.get("src/main/resources/fp/war-and-peace.txt")), StandardCharsets.UTF_8);
List<String> words = Arrays.asList(contents.split("[\\P{L}]+"));

long count = 0;
Expand All @@ -24,21 +29,40 @@ public static long countWords() throws IOException {

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);
.get("src/main/resources/fp/war-and-peace.txt")), StandardCharsets.UTF_8);
List<String> words = Arrays.asList(contents.split("[\\P{L}]+"));

// TODO 이 부분에 구현한다.

Map<String, Integer> wordMap = new HashMap<>();

for (String word : words) {
Copy link
Member

Choose a reason for hiding this comment

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

중복 제거를 위한 코드인가요? :) map으로 저장하신 의도가 궁금합니다!

Copy link
Author

Choose a reason for hiding this comment

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

맞습니다. 중복 제거를 위한 코드입니다. 👍

if (!wordMap.containsKey(word)) {
wordMap.put(word, 0);
}
wordMap.put(word, wordMap.get(word) + 1);
}

List<String> collect = wordMap.entrySet()
.stream().filter(entry -> entry.getValue() > 12)
.sorted(Comparator.comparing(entry -> - entry.getValue()))
.map(entry -> entry.getKey().toLowerCase())
.distinct()
.collect(Collectors.toList());

List<String> subList = new ArrayList<>(collect.subList(0, 100));
System.out.println(subList);
System.out.println(subList.size());
}

public static List<Integer> doubleNumbers(List<Integer> numbers) {
return numbers.stream().map(x -> 2 * x).collect(Collectors.toList());
}

public static long sumAll(List<Integer> numbers) {
return numbers.stream().reduce(0, (x, y) -> x + y);
return numbers.stream().reduce(0, Integer::sum);
}

public static long sumOverThreeAndDouble(List<Integer> numbers) {
return 0;
return Lambda.sumAllOverThree(numbers) * 2L;
}
}
}
15 changes: 8 additions & 7 deletions src/main/java/nextstep/optional/Expression.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package nextstep.optional;

import java.util.Arrays;

enum Expression {
PLUS("+"), MINUS("-"), TIMES("*"), DIVIDE("/");

Expand All @@ -14,12 +16,11 @@ private static boolean matchExpression(Expression e, String expression) {
}

static Expression of(String expression) {
for (Expression v : values()) {
if (matchExpression(v, expression)) {
return v;
}
}

throw new IllegalArgumentException(String.format("%s는 사칙연산에 해당하지 않는 표현식입니다.", expression));
return Arrays.stream(values())
.filter(expression1 -> matchExpression(expression1, expression))
.findFirst()
.orElseThrow(
Copy link
Member

Choose a reason for hiding this comment

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

줄 구분을 하니 깔끔하게 읽히네요
저도 줄바꿈하는 습관을 들여야겠습니다 👍🏼

() -> new IllegalArgumentException(String.format("%s는 사칙연산에 해당하지 않는 표현식입니다.", expression))
);
}
}
8 changes: 5 additions & 3 deletions src/main/java/nextstep/optional/User.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package nextstep.optional;

import java.util.Optional;

public class User {
private String name;
private Integer age;
Expand All @@ -25,15 +27,15 @@ public static boolean ageIsInRange1(User user) {
boolean isInRange = false;

if (user != null && user.getAge() != null
&& (user.getAge() >= 30
&& user.getAge() <= 45)) {
&& (user.getAge() >= 30
&& user.getAge() <= 45)) {
isInRange = true;
}
return isInRange;
}

public static boolean ageIsInRange2(User user) {
return false;
return user != null && user.getAge() != null && (user.getAge() >= 30 && user.getAge() <= 45);
Copy link
Member

Choose a reason for hiding this comment

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

Optional을 사용하여 구현하는 부분이었던 것 같은데, 사용되지 않았네요.

Copy link
Author

Choose a reason for hiding this comment

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

그렇군요..!! 수정하겠습니다. ☺️

}

@Override
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/nextstep/optional/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class Users {
static final User DEFAULT_USER = new User("codesquad", 100);
Expand All @@ -12,12 +13,7 @@ public class Users {
new User("jk", 40),
new User("honux", 45));

User getUser(String name) {
for (User user : users) {
if (user.matchName(name)) {
return user;
}
}
return DEFAULT_USER;
Optional<User> getUser(String name) {
return users.stream().filter(user -> user.matchName(name)).findFirst();
Copy link
Member

Choose a reason for hiding this comment

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

orElse(DEFAULT_USER)가 이부분에 구현되어야 하지 않나요?!

Copy link
Author

Choose a reason for hiding this comment

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

Optional로 값을 리턴해 NUll 값을 허용했는데, Users에서 Optional을 넘기는 방식이 옳은건지 궁금해지네요! ☺️ 말씀하신 것처럼 현재 애플리케이션에서는 존재하지 않는다면 DEFAULT_USER로 리턴하는 방식이 맞는 것 같습니다.

}
}
15 changes: 3 additions & 12 deletions src/test/java/nextstep/fp/CarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,15 @@ public class CarTest {
@Test
public void 이동() {
Car car = new Car("pobi", 0);
Car actual = car.move(new MoveStrategy() {
@Override
public boolean isMovable() {
return true;
}
});
Car actual = car.move(() -> true);

assertThat(actual).isEqualTo(new Car("pobi", 1));
}

@Test
public void 정지() {
Car car = new Car("pobi", 0);
Car actual = car.move(new MoveStrategy() {
@Override
public boolean isMovable() {
return false;
}
});
Car actual = car.move(() -> false);
assertThat(actual).isEqualTo(new Car("pobi", 0));
}
}
6 changes: 3 additions & 3 deletions src/test/java/nextstep/optional/ExpressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public void of() {
@Test
public void notValidExpression() {
assertThatIllegalArgumentException()
.isThrownBy(() -> {
Expression.of("&");
});
.isThrownBy(() -> {
Expression.of("&");
});
}
}
8 changes: 6 additions & 2 deletions src/test/java/nextstep/optional/UsersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ public class UsersTest {
@Test
public void getUser() {
Users users = new Users();
assertThat(users.getUser("crong")).isEqualTo(new User("crong", 35));
assertThat(getUser(users, "crong")).isEqualTo(new User("crong", 35));
}


@Test
public void getDefaultUser() {
Users users = new Users();
assertThat(users.getUser("codesquard")).isEqualTo(Users.DEFAULT_USER);
assertThat(getUser(users, "codesquard")).isEqualTo(Users.DEFAULT_USER);
}

private User getUser(Users users, String codesquard) {
return users.getUser(codesquard).orElse(Users.DEFAULT_USER);
}
}