[๐ ์ฌ์ดํด2 - ๋ฏธ์ (๋ธ๋์ญ ๊ฒ์ ์คํ)] ์๋ก ๋ฏธ์ ์ ์ถํฉ๋๋ค.#1109
[๐ ์ฌ์ดํด2 - ๋ฏธ์
(๋ธ๋์ญ ๊ฒ์ ์คํ)] ์๋ก ๋ฏธ์
์ ์ถํฉ๋๋ค.#1109sun007021 wants to merge 29 commits intowoowacourse:sun007021from
Conversation
hyeonic
left a comment
There was a problem hiding this comment.
์๋ ํ์ธ์ ์๋ก ~ ๋น ๋ฅด๊ฒ ๊ตฌํํด์ฃผ์ จ๋ค์! ๊ถ๊ธํ ๋ถ๋ถ ๊ฐ๋จํ ์ฝ๋ฉํธ ๋จ๊ฒผ์ผ๋ ํ์ธํด์ฃผ์ธ์!
ํ์ฌ push๋ ์ํ์์ง๋ง Rank์ Shape๋ฅผ Enum์ผ๋ก ๋ณํํ๋ ์์ ์ ์งํ์ค์ ๋๋ค. ์ด๋,
๊ธฐ์กด์๋ ACE์ ๊ฐ์ 11๋ก ๊ณ ์ ํ์๊ณ ๋์ค์ Cards์์ ์ดํฉ ๊ณ์ฐ์์ 10์ฉ ๋บ์ ์ ํ๋ ๋ฐฉํฅ์ผ๋ก ์งํ์ ํ์๋๋ฐ ์ด๊ฒ ์ฌ๋ฐ๋ฅธ ์ฑ ์ ๋ถ๋ฆฌ๊ฐ ๋ง์์ง ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค.
๋ง์ฝ ACE๊ฐ ์ฒ๋ฆฌ์ ๋ํ ๊ท์น์ด ๋ฐ๋๋ค๋ฉด ENUM๋ง ์์ ํด์ฃผ๋๋ก ๊ตฌ์กฐ๋ฅผ ๋ง๋๋ ํธ์ด ์ฑ ์์ด ํ์คํ๋ค๊ณ ์๊ฐํ์๋๋ฐ ํ์ฌ ์ ์์ ๋ฐ๋ฅธ ๊ณ์ฐ์ด ํ์ํด์ Enum์์ ์ฒ๋ฆฌํ๋๋ก ํ์ง๋ ๋ชป ํ ๊ฒ ๊ฐ์ต๋๋ค.
10์ฉ ๋บ์ ํ๋ ์ฐ์ฐ์ ์ฑ ์์ ๋๊ตฌ์๊ฒ ๋๋์ ๋ฐ๋ผ ๋ฌ๋ผ์ง์ง ์์๊น ์ถ๋ค์ ใ ใ ์๋ก ์ด ์๊ฐํ๋ ์ฌ๋ฐ๋ฅธ ์ฑ ์ ๋ถ๋ฆฌ๋ ๋ฌด์์ธ๊ฐ์??
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (!(o instanceof Card card)) { | ||
| return false; | ||
| } | ||
| return rank.equals(card.rank) && | ||
| shape.equals(card.shape); | ||
| } | ||
|
|
There was a problem hiding this comment.
equals ๊ตฌํ์ ์ ์ ๊ฑฐํ์๋์?
There was a problem hiding this comment.
๊ธฐ์กด์ Card๊ฐ ์ผ๋ฐ ํด๋์ค์๋๋ฐ ์ํ๊ฐ ๋ณํ์ง ์๋ ๊ฐ์ฒด์ธ๊ฒ ๊ฐ์ Record๋ก ๋ณ๊ฒฝํ์์ต๋๋ค.
์ด๋, Record ์ฌ์ฉ์ equals, hashCode ๋ฑ์ ๋ฉ์๋๋ฅผ ์๋์ผ๋ก ์์ฑํด์ฃผ๊ธฐ์ ๋ถํ์ํ๋ค๊ณ ํ๋จ๋์ด ์ ๊ฑฐํ์์ต๋๋ค.
| } | ||
| } | ||
| return count; | ||
| return (int) cards.stream().filter(card -> card.rank().equals("A")).count(); |
There was a problem hiding this comment.
card์๊ฒ ace ์ฌ๋ถ๋ฅผ ๋ฌผ์ด๋ณด๋ ๊ฑด ์ด๋ป๊น์?
There was a problem hiding this comment.
์ธ๋ถ์์ ์ง์ ๋น๊ตํ๋ ๊ฒ ๋ณด๋ค card์์ฒด์์ ์ฌ๋ถ๋ฅผ ๋ฐํํด์ฃผ๋ฉด ํจ์ฌ ๊น๋ํด์ง๊ฒ ๋ค์!
| for (Map.Entry<Player, ScoreCompareResult> entry : playerResults.entrySet()) { | ||
| Player player = entry.getKey(); | ||
| ScoreCompareResult result = entry.getValue(); | ||
|
|
||
| double profit = player.calculateProfit(result); | ||
| dealerProfit += profit * -1.0; | ||
| playerProfits.put(player, profit); | ||
| } |
There was a problem hiding this comment.
double์ ๊ธฐ๋ฐ์ผ๋ก ํ ์ค์ ์ฐ์ฐ์ ์์ ํ ๊น์? ๊ฐ์ด ํฐ ๊ฒฝ์ฐ ์ด๋ค ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์๋์?
There was a problem hiding this comment.
์ ๋ฐ๋ ๋ฌธ์ ๋๋ฌธ์ ์ ํํ ๊ณ์ฐ์ด ์ด๋ ต๊ณ == ๊ฐ์ ์ฐ์ฐ์ด ์์๊ณผ ๋ค๋ฅด๊ฒ ๋์ ํ ์ ์์ต๋๋ค!
์ด๋ฒ์ ์ฐพ์๋ณด๋ BigDecimal์ ์ฌ์ฉํด ์ด๋ฌํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์๋ค์. ์ด ๋ฐฉ์์ผ๋ก ๋ฐ๊ฟ๋ณด๊ฒ ์ต๋๋ค!
| - [x] ์์ต ๊ณ์ฐ | ||
| - [x] ํ๋ ์ด์ด๋ ์น๋ฆฌ์ ๋ฐฐํ ๊ธ์ก ๋งํผ ์์ต์ ์ป๋๋ค. | ||
| - [x] ํ๋ ์ด์ด๊ฐ ๋ธ๋์ญ์ธ์ง ํ๋จํ ์ ์๋ค. | ||
| - ์ฒ์ ๋ฐ์ 2๊ฐ์ ์นด๋๊ฐ 21์ธ ๊ฒฝ์ฐ ๋ธ๋์ญ์ผ๋ก ํ๋จ | ||
| - [x] ์ฐธ๊ฐ์๊ฐ ๋ธ๋์ญ์ธ ๊ฒฝ์ฐ ๋ฐฐํ ๊ธ์ก์ 1.5๋ฐฐ ์์ต์ ์ป๋๋ค. | ||
| - [x] ํ๋ ์ด์ด๋ ํจ๋ฐฐ์ ๋ฐฐํ ๊ธ์ก ๋งํผ ์ํด๋ฅผ ๋ณธ๋ค | ||
| - [x] ํ๋ ์ด์ด๋ ๋ฌด์น๋ถ์ ๋ฐฐํ ๊ธ์ก ๋งํผ ์์ต๋ ์ํด๋ ๋ณด์ง ์๋๋ค. | ||
| - [x] ๋๋ฌ๋ ํ๋ ์ด์ด๋ค์ด ํจ๋ฐฐํ ๊ธ์ก ๋งํผ ์์ต์ ์ป๋๋ค. | ||
| - [x] ๋๋ฌ๋ ํ๋ ์ด์ด๋ค์ด ์น๋ฆฌํ ๊ธ์ก ๋งํผ ์ํด๋ฅผ ๋ณธ๋ค. |
There was a problem hiding this comment.
์ ๊ท ์๊ตฌ์ฌํญ์ ์ ์ ๋ฆฌํ์๋ค์ ๐
| public record ProfitResults(Double dealerProfit, Map<Player, Double> playerProfit) { | ||
| } |
There was a problem hiding this comment.
๋ณ๋ค๋ฅธ ํ์๊ฐ ์์ง๋ง domain ํจํค์ง์ ์์นํ์ฌ ์๋ค์! ์ด๋ค ์ญํ ์ ํ๊ฒ ๋๋์?
There was a problem hiding this comment.
๊ธฐ์กด record๋ค์ค ์ด๊ฑธ ํฌํจํด์ ๋จ์ ์ถ๋ ฅ์ ์ํ record๋ค์ด ์์ด์ ํจํค์ง๋ฅผ ๋ถ๋ฆฌํด์ผ ํ ๊ฒ ๊ฐ์ต๋๋ค.
๋จ์ view ์ ๋ฌ์ฉ record๋ค์ dto ํจํค์ง๋ฅผ ๋ง๋ค์ด์ ๋ถ๋ฆฌํ๊ณ ์ ํฉ๋๋ค.
์ด๋, ๊ณ ๋ฏผ์ ProfitResults์ GameResults๋ ๋จ์ ์ถ๋ ฅ์ฉ์ด ์๋๋ผ Game๋ฑ ๋๋ฉ์ธ ๊ฐ์ฒด์์ ๋ฐํํ๋ ๊ฐ์ด๊ณ ๋๋ฉ์ธ ๊ณ์ธต๊ฐ ๊ฐ์ ์ ๋ฌํ ๋ ์ฌ์ฉ๋๋๋ฐ ์ด ๊ฒฝ์ฐ์๋ dtoํจํค์ง ๋ฑ์ผ๋ก ๋ถ๋ฆฌํ๋๊ฒ ๋ง๋์ง ๊ถ๊ธํฉ๋๋ค!
| public record PlayerProfitResultDto(String playerName, double profit) { | ||
|
|
||
| public static PlayerProfitResultDto from(Player player, Double profit) { | ||
| return new PlayerProfitResultDto(player.getName(), profit); | ||
| } | ||
| } |
There was a problem hiding this comment.
์ด๋ค ๊ฑด dto๊ฐ ๋ถ์ด ์๊ณ ์ด๋ค ๊ฐ์ฒด๋ ์๋ฌด๊ฒ๋ ์ ๋ถ์ด ์๋๋ฐ ์ ํ์ ๊ธฐ์ค์ด ๋ฌด์์ธ๊ฐ์?
There was a problem hiding this comment.
์ฌ์ดํด2 ์งํํ๋ฉด์ ๊ธฐ์กด๊ณผ ๊ธฐ์ค์ด ์์ด๋ฉฐ ํต์ผ๋์ง ์์ ๊ฒ ๊ฐ์ต๋๋ค.
card๋ฅผ ์ ์ธํ ๋๋จธ์ง๋ ๋ชจ๋ ๋ฐ์ด์ ์ ๋ฌ์ ์ญํ ๋ง ํ๊ธฐ์ dto๋ฅผ ๋ถ์ด๋๊ฒ ๋ง๋ค๊ณ ์๊ฐํฉ๋๋ค.
๋ค๋ง, ์ด๋ฒ์ dtoํจํค์ง๋ฅผ ๋ถ๋ฆฌํ๋ฉฐ dtoํจํค์ง์ ์์นํ๊ธฐ์ ํด๋์ค๋ช
์๋ dto๋ฅผ ๋ถ์ด๋๊ฑด ์ค๋ณต๋์ด ๋ถํ์ํ๋ค๊ณ ์๊ฐํด ๋ชจ๋ ์ ๊ฑฐํ์์ต๋๋ค
| private static final double BLACKJACK_PROFIT_RATE = 1.5; | ||
|
|
||
| private final String name; | ||
| private final int bettingAmount; |
There was a problem hiding this comment.
๋์ ๋ค๋ฃจ๋ ๋๋ฉ์ธ์์ int๋ผ๋ ์๋ฃํ์ ์ ํฉํ ๊น์?
There was a problem hiding this comment.
int๋ ๋ฒ์๊ฐ 21์ต์ ๋๋ก ๋์ ๋ค๋ฃจ๋ ๋๋ฉ์ธ์์๋ ์ค๋ฒํ๋ก์ฐ๊ฐ ๋ฐ์ํ ์ ์์ด ์ ํฉํ์ง ์์ ๊ฒ ๊ฐ์ต๋๋ค. ์ด๋ฌํ ๋ถ๋ถ๋ค์ ์ ํ ์๊ฐํ์ง ๋ชปํ์์๋ค์
์ด๋ถ๋ถ๋ ๋ง์ฐฌ๊ฐ์ง๋ก BigDecimal ์ฌ์ฉํ๋๋ก ๋ณ๊ฒฝํ๊ฒ ์ต๋๋ค
| public double calculateProfit(ScoreCompareResult compareResult) { | ||
| if (compareResult == ScoreCompareResult.PLAYER_WIN) { | ||
| return calculateProfitWhenPlayerWins(); | ||
| } | ||
| if (compareResult == ScoreCompareResult.PLAYER_LOSE) { | ||
| return -bettingAmount; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| private double calculateProfitWhenPlayerWins() { | ||
| if (isBlackjack()) { | ||
| return (bettingAmount * BLACKJACK_PROFIT_RATE); | ||
| } | ||
| return bettingAmount; | ||
| } |
There was a problem hiding this comment.
์ด๋ฒ ๊ธฐ๋ฅ์ ๊ตฌํํ๋ฉด์ ๊ธฐ์กด ์ฝ๋๋ค์ ๊ตฌ์กฐ๋ฅผ ๋ฆฌํฉํฐ๋ง ํ ๋ถ๋ถ๋ค์ด ๋ง์์ต๋๋ค. ํ์ฌ ๊ฐ ๊ฐ์ฒด์ ์ฑ ์์ด ์ ๋ถ๋ฆฌ๋ ๊ฒ์ธ์ง ๊ถ๊ธํฉ๋๋ค.
์ด๋ฐ ๋ถ๋ถ๋ค์ ๋ง์ ํ์๋ ๊ฑธ๊น์~? ๊ฐ๊ฐ์ ์ฑ ์์ ๊ฒฐ๊ตญ ๊ฐ์ฒด๊ฐ ๊ฐ์ง ์ํ๋ฅผ ์ธ๋ถ์ ์์กด ์์ด ๋ณธ์ธ์ด ์ ์ดํ๊ณ ํด์ํ ์ ์์ ๋ ์ ํฉํ ์์น์ ์๋ค๊ณ ์๊ฐํด์ ใ ใ Player ๊ด์ ์์ ์นํจ ์ฌ๋ถ ๋ฑ๊ณผ ๊ฐ์ ์ ๋ณด๋ฅผ ์์กดํด๋ ๊ด์ฐฎ์์ง ๊ณ ๋ฏผํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค์!
์ฒดํฌ ๋ฆฌ์คํธ
test๋ฅผ ์คํํ์ ๋, ๋ชจ๋ ํ ์คํธ๊ฐ ์ ์์ ์ผ๋ก ํต๊ณผํ๋์?์ด๋ค ๋ถ๋ถ์ ์ง์คํ์ฌ ๋ฆฌ๋ทฐํด์ผ ํ ๊น์?
๊ธฐ์กด์๋ ACE์ ๊ฐ์ 11๋ก ๊ณ ์ ํ์๊ณ ๋์ค์ Cards์์ ์ดํฉ ๊ณ์ฐ์์ 10์ฉ ๋บ์ ์ ํ๋ ๋ฐฉํฅ์ผ๋ก ์งํ์ ํ์๋๋ฐ ์ด๊ฒ ์ฌ๋ฐ๋ฅธ ์ฑ ์ ๋ถ๋ฆฌ๊ฐ ๋ง์์ง ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค.
๋ง์ฝ ACE๊ฐ ์ฒ๋ฆฌ์ ๋ํ ๊ท์น์ด ๋ฐ๋๋ค๋ฉด ENUM๋ง ์์ ํด์ฃผ๋๋ก ๊ตฌ์กฐ๋ฅผ ๋ง๋๋ ํธ์ด ์ฑ ์์ด ํ์คํ๋ค๊ณ ์๊ฐํ์๋๋ฐ ํ์ฌ ์ ์์ ๋ฐ๋ฅธ ๊ณ์ฐ์ด ํ์ํด์ Enum์์ ์ฒ๋ฆฌํ๋๋ก ํ์ง๋ ๋ชป ํ ๊ฒ ๊ฐ์ต๋๋ค.