-
Notifications
You must be signed in to change notification settings - Fork 550
[๐ ์ฌ์ดํด2 - ๋ฏธ์ (๋ธ๋์ญ ๋ฒ ํ )] ์์ค ๋ฏธ์ ์ ์ถํฉ๋๋ค. #1120
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
base: huiyeongkim
Are you sure you want to change the base?
Changes from all commits
eec36bb
ac4041f
549740e
66335df
f265b88
e11989d
97d660e
4a44d63
dce0004
e1c75d8
203ca3b
21809b5
b682e3e
df28a15
d524bbb
215e243
680f000
efba602
cb863b2
b8c8928
8258839
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,35 @@ | ||
| package domain.money; | ||
|
|
||
| import domain.card.MatchResult; | ||
|
|
||
| import static util.BlackJackConstant.BLACKJACK_MULTIPLIER; | ||
|
|
||
| public class BettingResult { | ||
|
|
||
| private final int earnings; | ||
|
|
||
| private BettingResult(int earnings) { | ||
| this.earnings = earnings; | ||
| } | ||
|
|
||
| public static BettingResult from(int betAmount, MatchResult matchResult, boolean isBlackJack) { | ||
| if (matchResult == MatchResult.WIN && isBlackJack) { | ||
| return new BettingResult((int) (betAmount * BLACKJACK_MULTIPLIER)); | ||
| } | ||
| if (matchResult == MatchResult.WIN) { | ||
| return new BettingResult(betAmount); | ||
| } | ||
| if (matchResult == MatchResult.LOSE) { | ||
| return new BettingResult(-betAmount); | ||
| } | ||
| return new BettingResult(0); | ||
| } | ||
|
|
||
| public int getEarnings() { | ||
| return earnings; | ||
| } | ||
|
|
||
| public int reverse() { | ||
| return -earnings; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package domain.money; | ||
|
|
||
| public class Money { | ||
|
|
||
| private final Integer money; | ||
|
|
||
| public Money(Integer money) { | ||
| validate(money); | ||
| this.money = money; | ||
| } | ||
|
|
||
| public Integer getValue() { | ||
| return money; | ||
| } | ||
|
|
||
| private void validate(Integer money) { | ||
| if (money < 0) { | ||
| throw new IllegalArgumentException("๋์ ์์๊ฐ ๋ ์ ์์ต๋๋ค."); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,41 @@ | ||
| package domain.participant; | ||
|
|
||
| import domain.money.Money; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import static util.BlackJackConstant.MAX_NAME_LENGTH; | ||
|
|
||
| public class Player extends Participant { | ||
|
|
||
| private static final String STRING_REGEX = "^[a-zA-Z]*$"; | ||
|
|
||
| private final String name; | ||
| private Optional<Money> money = Optional.empty(); | ||
|
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. ์ด๋ถ๋ถ์ PR ๋ณธ๋ฌธ 2๋ฒ์์๋ ์ ์ด์ฃผ์ จ๋๋ฐ, ์ ์๊ฐ์ ์์ ์ด ํ์ํด ๋ณด์ ๋๋ค. ์ฐ์ ์ ์ด์ฃผ์ ๊ฒ์ฒ๋ผ, money๋ ๊ฐ์ฒด ์์ฑ ์์ ์ ์ ์๋๋ ๊ฒ์ด ์ข๋ค๊ณ ์๊ฐํฉ๋๋ค.
์์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ๊ณ ๋ฏผํด๋ณด์๋ฉด...
|
||
|
|
||
| public Player(String name) { | ||
| validateNameLength(name); | ||
| validateOnlyEnglish(name); | ||
| this.name = name; | ||
| } | ||
|
|
||
| public void bet(Integer money) { | ||
| if (this.money.isPresent()) { | ||
| throw new IllegalArgumentException("๋ฒ ํ ๊ธ์ก์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค."); | ||
| } | ||
| this.money = Optional.of(new Money(money)); | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public Money getMoney() { | ||
| return money.orElseThrow(() -> | ||
| new IllegalStateException("๋ฒ ํ ๊ธ์ก์ด ์ค์ ๋์ง ์์์ต๋๋ค.")); | ||
| } | ||
|
|
||
| private void validateNameLength(String name) { | ||
| if (name.isEmpty() || name.length() > MAX_NAME_LENGTH) { | ||
| throw new IllegalArgumentException("ํ๋ ์ด์ด ์ด๋ฆ์ 1๊ธ์ ์ด์ 8๊ธ์ ์ดํ์ฌ์ผ ํฉ๋๋ค."); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,8 @@ | |
| import domain.participant.Dealer; | ||
| import domain.participant.Player; | ||
| import domain.participant.Players; | ||
| import domain.money.BettingResult; | ||
|
|
||
| import java.util.EnumMap; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
|
|
@@ -45,11 +45,30 @@ public Map<String, MatchResult> calculateResults() { | |
| return playerResults; | ||
| } | ||
|
|
||
| public Map<MatchResult, Integer> calculateDealerResult(Map<String, MatchResult> playerResults) { | ||
| Map<MatchResult, Integer> dealerResult = new EnumMap<>(MatchResult.class); | ||
| public Map<String, BettingResult> calculateBettingResults() { | ||
| Map<String, MatchResult> matchResults = calculateResults(); | ||
| Map<String, BettingResult> bettingResults = new LinkedHashMap<>(); | ||
|
Comment on lines
+48
to
+50
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. ์ด๋ถ๋ถ์ ์ ์์ ํด ์ฃผ์ จ๋ค์! PR ๋ณธ๋ฌธ์๋ ๊ด๋ จ๋ ๋ด์ฉ ์ ์ด์ฃผ์ ์, ์ ์๊ฒฌ๋ ๋จ๊ธฐ๊ฒ ์ต๋๋ค. ์ฐ์ ์ด๋ค ๋ฉ์๋์ ๋์ ์์ ์, ์๋๋ ๊ธฐ์กด ๋์์ ์ํฅ์ ์ค ์ ์๋ค๋ ์ ์ ๊ณต๊ฐํฉ๋๋ค.
๋ค๋ง ์ด๋ฌํ ํ
์คํธ ๋ฐ ํ ๋ด ์ฝ๋ ๋ฆฌ๋ทฐ๊ฐ ์ ํ๋๋ค๋ฉด, ๊ธฐ์กด ๋ฉ์๋๋ฅผ ์์ ํ๋ ๊ฒ ์์ฒด์๋ ๋ฌธ์ ์๋ค๊ณ ์๊ฐํฉ๋๋ค. ๋ค๋ง ๋ฉ์๋ ํ๋๋ ๋ช
ํํ ์์ ๋จ์์ ์ฑ
์์ ๊ฐ์ ธ์ผ ํ๊ธฐ ๋๋ฌธ์, ์ด๋ฒ ์์ ์์๋ ๋ฒ ํ ๊ธฐ๋ฅ์ด ์ถ๊ฐ๋์๊ณ , ์ด๋ฅผ ๊ธฐ์กด ๊ธฐ๋ฅ + ๋ฒ ํ ๊ธฐ๋ฅ์ ์ํํ๋ ๋ฉ์๋๋ฅผ ๋ง๋ค์ด์ ํด๊ฒฐํ๊ธฐ ๋๋ฌธ์ ๋งค์ฐ ์ข์ ๋ฐฉํฅ์ด๋ผ๊ณ ์๊ฐ๋ฉ๋๋ค. |
||
|
|
||
| for (MatchResult matchResult : playerResults.values()) { | ||
| dealerResult.put(matchResult.reverse(), dealerResult.getOrDefault(matchResult.reverse(), 0) + 1); | ||
| for (Player player : players.getPlayers()) { | ||
| MatchResult matchResult = matchResults.get(player.getName()); | ||
|
|
||
| BettingResult result = BettingResult.from( | ||
| player.getMoney().getValue(), | ||
| matchResult, | ||
| player.getHand().isBlackJack() | ||
| ); | ||
| bettingResults.put(player.getName(), result); | ||
| } | ||
|
|
||
| return bettingResults; | ||
| } | ||
|
|
||
| public Integer calculateDealerResult(Map<String, BettingResult> playerResults) { | ||
| int dealerResult = 0; | ||
|
|
||
| for (BettingResult bettingResult : playerResults.values()) { | ||
| int reversedResult = bettingResult.reverse(); | ||
| dealerResult += reversedResult; | ||
| } | ||
|
|
||
| return dealerResult; | ||
|
|
@@ -109,6 +128,11 @@ private boolean handleBlackJack(Player player, Map<String, MatchResult> playerRe | |
| playerResults.put(player.getName(), MatchResult.DRAW); | ||
| return true; | ||
| } | ||
| if (player.getHand().isBlackJack() && dealer.getHand().isBlackJack()) { | ||
| playerResults.put(player.getName(), MatchResult.DRAW); | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ public class InputView { | |
|
|
||
| private static final String COMMA_DELIMITER = ","; | ||
| private static final String BINARY_REGEX = "[yn]"; | ||
| private static final String BINARY_Y = "y"; | ||
|
|
||
| private final Scanner sc = new Scanner(System.in); | ||
|
|
||
|
|
@@ -16,6 +17,18 @@ public List<String> readPlayers() { | |
| return splitPlayerNames(userInput()); | ||
| } | ||
|
|
||
| public int readBettingAmount(String name) { | ||
| while (true) { | ||
| try { | ||
| System.out.printf("%s์ ๋ฐฐํ ๊ธ์ก์?%n", name); | ||
| String money = userInput(); | ||
| return Integer.parseInt(money); | ||
| } catch (IllegalArgumentException e) { | ||
| OutputView.printErrorMessage("์๋ชป๋ ์ ๋ ฅ์ ๋๋ค. ๋ค์ ์ ๋ ฅํด์ฃผ์ธ์."); | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+20
to
+30
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. ์ ๊ท ์
๋ ฅ ๋ถ๋ถ ์ ์์ฑํด์ฃผ์ ๊ฒ ํ์ธํ์ต๋๋ค.
์ด๋ฅผ ํด๊ฒฐํ๋ ค๋ฉด, Player ๋ฐ Money ๊ฐ์ฒด์ ์์ฑ์์ ์ด try-catch ๋ด๋ก ๋ค์ด๊ฐ์ผ ํฉ๋๋ค. 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. ์ถ๊ฐ๋ก, ์ง๊ธ InputView ๋ด์ ๊ฒ์ฆ ๋ก์ง์ด validate ๋ผ๋ ์ด๋ฆ์ด ๋ถ์ ๋ฉ์๋ ์ธ์์๋ ์ํ๋๋ ๊ฒ์ผ๋ก ๋ณด์ ๋๋ค.
๊ทธ๋์ ์ฐ์ , validation ๋ฉ์๋๋ค์ ๋ชจ๋ ๋ณ๊ฐ ๋ฉ์๋๋ก ๋ถ๋ฆฌํ์ผ๋ฉด ์ข๊ฒ ์ต๋๋ค ๊ทธ๋ฆฌ๊ณ ๋ฏธ์ ์งํ์ค์ ์ฌ์ ๊ฐ ์๋ค๋ฉด, ์ถ๊ฐ๋ก ์๋ ์ฌํญ์ ๊ตฌํํด์ฃผ์๋ฉด ์ข๊ฒ ์ต๋๋ค.
|
||
|
|
||
| public boolean readPlayerToHitUntilValid(String name) { | ||
| while (true) { | ||
| try { | ||
|
|
@@ -42,7 +55,7 @@ private boolean validateBinaryOption(String userInput) { | |
| throw new IllegalArgumentException("์๋ชป๋ ์ ๋ ฅ์ ๋๋ค. ๋ค์ ์ ๋ ฅํด์ฃผ์ธ์."); | ||
| } | ||
|
|
||
| return userInput.equals("y"); | ||
| return userInput.equals(BINARY_Y); | ||
| } | ||
|
|
||
| private String userInput() { | ||
|
|
||
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.
(int) (betAmount * BLACKJACK_MULTIPLIER) ๋ถ๋ถ์์ ์ฐ์ฐ ๋ฐ casting์ด ๋ชจ๋ ์ํ๋๊ณ ์๋๋ฐ์,
์ด๋ถ๋ถ์ ๋ณ๋ ๋ฉ์๋๋ก ๋ถ๋ฆฌํด๋ ์ข์๋ฏ ํฉ๋๋ค.