Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
cb6c3ad
docs(rentCar): 연료주입 기능요구사항 정의
hyoenju Feb 11, 2022
c0947c3
feat(main): Application 클래스 생성
hyoenju Feb 11, 2022
527c83f
feat(car): Car 추상 클래스 생성
hyoenju Feb 11, 2022
7b498fa
feat(car): Car 상속 클래스 생성
hyoenju Feb 11, 2022
9f5362d
feat: empty.txt 생성
hyoenju Feb 11, 2022
3498e4c
feat(company): RentCompany 클래스 생성
hyoenju Feb 11, 2022
ebcb132
test(company): RentCompany 클래스 생성 테스트
hyoenju Feb 11, 2022
d8cb870
feat(company): Car 객체 추가
hyoenju Feb 11, 2022
80dd03e
test(company): Car 객체 추가 테스트
hyoenju Feb 11, 2022
8cafc20
feat(car): getter 정의
hyoenju Feb 11, 2022
0687344
feat(company): 레포트 생성 기능 구현
hyoenju Feb 11, 2022
19286f8
test(company): 레포트 생성 기능 테스트
hyoenju Feb 11, 2022
d8cc419
docs: Update README.md
hyoenju Feb 11, 2022
cf06f9e
feat(main): main method 정의
hyoenju Feb 11, 2022
a06b01f
feat: empty.txt 삭제
hyoenju Feb 11, 2022
ebcaf92
remove: empty.txt 삭제
hyoenju Feb 11, 2022
f4c30c4
feat(view): inputView 클래스 생성
sk11ingyuhwang Feb 14, 2022
bfded44
remove: 불필요한 파일 삭제
sk11ingyuhwang Feb 14, 2022
e518287
test: 테스트 코드 작성
sk11ingyuhwang Feb 14, 2022
8c65c2f
feat(view): OutputView 클래스 생성, InputView 로직 수정
sk11ingyuhwang Feb 14, 2022
1fa56ff
feat(rentCar): enum으로 rentCar 타입 결정
sk11ingyuhwang Feb 14, 2022
e43b483
feat(Application): Application에서 Controller 호출
sk11ingyuhwang Feb 14, 2022
512b778
Merge branch 'step1' of https://github.com/hyoenju/java-blackjack int…
sk11ingyuhwang Feb 14, 2022
aeaab59
docs: 연료 주입 기능 구현 완료
sk11ingyuhwang Feb 14, 2022
bbfecb5
docs: 블랙잭 기능 명세서 작성
sk11ingyuhwang Feb 14, 2022
9146c02
feat(participant): 부모 클래스(Person) 생성
hyoenju Feb 15, 2022
875efb0
feat(participant): Participant 클래스 생성
hyoenju Feb 15, 2022
165f03c
test(participant): Participant 클래스 생성 테스트
hyoenju Feb 15, 2022
f1b626d
feat(card): cardNumber enum 클래스 생성
hyoenju Feb 15, 2022
fd92fd7
feat(card): cardPattern enum 클래스 생성
hyoenju Feb 15, 2022
25e555a
feat(card): Card 클래스 생성
hyoenju Feb 15, 2022
d45bbcf
feat(card): CardDeck 클래스 생성
hyoenju Feb 15, 2022
da370f9
test(card): CardDeck 클래스 생성 테스트
hyoenju Feb 15, 2022
75ab3a5
test(input): InputView 클래스 생성
hyoenju Feb 15, 2022
17c3904
feat(player): Participant -> Player 로 클래스 이름 변경
hyoenju Feb 15, 2022
bea5c54
feat(dealer): Dealer 클래스 생성
hyoenju Feb 15, 2022
c571bc5
style(player): Participant -> Player 로 클래스 이름 변경
hyoenju Feb 15, 2022
e79cfef
feat(card) : Card 생성, 추가를 위한 인터페이스 생성
hyoenju Feb 15, 2022
00f6f1d
feat(view) : InputView, OuputView 클래스 생성, 기능 구현
hyoenju Feb 15, 2022
dfa02ad
feat(card) : 게임 진행 위한 카드 객체 생성
hyoenju Feb 15, 2022
0646780
feat(dealer) : Dealer 클래스 생성
hyoenju Feb 15, 2022
13cc902
feat(main) : blackjack 기능 구현
hyoenju Feb 15, 2022
5403ec2
remove(test) : Remove Test
hyoenju Feb 15, 2022
6c4694f
refactor(view): InputView, OutputView 정적 팩토리 메소드로 변경
sk11ingyuhwang Feb 16, 2022
72bb5f9
feat(game): game 클래스 생성
sk11ingyuhwang Feb 16, 2022
db82882
refactor(view): OutputView 클래스 수정
sk11ingyuhwang Feb 16, 2022
089ff30
test(player): PlayerTest 코드 작성
sk11ingyuhwang Feb 16, 2022
181a0e4
test(dealer): DealerTest 코드 작성
sk11ingyuhwang Feb 16, 2022
d08b32a
test(dealer): Game 테스트 코드 작성
sk11ingyuhwang Feb 16, 2022
f86f7d4
refactor(main): Controller new 연산자로 생성
sk11ingyuhwang Feb 16, 2022
ec8a9c6
refactor(main): 구현된 기능 분리 작업
sk11ingyuhwang Feb 17, 2022
2e180aa
feat(card) : Card 객체 생성 && test(card)
hyoenju Feb 17, 2022
a7bb315
feat(cards) : Card 객체 생성, 계산 && test(card)
hyoenju Feb 17, 2022
57fb0f5
refactor(cardDeck) : 파일 위치 이동
hyoenju Feb 17, 2022
7f6b559
refactor(cardDeck) : 매직넘버 생성, final 정의
hyoenju Feb 17, 2022
db9372d
refactor(number) : 파일 위치 이동
hyoenju Feb 17, 2022
4f8dbcd
refactor(number) : final 정의
hyoenju Feb 17, 2022
aa60018
refactor(person): 파일 이동, final 정의
hyoenju Feb 17, 2022
1b5e63c
refactor(state): 공통 기능 분리
hyoenju Feb 17, 2022
bf9ab2c
delete & remove
hyoenju Feb 17, 2022
dccbe46
feat(view): 결과 출력 메소드 구현
hyoenju Feb 17, 2022
ce019ba
feat(winner): 게임 결과 메소드 구현 && test(winner)
hyoenju Feb 17, 2022
5558990
feat(control): Controller 기능 구현
hyoenju Feb 17, 2022
ffb0e10
refactor(game): game 기능 구현 && test(game)
hyoenju Feb 17, 2022
d04be38
refactor: reformat Codes
hyoenju Feb 17, 2022
aca0ca1
Update README.md
hyoenju Feb 17, 2022
b83a693
refactor: 디버깅용 print 삭제
hyoenju Feb 18, 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
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
# java-blackjack
# 연료 주입

## 기능요구사항

- [x] 렌터카는 Sonata 2대, Avante 1대, K5 2대로 총 5대의 차량을 보유하고 있다.
- [x] 차량 별 연비 정보를 저장하고 있다.
- Sonata : 10km/리터
- Avante : 15km/리터
- K5 : 13km/리터)
- 차량 별 보유 대수를 저장하고 있다.
- [x] 고객은 여행할 목적지의 대략적인 이동거리를 입력 받는다.
- [x] 이 이동거리를 활용해 차량 별로 필요한 연료를 주입한다.
- [x] 차량 별로 주입해야 할 연료량을 확인할 수 있는 보고서를 생성한다.
- [x] 보고서를 출력한다.

# java-blackjack

- [x] 게임에 참여할 사람의 이름을 입력하세요.
- [x] 게임이 시작하면 참여한 사람과 딜러는 카드 2개씩을 받는다.
- [x] 카드 숫자를 기본으로 하며, 예외로 Ace는 1 또는 11로 계산할 수 있으며, King, Queen, Jack은 각각 10으로 계산한다.
- [x] 게임에 참여한 사람 순서대로 카드를 더 받을지 물어보고 카드를 제공한다. 21점을 넘지 않을 경우 원한다면 얼마든지 카드를 계속 뽑을 수 있다. 단, 여기서 카드의 합은 21점을 넘어가지 않는다.
- [x] 게임에 딜러도 기존에 받았던 카드들의 합이 16점 이하이면 한장의 카드를 더 받아야 한다. 17점 이상이면 추가로 받을 수 없다.
- [x] 블랙잭 게임은 딜러와 플레이어 중 카드의 합이 21점 또는 21점에 가장 가까운 숫자를 가지는 쪽이 이기는 게임이다.
- [x] 블랙잭 게임을 완료한 후 각 플레이어별로 승패를 출력한다.
11 changes: 11 additions & 0 deletions src/main/java/blackjack/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package blackjack;

import blackjack.controller.Controller;

public class Application {

public static void main(String[] args) {
Controller controller = new Controller();
controller.run();
}
}
65 changes: 65 additions & 0 deletions src/main/java/blackjack/controller/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package blackjack.controller;

import blackjack.domain.Game;
import blackjack.domain.Winner;
import blackjack.domain.card.CardDeck;
import blackjack.domain.player.Player;
import blackjack.domain.state.Gameable;
import blackjack.domain.state.State;
import blackjack.view.InputView;
import blackjack.view.OutputView;

public class Controller {

private static void initGame(Game game) {
OutputView.printStartMessage(game);
OutputView.printDealerCard(game.getDealer());
OutputView.printPlayerCard(game.getPlayers());
}

public void run() {
Game game = new Game(InputView.inputPlayers());

initGame(game);
playGame(game);
finishGame(game);
}

private void playGame(Game game) {
game.getPlayers().forEach(this::receive);
if (game.giveCardToDealer()) {
OutputView.printMessageToGiveCardToDealer();
}
}

private void finishGame(Game game) {
Winner winner = new Winner(game);

OutputView.printGameResults(game.getDealer(), game.getPlayers());

OutputView.printGameWinOrLose(game.getDealer(), winner.calculateDealerGameResult());
game.getPlayers().forEach(
player -> OutputView.printGameWinOrLose(
player, winner.calculatePlayerGameResult(player)
)
);
}

public void receive(Player player) {
Gameable gameable = player.getCards();
String yesOrNo = "";
do {
yesOrNo = InputView.inputYesOrNo(player.getName());
if (yesOrNo.equals("y")) {
gameable.addCard(CardDeck.pop());
OutputView.printCurrentCardsState(player.getName(), player.getCards());
gameable = gameable.judge();
}
if (yesOrNo.equals("n")) {
gameable = new State(gameable.cards(), false);
}
} while (gameable.isEnd());
}
Comment on lines +48 to +62

Choose a reason for hiding this comment

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

함수(또는 메서드)의 길이가 10라인을 넘어가지 않도록 구현한다.
요구사항에 맞도록 구현해보면 어떨까요?



}
64 changes: 64 additions & 0 deletions src/main/java/blackjack/domain/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package blackjack.domain;

import blackjack.domain.card.CardDeck;
import blackjack.domain.player.Dealer;
import blackjack.domain.player.Player;
import blackjack.domain.state.State;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Game {

private static final int PASS_CARD_NUMBER = 2;
private static final String DEALER_NAME = "딜러";
private final static int DEALER_THRESHOLD = 16;

private final List<Player> players;

Choose a reason for hiding this comment

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

players를 일급컬렉션으로 분리해주는건 어떨까요?
책임을 조금 더 분리할 수 있을 것 같습니다.

private final Dealer dealer;

public Game(Dealer dealer, List<Player> players) {
this.players = players;
this.dealer = dealer;
}

public Game(List<String> playerNames) {
this(new Dealer(DEALER_NAME, handOutCards()), playerNames.stream()
.map(Game::createPlayer)
.collect(Collectors.toList()));
}

private static Player createPlayer(String name) {
return new Player(name, handOutCards());
}

private static State handOutCards() {
return new State(CardDeck.pop(PASS_CARD_NUMBER), true);
}

public boolean giveCardToDealer() {
if (dealer.getCards().cards().sumScore() <= DEALER_THRESHOLD) {

Choose a reason for hiding this comment

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

디미터 법칙을 위반하고 있네요.
dealer.getCards().cards().sumScore() <= DEALER_THRESHOLD 이 부분에 대한 책임을 넘겨주고 결과만 받아서 사용하도록 하면 어떨까요?

dealer.getCards().addCard(CardDeck.pop());
return true;
}
return false;
}

public int getTotalScoreOfPlayer(Player player) {
return player.getCards().cards().sumScore();
}

public List<Integer> getScoresOfPlayers() {
return players.stream()
.mapToInt(this::getTotalScoreOfPlayer)
.boxed().collect(Collectors.toList());
}

public List<Player> getPlayers() {
return Collections.unmodifiableList(players);
}

public Dealer getDealer() {
return dealer;
}
}
57 changes: 57 additions & 0 deletions src/main/java/blackjack/domain/Winner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package blackjack.domain;

import blackjack.domain.player.Player;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Winner {

private final int BLACKJACK = 21;
private final Game game;

Choose a reason for hiding this comment

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

Winner 라는 클래스가 상태값으로 Game을 가지고 있네요.
Winner우승자들을 상태값으로 가지고 있는게 맞지 않을까요? ″̮

아니면 조금 더 적절한 이름으로 변경해주는건 어떨까요?
한번 고민해보면 좋을 것 같습니다.


public Winner(final Game game) {
this.game = game;
}

public List<Integer> calculateDealerGameResult() {
int dealerScore = game.getDealer().getCards().cards().sumScore();
return calculateGameResult(dealerScore);
}

public List<Integer> calculatePlayerGameResult(final Player player) {
int playerScore = player.getCards().cards().sumScore();
return calculateGameResult(playerScore);
}

private List<Integer> calculateGameResult(final int sourceScore) {
List<Integer> targetScores = game.getScoresOfPlayers().stream()
.map(this::convertZeroScore)
.collect(Collectors.toList());
return Arrays.asList(countWin(sourceScore, targetScores),
countLose(sourceScore, targetScores));
}

private int convertZeroScore(final int score) {
if (score > BLACKJACK) {
return 0;
}
return score;
}

private int countWin(final int sourceScore, final List<Integer> targetScores) {
if (sourceScore > BLACKJACK) {
return 0;
}
return Long.valueOf(targetScores.stream()
.filter(targetScore -> sourceScore > targetScore).count()).intValue();
}

private int countLose(final int sourceScore, final List<Integer> targetScores) {
if (sourceScore > BLACKJACK) {
return targetScores.size();
}
return Long.valueOf(targetScores.stream()
.filter(targetScore -> sourceScore < targetScore).count()).intValue();
}
}
21 changes: 21 additions & 0 deletions src/main/java/blackjack/domain/card/Card.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package blackjack.domain.card;

public class Card {

private final CardNumber cardNumber;
private final CardPattern cardPattern;

public Card(final CardNumber cardNumber, final CardPattern cardPattern) {
this.cardNumber = cardNumber;
this.cardPattern = cardPattern;
}

@Override
public String toString() {
return cardNumber.getName() + cardPattern.getName();
}

public CardNumber getCardNumber() {
return cardNumber;
}
}
35 changes: 35 additions & 0 deletions src/main/java/blackjack/domain/card/CardDeck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package blackjack.domain.card;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CardDeck {

private static final int FIRST_INDEX = 0;
private static List<Card> cards = new ArrayList<>();

static {
Arrays.stream(CardNumber.values()).forEach(
cardNumber -> Arrays.stream(CardPattern.values()).forEach(
cardPattern -> cards.add(new Card(cardNumber, cardPattern))
)
);
Collections.shuffle(cards);
}

public static Cards pop(final int count) {
List<Card> newCards = new ArrayList<>(cards.subList(FIRST_INDEX, count));
cards = cards.subList(count, cards.size() - count);
return new Cards(newCards);
}

public static Card pop() {
return cards.remove(FIRST_INDEX);
}

public static List<Card> getCards() {
return Collections.unmodifiableList(cards);
}
}
33 changes: 33 additions & 0 deletions src/main/java/blackjack/domain/card/CardNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package blackjack.domain.card;

public enum CardNumber {
ACE("A", 11),
TWO("2", 2),
THREE("3", 3),
FOUR("4", 4),
FIVE("5", 5),
SIX("6", 6),
SEVEN("7", 7),
EIGHT("8", 8),
NINE("9", 9),
JACK("J", 10),
QUEEN("Q", 10),
KING("K", 10);

private final String name;
private final int score;

CardNumber(final String name, final int score) {
this.name = name;
this.score = score;
}

public String getName() {
return name;
}

public int getScore() {
return score;
}

}
18 changes: 18 additions & 0 deletions src/main/java/blackjack/domain/card/CardPattern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package blackjack.domain.card;

public enum CardPattern {
DIAMOND("다이아몬드"),
CLOVER("클로버"),
HEART("하트"),
SPADE("스페이드");

private final String name;

CardPattern(final String name) {
this.name = name;
}

public String getName() {
return name;
}
}
46 changes: 46 additions & 0 deletions src/main/java/blackjack/domain/card/Cards.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package blackjack.domain.card;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Cards {

private final static int BLACKJACK = 21;
private final List<Card> cards;

public Cards(final List<Card> cards) {
this.cards = cards;
}

private static int getScoreToSum(final int score1, final int score2) {
if ((score2 == CardNumber.ACE.getScore()) && (score1 + CardNumber.ACE.getScore() > 21)) {
return 1;
}
return score2;
}

public void add(final Card card) {
cards.add(card);
}

public boolean sum() {
return sumScore() >= BLACKJACK;
}

public int sumScore() {
Comparator<Card> comparator = (a, b) -> b.getCardNumber().getScore() - a.getCardNumber()
.getScore();

List<Card> newCards = new ArrayList<>(cards);
newCards.sort(comparator);

return newCards.stream()
.mapToInt(card -> card.getCardNumber().getScore())
.reduce(0, (a, b) -> a + getScoreToSum(a, b));
}

public List<Card> getCards() {
return cards;
}
}
Loading