-
Notifications
You must be signed in to change notification settings - Fork 389
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단계 - 블랙잭 베팅] 리브(김민주) 미션 제출합니다. #695
Changes from 28 commits
39a9f08
1758848
48cf9fa
2dae152
3714c5f
4daf110
629f779
ebbbf67
351d619
1e0a610
0b7e558
4c98d73
ddffb0c
4e88564
8fed05d
a412a89
222eb48
40fccda
847eab2
8aee5df
fe6f360
20388a4
608df98
b758eaa
1a7335e
3ede65e
d4d04c4
784df14
eb4e010
1342326
de58ed6
cca88cf
9b85741
342fbd0
af6a018
797d6db
16acf7a
bf22dd7
98c12c1
d668349
ee98739
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package blackjack.dto; | ||
|
||
import blackjack.model.money.Money; | ||
import blackjack.model.participant.Player; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public record NameProfit(String name, int profit) { | ||
|
||
public static List<NameProfit> createNameProfits(final Map<Player, Money> playerProfit) { | ||
final List<NameProfit> nameProfits = new ArrayList<>(); | ||
for (Player player : playerProfit.keySet()) { | ||
final int profit = playerProfit.get(player).getMoney(); | ||
nameProfits.add(new NameProfit(player.getName(), profit)); | ||
} | ||
return nameProfits; | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package blackjack.model.money; | ||
|
||
import blackjack.model.participant.Player; | ||
import blackjack.model.result.ResultCommand; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
public class Bets { | ||
private final Map<Player, Money> bets; | ||
|
||
public Bets() { | ||
this.bets = new LinkedHashMap<>(); | ||
} | ||
|
||
public void addBet(final Player player, final int money) { | ||
validatePositiveMoney(money); | ||
bets.put(player, new Money(money)); | ||
} | ||
|
||
private void validatePositiveMoney(final int money) { | ||
if (money <= 0) { | ||
throw new IllegalArgumentException("0원 이하의 금액을 베팅할 수 없습니다."); | ||
} | ||
} | ||
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. Money 가 유효한 범위인지는 원시값 포장을 해두었으니 돈이 생성될 때 검증하는게 더 안전하지 않을까요? Bets 가 아닌 다른 곳에서 Money 를 생성하게되면 음수가 생성될 수도 있을 것 같아요~ 🤔 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. 패배했을 경우 -1 이기 때문에 열어두셨군요. 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. 베팅 머니와, 정산 머니를 나누어 |
||
|
||
public Map<Player, Money> calculatePlayersProfit(final Map<Player, ResultCommand> result) { | ||
final Map<Player, Money> playersProfit = new LinkedHashMap<>(); | ||
for (Player player : result.keySet()) { | ||
playersProfit.put(player, calculatePlayerProfit(result, player)); | ||
} | ||
return playersProfit; | ||
} | ||
|
||
private Money calculatePlayerProfit(final Map<Player, ResultCommand> result, final Player player) { | ||
final double multiplier = result.get(player).getRate(); | ||
return bets.get(player).multiply(multiplier); | ||
} | ||
|
||
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. 시그니처에 Map 을 넘겨주지 않고 rate 를 바로 넘겨주면 좋을 것 같아요. 계산이 필요한 플레이어 외에 다른 플레이어의 정보를 꺼내 올 수 있는 여지를 막도록요! ㅎㅎ 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. 바로 반영하였습니다! |
||
public Money calculateDealerProfit(final Map<Player, ResultCommand> result) { | ||
final Map<Player, Money> playersProfit = calculatePlayersProfit(result); | ||
final int playerTotalProfit = playersProfit.values().stream() | ||
.mapToInt(Money::getMoney) | ||
.sum(); | ||
return new Money(playerTotalProfit).multiply(-1); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package blackjack.model.money; | ||
|
||
import java.util.Objects; | ||
|
||
public class Money { | ||
private final int money; | ||
|
||
public Money(final int money) { | ||
this.money = money; | ||
} | ||
|
||
public Money multiply(final double multiplier) { | ||
return new Money((int) (money * multiplier)); | ||
} | ||
|
||
public int getMoney() { | ||
return money; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
Money money1 = (Money) o; | ||
return money == money1.money; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(money); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,16 @@ | |
|
||
public abstract class Participant { | ||
|
||
protected final Hand hand; | ||
protected Hand hand; | ||
|
||
public Participant() { | ||
this.hand = new Hand(); | ||
} | ||
|
||
public void receiveInitialCards(final List<Card> cards) { | ||
hand.add(cards); | ||
for (Card card : cards) { | ||
hand.add(card); | ||
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. 손에 카드를 추가하는 형태로 변경하셨군요 좋습니다 👍🏻 |
||
} | ||
} | ||
|
||
public void draw(final Card card) { | ||
|
@@ -33,6 +35,10 @@ public boolean isBust() { | |
return hand.isBust(); | ||
} | ||
|
||
public boolean isBlackJack() { | ||
return hand.isBlackJack(); | ||
} | ||
|
||
public boolean hasManyCardsThan(Participant other) { | ||
return hand.hasManyThan(other.hand); | ||
} | ||
|
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.
장부나 정산서 처럼 금액을 핸들링한다는 느낌의 네이밍이면 좋을 것 같아요 ㅎㅎ
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.
ProfitStatement로 변경하였습니다. 좋은 의견 감사합니다!😊