From e8dba5ff16e3273494938b5ff1310afc91b09354 Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Tue, 10 Mar 2020 16:23:15 +0900 Subject: [PATCH 01/31] =?UTF-8?q?feat=20:=20=EC=83=81=EC=86=8D,=20interfac?= =?UTF-8?q?e=20=ED=95=99=EC=8A=B5=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=9A=94?= =?UTF-8?q?=EA=B5=AC=EC=82=AC=ED=95=AD=201=EB=8B=A8=EA=B3=84=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/empty.txt | 0 src/main/java/study/RentCompany.java | 37 +++++++++++++++++++ src/main/java/study/car/Avante.java | 29 +++++++++++++++ src/main/java/study/car/Car.java | 34 +++++++++++++++++ src/main/java/study/car/K5.java | 29 +++++++++++++++ src/main/java/study/car/Sonata.java | 29 +++++++++++++++ src/test/java/empty.txt | 0 src/test/java/study/RentCompanyTest.java | 47 ++++++++++++++++++++++++ src/test/java/study/car/AvanteTest.java | 12 ++++++ src/test/java/study/car/K5Test.java | 12 ++++++ src/test/java/study/car/SonataTest.java | 12 ++++++ 11 files changed, 241 insertions(+) delete mode 100644 src/main/java/empty.txt create mode 100644 src/main/java/study/RentCompany.java create mode 100644 src/main/java/study/car/Avante.java create mode 100644 src/main/java/study/car/Car.java create mode 100644 src/main/java/study/car/K5.java create mode 100644 src/main/java/study/car/Sonata.java delete mode 100644 src/test/java/empty.txt create mode 100644 src/test/java/study/RentCompanyTest.java create mode 100644 src/test/java/study/car/AvanteTest.java create mode 100644 src/test/java/study/car/K5Test.java create mode 100644 src/test/java/study/car/SonataTest.java diff --git a/src/main/java/empty.txt b/src/main/java/empty.txt deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/main/java/study/RentCompany.java b/src/main/java/study/RentCompany.java new file mode 100644 index 0000000000..a36e2b1390 --- /dev/null +++ b/src/main/java/study/RentCompany.java @@ -0,0 +1,37 @@ +package study; + +import java.util.ArrayList; +import java.util.List; + +import study.car.Car; + +public class RentCompany { + private static final String NEWLINE = System.getProperty("line.separator"); + + private final List cars; + + private RentCompany() { + this.cars = new ArrayList<>(); + } + + public static RentCompany create() { + return new RentCompany(); + } + + public void addCar(Car car) { + cars.add(car); + } + + public List getCars() { + return cars; + } + + public String generateReport() { + StringBuilder carsReport = new StringBuilder(); + for (Car car : cars) { + carsReport.append(car); + carsReport.append(NEWLINE); + } + return carsReport.toString(); + } +} diff --git a/src/main/java/study/car/Avante.java b/src/main/java/study/car/Avante.java new file mode 100644 index 0000000000..6f4135fe0a --- /dev/null +++ b/src/main/java/study/car/Avante.java @@ -0,0 +1,29 @@ +package study.car; + +public class Avante extends Car { + public Avante(int tripDistance) { + this.name = "Avante"; + this.distancePerLiter = 15; + this.tripDistance = tripDistance; + } + + @Override + double getDistancePerLiter() { + return distancePerLiter; + } + + @Override + double getTripDistance() { + return tripDistance; + } + + @Override + String getName() { + return name; + } + + @Override + double getChargeQuantity() { + return super.getChargeQuantity(); + } +} diff --git a/src/main/java/study/car/Car.java b/src/main/java/study/car/Car.java new file mode 100644 index 0000000000..38680fc0e1 --- /dev/null +++ b/src/main/java/study/car/Car.java @@ -0,0 +1,34 @@ +package study.car; + +public abstract class Car { + protected String name; + protected int distancePerLiter; + protected int tripDistance; + + /** + * 리터당 이동 거리. 즉, 연비 + */ + abstract double getDistancePerLiter(); + + /** + * 여행하려는 거리 + */ + abstract double getTripDistance(); + + /** + * 차종의 이름 + */ + abstract String getName(); + + /** + * 주입해야할 연료량을 구한다. + */ + double getChargeQuantity() { + return getTripDistance() / getDistancePerLiter(); + } + + @Override + public String toString() { + return getName() + " : " + (int)getChargeQuantity() + "리터"; + } +} diff --git a/src/main/java/study/car/K5.java b/src/main/java/study/car/K5.java new file mode 100644 index 0000000000..e66e8a5487 --- /dev/null +++ b/src/main/java/study/car/K5.java @@ -0,0 +1,29 @@ +package study.car; + +public class K5 extends Car { + public K5(int tripDistance) { + this.name = "K5"; + this.distancePerLiter = 13; + this.tripDistance = tripDistance; + } + + @Override + double getDistancePerLiter() { + return distancePerLiter; + } + + @Override + double getTripDistance() { + return tripDistance; + } + + @Override + String getName() { + return name; + } + + @Override + double getChargeQuantity() { + return super.getChargeQuantity(); + } +} diff --git a/src/main/java/study/car/Sonata.java b/src/main/java/study/car/Sonata.java new file mode 100644 index 0000000000..d172b79dad --- /dev/null +++ b/src/main/java/study/car/Sonata.java @@ -0,0 +1,29 @@ +package study.car; + +public class Sonata extends Car { + public Sonata(int tripDistance) { + this.name = "Sonata"; + this.distancePerLiter = 10; + this.tripDistance = tripDistance; + } + + @Override + double getDistancePerLiter() { + return distancePerLiter; + } + + @Override + double getTripDistance() { + return tripDistance; + } + + @Override + String getName() { + return name; + } + + @Override + double getChargeQuantity() { + return super.getChargeQuantity(); + } +} diff --git a/src/test/java/empty.txt b/src/test/java/empty.txt deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/test/java/study/RentCompanyTest.java b/src/test/java/study/RentCompanyTest.java new file mode 100644 index 0000000000..2674a2d478 --- /dev/null +++ b/src/test/java/study/RentCompanyTest.java @@ -0,0 +1,47 @@ +package study; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import study.car.Avante; +import study.car.K5; +import study.car.Sonata; + +public class RentCompanyTest { + private static final String NEWLINE = System.getProperty("line.separator"); + + @DisplayName("create_RentCompany 인스턴스 생성") + @Test + void create_GenerateInstance() { + assertThat(RentCompany.create()).isInstanceOf(RentCompany.class); + } + + @Test + void addCar_CarInstance_InputToCarsList() { + RentCompany company = RentCompany.create(); + K5 car = new K5(100); + company.addCar(car); + assertThat(company.getCars()).contains(car); + } + + @Test + public void report() throws Exception { + RentCompany company = RentCompany.create(); // factory method를 사용해 생성 + company.addCar(new Sonata(150)); + company.addCar(new K5(260)); + company.addCar(new Sonata(120)); + company.addCar(new Avante(300)); + company.addCar(new K5(390)); + + String report = company.generateReport(); + assertThat(report).isEqualTo( + "Sonata : 15리터" + NEWLINE + + "K5 : 20리터" + NEWLINE + + "Sonata : 12리터" + NEWLINE + + "Avante : 20리터" + NEWLINE + + "K5 : 30리터" + NEWLINE + ); + } +} diff --git a/src/test/java/study/car/AvanteTest.java b/src/test/java/study/car/AvanteTest.java new file mode 100644 index 0000000000..0cc1bfa6d5 --- /dev/null +++ b/src/test/java/study/car/AvanteTest.java @@ -0,0 +1,12 @@ +package study.car; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class AvanteTest { + @Test + void Avante_InputTripDistance_GenerateInstance() { + assertThat(new Avante(100)).isInstanceOf(Avante.class); + } +} diff --git a/src/test/java/study/car/K5Test.java b/src/test/java/study/car/K5Test.java new file mode 100644 index 0000000000..80caac58ad --- /dev/null +++ b/src/test/java/study/car/K5Test.java @@ -0,0 +1,12 @@ +package study.car; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class K5Test { + @Test + void K5_InputTripDistance_GenerateInstance() { + assertThat(new K5(100)).isInstanceOf(K5.class); + } +} diff --git a/src/test/java/study/car/SonataTest.java b/src/test/java/study/car/SonataTest.java new file mode 100644 index 0000000000..7a542a9c19 --- /dev/null +++ b/src/test/java/study/car/SonataTest.java @@ -0,0 +1,12 @@ +package study.car; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class SonataTest { + @Test + void Sonata_InputTripDistance_GenerateInstance() { + assertThat(new Sonata(100)).isInstanceOf(Sonata.class); + } +} From 370dc4ce5f033d9cc22a5c8703639d90f4708d24 Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Tue, 10 Mar 2020 17:01:46 +0900 Subject: [PATCH 02/31] =?UTF-8?q?feat=20:=20=EC=83=81=EC=86=8D,=20interfac?= =?UTF-8?q?e=20=ED=95=99=EC=8A=B5=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=9A=94?= =?UTF-8?q?=EA=B5=AC=EC=82=AC=ED=95=AD=202=EB=8B=A8=EA=B3=84=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/study/RentCompany.java | 15 +++++---- src/main/java/study/car/Avante.java | 22 +----------- src/main/java/study/car/Car.java | 37 ++++++++------------- src/main/java/study/car/ChargeQuantity.java | 7 ++++ src/main/java/study/car/K5.java | 22 +----------- src/main/java/study/car/Sonata.java | 22 +----------- 6 files changed, 33 insertions(+), 92 deletions(-) create mode 100644 src/main/java/study/car/ChargeQuantity.java diff --git a/src/main/java/study/RentCompany.java b/src/main/java/study/RentCompany.java index a36e2b1390..6b423965ff 100644 --- a/src/main/java/study/RentCompany.java +++ b/src/main/java/study/RentCompany.java @@ -3,12 +3,12 @@ import java.util.ArrayList; import java.util.List; -import study.car.Car; +import study.car.ChargeQuantity; public class RentCompany { private static final String NEWLINE = System.getProperty("line.separator"); - private final List cars; + private final List cars; private RentCompany() { this.cars = new ArrayList<>(); @@ -18,18 +18,21 @@ public static RentCompany create() { return new RentCompany(); } - public void addCar(Car car) { + public void addCar(ChargeQuantity car) { cars.add(car); } - public List getCars() { + public List getCars() { return cars; } public String generateReport() { StringBuilder carsReport = new StringBuilder(); - for (Car car : cars) { - carsReport.append(car); + for (ChargeQuantity car : cars) { + carsReport.append(car.getName()) + .append(" : ") + .append((int)car.getChargeQuantity()) + .append("리터"); carsReport.append(NEWLINE); } return carsReport.toString(); diff --git a/src/main/java/study/car/Avante.java b/src/main/java/study/car/Avante.java index 6f4135fe0a..99f392b2e6 100644 --- a/src/main/java/study/car/Avante.java +++ b/src/main/java/study/car/Avante.java @@ -1,29 +1,9 @@ package study.car; public class Avante extends Car { - public Avante(int tripDistance) { + public Avante(double tripDistance) { this.name = "Avante"; this.distancePerLiter = 15; this.tripDistance = tripDistance; } - - @Override - double getDistancePerLiter() { - return distancePerLiter; - } - - @Override - double getTripDistance() { - return tripDistance; - } - - @Override - String getName() { - return name; - } - - @Override - double getChargeQuantity() { - return super.getChargeQuantity(); - } } diff --git a/src/main/java/study/car/Car.java b/src/main/java/study/car/Car.java index 38680fc0e1..ed8244a904 100644 --- a/src/main/java/study/car/Car.java +++ b/src/main/java/study/car/Car.java @@ -1,34 +1,25 @@ package study.car; -public abstract class Car { +public class Car implements ChargeQuantity { protected String name; - protected int distancePerLiter; - protected int tripDistance; + protected double distancePerLiter; + protected double tripDistance; - /** - * 리터당 이동 거리. 즉, 연비 - */ - abstract double getDistancePerLiter(); - - /** - * 여행하려는 거리 - */ - abstract double getTripDistance(); + protected double getDistancePerLiter() { + return distancePerLiter; + } - /** - * 차종의 이름 - */ - abstract String getName(); + protected double getTripDistance() { + return tripDistance; + } - /** - * 주입해야할 연료량을 구한다. - */ - double getChargeQuantity() { - return getTripDistance() / getDistancePerLiter(); + @Override + public String getName() { + return name; } @Override - public String toString() { - return getName() + " : " + (int)getChargeQuantity() + "리터"; + public double getChargeQuantity() { + return getTripDistance() / getDistancePerLiter(); } } diff --git a/src/main/java/study/car/ChargeQuantity.java b/src/main/java/study/car/ChargeQuantity.java new file mode 100644 index 0000000000..7d00616bd1 --- /dev/null +++ b/src/main/java/study/car/ChargeQuantity.java @@ -0,0 +1,7 @@ +package study.car; + +public interface ChargeQuantity { + String getName(); + + double getChargeQuantity(); +} diff --git a/src/main/java/study/car/K5.java b/src/main/java/study/car/K5.java index e66e8a5487..340e40e21c 100644 --- a/src/main/java/study/car/K5.java +++ b/src/main/java/study/car/K5.java @@ -1,29 +1,9 @@ package study.car; public class K5 extends Car { - public K5(int tripDistance) { + public K5(double tripDistance) { this.name = "K5"; this.distancePerLiter = 13; this.tripDistance = tripDistance; } - - @Override - double getDistancePerLiter() { - return distancePerLiter; - } - - @Override - double getTripDistance() { - return tripDistance; - } - - @Override - String getName() { - return name; - } - - @Override - double getChargeQuantity() { - return super.getChargeQuantity(); - } } diff --git a/src/main/java/study/car/Sonata.java b/src/main/java/study/car/Sonata.java index d172b79dad..a4a05aa009 100644 --- a/src/main/java/study/car/Sonata.java +++ b/src/main/java/study/car/Sonata.java @@ -1,29 +1,9 @@ package study.car; public class Sonata extends Car { - public Sonata(int tripDistance) { + public Sonata(double tripDistance) { this.name = "Sonata"; this.distancePerLiter = 10; this.tripDistance = tripDistance; } - - @Override - double getDistancePerLiter() { - return distancePerLiter; - } - - @Override - double getTripDistance() { - return tripDistance; - } - - @Override - String getName() { - return name; - } - - @Override - double getChargeQuantity() { - return super.getChargeQuantity(); - } } From 8c679ad6cab5bb1297ede23ea957b7f04f427b4d Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Tue, 10 Mar 2020 18:00:18 +0900 Subject: [PATCH 03/31] =?UTF-8?q?docs=20:=20=EA=B5=AC=ED=98=84=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EB=AA=A9=EB=A1=9D=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/README.md b/README.md index 3bcfc25784..fb3ef008f4 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,47 @@ # java-blackjack + 블랙잭 게임 미션 저장소 +
+ +## 구현 기능 목록 + +- 참여할 플레이어들의 이름을 입력받는 기능 + + - 이름을 쉼표를 기준으로 나누는 기능 + +- 유저(딜러, 플레이어)들에게 카드를 2장씩 나누어줬다는 메시지를 출력하는 기능 + +- 유저들이 받은 카드를 출력하는 기능 + + - 딜러는 1장, 플레이어는 2장을 출력하는 기능 + +- 각각의 플레이어에 대한 점수를 계산하는 기능 + + - 각각의 플레이어에게 카드를 더 받을지 여부를 입력받는 기능 + + - 카드를 받은 후, 현재 보유한 카드를 출력하는 기능 + + - 점수가 21이 초과하는지 확인하는 기능 + + - Ace의 경우 1점 또는 11점으로 계산이 가능한 기능 + + - King, Queen, Jack의 경우 10점으로 계산하는 기능 + +- 딜러에 대한 점수를 계산하는 기능 + + - 딜러의 점수가 16점 이하이면 한장의 카드를 추가로 받는 기능 + + - 딜러의 점수가 17점 이상이면 카드를 추가로 받지 못하는 기능 + +- 유저(딜러, 플레이어)들의 최종 카드 상태와 점수를 출력하는 기능 + +- 유저(딜러, 플레이어)들의 최종 결과를 출력하는 기능 + + - 딜러보다 플레이어가 점수가 높으면 승, 낮으면 패로 계산하는 기능 + +
+ ## 우아한테크코스 코드리뷰 + * [온라인 코드 리뷰 과정](https://github.com/woowacourse/woowacourse-docs/blob/master/maincourse/README.md) \ No newline at end of file From 7c3b1895ae36ae057bfbabbbaafd6a47f21b179d Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Tue, 10 Mar 2020 20:26:21 +0900 Subject: [PATCH 04/31] =?UTF-8?q?feat=20:=20Card=20=EC=9D=B8=EC=8A=A4?= =?UTF-8?q?=ED=84=B4=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Symbol 열거형 인스턴스 구현. 2. Type 열거형 인스턴스 구현. --- src/main/java/blackjack/domain/card/Card.java | 11 +++++++++ .../java/blackjack/domain/card/Symbol.java | 23 +++++++++++++++++++ src/main/java/blackjack/domain/card/Type.java | 8 +++++++ .../java/blackjack/domain/card/CardTest.java | 12 ++++++++++ 4 files changed, 54 insertions(+) create mode 100644 src/main/java/blackjack/domain/card/Card.java create mode 100644 src/main/java/blackjack/domain/card/Symbol.java create mode 100644 src/main/java/blackjack/domain/card/Type.java create mode 100644 src/test/java/blackjack/domain/card/CardTest.java diff --git a/src/main/java/blackjack/domain/card/Card.java b/src/main/java/blackjack/domain/card/Card.java new file mode 100644 index 0000000000..0dc31e82fc --- /dev/null +++ b/src/main/java/blackjack/domain/card/Card.java @@ -0,0 +1,11 @@ +package blackjack.domain.card; + +public class Card { + private final Symbol symbol; + private final Type type; + + public Card(Symbol symbol, Type type) { + this.symbol = symbol; + this.type = type; + } +} diff --git a/src/main/java/blackjack/domain/card/Symbol.java b/src/main/java/blackjack/domain/card/Symbol.java new file mode 100644 index 0000000000..50df4c8428 --- /dev/null +++ b/src/main/java/blackjack/domain/card/Symbol.java @@ -0,0 +1,23 @@ +package blackjack.domain.card; + +public enum Symbol { + ACE(1), + TWO(2), + THREE(3), + FOUR(4), + FIVE(5), + SIX(6), + SEVEN(7), + EIGHT(8), + NINE(9), + TEN(10), + JACK(10), + QUEEN(10), + KING(10); + + private final int symbol; + + Symbol(int symbol) { + this.symbol = symbol; + } +} diff --git a/src/main/java/blackjack/domain/card/Type.java b/src/main/java/blackjack/domain/card/Type.java new file mode 100644 index 0000000000..a8810a4b18 --- /dev/null +++ b/src/main/java/blackjack/domain/card/Type.java @@ -0,0 +1,8 @@ +package blackjack.domain.card; + +public enum Type { + SPADE, + HEART, + CLUB, + DIAMOND; +} diff --git a/src/test/java/blackjack/domain/card/CardTest.java b/src/test/java/blackjack/domain/card/CardTest.java new file mode 100644 index 0000000000..9503bb2069 --- /dev/null +++ b/src/test/java/blackjack/domain/card/CardTest.java @@ -0,0 +1,12 @@ +package blackjack.domain.card; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class CardTest { + @Test + void Card_SymbolAndType_GenerateInstance() { + assertThat(new Card(Symbol.ACE, Type.CLUB)).isInstanceOf(Card.class); + } +} From 97c52120735dcc9b64d950d74cbe30eb77097e3b Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Wed, 11 Mar 2020 13:08:34 +0900 Subject: [PATCH 05/31] =?UTF-8?q?feat=20:=20CardRepository=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/domain/card/CardRepository.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/main/java/blackjack/domain/card/CardRepository.java diff --git a/src/main/java/blackjack/domain/card/CardRepository.java b/src/main/java/blackjack/domain/card/CardRepository.java new file mode 100644 index 0000000000..5e8a37c80a --- /dev/null +++ b/src/main/java/blackjack/domain/card/CardRepository.java @@ -0,0 +1,31 @@ +package blackjack.domain.card; + +import static java.util.stream.Collectors.*; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Stream; + +public class CardRepository { + private static final List CARDS; + + static { + CARDS = create(); + } + + private static List create() { + return Arrays.stream(Symbol.values()) + .flatMap(CardRepository::createByType) + .collect(collectingAndThen(toList(), Collections::unmodifiableList)); + } + + private static Stream createByType(Symbol symbol) { + return Arrays.stream(Type.values()) + .map(type -> new Card(symbol, type)); + } + + public static List cards() { + return CARDS; + } +} From 557148bd99657cd4043f1e294cd4957be13ed66c Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Wed, 11 Mar 2020 13:51:27 +0900 Subject: [PATCH 06/31] =?UTF-8?q?feat=20:=20Deck=20=EC=9D=B8=EC=8A=A4?= =?UTF-8?q?=ED=84=B4=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/card/Deck.java | 28 +++++++++++++++++++ .../java/blackjack/domain/card/DeckTest.java | 19 +++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/main/java/blackjack/domain/card/Deck.java create mode 100644 src/test/java/blackjack/domain/card/DeckTest.java diff --git a/src/main/java/blackjack/domain/card/Deck.java b/src/main/java/blackjack/domain/card/Deck.java new file mode 100644 index 0000000000..3b1552a0a3 --- /dev/null +++ b/src/main/java/blackjack/domain/card/Deck.java @@ -0,0 +1,28 @@ +package blackjack.domain.card; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class Deck { + private static final int TOP = 0; + + private List cards; + + public Deck() { + this.cards = refill(CardRepository.cards()); + } + + private List refill(List cards) { + List refillCards = new ArrayList<>(cards); + Collections.shuffle(refillCards); + return refillCards; + } + + public Card draw() { + if (cards.isEmpty()) { + cards = refill(CardRepository.cards()); + } + return cards.remove(TOP); + } +} diff --git a/src/test/java/blackjack/domain/card/DeckTest.java b/src/test/java/blackjack/domain/card/DeckTest.java new file mode 100644 index 0000000000..40aa435709 --- /dev/null +++ b/src/test/java/blackjack/domain/card/DeckTest.java @@ -0,0 +1,19 @@ +package blackjack.domain.card; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class DeckTest { + @Test + void Deck_GetCardsFromCardRepository_GenerateInstance() { + assertThat(new Deck()).isInstanceOf(Deck.class); + } + + @Test + void draw_UserDrawCardFromDeck_ReturnCard() { + Deck deck = new Deck(); + + assertThat(deck.draw()).isInstanceOf(Card.class); + } +} From 2e2526131a202fbbc0a1101497114245a96a3e39 Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Wed, 11 Mar 2020 16:25:51 +0900 Subject: [PATCH 07/31] =?UTF-8?q?feat=20:=20Hand=20=EC=9D=B8=EC=8A=A4?= =?UTF-8?q?=ED=84=B4=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/card/Hand.java | 16 ++++++++++++++ .../java/blackjack/domain/card/HandTest.java | 21 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/main/java/blackjack/domain/card/Hand.java create mode 100644 src/test/java/blackjack/domain/card/HandTest.java diff --git a/src/main/java/blackjack/domain/card/Hand.java b/src/main/java/blackjack/domain/card/Hand.java new file mode 100644 index 0000000000..5dce97df99 --- /dev/null +++ b/src/main/java/blackjack/domain/card/Hand.java @@ -0,0 +1,16 @@ +package blackjack.domain.card; + +import java.util.ArrayList; +import java.util.List; + +public class Hand { + private final List cards; + + public Hand() { + this.cards = new ArrayList<>(); + } + + public void add(Card card) { + cards.add(card); + } +} diff --git a/src/test/java/blackjack/domain/card/HandTest.java b/src/test/java/blackjack/domain/card/HandTest.java new file mode 100644 index 0000000000..8d61c9db89 --- /dev/null +++ b/src/test/java/blackjack/domain/card/HandTest.java @@ -0,0 +1,21 @@ +package blackjack.domain.card; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class HandTest { + @Test + void Hand_GenerateInstance() { + assertThat(new Hand()).isInstanceOf(Hand.class); + } + + @Test + void add_Card_AddCardToHand() { + Hand hand = new Hand(); + Card card = new Card(Symbol.ACE, Type.CLUB); + hand.add(card); + + assertThat(hand).extracting("cards").asList().contains(card); + } +} From 45e8ff69ae023263b9fbec254f07b00c4b614d10 Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Wed, 11 Mar 2020 16:59:13 +0900 Subject: [PATCH 08/31] =?UTF-8?q?feat=20:=20User=20=EC=9D=B8=EC=8A=A4?= =?UTF-8?q?=ED=84=B4=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Hand 인스턴스의 add 메서드 오버로딩. --- src/main/java/blackjack/domain/card/Hand.java | 4 ++ .../domain/user/InvalidUserException.java | 10 ++++ src/main/java/blackjack/domain/user/User.java | 44 ++++++++++++++++++ .../java/blackjack/domain/card/HandTest.java | 12 +++++ .../java/blackjack/domain/user/UserTest.java | 46 +++++++++++++++++++ 5 files changed, 116 insertions(+) create mode 100644 src/main/java/blackjack/domain/user/InvalidUserException.java create mode 100644 src/main/java/blackjack/domain/user/User.java create mode 100644 src/test/java/blackjack/domain/user/UserTest.java diff --git a/src/main/java/blackjack/domain/card/Hand.java b/src/main/java/blackjack/domain/card/Hand.java index 5dce97df99..506f6c6987 100644 --- a/src/main/java/blackjack/domain/card/Hand.java +++ b/src/main/java/blackjack/domain/card/Hand.java @@ -13,4 +13,8 @@ public Hand() { public void add(Card card) { cards.add(card); } + + public void add(List cards) { + this.cards.addAll(cards); + } } diff --git a/src/main/java/blackjack/domain/user/InvalidUserException.java b/src/main/java/blackjack/domain/user/InvalidUserException.java new file mode 100644 index 0000000000..3f93026248 --- /dev/null +++ b/src/main/java/blackjack/domain/user/InvalidUserException.java @@ -0,0 +1,10 @@ +package blackjack.domain.user; + +public class InvalidUserException extends IllegalArgumentException { + public static final String NULL_OR_EMPTY = "유효하지 않은 유저의 이름입니다."; + public static final String INVALID_DRAW_NUMBER = "뽑을 수 있는 카드 수가 유효하지 않습니다."; + + public InvalidUserException(String s) { + super(s); + } +} diff --git a/src/main/java/blackjack/domain/user/User.java b/src/main/java/blackjack/domain/user/User.java new file mode 100644 index 0000000000..48f2cf7cd6 --- /dev/null +++ b/src/main/java/blackjack/domain/user/User.java @@ -0,0 +1,44 @@ +package blackjack.domain.user; + +import static java.util.stream.Collectors.*; + +import java.util.List; +import java.util.Objects; +import java.util.stream.IntStream; + +import blackjack.domain.card.Card; +import blackjack.domain.card.Deck; +import blackjack.domain.card.Hand; + +public class User { + private static final int DRAW_LOWER_BOUND = 1; + + private final String name; + private final Hand hand; + + public User(String name) { + validate(name); + this.name = name; + this.hand = new Hand(); + } + + private void validate(String name) { + if (Objects.isNull(name) || name.isEmpty()) { + throw new InvalidUserException(InvalidUserException.NULL_OR_EMPTY); + } + } + + public void draw(Deck deck, int drawNumber) { + validateDrawNumber(drawNumber); + List cards = IntStream.range(0, drawNumber) + .mapToObj(e -> deck.draw()) + .collect(toList()); + hand.add(cards); + } + + private void validateDrawNumber(int drawNumber) { + if (drawNumber < DRAW_LOWER_BOUND) { + throw new InvalidUserException(InvalidUserException.INVALID_DRAW_NUMBER); + } + } +} diff --git a/src/test/java/blackjack/domain/card/HandTest.java b/src/test/java/blackjack/domain/card/HandTest.java index 8d61c9db89..db9179b79d 100644 --- a/src/test/java/blackjack/domain/card/HandTest.java +++ b/src/test/java/blackjack/domain/card/HandTest.java @@ -2,6 +2,9 @@ import static org.assertj.core.api.Assertions.*; +import java.util.Arrays; +import java.util.List; + import org.junit.jupiter.api.Test; class HandTest { @@ -18,4 +21,13 @@ void add_Card_AddCardToHand() { assertThat(hand).extracting("cards").asList().contains(card); } + + @Test + void add_CardList_AddCardsToHand() { + Hand hand = new Hand(); + List cards = Arrays.asList(new Card(Symbol.ACE, Type.CLUB), new Card(Symbol.EIGHT, Type.DIAMOND)); + hand.add(cards); + + assertThat(hand).extracting("cards").asList().containsAll(cards); + } } diff --git a/src/test/java/blackjack/domain/user/UserTest.java b/src/test/java/blackjack/domain/user/UserTest.java new file mode 100644 index 0000000000..9ad1196396 --- /dev/null +++ b/src/test/java/blackjack/domain/user/UserTest.java @@ -0,0 +1,46 @@ +package blackjack.domain.user; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; + +import blackjack.domain.card.Card; +import blackjack.domain.card.Deck; + +class UserTest { + @Test + void User_InputUserName_GenerateInstance() { + assertThat(new User("user")).isInstanceOf(User.class); + } + + @ParameterizedTest + @NullAndEmptySource + void validate_InvalidUserName_InvalidUserExceptionThrown(String name) { + assertThatThrownBy(() -> new User(name)) + .isInstanceOf(InvalidUserException.class) + .hasMessage(InvalidUserException.NULL_OR_EMPTY); + } + + @Test + void draw_NumberOfCards_ReturnCardList() { + User user = new User("user"); + Deck deck = new Deck(); + user.draw(deck, 2); + + assertThat(user).extracting("hand.cards").asList() + .hasOnlyElementsOfType(Card.class) + .hasSize(2); + } + + @Test + void validateDrawNumber_InvalidNumberOfCards_InvalidUserExceptionThrown() { + User user = new User("user"); + Deck deck = new Deck(); + + assertThatThrownBy(() -> user.draw(deck, 0)) + .isInstanceOf(InvalidUserException.class) + .hasMessage(InvalidUserException.INVALID_DRAW_NUMBER); + } +} From 89c0b31545bf1283f0ef6e664e645855df026aca Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Wed, 11 Mar 2020 17:22:43 +0900 Subject: [PATCH 09/31] =?UTF-8?q?test=20:=20Deck=20=EC=9D=B8=EC=8A=A4?= =?UTF-8?q?=ED=84=B4=EC=8A=A4=EC=99=80=20User=20=EC=9D=B8=EC=8A=A4?= =?UTF-8?q?=ED=84=B4=EC=8A=A4=EC=9D=98=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/card/DeckTest.java | 21 +++++++++++++++++++ .../java/blackjack/domain/user/UserTest.java | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/test/java/blackjack/domain/card/DeckTest.java b/src/test/java/blackjack/domain/card/DeckTest.java index 40aa435709..a137e08757 100644 --- a/src/test/java/blackjack/domain/card/DeckTest.java +++ b/src/test/java/blackjack/domain/card/DeckTest.java @@ -2,7 +2,12 @@ import static org.assertj.core.api.Assertions.*; +import java.util.stream.Stream; + import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; class DeckTest { @Test @@ -16,4 +21,20 @@ void draw_UserDrawCardFromDeck_ReturnCard() { assertThat(deck.draw()).isInstanceOf(Card.class); } + + @ParameterizedTest + @MethodSource("provideEmptyDeck") + void refill_EmptyDeck_RefillDeck(Deck deck) { + assertThat(deck.draw()).isInstanceOf(Card.class); + } + + private static Stream provideEmptyDeck() { + int initDeckSize = CardRepository.cards().size(); + Deck deck = new Deck(); + + for (int i = 0; i < initDeckSize; i++) { + deck.draw(); + } + return Stream.of(Arguments.of(deck)); + } } diff --git a/src/test/java/blackjack/domain/user/UserTest.java b/src/test/java/blackjack/domain/user/UserTest.java index 9ad1196396..d2f3322d7c 100644 --- a/src/test/java/blackjack/domain/user/UserTest.java +++ b/src/test/java/blackjack/domain/user/UserTest.java @@ -24,7 +24,7 @@ void validate_InvalidUserName_InvalidUserExceptionThrown(String name) { } @Test - void draw_NumberOfCards_ReturnCardList() { + void draw_DrawNumberOfCards_AddDrawCardsToHand() { User user = new User("user"); Deck deck = new Deck(); user.draw(deck, 2); From bf5e0bf2ce3c74038df98bd40d3834640a2bb5ff Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Wed, 11 Mar 2020 19:34:28 +0900 Subject: [PATCH 10/31] =?UTF-8?q?refactor=20:=20User=20=EC=9D=B8=EC=8A=A4?= =?UTF-8?q?=ED=84=B4=EC=8A=A4=20draw=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=98=A4=EB=B2=84=EB=A1=9C=EB=94=A9=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/domain/user/User.java | 4 ++++ src/test/java/blackjack/domain/user/UserTest.java | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/main/java/blackjack/domain/user/User.java b/src/main/java/blackjack/domain/user/User.java index 48f2cf7cd6..379a262e33 100644 --- a/src/main/java/blackjack/domain/user/User.java +++ b/src/main/java/blackjack/domain/user/User.java @@ -28,6 +28,10 @@ private void validate(String name) { } } + public void draw(Deck deck) { + hand.add(deck.draw()); + } + public void draw(Deck deck, int drawNumber) { validateDrawNumber(drawNumber); List cards = IntStream.range(0, drawNumber) diff --git a/src/test/java/blackjack/domain/user/UserTest.java b/src/test/java/blackjack/domain/user/UserTest.java index d2f3322d7c..d9eb6e3c52 100644 --- a/src/test/java/blackjack/domain/user/UserTest.java +++ b/src/test/java/blackjack/domain/user/UserTest.java @@ -23,6 +23,17 @@ void validate_InvalidUserName_InvalidUserExceptionThrown(String name) { .hasMessage(InvalidUserException.NULL_OR_EMPTY); } + @Test + void draw_DrawCard_AddDrawCardToHand() { + User user = new User("user"); + Deck deck = new Deck(); + user.draw(deck); + + assertThat(user).extracting("hand.cards").asList() + .hasOnlyElementsOfType(Card.class) + .hasSize(1); + } + @Test void draw_DrawNumberOfCards_AddDrawCardsToHand() { User user = new User("user"); From 7bf52b0da007f42051891c6a10676cb84487deef Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Thu, 12 Mar 2020 12:33:04 +0900 Subject: [PATCH 11/31] =?UTF-8?q?feat=20:=20Score=20=EC=9D=B8=EC=8A=A4?= =?UTF-8?q?=ED=84=B4=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.Hand 인스턴스 패키지 이동. --- .../java/blackjack/domain/card/Symbol.java | 4 ++ .../domain/{card => user/hand}/Hand.java | 4 +- .../blackjack/domain/user/hand/Score.java | 46 +++++++++++++++++++ .../java/blackjack/domain/card/HandTest.java | 2 + .../blackjack/domain/user/hand/ScoreTest.java | 38 +++++++++++++++ 5 files changed, 93 insertions(+), 1 deletion(-) rename src/main/java/blackjack/domain/{card => user/hand}/Hand.java (79%) create mode 100644 src/main/java/blackjack/domain/user/hand/Score.java create mode 100644 src/test/java/blackjack/domain/user/hand/ScoreTest.java diff --git a/src/main/java/blackjack/domain/card/Symbol.java b/src/main/java/blackjack/domain/card/Symbol.java index 50df4c8428..f91b99d8bd 100644 --- a/src/main/java/blackjack/domain/card/Symbol.java +++ b/src/main/java/blackjack/domain/card/Symbol.java @@ -20,4 +20,8 @@ public enum Symbol { Symbol(int symbol) { this.symbol = symbol; } + + public int getSymbol() { + return symbol; + } } diff --git a/src/main/java/blackjack/domain/card/Hand.java b/src/main/java/blackjack/domain/user/hand/Hand.java similarity index 79% rename from src/main/java/blackjack/domain/card/Hand.java rename to src/main/java/blackjack/domain/user/hand/Hand.java index 506f6c6987..401cc71373 100644 --- a/src/main/java/blackjack/domain/card/Hand.java +++ b/src/main/java/blackjack/domain/user/hand/Hand.java @@ -1,8 +1,10 @@ -package blackjack.domain.card; +package blackjack.domain.user.hand; import java.util.ArrayList; import java.util.List; +import blackjack.domain.card.Card; + public class Hand { private final List cards; diff --git a/src/main/java/blackjack/domain/user/hand/Score.java b/src/main/java/blackjack/domain/user/hand/Score.java new file mode 100644 index 0000000000..dd0a66d5eb --- /dev/null +++ b/src/main/java/blackjack/domain/user/hand/Score.java @@ -0,0 +1,46 @@ +package blackjack.domain.user.hand; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +import blackjack.domain.card.Symbol; + +public class Score { + private final static Map CACHE = new HashMap<>(); + + private final int score; + + static { + Arrays.stream(Symbol.values()) + .map(Symbol::getSymbol) + .forEach(symbolValue -> + CACHE.putIfAbsent(symbolValue, new Score(symbolValue))); + } + + public Score() { + this(0); + } + + private Score(int score) { + this.score = score; + } + + public static Score valueOf(int number) { + Score score = CACHE.get(number); + + if (Objects.isNull(score)) { + score = new Score(number); + } + return score; + } + + public static Score valueOf(Symbol symbol) { + return CACHE.get(symbol.getSymbol()); + } + + public Score add(Score score) { + return new Score(this.score + score.score); + } +} diff --git a/src/test/java/blackjack/domain/card/HandTest.java b/src/test/java/blackjack/domain/card/HandTest.java index db9179b79d..1d3b568c3e 100644 --- a/src/test/java/blackjack/domain/card/HandTest.java +++ b/src/test/java/blackjack/domain/card/HandTest.java @@ -7,6 +7,8 @@ import org.junit.jupiter.api.Test; +import blackjack.domain.user.hand.Hand; + class HandTest { @Test void Hand_GenerateInstance() { diff --git a/src/test/java/blackjack/domain/user/hand/ScoreTest.java b/src/test/java/blackjack/domain/user/hand/ScoreTest.java new file mode 100644 index 0000000000..387b3f0615 --- /dev/null +++ b/src/test/java/blackjack/domain/user/hand/ScoreTest.java @@ -0,0 +1,38 @@ +package blackjack.domain.user.hand; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +import blackjack.domain.card.Symbol; + +class ScoreTest { + @Test + void Score_GenerateInstance() { + assertThat(new Score()).isInstanceOf(Score.class) + .extracting("score").isEqualTo(0); + } + + @Test + void valueOf_InputInteger_ReturnInstance() { + assertThat(Score.valueOf(10)).isInstanceOf(Score.class) + .extracting("score").isEqualTo(10); + } + + @ParameterizedTest + @EnumSource(value = Symbol.class) + void valueOf_InputSymbol_ReturnInstance(Symbol symbol) { + assertThat(Score.valueOf(symbol)).isInstanceOf(Score.class) + .extracting("score").isEqualTo(symbol.getSymbol()); + } + + @Test + void add_InputScore_addScore() { + Score score1 = Score.valueOf(Symbol.TWO); + Score score2 = Score.valueOf(Symbol.EIGHT); + + assertThat(score1.add(score2)).extracting("score").isEqualTo(10); + } +} From b8198b3480b99aad7a26bd6d1771fb9de90997aa Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Thu, 12 Mar 2020 14:40:14 +0900 Subject: [PATCH 12/31] =?UTF-8?q?feat=20:=20Hand=20=EC=9D=B8=EC=8A=A4?= =?UTF-8?q?=ED=84=B4=EC=8A=A4=EC=9D=98=20=EC=B9=B4=EB=93=9C=20=EC=A0=90?= =?UTF-8?q?=EC=88=98=EB=A5=BC=20=EA=B3=84=EC=82=B0=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Hand 인스턴스의 카드 점수를 계산하여 반환하는 기능 구현. 2. Hand 인스턴스에서 카드를 뽑을 수 있는지 여부를 판단하는 기능 구현. 3. Score 인스턴스에 점수의 합을 구하는 기능 구현. --- src/main/java/blackjack/domain/card/Card.java | 16 +++++++ .../java/blackjack/domain/user/hand/Hand.java | 13 ++++++ .../blackjack/domain/user/hand/Score.java | 42 +++++++++++++---- .../java/blackjack/domain/card/HandTest.java | 43 ++++++++++++++++- .../blackjack/domain/user/hand/ScoreTest.java | 46 +++++++++++++------ 5 files changed, 138 insertions(+), 22 deletions(-) diff --git a/src/main/java/blackjack/domain/card/Card.java b/src/main/java/blackjack/domain/card/Card.java index 0dc31e82fc..0d9e524a86 100644 --- a/src/main/java/blackjack/domain/card/Card.java +++ b/src/main/java/blackjack/domain/card/Card.java @@ -8,4 +8,20 @@ public Card(Symbol symbol, Type type) { this.symbol = symbol; this.type = type; } + + public boolean isAce() { + return this.symbol.equals(Symbol.ACE); + } + + public int getSymbolValue() { + return symbol.getSymbol(); + } + + public Symbol getSymbol() { + return symbol; + } + + public Type getType() { + return type; + } } diff --git a/src/main/java/blackjack/domain/user/hand/Hand.java b/src/main/java/blackjack/domain/user/hand/Hand.java index 401cc71373..6b941a84b6 100644 --- a/src/main/java/blackjack/domain/user/hand/Hand.java +++ b/src/main/java/blackjack/domain/user/hand/Hand.java @@ -19,4 +19,17 @@ public void add(Card card) { public void add(List cards) { this.cards.addAll(cards); } + + public Score calculateScore() { + Score score = Score.ZERO; + + for (Card card : cards) { + score = score.add(card); + } + return score; + } + + public boolean canDrawBy(int drawableMaxScore) { + return calculateScore().isLowerThan(drawableMaxScore); + } } diff --git a/src/main/java/blackjack/domain/user/hand/Score.java b/src/main/java/blackjack/domain/user/hand/Score.java index dd0a66d5eb..7ba27b2db2 100644 --- a/src/main/java/blackjack/domain/user/hand/Score.java +++ b/src/main/java/blackjack/domain/user/hand/Score.java @@ -5,10 +5,14 @@ import java.util.Map; import java.util.Objects; +import blackjack.domain.card.Card; import blackjack.domain.card.Symbol; public class Score { + public final static Score ZERO = new Score(0); private final static Map CACHE = new HashMap<>(); + private static final int ADDITIONAL_ACE_SCORE = 10; + private static final int BUST_SCORE = 22; private final int score; @@ -19,10 +23,6 @@ public class Score { CACHE.putIfAbsent(symbolValue, new Score(symbolValue))); } - public Score() { - this(0); - } - private Score(int score) { this.score = score; } @@ -36,11 +36,37 @@ public static Score valueOf(int number) { return score; } - public static Score valueOf(Symbol symbol) { - return CACHE.get(symbol.getSymbol()); + public static Score valueOf(Card card) { + return CACHE.get(card.getSymbolValue()); + } + + public Score add(Card card) { + int currentScore = score + card.getSymbolValue(); + + if (card.isAce() && currentScore + ADDITIONAL_ACE_SCORE < BUST_SCORE) { + return Score.valueOf(currentScore + ADDITIONAL_ACE_SCORE); + } + return Score.valueOf(currentScore); + } + + public boolean isLowerThan(int score) { + return this.score < score; + } + + @Override + public boolean equals(Object object) { + if (this == object) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } + Score that = (Score)object; + return score == that.score; } - public Score add(Score score) { - return new Score(this.score + score.score); + @Override + public int hashCode() { + return Objects.hash(score); } } diff --git a/src/test/java/blackjack/domain/card/HandTest.java b/src/test/java/blackjack/domain/card/HandTest.java index 1d3b568c3e..f4c4f47f2d 100644 --- a/src/test/java/blackjack/domain/card/HandTest.java +++ b/src/test/java/blackjack/domain/card/HandTest.java @@ -6,8 +6,11 @@ import java.util.List; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import blackjack.domain.user.hand.Hand; +import blackjack.domain.user.hand.Score; class HandTest { @Test @@ -27,9 +30,47 @@ void add_Card_AddCardToHand() { @Test void add_CardList_AddCardsToHand() { Hand hand = new Hand(); - List cards = Arrays.asList(new Card(Symbol.ACE, Type.CLUB), new Card(Symbol.EIGHT, Type.DIAMOND)); + List cards = Arrays.asList( + new Card(Symbol.ACE, Type.CLUB), + new Card(Symbol.EIGHT, Type.DIAMOND)); hand.add(cards); assertThat(hand).extracting("cards").asList().containsAll(cards); } + + @Test + void calculateScore_SumCards() { + Hand hand = new Hand(); + List cards = Arrays.asList( + new Card(Symbol.TWO, Type.CLUB), + new Card(Symbol.EIGHT, Type.DIAMOND)); + hand.add(cards); + + Score expected = Score.valueOf(10); + assertThat(hand.calculateScore()).isEqualTo(expected); + } + + @Test + void calculateScore_WithAceCard_SumCards() { + Hand hand = new Hand(); + List cards = Arrays.asList( + new Card(Symbol.ACE, Type.CLUB), + new Card(Symbol.EIGHT, Type.DIAMOND)); + hand.add(cards); + + Score expected = Score.valueOf(19); + assertThat(hand.calculateScore()).isEqualTo(expected); + } + + @ParameterizedTest + @CsvSource(value = {"10,false", "11,true"}) + void canDraw_DrawableMaxScore_ReturnDrawableStatus(int drawableMaxScore, boolean expected) { + Hand hand = new Hand(); + List cards = Arrays.asList( + new Card(Symbol.TWO, Type.CLUB), + new Card(Symbol.EIGHT, Type.DIAMOND)); + hand.add(cards); + + assertThat(hand.canDrawBy(drawableMaxScore)).isEqualTo(expected); + } } diff --git a/src/test/java/blackjack/domain/user/hand/ScoreTest.java b/src/test/java/blackjack/domain/user/hand/ScoreTest.java index 387b3f0615..91014fcc2a 100644 --- a/src/test/java/blackjack/domain/user/hand/ScoreTest.java +++ b/src/test/java/blackjack/domain/user/hand/ScoreTest.java @@ -4,17 +4,14 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.EnumSource; +import blackjack.domain.card.Card; import blackjack.domain.card.Symbol; +import blackjack.domain.card.Type; class ScoreTest { - @Test - void Score_GenerateInstance() { - assertThat(new Score()).isInstanceOf(Score.class) - .extracting("score").isEqualTo(0); - } - @Test void valueOf_InputInteger_ReturnInstance() { assertThat(Score.valueOf(10)).isInstanceOf(Score.class) @@ -23,16 +20,39 @@ void valueOf_InputInteger_ReturnInstance() { @ParameterizedTest @EnumSource(value = Symbol.class) - void valueOf_InputSymbol_ReturnInstance(Symbol symbol) { - assertThat(Score.valueOf(symbol)).isInstanceOf(Score.class) - .extracting("score").isEqualTo(symbol.getSymbol()); + void valueOf_InputCard_ReturnInstance(Symbol symbol) { + Card card = new Card(symbol, Type.CLUB); + + assertThat(Score.valueOf(card)).isInstanceOf(Score.class) + .extracting("score").isEqualTo(card.getSymbolValue()); } @Test - void add_InputScore_addScore() { - Score score1 = Score.valueOf(Symbol.TWO); - Score score2 = Score.valueOf(Symbol.EIGHT); + void add_InputCard_addScore() { + Card card = new Card(Symbol.TWO, Type.CLUB); + assertThat(Score.ZERO.add(card)).extracting("score").isEqualTo(2); + } + + @Test + void add_ACECardAddBy11_addScore() { + Card card = new Card(Symbol.ACE, Type.CLUB); + + assertThat(Score.ZERO.add(card)).extracting("score").isEqualTo(11); + } + + @Test + void add_ACECardAddBy1_addScore() { + Card card = new Card(Symbol.ACE, Type.CLUB); + Score score = Score.valueOf(11); + + assertThat(score.add(card)).extracting("score").isEqualTo(12); + } + + @ParameterizedTest + @CsvSource(value = {"9,false", "10,true"}) + void isLowerThan_InputIntegerScore_ReturnCompareResult(int value, boolean expected) { + Score score = Score.valueOf(9); - assertThat(score1.add(score2)).extracting("score").isEqualTo(10); + assertThat(score.isLowerThan(value)).isEqualTo(expected); } } From a127551d8b0fb8c5ad07c06bd90b68ef20b825f1 Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Thu, 12 Mar 2020 15:34:58 +0900 Subject: [PATCH 13/31] =?UTF-8?q?feat=20:=20User=20=EC=9D=B8=EC=8A=A4?= =?UTF-8?q?=ED=84=B4=EC=8A=A4=EB=A5=BC=20=EC=B6=94=EC=83=81=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=EB=A1=9C=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20?= =?UTF-8?q?=EC=9D=B4=EB=A5=BC=20=EC=83=81=EC=86=8D=EB=B0=9B=EB=8A=94=20Dea?= =?UTF-8?q?ler,=20Player=20=EC=9D=B8=EC=8A=A4=ED=84=B4=EC=8A=A4=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- src/main/java/blackjack/domain/card/Card.java | 8 ---- .../java/blackjack/domain/user/Dealer.java | 14 ++++++ .../java/blackjack/domain/user/Player.java | 14 ++++++ src/main/java/blackjack/domain/user/User.java | 18 ++++---- .../blackjack/domain/user/DealerTest.java | 46 +++++++++++++++++++ .../blackjack/domain/user/PlayerTest.java | 42 +++++++++++++++++ .../java/blackjack/domain/user/UserTest.java | 12 +++-- 8 files changed, 134 insertions(+), 22 deletions(-) create mode 100644 src/main/java/blackjack/domain/user/Dealer.java create mode 100644 src/main/java/blackjack/domain/user/Player.java create mode 100644 src/test/java/blackjack/domain/user/DealerTest.java create mode 100644 src/test/java/blackjack/domain/user/PlayerTest.java diff --git a/.gitignore b/.gitignore index 76611b072e..d49cfdc4cd 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,4 @@ bin/ .idea *.iws *.iml -*.ipr \ No newline at end of file +*.iprtest\ diff --git a/src/main/java/blackjack/domain/card/Card.java b/src/main/java/blackjack/domain/card/Card.java index 0d9e524a86..1fb9b5e538 100644 --- a/src/main/java/blackjack/domain/card/Card.java +++ b/src/main/java/blackjack/domain/card/Card.java @@ -16,12 +16,4 @@ public boolean isAce() { public int getSymbolValue() { return symbol.getSymbol(); } - - public Symbol getSymbol() { - return symbol; - } - - public Type getType() { - return type; - } } diff --git a/src/main/java/blackjack/domain/user/Dealer.java b/src/main/java/blackjack/domain/user/Dealer.java new file mode 100644 index 0000000000..09bf8e6dc1 --- /dev/null +++ b/src/main/java/blackjack/domain/user/Dealer.java @@ -0,0 +1,14 @@ +package blackjack.domain.user; + +public class Dealer extends User { + private static final int DEALER_DRAWABLE_MAX_SCORE = 17; + + public Dealer(String name) { + super(name); + } + + @Override + boolean canDraw() { + return hand.canDrawBy(DEALER_DRAWABLE_MAX_SCORE); + } +} diff --git a/src/main/java/blackjack/domain/user/Player.java b/src/main/java/blackjack/domain/user/Player.java new file mode 100644 index 0000000000..2f6669c611 --- /dev/null +++ b/src/main/java/blackjack/domain/user/Player.java @@ -0,0 +1,14 @@ +package blackjack.domain.user; + +public class Player extends User { + private static final int PLAYER_DRAWABLE_MAX_SCORE = 22; + + public Player(String name) { + super(name); + } + + @Override + boolean canDraw() { + return hand.canDrawBy(PLAYER_DRAWABLE_MAX_SCORE); + } +} diff --git a/src/main/java/blackjack/domain/user/User.java b/src/main/java/blackjack/domain/user/User.java index 379a262e33..2f1a8604ee 100644 --- a/src/main/java/blackjack/domain/user/User.java +++ b/src/main/java/blackjack/domain/user/User.java @@ -8,13 +8,13 @@ import blackjack.domain.card.Card; import blackjack.domain.card.Deck; -import blackjack.domain.card.Hand; +import blackjack.domain.user.hand.Hand; -public class User { +public abstract class User { private static final int DRAW_LOWER_BOUND = 1; - private final String name; - private final Hand hand; + protected final String name; + protected final Hand hand; public User(String name) { validate(name); @@ -23,7 +23,7 @@ public User(String name) { } private void validate(String name) { - if (Objects.isNull(name) || name.isEmpty()) { + if (Objects.isNull(name) || name.trim().isEmpty()) { throw new InvalidUserException(InvalidUserException.NULL_OR_EMPTY); } } @@ -34,15 +34,17 @@ public void draw(Deck deck) { public void draw(Deck deck, int drawNumber) { validateDrawNumber(drawNumber); - List cards = IntStream.range(0, drawNumber) + List drawCards = IntStream.range(0, drawNumber) .mapToObj(e -> deck.draw()) .collect(toList()); - hand.add(cards); + hand.add(drawCards); } - private void validateDrawNumber(int drawNumber) { + protected void validateDrawNumber(int drawNumber) { if (drawNumber < DRAW_LOWER_BOUND) { throw new InvalidUserException(InvalidUserException.INVALID_DRAW_NUMBER); } } + + abstract boolean canDraw(); } diff --git a/src/test/java/blackjack/domain/user/DealerTest.java b/src/test/java/blackjack/domain/user/DealerTest.java new file mode 100644 index 0000000000..651d1473c5 --- /dev/null +++ b/src/test/java/blackjack/domain/user/DealerTest.java @@ -0,0 +1,46 @@ +package blackjack.domain.user; + +import static org.assertj.core.api.Assertions.*; + +import java.util.stream.Stream; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import blackjack.domain.card.Deck; + +class DealerTest { + + @Test + void Dealer_InputDealerName_GenerateInstance() { + Dealer dealer = new Dealer("dealer"); + + assertThat(dealer).isInstanceOf(Dealer.class); + assertThat(dealer).isInstanceOf(User.class); + } + + @Test + void canDraw_CurrentScoreLowerThanDrawableMaxScore_ReturnTrue() { + assertThat(new Dealer("dealer").canDraw()).isTrue(); + } + + @ParameterizedTest + @MethodSource("provideUndrawableDealer") + void canDraw_CurrentScoreMoreThanDrawableMaxScore_ReturnFalse(Dealer dealer) { + assertThat(dealer.canDraw()).isFalse(); + } + + private static Stream provideUndrawableDealer() { + final int WORST_CASE_OF_DRAWABLE_COUNT = 12; + + Dealer dealer = new Dealer("dealer"); + Deck deck = new Deck(); + + for (int i = 0; i < WORST_CASE_OF_DRAWABLE_COUNT; i++) { + dealer.draw(deck); + } + return Stream.of(Arguments.of(dealer)); + } +} diff --git a/src/test/java/blackjack/domain/user/PlayerTest.java b/src/test/java/blackjack/domain/user/PlayerTest.java new file mode 100644 index 0000000000..cd8a8da8ec --- /dev/null +++ b/src/test/java/blackjack/domain/user/PlayerTest.java @@ -0,0 +1,42 @@ +package blackjack.domain.user; + +import static org.assertj.core.api.Assertions.*; + +import java.util.stream.Stream; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import blackjack.domain.card.Deck; + +class PlayerTest { + @Test + void Player_InputPlayerName_GenerateInstance() { + assertThat(new Player("player")).isInstanceOf(Player.class); + } + + @Test + void canDraw_CurrentScoreLowerThanDrawableMaxScore_ReturnTrue() { + assertThat(new Player("player").canDraw()).isTrue(); + } + + @ParameterizedTest + @MethodSource("provideUndrawablePlayer") + void canDraw_CurrentScoreMoreThanDrawableMaxScore_ReturnFalse(Player player) { + assertThat(player.canDraw()).isFalse(); + } + + private static Stream provideUndrawablePlayer() { + final int WORST_CASE_OF_DRAWABLE_COUNT = 12; + + Player player = new Player("player"); + Deck deck = new Deck(); + + for (int i = 0; i < WORST_CASE_OF_DRAWABLE_COUNT; i++) { + player.draw(deck); + } + return Stream.of(Arguments.of(player)); + } +} diff --git a/src/test/java/blackjack/domain/user/UserTest.java b/src/test/java/blackjack/domain/user/UserTest.java index d9eb6e3c52..ea63d9da4f 100644 --- a/src/test/java/blackjack/domain/user/UserTest.java +++ b/src/test/java/blackjack/domain/user/UserTest.java @@ -12,20 +12,22 @@ class UserTest { @Test void User_InputUserName_GenerateInstance() { - assertThat(new User("user")).isInstanceOf(User.class); + User user = new Dealer("user"); + + assertThat(user).isInstanceOf(User.class); } @ParameterizedTest @NullAndEmptySource void validate_InvalidUserName_InvalidUserExceptionThrown(String name) { - assertThatThrownBy(() -> new User(name)) + assertThatThrownBy(() -> new Dealer(name)) .isInstanceOf(InvalidUserException.class) .hasMessage(InvalidUserException.NULL_OR_EMPTY); } @Test void draw_DrawCard_AddDrawCardToHand() { - User user = new User("user"); + User user = new Dealer("user"); Deck deck = new Deck(); user.draw(deck); @@ -36,7 +38,7 @@ void draw_DrawCard_AddDrawCardToHand() { @Test void draw_DrawNumberOfCards_AddDrawCardsToHand() { - User user = new User("user"); + User user = new Dealer("user"); Deck deck = new Deck(); user.draw(deck, 2); @@ -47,7 +49,7 @@ void draw_DrawNumberOfCards_AddDrawCardsToHand() { @Test void validateDrawNumber_InvalidNumberOfCards_InvalidUserExceptionThrown() { - User user = new User("user"); + User user = new Dealer("user"); Deck deck = new Deck(); assertThatThrownBy(() -> user.draw(deck, 0)) From 15967670365f0321fe8c68b48e16204027c06dd3 Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Thu, 12 Mar 2020 15:48:39 +0900 Subject: [PATCH 14/31] =?UTF-8?q?feat=20:=20DrawOpinion=20=EC=97=B4?= =?UTF-8?q?=EA=B1=B0=ED=98=95=20=EC=9D=B8=EC=8A=A4=ED=84=B4=EC=8A=A4=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/DrawOpinion.java | 21 +++++++++++++++++++ .../domain/InvalidDrawOpinionException.java | 9 ++++++++ .../blackjack/domain/DrawOpinionTest.java | 19 +++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 src/main/java/blackjack/domain/DrawOpinion.java create mode 100644 src/main/java/blackjack/domain/InvalidDrawOpinionException.java create mode 100644 src/test/java/blackjack/domain/DrawOpinionTest.java diff --git a/src/main/java/blackjack/domain/DrawOpinion.java b/src/main/java/blackjack/domain/DrawOpinion.java new file mode 100644 index 0000000000..fa0b3b404f --- /dev/null +++ b/src/main/java/blackjack/domain/DrawOpinion.java @@ -0,0 +1,21 @@ +package blackjack.domain; + +import java.util.Arrays; + +public enum DrawOpinion { + YES("y"), + NO("n"); + + private final String opinion; + + DrawOpinion(String opinion) { + this.opinion = opinion; + } + + public static DrawOpinion of(String opinion) { + return Arrays.stream(values()) + .filter(drawOpinion -> drawOpinion.opinion.equals(opinion)) + .findAny() + .orElseThrow(() -> new InvalidDrawOpinionException(InvalidDrawOpinionException.INVALID)); + } +} diff --git a/src/main/java/blackjack/domain/InvalidDrawOpinionException.java b/src/main/java/blackjack/domain/InvalidDrawOpinionException.java new file mode 100644 index 0000000000..d603c2fc85 --- /dev/null +++ b/src/main/java/blackjack/domain/InvalidDrawOpinionException.java @@ -0,0 +1,9 @@ +package blackjack.domain; + +public class InvalidDrawOpinionException extends IllegalArgumentException { + public static final String INVALID = "y 또는 n만 입력 가능합니다."; + + public InvalidDrawOpinionException(String s) { + super(s); + } +} diff --git a/src/test/java/blackjack/domain/DrawOpinionTest.java b/src/test/java/blackjack/domain/DrawOpinionTest.java new file mode 100644 index 0000000000..3d805d981c --- /dev/null +++ b/src/test/java/blackjack/domain/DrawOpinionTest.java @@ -0,0 +1,19 @@ +package blackjack.domain; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class DrawOpinionTest { + @Test + void of_InputDrawOpinion_ReturnInstance() { + assertThat(DrawOpinion.of("y")).isEqualTo(DrawOpinion.YES); + } + + @Test + void of_InvalidInputDrawOpinion_InvalidDrawOpinionExceptionThrown() { + assertThatThrownBy(() -> DrawOpinion.of("k")) + .isInstanceOf(InvalidDrawOpinionException.class) + .hasMessage(InvalidDrawOpinionException.INVALID); + } +} From c60f13d8a492809b6644b90870d0fd47748aa4ce Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Thu, 12 Mar 2020 16:08:12 +0900 Subject: [PATCH 15/31] =?UTF-8?q?feat=20:=20User=EC=9D=98=20=EB=B2=84?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=97=AC=EB=B6=80=EB=A5=BC=20=ED=8C=90?= =?UTF-8?q?=EB=8B=A8=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/blackjack/domain/user/Dealer.java | 2 +- .../java/blackjack/domain/user/Player.java | 2 +- src/main/java/blackjack/domain/user/User.java | 5 +++++ .../java/blackjack/domain/user/hand/Hand.java | 4 ---- .../blackjack/domain/user/hand/Score.java | 4 ++++ .../java/blackjack/domain/card/HandTest.java | 14 ------------ .../java/blackjack/domain/user/UserTest.java | 22 +++++++++++++++++++ .../blackjack/domain/user/hand/ScoreTest.java | 8 +++++++ 8 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/main/java/blackjack/domain/user/Dealer.java b/src/main/java/blackjack/domain/user/Dealer.java index 09bf8e6dc1..2ca19b6e7d 100644 --- a/src/main/java/blackjack/domain/user/Dealer.java +++ b/src/main/java/blackjack/domain/user/Dealer.java @@ -9,6 +9,6 @@ public Dealer(String name) { @Override boolean canDraw() { - return hand.canDrawBy(DEALER_DRAWABLE_MAX_SCORE); + return hand.calculateScore().isLowerThan(DEALER_DRAWABLE_MAX_SCORE); } } diff --git a/src/main/java/blackjack/domain/user/Player.java b/src/main/java/blackjack/domain/user/Player.java index 2f6669c611..959098b082 100644 --- a/src/main/java/blackjack/domain/user/Player.java +++ b/src/main/java/blackjack/domain/user/Player.java @@ -9,6 +9,6 @@ public Player(String name) { @Override boolean canDraw() { - return hand.canDrawBy(PLAYER_DRAWABLE_MAX_SCORE); + return hand.calculateScore().isLowerThan(PLAYER_DRAWABLE_MAX_SCORE); } } diff --git a/src/main/java/blackjack/domain/user/User.java b/src/main/java/blackjack/domain/user/User.java index 2f1a8604ee..e2facd8d76 100644 --- a/src/main/java/blackjack/domain/user/User.java +++ b/src/main/java/blackjack/domain/user/User.java @@ -12,6 +12,7 @@ public abstract class User { private static final int DRAW_LOWER_BOUND = 1; + private static final int BLACKJACK_SCORE = 21; protected final String name; protected final Hand hand; @@ -46,5 +47,9 @@ protected void validateDrawNumber(int drawNumber) { } } + public boolean isBust() { + return hand.calculateScore().isMoreThan(BLACKJACK_SCORE); + } + abstract boolean canDraw(); } diff --git a/src/main/java/blackjack/domain/user/hand/Hand.java b/src/main/java/blackjack/domain/user/hand/Hand.java index 6b941a84b6..23daef427f 100644 --- a/src/main/java/blackjack/domain/user/hand/Hand.java +++ b/src/main/java/blackjack/domain/user/hand/Hand.java @@ -28,8 +28,4 @@ public Score calculateScore() { } return score; } - - public boolean canDrawBy(int drawableMaxScore) { - return calculateScore().isLowerThan(drawableMaxScore); - } } diff --git a/src/main/java/blackjack/domain/user/hand/Score.java b/src/main/java/blackjack/domain/user/hand/Score.java index 7ba27b2db2..e4ae1fb3ce 100644 --- a/src/main/java/blackjack/domain/user/hand/Score.java +++ b/src/main/java/blackjack/domain/user/hand/Score.java @@ -53,6 +53,10 @@ public boolean isLowerThan(int score) { return this.score < score; } + public boolean isMoreThan(int score) { + return this.score > score; + } + @Override public boolean equals(Object object) { if (this == object) { diff --git a/src/test/java/blackjack/domain/card/HandTest.java b/src/test/java/blackjack/domain/card/HandTest.java index f4c4f47f2d..73794955a4 100644 --- a/src/test/java/blackjack/domain/card/HandTest.java +++ b/src/test/java/blackjack/domain/card/HandTest.java @@ -6,8 +6,6 @@ import java.util.List; import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; import blackjack.domain.user.hand.Hand; import blackjack.domain.user.hand.Score; @@ -61,16 +59,4 @@ void calculateScore_WithAceCard_SumCards() { Score expected = Score.valueOf(19); assertThat(hand.calculateScore()).isEqualTo(expected); } - - @ParameterizedTest - @CsvSource(value = {"10,false", "11,true"}) - void canDraw_DrawableMaxScore_ReturnDrawableStatus(int drawableMaxScore, boolean expected) { - Hand hand = new Hand(); - List cards = Arrays.asList( - new Card(Symbol.TWO, Type.CLUB), - new Card(Symbol.EIGHT, Type.DIAMOND)); - hand.add(cards); - - assertThat(hand.canDrawBy(drawableMaxScore)).isEqualTo(expected); - } } diff --git a/src/test/java/blackjack/domain/user/UserTest.java b/src/test/java/blackjack/domain/user/UserTest.java index ea63d9da4f..6a3cd834ed 100644 --- a/src/test/java/blackjack/domain/user/UserTest.java +++ b/src/test/java/blackjack/domain/user/UserTest.java @@ -2,8 +2,12 @@ import static org.assertj.core.api.Assertions.*; +import java.util.stream.Stream; + import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.NullAndEmptySource; import blackjack.domain.card.Card; @@ -56,4 +60,22 @@ void validateDrawNumber_InvalidNumberOfCards_InvalidUserExceptionThrown() { .isInstanceOf(InvalidUserException.class) .hasMessage(InvalidUserException.INVALID_DRAW_NUMBER); } + + @ParameterizedTest + @MethodSource("provideBustUser") + void isBust_BustScoreUser_ReturnTrue(User user) { + assertThat(user.isBust()).isTrue(); + } + + private static Stream provideBustUser() { + final int WORST_CASE_OF_DRAWABLE_COUNT = 12; + + User user = new Player("user"); + Deck deck = new Deck(); + + for (int i = 0; i < WORST_CASE_OF_DRAWABLE_COUNT; i++) { + user.draw(deck); + } + return Stream.of(Arguments.of(user)); + } } diff --git a/src/test/java/blackjack/domain/user/hand/ScoreTest.java b/src/test/java/blackjack/domain/user/hand/ScoreTest.java index 91014fcc2a..239708cb97 100644 --- a/src/test/java/blackjack/domain/user/hand/ScoreTest.java +++ b/src/test/java/blackjack/domain/user/hand/ScoreTest.java @@ -55,4 +55,12 @@ void isLowerThan_InputIntegerScore_ReturnCompareResult(int value, boolean expect assertThat(score.isLowerThan(value)).isEqualTo(expected); } + + @ParameterizedTest + @CsvSource(value = {"8,true", "9,false"}) + void isMoreThan_InputIntegerScore_ReturnCompareResult(int value, boolean expected) { + Score score = Score.valueOf(9); + + assertThat(score.isMoreThan(value)).isEqualTo(expected); + } } From 3fe30559b2b1360a30f08ad3d63d29838f9243b7 Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Thu, 12 Mar 2020 16:28:36 +0900 Subject: [PATCH 16/31] =?UTF-8?q?feat=20:=20PlayerFactory=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. User 추상 클래스에 equals 오버라이딩. --- .../blackjack/domain/user/PlayerFactory.java | 16 +++++++++++++++ src/main/java/blackjack/domain/user/User.java | 19 +++++++++++++++++- .../domain/user/PlayerFactoryTest.java | 20 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/main/java/blackjack/domain/user/PlayerFactory.java create mode 100644 src/test/java/blackjack/domain/user/PlayerFactoryTest.java diff --git a/src/main/java/blackjack/domain/user/PlayerFactory.java b/src/main/java/blackjack/domain/user/PlayerFactory.java new file mode 100644 index 0000000000..67feb272cb --- /dev/null +++ b/src/main/java/blackjack/domain/user/PlayerFactory.java @@ -0,0 +1,16 @@ +package blackjack.domain.user; + +import static java.util.stream.Collectors.*; + +import java.util.Arrays; +import java.util.List; + +public class PlayerFactory { + private static final String DELIMITER = ","; + + public static List create(String playerNames) { + return Arrays.stream(playerNames.split(DELIMITER)) + .map(Player::new) + .collect(toList()); + } +} diff --git a/src/main/java/blackjack/domain/user/User.java b/src/main/java/blackjack/domain/user/User.java index e2facd8d76..a468f5694c 100644 --- a/src/main/java/blackjack/domain/user/User.java +++ b/src/main/java/blackjack/domain/user/User.java @@ -19,7 +19,7 @@ public abstract class User { public User(String name) { validate(name); - this.name = name; + this.name = name.trim(); this.hand = new Hand(); } @@ -52,4 +52,21 @@ public boolean isBust() { } abstract boolean canDraw(); + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + User that = (User)o; + return name.equals(that.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } } diff --git a/src/test/java/blackjack/domain/user/PlayerFactoryTest.java b/src/test/java/blackjack/domain/user/PlayerFactoryTest.java new file mode 100644 index 0000000000..2385251da4 --- /dev/null +++ b/src/test/java/blackjack/domain/user/PlayerFactoryTest.java @@ -0,0 +1,20 @@ +package blackjack.domain.user; + +import static org.assertj.core.api.Assertions.*; + +import java.util.Arrays; +import java.util.List; + +import org.junit.jupiter.api.Test; + +class PlayerFactoryTest { + @Test + void create_PlayerNames_GeneratePlayerList() { + List players = Arrays.asList( + new Player("pobi"), + new Player("sony"), + new Player("stitch")); + + assertThat(PlayerFactory.create("pobi, sony, stitch")).isEqualTo(players); + } +} From b5f8eee2dbccc64853e6e68089df4e1f7d44e53a Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Thu, 12 Mar 2020 16:39:05 +0900 Subject: [PATCH 17/31] =?UTF-8?q?feat=20:=20Player=EB=93=A4=EC=9D=98=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=EC=9D=84=20=EC=9E=85=EB=A0=A5=EB=B0=9B?= =?UTF-8?q?=EC=95=84=20=EC=83=9D=EC=84=B1=ED=95=98=EB=8A=94=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/Application.java | 21 +++++++++++++++++ .../controller/BlackjackController.java | 23 +++++++++++++++++++ src/main/java/blackjack/view/InputView.java | 12 ++++++++++ 3 files changed, 56 insertions(+) create mode 100644 src/main/java/blackjack/Application.java create mode 100644 src/main/java/blackjack/controller/BlackjackController.java create mode 100644 src/main/java/blackjack/view/InputView.java diff --git a/src/main/java/blackjack/Application.java b/src/main/java/blackjack/Application.java new file mode 100644 index 0000000000..04cbf46635 --- /dev/null +++ b/src/main/java/blackjack/Application.java @@ -0,0 +1,21 @@ +package blackjack; + +import java.util.List; + +import blackjack.controller.BlackjackController; +import blackjack.domain.card.Deck; +import blackjack.domain.user.Dealer; +import blackjack.domain.user.Player; +import blackjack.domain.user.PlayerFactory; +import blackjack.view.InputView; + +public class Application { + public static void main(String[] args) { + Deck deck = new Deck(); + Dealer dealer = new Dealer("dealer"); + List players = PlayerFactory.create(InputView.inputPlayerNames()); + + BlackjackController blackjackController = new BlackjackController(deck, dealer, players); + blackjackController.playGame(); + } +} diff --git a/src/main/java/blackjack/controller/BlackjackController.java b/src/main/java/blackjack/controller/BlackjackController.java new file mode 100644 index 0000000000..efb9232ed1 --- /dev/null +++ b/src/main/java/blackjack/controller/BlackjackController.java @@ -0,0 +1,23 @@ +package blackjack.controller; + +import java.util.List; + +import blackjack.domain.card.Deck; +import blackjack.domain.user.Dealer; +import blackjack.domain.user.Player; + +public class BlackjackController { + private final Deck deck; + private final Dealer dealer; + private final List players; + + public BlackjackController(Deck deck, Dealer dealer, List players) { + this.deck = deck; + this.dealer = dealer; + this.players = players; + } + + public void playGame() { + + } +} diff --git a/src/main/java/blackjack/view/InputView.java b/src/main/java/blackjack/view/InputView.java new file mode 100644 index 0000000000..3705fe1705 --- /dev/null +++ b/src/main/java/blackjack/view/InputView.java @@ -0,0 +1,12 @@ +package blackjack.view; + +import java.util.Scanner; + +public class InputView { + private static final Scanner SCANNER = new Scanner(System.in); + + public static String inputPlayerNames() { + System.out.println("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)"); + return SCANNER.nextLine(); + } +} From 5974bf7f6cabd0d3d1df5c2451446b7f10a133f5 Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Thu, 12 Mar 2020 17:32:00 +0900 Subject: [PATCH 18/31] =?UTF-8?q?feat=20:=20User=EB=93=A4=EC=9D=B4=20?= =?UTF-8?q?=EC=B2=98=EC=9D=8C=202=EC=9E=A5=EC=9D=98=20=EC=B9=B4=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20=EB=B0=9B=EC=95=98=EB=8B=A4=EB=8A=94=20=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=20=EC=B6=9C=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackjackController.java | 15 +++++++ src/main/java/blackjack/domain/card/Card.java | 7 +++- .../java/blackjack/domain/card/Symbol.java | 38 +++++++++-------- src/main/java/blackjack/domain/card/Type.java | 18 ++++++-- .../java/blackjack/domain/user/Dealer.java | 9 ++++ .../java/blackjack/domain/user/Player.java | 9 ++++ src/main/java/blackjack/domain/user/User.java | 12 ++++++ .../java/blackjack/domain/user/hand/Hand.java | 4 ++ .../blackjack/domain/user/hand/Score.java | 2 +- src/main/java/blackjack/util/StringUtil.java | 26 ++++++++++++ src/main/java/blackjack/view/OutputView.java | 19 +++++++++ .../java/blackjack/util/StringUtilTest.java | 41 +++++++++++++++++++ 12 files changed, 178 insertions(+), 22 deletions(-) create mode 100644 src/main/java/blackjack/util/StringUtil.java create mode 100644 src/main/java/blackjack/view/OutputView.java create mode 100644 src/test/java/blackjack/util/StringUtilTest.java diff --git a/src/main/java/blackjack/controller/BlackjackController.java b/src/main/java/blackjack/controller/BlackjackController.java index efb9232ed1..c74ec94a9a 100644 --- a/src/main/java/blackjack/controller/BlackjackController.java +++ b/src/main/java/blackjack/controller/BlackjackController.java @@ -1,12 +1,17 @@ package blackjack.controller; +import java.util.ArrayList; import java.util.List; import blackjack.domain.card.Deck; import blackjack.domain.user.Dealer; import blackjack.domain.user.Player; +import blackjack.domain.user.User; +import blackjack.view.OutputView; public class BlackjackController { + private static final int INITIAL_DRAW_NUMBER = 2; + private final Deck deck; private final Dealer dealer; private final List players; @@ -18,6 +23,16 @@ public BlackjackController(Deck deck, Dealer dealer, List players) { } public void playGame() { + List users = generateUsers(); + + users.forEach(user -> user.draw(deck, INITIAL_DRAW_NUMBER)); + OutputView.printUsersInitialDraw(INITIAL_DRAW_NUMBER, users); + } + private List generateUsers() { + List users = new ArrayList<>(); + users.add(dealer); + users.addAll(players); + return users; } } diff --git a/src/main/java/blackjack/domain/card/Card.java b/src/main/java/blackjack/domain/card/Card.java index 1fb9b5e538..a29036755e 100644 --- a/src/main/java/blackjack/domain/card/Card.java +++ b/src/main/java/blackjack/domain/card/Card.java @@ -14,6 +14,11 @@ public boolean isAce() { } public int getSymbolValue() { - return symbol.getSymbol(); + return symbol.getScore(); + } + + @Override + public String toString() { + return symbol.getSymbol() + type.getType(); } } diff --git a/src/main/java/blackjack/domain/card/Symbol.java b/src/main/java/blackjack/domain/card/Symbol.java index f91b99d8bd..f0a406cab0 100644 --- a/src/main/java/blackjack/domain/card/Symbol.java +++ b/src/main/java/blackjack/domain/card/Symbol.java @@ -1,27 +1,33 @@ package blackjack.domain.card; public enum Symbol { - ACE(1), - TWO(2), - THREE(3), - FOUR(4), - FIVE(5), - SIX(6), - SEVEN(7), - EIGHT(8), - NINE(9), - TEN(10), - JACK(10), - QUEEN(10), - KING(10); + ACE(1, "A"), + TWO(2, "2"), + THREE(3, "3"), + FOUR(4, "4"), + FIVE(5, "5"), + SIX(6, "6"), + SEVEN(7, "7"), + EIGHT(8, "8"), + NINE(9, "9"), + TEN(10, "10"), + JACK(10, "J"), + QUEEN(10, "Q"), + KING(10, "K"); - private final int symbol; + private final int score; + private final String symbol; - Symbol(int symbol) { + Symbol(int score, String symbol) { + this.score = score; this.symbol = symbol; } - public int getSymbol() { + public int getScore() { + return score; + } + + public String getSymbol() { return symbol; } } diff --git a/src/main/java/blackjack/domain/card/Type.java b/src/main/java/blackjack/domain/card/Type.java index a8810a4b18..ead4374a17 100644 --- a/src/main/java/blackjack/domain/card/Type.java +++ b/src/main/java/blackjack/domain/card/Type.java @@ -1,8 +1,18 @@ package blackjack.domain.card; public enum Type { - SPADE, - HEART, - CLUB, - DIAMOND; + SPADE("스페이드"), + HEART("하트"), + CLUB("클럽"), + DIAMOND("다이아몬드"); + + private final String type; + + Type(String type) { + this.type = type; + } + + public String getType() { + return type; + } } diff --git a/src/main/java/blackjack/domain/user/Dealer.java b/src/main/java/blackjack/domain/user/Dealer.java index 2ca19b6e7d..7f6391a45e 100644 --- a/src/main/java/blackjack/domain/user/Dealer.java +++ b/src/main/java/blackjack/domain/user/Dealer.java @@ -1,5 +1,9 @@ package blackjack.domain.user; +import java.util.List; + +import blackjack.domain.card.Card; + public class Dealer extends User { private static final int DEALER_DRAWABLE_MAX_SCORE = 17; @@ -11,4 +15,9 @@ public Dealer(String name) { boolean canDraw() { return hand.calculateScore().isLowerThan(DEALER_DRAWABLE_MAX_SCORE); } + + @Override + public List getInitialHand() { + return hand.getCards().subList(0, 1); + } } diff --git a/src/main/java/blackjack/domain/user/Player.java b/src/main/java/blackjack/domain/user/Player.java index 959098b082..5f997ec2bf 100644 --- a/src/main/java/blackjack/domain/user/Player.java +++ b/src/main/java/blackjack/domain/user/Player.java @@ -1,5 +1,9 @@ package blackjack.domain.user; +import java.util.List; + +import blackjack.domain.card.Card; + public class Player extends User { private static final int PLAYER_DRAWABLE_MAX_SCORE = 22; @@ -11,4 +15,9 @@ public Player(String name) { boolean canDraw() { return hand.calculateScore().isLowerThan(PLAYER_DRAWABLE_MAX_SCORE); } + + @Override + public List getInitialHand() { + return super.getHand(); + } } diff --git a/src/main/java/blackjack/domain/user/User.java b/src/main/java/blackjack/domain/user/User.java index a468f5694c..8be4b0ea8b 100644 --- a/src/main/java/blackjack/domain/user/User.java +++ b/src/main/java/blackjack/domain/user/User.java @@ -53,6 +53,18 @@ public boolean isBust() { abstract boolean canDraw(); + public String getName() { + return name; + } + + public List getInitialHand() { + return getHand(); + } + + public List getHand() { + return hand.getCards(); + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/src/main/java/blackjack/domain/user/hand/Hand.java b/src/main/java/blackjack/domain/user/hand/Hand.java index 23daef427f..838571dfdd 100644 --- a/src/main/java/blackjack/domain/user/hand/Hand.java +++ b/src/main/java/blackjack/domain/user/hand/Hand.java @@ -28,4 +28,8 @@ public Score calculateScore() { } return score; } + + public List getCards() { + return cards; + } } diff --git a/src/main/java/blackjack/domain/user/hand/Score.java b/src/main/java/blackjack/domain/user/hand/Score.java index e4ae1fb3ce..b9938356ba 100644 --- a/src/main/java/blackjack/domain/user/hand/Score.java +++ b/src/main/java/blackjack/domain/user/hand/Score.java @@ -18,7 +18,7 @@ public class Score { static { Arrays.stream(Symbol.values()) - .map(Symbol::getSymbol) + .map(Symbol::getScore) .forEach(symbolValue -> CACHE.putIfAbsent(symbolValue, new Score(symbolValue))); } diff --git a/src/main/java/blackjack/util/StringUtil.java b/src/main/java/blackjack/util/StringUtil.java new file mode 100644 index 0000000000..f9bcdcbaa3 --- /dev/null +++ b/src/main/java/blackjack/util/StringUtil.java @@ -0,0 +1,26 @@ +package blackjack.util; + +import static java.util.stream.Collectors.*; + +import java.util.List; + +import blackjack.domain.card.Card; +import blackjack.domain.user.Player; +import blackjack.domain.user.User; + +public class StringUtil { + private static final String DELIMITER = ", "; + + public static String joinPlayerNames(List players) { + return players.stream() + .filter(user -> user.getClass().equals(Player.class)) + .map(User::getName) + .collect(joining(DELIMITER)); + } + + public static String joinCards(List cards) { + return cards.stream() + .map(Card::toString) + .collect(joining(DELIMITER)); + } +} diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java new file mode 100644 index 0000000000..773d60fe3b --- /dev/null +++ b/src/main/java/blackjack/view/OutputView.java @@ -0,0 +1,19 @@ +package blackjack.view; + +import java.util.List; + +import blackjack.domain.user.User; +import blackjack.util.StringUtil; + +public class OutputView { + private static final String INITIAL_DRAW_FORMAT = "dealer와 %s에게 %d장의 나누었습니다."; + + public static void printUsersInitialDraw(int initialDrawNumber, List users) { + System.out.println(String.format(INITIAL_DRAW_FORMAT, StringUtil.joinPlayerNames(users), initialDrawNumber)); + + for (User user : users) { + System.out.println(user.getName() + ": " + StringUtil.joinCards(user.getInitialHand())); + } + System.out.println(); + } +} diff --git a/src/test/java/blackjack/util/StringUtilTest.java b/src/test/java/blackjack/util/StringUtilTest.java new file mode 100644 index 0000000000..cf501b7387 --- /dev/null +++ b/src/test/java/blackjack/util/StringUtilTest.java @@ -0,0 +1,41 @@ +package blackjack.util; + +import static org.assertj.core.api.Assertions.*; + +import java.util.Arrays; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import blackjack.domain.card.Card; +import blackjack.domain.card.Symbol; +import blackjack.domain.card.Type; +import blackjack.domain.user.Dealer; +import blackjack.domain.user.Player; +import blackjack.domain.user.User; + +class StringUtilTest { + @Test + void joinPlayerNames_PlayerList_JoinPlayerNames() { + List users = Arrays.asList( + new Dealer("dealer"), + new Player("pobi"), + new Player("sony"), + new Player("stitch")); + + String expected = "pobi, sony, stitch"; + assertThat(StringUtil.joinPlayerNames(users)).isEqualTo(expected); + } + + @Test + void joinCards_CardList_JoinCards() { + List cards = Arrays.asList( + new Card(Symbol.ACE, Type.CLUB), + new Card(Symbol.TWO, Type.HEART), + new Card(Symbol.EIGHT, Type.DIAMOND) + ); + + String expected = "A클럽, 2하트, 8다이아몬드"; + assertThat(StringUtil.joinCards(cards)).isEqualTo(expected); + } +} From fcef622bd155a2eb85f42027ccc59e46b3875bd7 Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Thu, 12 Mar 2020 17:36:07 +0900 Subject: [PATCH 19/31] =?UTF-8?q?test=20:=20User=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/blackjack/domain/user/UserTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/blackjack/domain/user/UserTest.java b/src/test/java/blackjack/domain/user/UserTest.java index 6a3cd834ed..ddee59d731 100644 --- a/src/test/java/blackjack/domain/user/UserTest.java +++ b/src/test/java/blackjack/domain/user/UserTest.java @@ -35,7 +35,7 @@ void draw_DrawCard_AddDrawCardToHand() { Deck deck = new Deck(); user.draw(deck); - assertThat(user).extracting("hand.cards").asList() + assertThat(user).extracting("hand").asList() .hasOnlyElementsOfType(Card.class) .hasSize(1); } @@ -46,7 +46,7 @@ void draw_DrawNumberOfCards_AddDrawCardsToHand() { Deck deck = new Deck(); user.draw(deck, 2); - assertThat(user).extracting("hand.cards").asList() + assertThat(user).extracting("hand").asList() .hasOnlyElementsOfType(Card.class) .hasSize(2); } From 577558accaeb5908d0af1ad206f2045d9eee5790 Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Thu, 12 Mar 2020 19:39:44 +0900 Subject: [PATCH 20/31] =?UTF-8?q?refactor=20:=20User=20=EC=B6=94=EC=83=81?= =?UTF-8?q?=20=ED=81=B4=EB=9E=98=EC=8A=A4=EC=9D=98=20=EC=B6=94=EC=83=81=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=A0=91=EA=B7=BC=20=EC=A7=80?= =?UTF-8?q?=EC=A0=95=EC=9E=90=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/Application.java | 16 +++++++++++---- .../controller/BlackjackController.java | 20 +++---------------- .../java/blackjack/domain/user/Dealer.java | 2 +- .../java/blackjack/domain/user/Player.java | 2 +- src/main/java/blackjack/domain/user/User.java | 8 +++----- src/main/java/blackjack/view/OutputView.java | 5 +++-- 6 files changed, 23 insertions(+), 30 deletions(-) diff --git a/src/main/java/blackjack/Application.java b/src/main/java/blackjack/Application.java index 04cbf46635..a133d7c8a0 100644 --- a/src/main/java/blackjack/Application.java +++ b/src/main/java/blackjack/Application.java @@ -1,21 +1,29 @@ package blackjack; +import java.util.ArrayList; import java.util.List; import blackjack.controller.BlackjackController; import blackjack.domain.card.Deck; import blackjack.domain.user.Dealer; -import blackjack.domain.user.Player; import blackjack.domain.user.PlayerFactory; +import blackjack.domain.user.User; import blackjack.view.InputView; public class Application { public static void main(String[] args) { Deck deck = new Deck(); - Dealer dealer = new Dealer("dealer"); - List players = PlayerFactory.create(InputView.inputPlayerNames()); + List users = generateUsers(); - BlackjackController blackjackController = new BlackjackController(deck, dealer, players); + BlackjackController blackjackController = new BlackjackController(deck, users); blackjackController.playGame(); } + + private static List generateUsers() { + List users = new ArrayList<>(); + + users.add(new Dealer("dealer")); + users.addAll(PlayerFactory.create(InputView.inputPlayerNames())); + return users; + } } diff --git a/src/main/java/blackjack/controller/BlackjackController.java b/src/main/java/blackjack/controller/BlackjackController.java index c74ec94a9a..7fcdc9dc1e 100644 --- a/src/main/java/blackjack/controller/BlackjackController.java +++ b/src/main/java/blackjack/controller/BlackjackController.java @@ -1,11 +1,8 @@ package blackjack.controller; -import java.util.ArrayList; import java.util.List; import blackjack.domain.card.Deck; -import blackjack.domain.user.Dealer; -import blackjack.domain.user.Player; import blackjack.domain.user.User; import blackjack.view.OutputView; @@ -13,26 +10,15 @@ public class BlackjackController { private static final int INITIAL_DRAW_NUMBER = 2; private final Deck deck; - private final Dealer dealer; - private final List players; + private final List users; - public BlackjackController(Deck deck, Dealer dealer, List players) { + public BlackjackController(Deck deck, List users) { this.deck = deck; - this.dealer = dealer; - this.players = players; + this.users = users; } public void playGame() { - List users = generateUsers(); - users.forEach(user -> user.draw(deck, INITIAL_DRAW_NUMBER)); OutputView.printUsersInitialDraw(INITIAL_DRAW_NUMBER, users); } - - private List generateUsers() { - List users = new ArrayList<>(); - users.add(dealer); - users.addAll(players); - return users; - } } diff --git a/src/main/java/blackjack/domain/user/Dealer.java b/src/main/java/blackjack/domain/user/Dealer.java index 7f6391a45e..fc35cad75e 100644 --- a/src/main/java/blackjack/domain/user/Dealer.java +++ b/src/main/java/blackjack/domain/user/Dealer.java @@ -12,7 +12,7 @@ public Dealer(String name) { } @Override - boolean canDraw() { + public boolean canDraw() { return hand.calculateScore().isLowerThan(DEALER_DRAWABLE_MAX_SCORE); } diff --git a/src/main/java/blackjack/domain/user/Player.java b/src/main/java/blackjack/domain/user/Player.java index 5f997ec2bf..08c531ef09 100644 --- a/src/main/java/blackjack/domain/user/Player.java +++ b/src/main/java/blackjack/domain/user/Player.java @@ -12,7 +12,7 @@ public Player(String name) { } @Override - boolean canDraw() { + public boolean canDraw() { return hand.calculateScore().isLowerThan(PLAYER_DRAWABLE_MAX_SCORE); } diff --git a/src/main/java/blackjack/domain/user/User.java b/src/main/java/blackjack/domain/user/User.java index 8be4b0ea8b..fe5ab02a1b 100644 --- a/src/main/java/blackjack/domain/user/User.java +++ b/src/main/java/blackjack/domain/user/User.java @@ -51,16 +51,14 @@ public boolean isBust() { return hand.calculateScore().isMoreThan(BLACKJACK_SCORE); } - abstract boolean canDraw(); + public abstract boolean canDraw(); + + public abstract List getInitialHand(); public String getName() { return name; } - public List getInitialHand() { - return getHand(); - } - public List getHand() { return hand.getCards(); } diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 773d60fe3b..1ad45d67ab 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -6,14 +6,15 @@ import blackjack.util.StringUtil; public class OutputView { + private static final String NEWLINE = System.getProperty("line.separator"); private static final String INITIAL_DRAW_FORMAT = "dealer와 %s에게 %d장의 나누었습니다."; public static void printUsersInitialDraw(int initialDrawNumber, List users) { - System.out.println(String.format(INITIAL_DRAW_FORMAT, StringUtil.joinPlayerNames(users), initialDrawNumber)); + System.out.println( + String.format(NEWLINE + INITIAL_DRAW_FORMAT, StringUtil.joinPlayerNames(users), initialDrawNumber)); for (User user : users) { System.out.println(user.getName() + ": " + StringUtil.joinCards(user.getInitialHand())); } - System.out.println(); } } From 625ee56708e715fea6009ea84a7fb762a7f8e8b4 Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Thu, 12 Mar 2020 20:10:50 +0900 Subject: [PATCH 21/31] =?UTF-8?q?feat=20:=20=EA=B0=81=EA=B0=81=EC=9D=98=20?= =?UTF-8?q?=ED=94=8C=EB=A0=88=EC=9D=B4=EC=96=B4=EB=93=A4=EC=9D=B4=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=EB=A5=BC=20=EB=8D=94=20=EB=B0=9B=EC=9D=84?= =?UTF-8?q?=EC=A7=80=20=EC=97=AC=EB=B6=80=EC=97=90=20=EB=94=B0=EB=9D=BC=20?= =?UTF-8?q?draw=EB=A5=BC=20=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/Application.java | 2 +- .../controller/BlackjackController.java | 24 +++++++++++++++++++ .../java/blackjack/domain/user/Dealer.java | 1 + src/main/java/blackjack/view/InputView.java | 8 +++++++ src/main/java/blackjack/view/OutputView.java | 16 +++++++++---- 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/main/java/blackjack/Application.java b/src/main/java/blackjack/Application.java index a133d7c8a0..d1f71532c5 100644 --- a/src/main/java/blackjack/Application.java +++ b/src/main/java/blackjack/Application.java @@ -22,7 +22,7 @@ public static void main(String[] args) { private static List generateUsers() { List users = new ArrayList<>(); - users.add(new Dealer("dealer")); + users.add(new Dealer(Dealer.NAME)); users.addAll(PlayerFactory.create(InputView.inputPlayerNames())); return users; } diff --git a/src/main/java/blackjack/controller/BlackjackController.java b/src/main/java/blackjack/controller/BlackjackController.java index 7fcdc9dc1e..f319ae38e3 100644 --- a/src/main/java/blackjack/controller/BlackjackController.java +++ b/src/main/java/blackjack/controller/BlackjackController.java @@ -2,8 +2,11 @@ import java.util.List; +import blackjack.domain.DrawOpinion; import blackjack.domain.card.Deck; +import blackjack.domain.user.Player; import blackjack.domain.user.User; +import blackjack.view.InputView; import blackjack.view.OutputView; public class BlackjackController { @@ -20,5 +23,26 @@ public BlackjackController(Deck deck, List users) { public void playGame() { users.forEach(user -> user.draw(deck, INITIAL_DRAW_NUMBER)); OutputView.printUsersInitialDraw(INITIAL_DRAW_NUMBER, users); + + drawCardsEachPlayerByOpinion(); + + } + + private void drawCardsEachPlayerByOpinion() { + users.stream() + .filter(user -> user.getClass().equals(Player.class)) + .forEach(this::drawCardsByOpinion); + } + + private void drawCardsByOpinion(User player) { + while (player.canDraw()) { + DrawOpinion drawOpinion = DrawOpinion.of(InputView.inputDrawOpinion(player)); + + if (DrawOpinion.NO.equals(drawOpinion)) { + break; + } + player.draw(deck); + OutputView.printUserHand(player, player.getHand()); + } } } diff --git a/src/main/java/blackjack/domain/user/Dealer.java b/src/main/java/blackjack/domain/user/Dealer.java index fc35cad75e..42a3d19d7e 100644 --- a/src/main/java/blackjack/domain/user/Dealer.java +++ b/src/main/java/blackjack/domain/user/Dealer.java @@ -5,6 +5,7 @@ import blackjack.domain.card.Card; public class Dealer extends User { + public static final String NAME = "Dealer"; private static final int DEALER_DRAWABLE_MAX_SCORE = 17; public Dealer(String name) { diff --git a/src/main/java/blackjack/view/InputView.java b/src/main/java/blackjack/view/InputView.java index 3705fe1705..1a084f9e96 100644 --- a/src/main/java/blackjack/view/InputView.java +++ b/src/main/java/blackjack/view/InputView.java @@ -2,11 +2,19 @@ import java.util.Scanner; +import blackjack.domain.user.User; + public class InputView { private static final Scanner SCANNER = new Scanner(System.in); + private static final String DRAW_OPINION_FORMAT = "%s는 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)"; public static String inputPlayerNames() { System.out.println("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)"); return SCANNER.nextLine(); } + + public static String inputDrawOpinion(User user) { + System.out.println(String.format(DRAW_OPINION_FORMAT, user.getName())); + return SCANNER.nextLine(); + } } diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index 1ad45d67ab..c98b5ac144 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -2,19 +2,25 @@ import java.util.List; +import blackjack.domain.card.Card; +import blackjack.domain.user.Dealer; import blackjack.domain.user.User; import blackjack.util.StringUtil; public class OutputView { private static final String NEWLINE = System.getProperty("line.separator"); - private static final String INITIAL_DRAW_FORMAT = "dealer와 %s에게 %d장의 나누었습니다."; + private static final String INITIAL_DRAW_FORMAT = "%s와 %s에게 %d장의 나누었습니다."; public static void printUsersInitialDraw(int initialDrawNumber, List users) { System.out.println( - String.format(NEWLINE + INITIAL_DRAW_FORMAT, StringUtil.joinPlayerNames(users), initialDrawNumber)); + String.format(NEWLINE + INITIAL_DRAW_FORMAT, + Dealer.NAME, + StringUtil.joinPlayerNames(users), + initialDrawNumber)); + users.forEach(user -> printUserHand(user, user.getInitialHand())); + } - for (User user : users) { - System.out.println(user.getName() + ": " + StringUtil.joinCards(user.getInitialHand())); - } + public static void printUserHand(User user, List hand) { + System.out.println(user.getName() + ": " + StringUtil.joinCards(hand)); } } From c4b112f298e539508a81b730a076efac6a44c11d Mon Sep 17 00:00:00 2001 From: lxxjn0 Date: Thu, 12 Mar 2020 20:34:14 +0900 Subject: [PATCH 22/31] =?UTF-8?q?feat=20:=20=EB=94=9C=EB=9F=AC=EA=B0=80=20?= =?UTF-8?q?=EC=B9=B4=EB=93=9C=EB=A5=BC=20=EB=8D=94=20=EB=BD=91=EB=8A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/Application.java | 16 ++------ .../controller/BlackjackController.java | 38 +++++++++++++------ .../java/blackjack/domain/user/Dealer.java | 2 +- .../domain/user/InvalidUserException.java | 1 + .../java/blackjack/domain/user/Player.java | 2 +- .../blackjack/domain/user/hand/Score.java | 2 +- src/main/java/blackjack/view/OutputView.java | 5 +++ .../blackjack/domain/user/hand/ScoreTest.java | 2 +- 8 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/main/java/blackjack/Application.java b/src/main/java/blackjack/Application.java index d1f71532c5..8672b81669 100644 --- a/src/main/java/blackjack/Application.java +++ b/src/main/java/blackjack/Application.java @@ -1,29 +1,21 @@ package blackjack; -import java.util.ArrayList; import java.util.List; import blackjack.controller.BlackjackController; import blackjack.domain.card.Deck; import blackjack.domain.user.Dealer; +import blackjack.domain.user.Player; import blackjack.domain.user.PlayerFactory; -import blackjack.domain.user.User; import blackjack.view.InputView; public class Application { public static void main(String[] args) { Deck deck = new Deck(); - List users = generateUsers(); + Dealer dealer = new Dealer(Dealer.NAME); + List players = PlayerFactory.create(InputView.inputPlayerNames()); - BlackjackController blackjackController = new BlackjackController(deck, users); + BlackjackController blackjackController = new BlackjackController(deck, dealer, players); blackjackController.playGame(); } - - private static List generateUsers() { - List users = new ArrayList<>(); - - users.add(new Dealer(Dealer.NAME)); - users.addAll(PlayerFactory.create(InputView.inputPlayerNames())); - return users; - } } diff --git a/src/main/java/blackjack/controller/BlackjackController.java b/src/main/java/blackjack/controller/BlackjackController.java index f319ae38e3..907cf8317c 100644 --- a/src/main/java/blackjack/controller/BlackjackController.java +++ b/src/main/java/blackjack/controller/BlackjackController.java @@ -1,9 +1,11 @@ package blackjack.controller; +import java.util.ArrayList; import java.util.List; import blackjack.domain.DrawOpinion; import blackjack.domain.card.Deck; +import blackjack.domain.user.Dealer; import blackjack.domain.user.Player; import blackjack.domain.user.User; import blackjack.view.InputView; @@ -13,28 +15,35 @@ public class BlackjackController { private static final int INITIAL_DRAW_NUMBER = 2; private final Deck deck; - private final List users; + private final Dealer dealer; + private final List players; - public BlackjackController(Deck deck, List users) { + public BlackjackController(Deck deck, Dealer dealer, List players) { this.deck = deck; - this.users = users; + this.dealer = dealer; + this.players = players; } public void playGame() { - users.forEach(user -> user.draw(deck, INITIAL_DRAW_NUMBER)); - OutputView.printUsersInitialDraw(INITIAL_DRAW_NUMBER, users); + drawInitialCardsEachUser(); + drawCardsEachUsers(); + } - drawCardsEachPlayerByOpinion(); + private void drawInitialCardsEachUser() { + List users = new ArrayList<>(); + users.add(dealer); + users.addAll(players); + players.forEach(user -> user.draw(deck, INITIAL_DRAW_NUMBER)); + OutputView.printUsersInitialDraw(INITIAL_DRAW_NUMBER, users); } - private void drawCardsEachPlayerByOpinion() { - users.stream() - .filter(user -> user.getClass().equals(Player.class)) - .forEach(this::drawCardsByOpinion); + private void drawCardsEachUsers() { + players.forEach(this::drawCardsByOpinion); + drawCardsDealer(); } - private void drawCardsByOpinion(User player) { + private void drawCardsByOpinion(Player player) { while (player.canDraw()) { DrawOpinion drawOpinion = DrawOpinion.of(InputView.inputDrawOpinion(player)); @@ -45,4 +54,11 @@ private void drawCardsByOpinion(User player) { OutputView.printUserHand(player, player.getHand()); } } + + private void drawCardsDealer() { + while (dealer.canDraw()) { + dealer.draw(deck); + OutputView.printDealerDrawCard(); + } + } } diff --git a/src/main/java/blackjack/domain/user/Dealer.java b/src/main/java/blackjack/domain/user/Dealer.java index 42a3d19d7e..a1122b4ae6 100644 --- a/src/main/java/blackjack/domain/user/Dealer.java +++ b/src/main/java/blackjack/domain/user/Dealer.java @@ -6,7 +6,7 @@ public class Dealer extends User { public static final String NAME = "Dealer"; - private static final int DEALER_DRAWABLE_MAX_SCORE = 17; + public static final int DEALER_DRAWABLE_MAX_SCORE = 16; public Dealer(String name) { super(name); diff --git a/src/main/java/blackjack/domain/user/InvalidUserException.java b/src/main/java/blackjack/domain/user/InvalidUserException.java index 3f93026248..dd2f288f44 100644 --- a/src/main/java/blackjack/domain/user/InvalidUserException.java +++ b/src/main/java/blackjack/domain/user/InvalidUserException.java @@ -3,6 +3,7 @@ public class InvalidUserException extends IllegalArgumentException { public static final String NULL_OR_EMPTY = "유효하지 않은 유저의 이름입니다."; public static final String INVALID_DRAW_NUMBER = "뽑을 수 있는 카드 수가 유효하지 않습니다."; + public static final String NOT_EXIST = "유저가 존재하지 않습니다."; public InvalidUserException(String s) { super(s); diff --git a/src/main/java/blackjack/domain/user/Player.java b/src/main/java/blackjack/domain/user/Player.java index 08c531ef09..2bd8223446 100644 --- a/src/main/java/blackjack/domain/user/Player.java +++ b/src/main/java/blackjack/domain/user/Player.java @@ -5,7 +5,7 @@ import blackjack.domain.card.Card; public class Player extends User { - private static final int PLAYER_DRAWABLE_MAX_SCORE = 22; + private static final int PLAYER_DRAWABLE_MAX_SCORE = 21; public Player(String name) { super(name); diff --git a/src/main/java/blackjack/domain/user/hand/Score.java b/src/main/java/blackjack/domain/user/hand/Score.java index b9938356ba..c6dad05bb9 100644 --- a/src/main/java/blackjack/domain/user/hand/Score.java +++ b/src/main/java/blackjack/domain/user/hand/Score.java @@ -50,7 +50,7 @@ public Score add(Card card) { } public boolean isLowerThan(int score) { - return this.score < score; + return this.score <= score; } public boolean isMoreThan(int score) { diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index c98b5ac144..cde94fe251 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -10,6 +10,7 @@ public class OutputView { private static final String NEWLINE = System.getProperty("line.separator"); private static final String INITIAL_DRAW_FORMAT = "%s와 %s에게 %d장의 나누었습니다."; + private static final String DEALER_DRAW_FORMAT = "%s는 %d이하라 한장의 카드를 더 받았습니다."; public static void printUsersInitialDraw(int initialDrawNumber, List users) { System.out.println( @@ -23,4 +24,8 @@ public static void printUsersInitialDraw(int initialDrawNumber, List users public static void printUserHand(User user, List hand) { System.out.println(user.getName() + ": " + StringUtil.joinCards(hand)); } + + public static void printDealerDrawCard() { + System.out.println(String.format(DEALER_DRAW_FORMAT, Dealer.NAME, Dealer.DEALER_DRAWABLE_MAX_SCORE)); + } } diff --git a/src/test/java/blackjack/domain/user/hand/ScoreTest.java b/src/test/java/blackjack/domain/user/hand/ScoreTest.java index 239708cb97..0815b0f9fc 100644 --- a/src/test/java/blackjack/domain/user/hand/ScoreTest.java +++ b/src/test/java/blackjack/domain/user/hand/ScoreTest.java @@ -49,7 +49,7 @@ void add_ACECardAddBy1_addScore() { } @ParameterizedTest - @CsvSource(value = {"9,false", "10,true"}) + @CsvSource(value = {"8,false", "9,true"}) void isLowerThan_InputIntegerScore_ReturnCompareResult(int value, boolean expected) { Score score = Score.valueOf(9); From 6cbf3ef3445d9ce6a5a413ccc2659e0ac70a5e33 Mon Sep 17 00:00:00 2001 From: junyoung lee Date: Fri, 13 Mar 2020 14:10:52 +0900 Subject: [PATCH 23/31] =?UTF-8?q?feat=20:=20=EC=9C=A0=EC=A0=80=EB=93=A4?= =?UTF-8?q?=EC=9D=98=20=EC=B5=9C=EC=A2=85=20=EC=B9=B4=EB=93=9C=EC=99=80=20?= =?UTF-8?q?=EC=A0=90=EC=88=98=EB=A5=BC=20=EC=B6=9C=EB=A0=A5=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackjackController.java | 17 ++++++++++++++--- src/main/java/blackjack/domain/user/User.java | 4 ++++ .../java/blackjack/domain/user/hand/Score.java | 4 ++++ src/main/java/blackjack/view/OutputView.java | 4 ++++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackjackController.java b/src/main/java/blackjack/controller/BlackjackController.java index 907cf8317c..2dcb9a6d15 100644 --- a/src/main/java/blackjack/controller/BlackjackController.java +++ b/src/main/java/blackjack/controller/BlackjackController.java @@ -25,16 +25,21 @@ public BlackjackController(Deck deck, Dealer dealer, List players) { } public void playGame() { - drawInitialCardsEachUser(); + List users = generateUsers(); + drawInitialCardsEachUser(users); drawCardsEachUsers(); + printUsersCardsAndScore(users); } - private void drawInitialCardsEachUser() { + private List generateUsers() { List users = new ArrayList<>(); users.add(dealer); users.addAll(players); + return users; + } - players.forEach(user -> user.draw(deck, INITIAL_DRAW_NUMBER)); + private void drawInitialCardsEachUser(List users) { + users.forEach(user -> user.draw(deck, INITIAL_DRAW_NUMBER)); OutputView.printUsersInitialDraw(INITIAL_DRAW_NUMBER, users); } @@ -61,4 +66,10 @@ private void drawCardsDealer() { OutputView.printDealerDrawCard(); } } + + private void printUsersCardsAndScore(List users) { + for (User user : users) { + OutputView.printUserHandAndScore(user); + } + } } diff --git a/src/main/java/blackjack/domain/user/User.java b/src/main/java/blackjack/domain/user/User.java index fe5ab02a1b..326e071f88 100644 --- a/src/main/java/blackjack/domain/user/User.java +++ b/src/main/java/blackjack/domain/user/User.java @@ -63,6 +63,10 @@ public List getHand() { return hand.getCards(); } + public int getScore() { + return hand.calculateScore().getScore(); + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/src/main/java/blackjack/domain/user/hand/Score.java b/src/main/java/blackjack/domain/user/hand/Score.java index c6dad05bb9..3fae1ea1cd 100644 --- a/src/main/java/blackjack/domain/user/hand/Score.java +++ b/src/main/java/blackjack/domain/user/hand/Score.java @@ -57,6 +57,10 @@ public boolean isMoreThan(int score) { return this.score > score; } + public int getScore() { + return score; + } + @Override public boolean equals(Object object) { if (this == object) { diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index cde94fe251..aa45cb9f7f 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -28,4 +28,8 @@ public static void printUserHand(User user, List hand) { public static void printDealerDrawCard() { System.out.println(String.format(DEALER_DRAW_FORMAT, Dealer.NAME, Dealer.DEALER_DRAWABLE_MAX_SCORE)); } + + public static void printUserHandAndScore(User user) { + System.out.println(user.getName() + ": " + StringUtil.joinCards(user.getHand()) + " - 결과: " + user.getScore()); + } } From c2581d2b85304a02af177a15ac5bd607eb326234 Mon Sep 17 00:00:00 2001 From: junyoung lee Date: Fri, 13 Mar 2020 14:27:36 +0900 Subject: [PATCH 24/31] =?UTF-8?q?feat=20:=20ResultType=20=EC=97=B4?= =?UTF-8?q?=EA=B1=B0=ED=98=95=20=EC=9D=B8=EC=8A=A4=ED=84=B4=EC=8A=A4=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/domain/result/ResultType.java | 25 +++++++++++++++++ .../blackjack/domain/user/hand/Score.java | 4 +++ .../domain/result/ResultTypeTest.java | 27 +++++++++++++++++++ .../blackjack/domain/user/hand/ScoreTest.java | 9 +++++++ 4 files changed, 65 insertions(+) create mode 100644 src/main/java/blackjack/domain/result/ResultType.java create mode 100644 src/test/java/blackjack/domain/result/ResultTypeTest.java diff --git a/src/main/java/blackjack/domain/result/ResultType.java b/src/main/java/blackjack/domain/result/ResultType.java new file mode 100644 index 0000000000..d8c98b8e64 --- /dev/null +++ b/src/main/java/blackjack/domain/result/ResultType.java @@ -0,0 +1,25 @@ +package blackjack.domain.result; + +import blackjack.domain.user.hand.Score; + +public enum ResultType { + WIN("승"), + DRAW("무"), + LOSE("패"); + + private final String type; + + ResultType(String type) { + this.type = type; + } + + public static ResultType from(Score dealerScore, Score playerScore) { + if (dealerScore.isMoreThan(playerScore)) { + return LOSE; + } + if (playerScore.isMoreThan(dealerScore)) { + return WIN; + } + return DRAW; + } +} diff --git a/src/main/java/blackjack/domain/user/hand/Score.java b/src/main/java/blackjack/domain/user/hand/Score.java index 3fae1ea1cd..7b38c64e6f 100644 --- a/src/main/java/blackjack/domain/user/hand/Score.java +++ b/src/main/java/blackjack/domain/user/hand/Score.java @@ -57,6 +57,10 @@ public boolean isMoreThan(int score) { return this.score > score; } + public boolean isMoreThan(Score score) { + return this.score > score.score; + } + public int getScore() { return score; } diff --git a/src/test/java/blackjack/domain/result/ResultTypeTest.java b/src/test/java/blackjack/domain/result/ResultTypeTest.java new file mode 100644 index 0000000000..95b592c846 --- /dev/null +++ b/src/test/java/blackjack/domain/result/ResultTypeTest.java @@ -0,0 +1,27 @@ +package blackjack.domain.result; + +import static org.assertj.core.api.Assertions.*; + +import java.util.stream.Stream; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import blackjack.domain.user.hand.Score; + +class ResultTypeTest { + + @ParameterizedTest + @MethodSource("provideDealerAndPlayerScoreWithReturnType") + void from_DealerScoreAndPlayerScore_ReturnResultType(Score dealerScore, Score playerScore, ResultType expected) { + assertThat(ResultType.from(dealerScore, playerScore)).isEqualTo(expected); + } + + private static Stream provideDealerAndPlayerScoreWithReturnType() { + return Stream.of( + Arguments.arguments(Score.valueOf(10), Score.valueOf(11), ResultType.WIN), + Arguments.arguments(Score.valueOf(10), Score.valueOf(10), ResultType.DRAW), + Arguments.arguments(Score.valueOf(11), Score.valueOf(10), ResultType.LOSE)); + } +} diff --git a/src/test/java/blackjack/domain/user/hand/ScoreTest.java b/src/test/java/blackjack/domain/user/hand/ScoreTest.java index 0815b0f9fc..c5997ba843 100644 --- a/src/test/java/blackjack/domain/user/hand/ScoreTest.java +++ b/src/test/java/blackjack/domain/user/hand/ScoreTest.java @@ -63,4 +63,13 @@ void isMoreThan_InputIntegerScore_ReturnCompareResult(int value, boolean expecte assertThat(score.isMoreThan(value)).isEqualTo(expected); } + + @ParameterizedTest + @CsvSource(value = {"8,true", "9,false"}) + void isMoreThan_InputScore_ReturnCompareResult(int value, boolean expected) { + Score score = Score.valueOf(9); + Score cmpScore = Score.valueOf(value); + + assertThat(score.isMoreThan(cmpScore)).isEqualTo(expected); + } } From f8397a4289f73e07187719260e04b44fe053498b Mon Sep 17 00:00:00 2001 From: junyoung lee Date: Fri, 13 Mar 2020 17:18:47 +0900 Subject: [PATCH 25/31] =?UTF-8?q?feat=20:=20=EC=9C=A0=EC=A0=80=EB=93=A4?= =?UTF-8?q?=EC=9D=98=20=EC=B5=9C=EC=A2=85=20=EA=B2=B0=EA=B3=BC=EB=A5=BC=20?= =?UTF-8?q?=EA=B5=AC=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Report 인스턴스 구현. 2. ResultType 열거형 인스턴스의 from 메서드 수정. 3. 테스트를 위한 Dealer, Player 인스턴스의 정적 팩토리 메서드 구현. --- .../java/blackjack/domain/result/Report.java | 37 ++++++++++ .../blackjack/domain/result/ResultType.java | 22 +++--- .../java/blackjack/domain/user/Dealer.java | 8 +++ .../java/blackjack/domain/user/Player.java | 8 +++ src/main/java/blackjack/domain/user/User.java | 5 ++ .../blackjack/domain/result/ReportTest.java | 67 +++++++++++++++++++ .../domain/result/ResultTypeTest.java | 30 +++++++-- .../blackjack/domain/user/DealerTest.java | 16 ++++- .../blackjack/domain/user/PlayerTest.java | 15 +++++ 9 files changed, 191 insertions(+), 17 deletions(-) create mode 100644 src/main/java/blackjack/domain/result/Report.java create mode 100644 src/test/java/blackjack/domain/result/ReportTest.java diff --git a/src/main/java/blackjack/domain/result/Report.java b/src/main/java/blackjack/domain/result/Report.java new file mode 100644 index 0000000000..58c0809863 --- /dev/null +++ b/src/main/java/blackjack/domain/result/Report.java @@ -0,0 +1,37 @@ +package blackjack.domain.result; + +import static java.util.stream.Collectors.*; + +import java.util.List; +import java.util.Map; +import java.util.function.Function; + +import blackjack.domain.user.Dealer; +import blackjack.domain.user.Player; + +public class Report { + private final Map dealerResult; + private final Map playersResult; + + private Report(Map dealerResult, Map playersResult) { + this.dealerResult = dealerResult; + this.playersResult = playersResult; + } + + public static Report from(Dealer dealer, List players) { + return new Report( + generateDealerResult(dealer, players), + generatePlayersResult(dealer, players)); + } + + private static Map generatePlayersResult(Dealer dealer, List players) { + return players.stream() + .collect(toMap(Function.identity(), player -> ResultType.from(player, dealer))); + } + + private static Map generateDealerResult(Dealer dealer, List players) { + return players.stream() + .map(player -> ResultType.from(dealer, player)) + .collect(groupingBy(Function.identity(), counting())); + } +} diff --git a/src/main/java/blackjack/domain/result/ResultType.java b/src/main/java/blackjack/domain/result/ResultType.java index d8c98b8e64..8ffc733ae7 100644 --- a/src/main/java/blackjack/domain/result/ResultType.java +++ b/src/main/java/blackjack/domain/result/ResultType.java @@ -1,25 +1,29 @@ package blackjack.domain.result; -import blackjack.domain.user.hand.Score; +import blackjack.domain.user.User; public enum ResultType { WIN("승"), DRAW("무"), LOSE("패"); - private final String type; + private final String alias; - ResultType(String type) { - this.type = type; + ResultType(String alias) { + this.alias = alias; } - public static ResultType from(Score dealerScore, Score playerScore) { - if (dealerScore.isMoreThan(playerScore)) { - return LOSE; - } - if (playerScore.isMoreThan(dealerScore)) { + public static ResultType from(User targetUser, User compareUser) { + if (targetUser.getScore() > compareUser.getScore()) { return WIN; } + if (targetUser.getScore() < compareUser.getScore()) { + return LOSE; + } return DRAW; } + + public String getAlias() { + return alias; + } } diff --git a/src/main/java/blackjack/domain/user/Dealer.java b/src/main/java/blackjack/domain/user/Dealer.java index a1122b4ae6..658ffa3d2d 100644 --- a/src/main/java/blackjack/domain/user/Dealer.java +++ b/src/main/java/blackjack/domain/user/Dealer.java @@ -12,6 +12,14 @@ public Dealer(String name) { super(name); } + Dealer(String name, List cards) { + super(name, cards); + } + + public static Dealer valueOf(String name, List cards) { + return new Dealer(name, cards); + } + @Override public boolean canDraw() { return hand.calculateScore().isLowerThan(DEALER_DRAWABLE_MAX_SCORE); diff --git a/src/main/java/blackjack/domain/user/Player.java b/src/main/java/blackjack/domain/user/Player.java index 2bd8223446..b22f90cf4a 100644 --- a/src/main/java/blackjack/domain/user/Player.java +++ b/src/main/java/blackjack/domain/user/Player.java @@ -11,6 +11,14 @@ public Player(String name) { super(name); } + Player(String name, List cards) { + super(name, cards); + } + + public static Player valueOf(String name, List cards) { + return new Player(name, cards); + } + @Override public boolean canDraw() { return hand.calculateScore().isLowerThan(PLAYER_DRAWABLE_MAX_SCORE); diff --git a/src/main/java/blackjack/domain/user/User.java b/src/main/java/blackjack/domain/user/User.java index 326e071f88..e023abbcd9 100644 --- a/src/main/java/blackjack/domain/user/User.java +++ b/src/main/java/blackjack/domain/user/User.java @@ -23,6 +23,11 @@ public User(String name) { this.hand = new Hand(); } + User(String name, List cards) { + this(name); + this.hand.add(cards); + } + private void validate(String name) { if (Objects.isNull(name) || name.trim().isEmpty()) { throw new InvalidUserException(InvalidUserException.NULL_OR_EMPTY); diff --git a/src/test/java/blackjack/domain/result/ReportTest.java b/src/test/java/blackjack/domain/result/ReportTest.java new file mode 100644 index 0000000000..013d7771fb --- /dev/null +++ b/src/test/java/blackjack/domain/result/ReportTest.java @@ -0,0 +1,67 @@ +package blackjack.domain.result; + +import static org.assertj.core.api.Assertions.*; + +import java.util.Arrays; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import blackjack.domain.card.Card; +import blackjack.domain.card.Symbol; +import blackjack.domain.card.Type; +import blackjack.domain.user.Dealer; +import blackjack.domain.user.Player; +import blackjack.domain.user.PlayerFactory; + +class ReportTest { + private Dealer dealer; + private List players; + + @BeforeEach + void setUp() { + List dealerCards = Arrays.asList(new Card(Symbol.EIGHT, Type.HEART), new Card(Symbol.KING, Type.DIAMOND)); + List pobiCards = Arrays.asList(new Card(Symbol.QUEEN, Type.HEART), new Card(Symbol.KING, Type.DIAMOND)); + List sonyCards = Arrays.asList(new Card(Symbol.EIGHT, Type.HEART), new Card(Symbol.KING, Type.DIAMOND)); + List stitchCards = Arrays.asList(new Card(Symbol.SEVEN, Type.HEART), new Card(Symbol.KING, Type.DIAMOND)); + + Player pobi = Player.valueOf("pobi", pobiCards); + Player sony = Player.valueOf("sony", sonyCards); + Player stitch = Player.valueOf("stitch", stitchCards); + + dealer = Dealer.valueOf("dealer", dealerCards); + players = Arrays.asList(pobi, sony, stitch); + } + + @Test + void from_DealerAndPlayers_GenerateInstance() { + assertThat(Report.from(new Dealer("dealer"), PlayerFactory.create("pobi, sony, stitch"))) + .isInstanceOf(Report.class); + } + + @Test + void generatePlayersResult_dealerAndPlayers_MapOfPlayerAndResultType() { + Report report = Report.from(dealer, players); + + Map expected = new HashMap<>(); + expected.put(players.get(0), ResultType.WIN); + expected.put(players.get(1), ResultType.DRAW); + expected.put(players.get(2), ResultType.LOSE); + assertThat(report).extracting("playersResult").isEqualTo(expected); + } + + @Test + void generateDealerResult_dealerAndPlayers_MapOfResultTypeAndCount() { + Report report = Report.from(dealer, players); + + Map expected = new EnumMap<>(ResultType.class); + expected.put(ResultType.WIN, 1L); + expected.put(ResultType.DRAW, 1L); + expected.put(ResultType.LOSE, 1L); + assertThat(report).extracting("dealerResult").isEqualTo(expected); + } +} diff --git a/src/test/java/blackjack/domain/result/ResultTypeTest.java b/src/test/java/blackjack/domain/result/ResultTypeTest.java index 95b592c846..515d93589a 100644 --- a/src/test/java/blackjack/domain/result/ResultTypeTest.java +++ b/src/test/java/blackjack/domain/result/ResultTypeTest.java @@ -2,26 +2,42 @@ import static org.assertj.core.api.Assertions.*; +import java.util.Arrays; +import java.util.List; import java.util.stream.Stream; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import blackjack.domain.user.hand.Score; +import blackjack.domain.card.Card; +import blackjack.domain.card.Symbol; +import blackjack.domain.card.Type; +import blackjack.domain.user.Dealer; +import blackjack.domain.user.Player; +import blackjack.domain.user.User; class ResultTypeTest { - @ParameterizedTest @MethodSource("provideDealerAndPlayerScoreWithReturnType") - void from_DealerScoreAndPlayerScore_ReturnResultType(Score dealerScore, Score playerScore, ResultType expected) { - assertThat(ResultType.from(dealerScore, playerScore)).isEqualTo(expected); + void from_DealerScoreAndPlayerScore_ReturnResultType(User targetUser, User compareUser, ResultType expected) { + assertThat(ResultType.from(targetUser, compareUser)).isEqualTo(expected); } private static Stream provideDealerAndPlayerScoreWithReturnType() { + List dealerCards = Arrays.asList(new Card(Symbol.EIGHT, Type.HEART), new Card(Symbol.KING, Type.DIAMOND)); + List pobiCards = Arrays.asList(new Card(Symbol.QUEEN, Type.HEART), new Card(Symbol.KING, Type.DIAMOND)); + List sonyCards = Arrays.asList(new Card(Symbol.EIGHT, Type.HEART), new Card(Symbol.KING, Type.DIAMOND)); + List stitchCards = Arrays.asList(new Card(Symbol.SEVEN, Type.HEART), new Card(Symbol.KING, Type.DIAMOND)); + + Player pobi = Player.valueOf("pobi", pobiCards); + Player sony = Player.valueOf("sony", sonyCards); + Player stitch = Player.valueOf("stitch", stitchCards); + + Dealer dealer = Dealer.valueOf("dealer", dealerCards); return Stream.of( - Arguments.arguments(Score.valueOf(10), Score.valueOf(11), ResultType.WIN), - Arguments.arguments(Score.valueOf(10), Score.valueOf(10), ResultType.DRAW), - Arguments.arguments(Score.valueOf(11), Score.valueOf(10), ResultType.LOSE)); + Arguments.arguments(dealer, pobi, ResultType.LOSE), + Arguments.arguments(dealer, sony, ResultType.DRAW), + Arguments.arguments(dealer, stitch, ResultType.WIN)); } } diff --git a/src/test/java/blackjack/domain/user/DealerTest.java b/src/test/java/blackjack/domain/user/DealerTest.java index 651d1473c5..665d7ac752 100644 --- a/src/test/java/blackjack/domain/user/DealerTest.java +++ b/src/test/java/blackjack/domain/user/DealerTest.java @@ -2,6 +2,8 @@ import static org.assertj.core.api.Assertions.*; +import java.util.Arrays; +import java.util.List; import java.util.stream.Stream; import org.junit.jupiter.api.Test; @@ -9,10 +11,12 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import blackjack.domain.card.Card; import blackjack.domain.card.Deck; +import blackjack.domain.card.Symbol; +import blackjack.domain.card.Type; class DealerTest { - @Test void Dealer_InputDealerName_GenerateInstance() { Dealer dealer = new Dealer("dealer"); @@ -21,6 +25,16 @@ void Dealer_InputDealerName_GenerateInstance() { assertThat(dealer).isInstanceOf(User.class); } + @Test + void valueOf_InputDealerNameAndCards_GenerateInstance() { + List cards = Arrays.asList( + new Card(Symbol.SEVEN, Type.CLUB), + new Card(Symbol.TWO, Type.DIAMOND)); + + assertThat(new Dealer("dealer", cards)).isInstanceOf(Dealer.class) + .extracting("hand").isEqualTo(cards); + } + @Test void canDraw_CurrentScoreLowerThanDrawableMaxScore_ReturnTrue() { assertThat(new Dealer("dealer").canDraw()).isTrue(); diff --git a/src/test/java/blackjack/domain/user/PlayerTest.java b/src/test/java/blackjack/domain/user/PlayerTest.java index cd8a8da8ec..1389922b22 100644 --- a/src/test/java/blackjack/domain/user/PlayerTest.java +++ b/src/test/java/blackjack/domain/user/PlayerTest.java @@ -2,6 +2,8 @@ import static org.assertj.core.api.Assertions.*; +import java.util.Arrays; +import java.util.List; import java.util.stream.Stream; import org.junit.jupiter.api.Test; @@ -9,7 +11,10 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import blackjack.domain.card.Card; import blackjack.domain.card.Deck; +import blackjack.domain.card.Symbol; +import blackjack.domain.card.Type; class PlayerTest { @Test @@ -17,6 +22,16 @@ void Player_InputPlayerName_GenerateInstance() { assertThat(new Player("player")).isInstanceOf(Player.class); } + @Test + void valueOf_InputPlayerNameAndCards_GenerateInstance() { + List cards = Arrays.asList( + new Card(Symbol.SEVEN, Type.CLUB), + new Card(Symbol.TWO, Type.DIAMOND)); + + assertThat(new Player("player", cards)).isInstanceOf(Player.class) + .extracting("hand").isEqualTo(cards); + } + @Test void canDraw_CurrentScoreLowerThanDrawableMaxScore_ReturnTrue() { assertThat(new Player("player").canDraw()).isTrue(); From a2d19cd4ffa71140c9fa9f0ef72ed87672c53c9b Mon Sep 17 00:00:00 2001 From: junyoung lee Date: Fri, 13 Mar 2020 17:50:40 +0900 Subject: [PATCH 26/31] =?UTF-8?q?feat=20:=20=EC=9C=A0=EC=A0=80=EB=93=A4?= =?UTF-8?q?=EC=9D=98=20=EC=B5=9C=EC=A2=85=20=EA=B2=B0=EA=B3=BC=EB=A5=BC=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackjackController.java | 4 ++++ .../java/blackjack/domain/result/Report.java | 8 +++++++ src/main/java/blackjack/util/StringUtil.java | 19 +++++++++++++++ src/main/java/blackjack/view/OutputView.java | 23 +++++++++++++++++-- .../java/blackjack/util/StringUtilTest.java | 17 ++++++++++++-- 5 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackjackController.java b/src/main/java/blackjack/controller/BlackjackController.java index 2dcb9a6d15..4a45bc3c94 100644 --- a/src/main/java/blackjack/controller/BlackjackController.java +++ b/src/main/java/blackjack/controller/BlackjackController.java @@ -5,6 +5,7 @@ import blackjack.domain.DrawOpinion; import blackjack.domain.card.Deck; +import blackjack.domain.result.Report; import blackjack.domain.user.Dealer; import blackjack.domain.user.Player; import blackjack.domain.user.User; @@ -29,6 +30,9 @@ public void playGame() { drawInitialCardsEachUser(users); drawCardsEachUsers(); printUsersCardsAndScore(users); + + Report blackJackReport = Report.from(dealer, players); + OutputView.printBlackjackReport(blackJackReport); } private List generateUsers() { diff --git a/src/main/java/blackjack/domain/result/Report.java b/src/main/java/blackjack/domain/result/Report.java index 58c0809863..164744b1eb 100644 --- a/src/main/java/blackjack/domain/result/Report.java +++ b/src/main/java/blackjack/domain/result/Report.java @@ -34,4 +34,12 @@ private static Map generateDealerResult(Dealer dealer, List ResultType.from(dealer, player)) .collect(groupingBy(Function.identity(), counting())); } + + public Map getDealerResult() { + return dealerResult; + } + + public Map getPlayersResult() { + return playersResult; + } } diff --git a/src/main/java/blackjack/util/StringUtil.java b/src/main/java/blackjack/util/StringUtil.java index f9bcdcbaa3..589abebf3e 100644 --- a/src/main/java/blackjack/util/StringUtil.java +++ b/src/main/java/blackjack/util/StringUtil.java @@ -3,13 +3,18 @@ import static java.util.stream.Collectors.*; import java.util.List; +import java.util.Map; import blackjack.domain.card.Card; +import blackjack.domain.result.ResultType; +import blackjack.domain.user.Dealer; import blackjack.domain.user.Player; import blackjack.domain.user.User; public class StringUtil { private static final String DELIMITER = ", "; + private static final String SEPARATOR = ": "; + private static final String SPACE = " "; public static String joinPlayerNames(List players) { return players.stream() @@ -23,4 +28,18 @@ public static String joinCards(List cards) { .map(Card::toString) .collect(joining(DELIMITER)); } + + public static String joinDealerResult(Map dealerResult) { + return Dealer.NAME + SEPARATOR + joinResultTypeWithCount(dealerResult); + } + + private static String joinResultTypeWithCount(Map dealerResult) { + return dealerResult.entrySet().stream() + .map(entry -> { + long count = entry.getValue(); + ResultType resultType = entry.getKey(); + return count + resultType.getAlias(); + }) + .collect(joining(SPACE)); + } } diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index aa45cb9f7f..a7dc0b0b2e 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -1,9 +1,13 @@ package blackjack.view; import java.util.List; +import java.util.Map; import blackjack.domain.card.Card; +import blackjack.domain.result.Report; +import blackjack.domain.result.ResultType; import blackjack.domain.user.Dealer; +import blackjack.domain.user.Player; import blackjack.domain.user.User; import blackjack.util.StringUtil; @@ -11,6 +15,8 @@ public class OutputView { private static final String NEWLINE = System.getProperty("line.separator"); private static final String INITIAL_DRAW_FORMAT = "%s와 %s에게 %d장의 나누었습니다."; private static final String DEALER_DRAW_FORMAT = "%s는 %d이하라 한장의 카드를 더 받았습니다."; + private static final String FINAL_RESULT_MESSAGE = "## 최종 승패"; + private static final String SEPARATOR = ": "; public static void printUsersInitialDraw(int initialDrawNumber, List users) { System.out.println( @@ -22,7 +28,7 @@ public static void printUsersInitialDraw(int initialDrawNumber, List users } public static void printUserHand(User user, List hand) { - System.out.println(user.getName() + ": " + StringUtil.joinCards(hand)); + System.out.println(user.getName() + SEPARATOR + StringUtil.joinCards(hand)); } public static void printDealerDrawCard() { @@ -30,6 +36,19 @@ public static void printDealerDrawCard() { } public static void printUserHandAndScore(User user) { - System.out.println(user.getName() + ": " + StringUtil.joinCards(user.getHand()) + " - 결과: " + user.getScore()); + System.out.println(user.getName() + SEPARATOR + + StringUtil.joinCards(user.getHand()) + + " - 결과: " + user.getScore()); + } + + public static void printBlackjackReport(Report blackJackReport) { + System.out.println(NEWLINE + FINAL_RESULT_MESSAGE); + System.out.println(StringUtil.joinDealerResult(blackJackReport.getDealerResult())); + printPlayersResult(blackJackReport.getPlayersResult()); + } + + private static void printPlayersResult(Map playersResult) { + playersResult.forEach((player, resultType) -> + System.out.println(player.getName() + SEPARATOR + resultType.getAlias())); } } diff --git a/src/test/java/blackjack/util/StringUtilTest.java b/src/test/java/blackjack/util/StringUtilTest.java index cf501b7387..38ea6cc39c 100644 --- a/src/test/java/blackjack/util/StringUtilTest.java +++ b/src/test/java/blackjack/util/StringUtilTest.java @@ -3,20 +3,23 @@ import static org.assertj.core.api.Assertions.*; import java.util.Arrays; +import java.util.EnumMap; import java.util.List; +import java.util.Map; import org.junit.jupiter.api.Test; import blackjack.domain.card.Card; import blackjack.domain.card.Symbol; import blackjack.domain.card.Type; +import blackjack.domain.result.ResultType; import blackjack.domain.user.Dealer; import blackjack.domain.user.Player; import blackjack.domain.user.User; class StringUtilTest { @Test - void joinPlayerNames_PlayerList_JoinPlayerNames() { + void joinPlayerNames_PlayerList_JoinPlayerNamesToString() { List users = Arrays.asList( new Dealer("dealer"), new Player("pobi"), @@ -28,7 +31,7 @@ void joinPlayerNames_PlayerList_JoinPlayerNames() { } @Test - void joinCards_CardList_JoinCards() { + void joinCards_CardList_JoinCardsToString() { List cards = Arrays.asList( new Card(Symbol.ACE, Type.CLUB), new Card(Symbol.TWO, Type.HEART), @@ -38,4 +41,14 @@ void joinCards_CardList_JoinCards() { String expected = "A클럽, 2하트, 8다이아몬드"; assertThat(StringUtil.joinCards(cards)).isEqualTo(expected); } + + @Test + void joinDealerResult_MapOfResultTypeAndCount_JoinDealerResultToString() { + Map dealerResult = new EnumMap<>(ResultType.class); + dealerResult.put(ResultType.WIN, 2L); + dealerResult.put(ResultType.LOSE, 1L); + + String expected = "Dealer : 2승 1패"; + assertThat(StringUtil.joinDealerResult(dealerResult)).isEqualTo(expected); + } } From 18742a3bea6f0872672c39f8b23028fb3d165403 Mon Sep 17 00:00:00 2001 From: junyoung lee Date: Fri, 13 Mar 2020 17:52:48 +0900 Subject: [PATCH 27/31] =?UTF-8?q?test=20:=20StringUtil=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/blackjack/util/StringUtilTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/blackjack/util/StringUtilTest.java b/src/test/java/blackjack/util/StringUtilTest.java index 38ea6cc39c..7aba9907fc 100644 --- a/src/test/java/blackjack/util/StringUtilTest.java +++ b/src/test/java/blackjack/util/StringUtilTest.java @@ -48,7 +48,7 @@ void joinDealerResult_MapOfResultTypeAndCount_JoinDealerResultToString() { dealerResult.put(ResultType.WIN, 2L); dealerResult.put(ResultType.LOSE, 1L); - String expected = "Dealer : 2승 1패"; + String expected = "Dealer: 2승 1패"; assertThat(StringUtil.joinDealerResult(dealerResult)).isEqualTo(expected); } } From 4a9d6356d00887106a14af6a8e0d4faca306343f Mon Sep 17 00:00:00 2001 From: junyoung lee Date: Fri, 13 Mar 2020 22:09:37 +0900 Subject: [PATCH 28/31] =?UTF-8?q?refactor=20:=20OutputView=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EC=9D=BC=EB=B6=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blackjack/controller/BlackjackController.java | 14 +++++++------- src/main/java/blackjack/domain/result/Report.java | 3 ++- src/main/java/blackjack/domain/user/Player.java | 1 + src/main/java/blackjack/domain/user/hand/Hand.java | 4 ++-- .../java/blackjack/domain/user/hand/Score.java | 12 +++++------- src/main/java/blackjack/util/StringUtil.java | 6 ------ src/main/java/blackjack/view/OutputView.java | 9 ++++++--- src/test/java/blackjack/util/StringUtilTest.java | 2 +- 8 files changed, 24 insertions(+), 27 deletions(-) diff --git a/src/main/java/blackjack/controller/BlackjackController.java b/src/main/java/blackjack/controller/BlackjackController.java index 4a45bc3c94..ca1e3fe331 100644 --- a/src/main/java/blackjack/controller/BlackjackController.java +++ b/src/main/java/blackjack/controller/BlackjackController.java @@ -28,6 +28,8 @@ public BlackjackController(Deck deck, Dealer dealer, List players) { public void playGame() { List users = generateUsers(); drawInitialCardsEachUser(users); + OutputView.printUsersInitialDraw(INITIAL_DRAW_NUMBER, users); + drawCardsEachUsers(); printUsersCardsAndScore(users); @@ -44,7 +46,6 @@ private List generateUsers() { private void drawInitialCardsEachUser(List users) { users.forEach(user -> user.draw(deck, INITIAL_DRAW_NUMBER)); - OutputView.printUsersInitialDraw(INITIAL_DRAW_NUMBER, users); } private void drawCardsEachUsers() { @@ -53,15 +54,14 @@ private void drawCardsEachUsers() { } private void drawCardsByOpinion(Player player) { - while (player.canDraw()) { - DrawOpinion drawOpinion = DrawOpinion.of(InputView.inputDrawOpinion(player)); + DrawOpinion drawOpinion; - if (DrawOpinion.NO.equals(drawOpinion)) { - break; - } + do { + drawOpinion = DrawOpinion.of(InputView.inputDrawOpinion(player)); player.draw(deck); + OutputView.printUserHand(player, player.getHand()); - } + } while (player.canDraw() && DrawOpinion.YES.equals(drawOpinion)); } private void drawCardsDealer() { diff --git a/src/main/java/blackjack/domain/result/Report.java b/src/main/java/blackjack/domain/result/Report.java index 164744b1eb..f6e21bac3b 100644 --- a/src/main/java/blackjack/domain/result/Report.java +++ b/src/main/java/blackjack/domain/result/Report.java @@ -2,6 +2,7 @@ import static java.util.stream.Collectors.*; +import java.util.EnumMap; import java.util.List; import java.util.Map; import java.util.function.Function; @@ -32,7 +33,7 @@ private static Map generatePlayersResult(Dealer dealer, List private static Map generateDealerResult(Dealer dealer, List players) { return players.stream() .map(player -> ResultType.from(dealer, player)) - .collect(groupingBy(Function.identity(), counting())); + .collect(collectingAndThen(groupingBy(Function.identity(), counting()), EnumMap::new)); } public Map getDealerResult() { diff --git a/src/main/java/blackjack/domain/user/Player.java b/src/main/java/blackjack/domain/user/Player.java index b22f90cf4a..50c740dd63 100644 --- a/src/main/java/blackjack/domain/user/Player.java +++ b/src/main/java/blackjack/domain/user/Player.java @@ -21,6 +21,7 @@ public static Player valueOf(String name, List cards) { @Override public boolean canDraw() { + // NOTE : 의인화?! return hand.calculateScore().isLowerThan(PLAYER_DRAWABLE_MAX_SCORE); } diff --git a/src/main/java/blackjack/domain/user/hand/Hand.java b/src/main/java/blackjack/domain/user/hand/Hand.java index 838571dfdd..693b934dca 100644 --- a/src/main/java/blackjack/domain/user/hand/Hand.java +++ b/src/main/java/blackjack/domain/user/hand/Hand.java @@ -1,6 +1,6 @@ package blackjack.domain.user.hand; -import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; import blackjack.domain.card.Card; @@ -9,7 +9,7 @@ public class Hand { private final List cards; public Hand() { - this.cards = new ArrayList<>(); + this.cards = new LinkedList<>(); } public void add(Card card) { diff --git a/src/main/java/blackjack/domain/user/hand/Score.java b/src/main/java/blackjack/domain/user/hand/Score.java index 7b38c64e6f..6aadb9f48f 100644 --- a/src/main/java/blackjack/domain/user/hand/Score.java +++ b/src/main/java/blackjack/domain/user/hand/Score.java @@ -1,26 +1,24 @@ package blackjack.domain.user.hand; -import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Objects; +import java.util.stream.IntStream; import blackjack.domain.card.Card; import blackjack.domain.card.Symbol; public class Score { - public final static Score ZERO = new Score(0); - private final static Map CACHE = new HashMap<>(); + public static final Score ZERO = new Score(0); + private static final Map CACHE = new HashMap<>(); private static final int ADDITIONAL_ACE_SCORE = 10; private static final int BUST_SCORE = 22; private final int score; static { - Arrays.stream(Symbol.values()) - .map(Symbol::getScore) - .forEach(symbolValue -> - CACHE.putIfAbsent(symbolValue, new Score(symbolValue))); + IntStream.range(Symbol.ACE.getScore(), BUST_SCORE) + .forEach(score -> CACHE.put(score, new Score(score))); } private Score(int score) { diff --git a/src/main/java/blackjack/util/StringUtil.java b/src/main/java/blackjack/util/StringUtil.java index 589abebf3e..c65863286b 100644 --- a/src/main/java/blackjack/util/StringUtil.java +++ b/src/main/java/blackjack/util/StringUtil.java @@ -7,13 +7,11 @@ import blackjack.domain.card.Card; import blackjack.domain.result.ResultType; -import blackjack.domain.user.Dealer; import blackjack.domain.user.Player; import blackjack.domain.user.User; public class StringUtil { private static final String DELIMITER = ", "; - private static final String SEPARATOR = ": "; private static final String SPACE = " "; public static String joinPlayerNames(List players) { @@ -30,10 +28,6 @@ public static String joinCards(List cards) { } public static String joinDealerResult(Map dealerResult) { - return Dealer.NAME + SEPARATOR + joinResultTypeWithCount(dealerResult); - } - - private static String joinResultTypeWithCount(Map dealerResult) { return dealerResult.entrySet().stream() .map(entry -> { long count = entry.getValue(); diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index a7dc0b0b2e..e14b1a05b7 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -43,12 +43,15 @@ public static void printUserHandAndScore(User user) { public static void printBlackjackReport(Report blackJackReport) { System.out.println(NEWLINE + FINAL_RESULT_MESSAGE); - System.out.println(StringUtil.joinDealerResult(blackJackReport.getDealerResult())); + printUserResult(Dealer.NAME, StringUtil.joinDealerResult(blackJackReport.getDealerResult())); printPlayersResult(blackJackReport.getPlayersResult()); } private static void printPlayersResult(Map playersResult) { - playersResult.forEach((player, resultType) -> - System.out.println(player.getName() + SEPARATOR + resultType.getAlias())); + playersResult.forEach((player, resultType) -> printUserResult(player.getName(), resultType.getAlias())); + } + + private static void printUserResult(String name, String result) { + System.out.println(name + SEPARATOR + result); } } diff --git a/src/test/java/blackjack/util/StringUtilTest.java b/src/test/java/blackjack/util/StringUtilTest.java index 7aba9907fc..7d2d369775 100644 --- a/src/test/java/blackjack/util/StringUtilTest.java +++ b/src/test/java/blackjack/util/StringUtilTest.java @@ -48,7 +48,7 @@ void joinDealerResult_MapOfResultTypeAndCount_JoinDealerResultToString() { dealerResult.put(ResultType.WIN, 2L); dealerResult.put(ResultType.LOSE, 1L); - String expected = "Dealer: 2승 1패"; + String expected = "2승 1패"; assertThat(StringUtil.joinDealerResult(dealerResult)).isEqualTo(expected); } } From 4dc08f7d736eb23ca22a871d0956af2d03fb3e61 Mon Sep 17 00:00:00 2001 From: junyoung lee Date: Sat, 14 Mar 2020 13:43:42 +0900 Subject: [PATCH 29/31] =?UTF-8?q?refactor=20:=20=EB=B2=84=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80=20=EB=B0=8F?= =?UTF-8?q?=20=EA=B8=B0=ED=83=80=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BlackjackController.java | 12 +++---- src/main/java/blackjack/domain/card/Card.java | 20 +++++++++++ src/main/java/blackjack/domain/card/Deck.java | 22 ++++++++---- .../domain/card/InvalidDeckException.java | 9 +++++ .../blackjack/domain/result/ResultType.java | 25 ++++++++------ src/main/java/blackjack/domain/user/User.java | 9 +++-- .../java/blackjack/domain/user/hand/Hand.java | 11 ++++++ .../blackjack/domain/user/hand/Score.java | 17 ---------- .../java/blackjack/domain/card/DeckTest.java | 13 +++++++ .../java/blackjack/domain/card/HandTest.java | 13 +++++++ .../blackjack/domain/result/ReportTest.java | 21 +++++++----- .../domain/result/ResultTypeTest.java | 34 ++++++++++++------- .../blackjack/domain/user/DealerTest.java | 11 ++++++ .../java/blackjack/domain/user/UserTest.java | 22 ------------ 14 files changed, 149 insertions(+), 90 deletions(-) create mode 100644 src/main/java/blackjack/domain/card/InvalidDeckException.java diff --git a/src/main/java/blackjack/controller/BlackjackController.java b/src/main/java/blackjack/controller/BlackjackController.java index ca1e3fe331..6dbb866f33 100644 --- a/src/main/java/blackjack/controller/BlackjackController.java +++ b/src/main/java/blackjack/controller/BlackjackController.java @@ -54,14 +54,14 @@ private void drawCardsEachUsers() { } private void drawCardsByOpinion(Player player) { - DrawOpinion drawOpinion; - - do { - drawOpinion = DrawOpinion.of(InputView.inputDrawOpinion(player)); + while (player.canDraw() && wantDraw(player)) { player.draw(deck); - OutputView.printUserHand(player, player.getHand()); - } while (player.canDraw() && DrawOpinion.YES.equals(drawOpinion)); + } + } + + private boolean wantDraw(Player player) { + return DrawOpinion.YES.equals(DrawOpinion.of(InputView.inputDrawOpinion(player))); } private void drawCardsDealer() { diff --git a/src/main/java/blackjack/domain/card/Card.java b/src/main/java/blackjack/domain/card/Card.java index a29036755e..7b591d77af 100644 --- a/src/main/java/blackjack/domain/card/Card.java +++ b/src/main/java/blackjack/domain/card/Card.java @@ -1,5 +1,7 @@ package blackjack.domain.card; +import java.util.Objects; + public class Card { private final Symbol symbol; private final Type type; @@ -17,6 +19,24 @@ public int getSymbolValue() { return symbol.getScore(); } + @Override + public boolean equals(Object object) { + if (this == object) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } + Card card = (Card)object; + return symbol == card.symbol && + type == card.type; + } + + @Override + public int hashCode() { + return Objects.hash(symbol, type); + } + @Override public String toString() { return symbol.getSymbol() + type.getType(); diff --git a/src/main/java/blackjack/domain/card/Deck.java b/src/main/java/blackjack/domain/card/Deck.java index 3b1552a0a3..63c7e2cebe 100644 --- a/src/main/java/blackjack/domain/card/Deck.java +++ b/src/main/java/blackjack/domain/card/Deck.java @@ -1,28 +1,36 @@ package blackjack.domain.card; -import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedList; import java.util.List; public class Deck { - private static final int TOP = 0; - - private List cards; + private LinkedList cards; public Deck() { this.cards = refill(CardRepository.cards()); } - private List refill(List cards) { - List refillCards = new ArrayList<>(cards); + LinkedList refill(List cards) { + validate(cards); + LinkedList refillCards = new LinkedList<>(cards); Collections.shuffle(refillCards); return refillCards; } + private void validate(List cards) { + long distinctCardSize = new HashSet<>(cards).size(); + + if (cards.size() != distinctCardSize) { + throw new InvalidDeckException(InvalidDeckException.INVALID); + } + } + public Card draw() { if (cards.isEmpty()) { cards = refill(CardRepository.cards()); } - return cards.remove(TOP); + return cards.poll(); } } diff --git a/src/main/java/blackjack/domain/card/InvalidDeckException.java b/src/main/java/blackjack/domain/card/InvalidDeckException.java new file mode 100644 index 0000000000..d720f0d8be --- /dev/null +++ b/src/main/java/blackjack/domain/card/InvalidDeckException.java @@ -0,0 +1,9 @@ +package blackjack.domain.card; + +public class InvalidDeckException extends IllegalArgumentException { + public static final String INVALID = "카드 덱에 중복이 존재합니다."; + + public InvalidDeckException(String s) { + super(s); + } +} diff --git a/src/main/java/blackjack/domain/result/ResultType.java b/src/main/java/blackjack/domain/result/ResultType.java index 8ffc733ae7..6a9e60e6d8 100644 --- a/src/main/java/blackjack/domain/result/ResultType.java +++ b/src/main/java/blackjack/domain/result/ResultType.java @@ -1,26 +1,29 @@ package blackjack.domain.result; +import java.util.Arrays; +import java.util.function.Predicate; + import blackjack.domain.user.User; public enum ResultType { - WIN("승"), - DRAW("무"), - LOSE("패"); + WIN(value -> value > 0, "승"), + DRAW(value -> value == 0, "무"), + LOSE(value -> value < 0, "패"); + private Predicate compareResult; private final String alias; - ResultType(String alias) { + ResultType(Predicate compareResult, String alias) { + this.compareResult = compareResult; this.alias = alias; } public static ResultType from(User targetUser, User compareUser) { - if (targetUser.getScore() > compareUser.getScore()) { - return WIN; - } - if (targetUser.getScore() < compareUser.getScore()) { - return LOSE; - } - return DRAW; + return Arrays.stream(values()) + .filter(resultType -> resultType.compareResult + .test(Integer.compare(targetUser.getBustHandledScore(), compareUser.getBustHandledScore()))) + .findAny() + .orElseThrow(() -> new NullPointerException("유효하지 않은 결과 타입입니다.")); } public String getAlias() { diff --git a/src/main/java/blackjack/domain/user/User.java b/src/main/java/blackjack/domain/user/User.java index e023abbcd9..b6ad991fc2 100644 --- a/src/main/java/blackjack/domain/user/User.java +++ b/src/main/java/blackjack/domain/user/User.java @@ -12,7 +12,6 @@ public abstract class User { private static final int DRAW_LOWER_BOUND = 1; - private static final int BLACKJACK_SCORE = 21; protected final String name; protected final Hand hand; @@ -52,10 +51,6 @@ protected void validateDrawNumber(int drawNumber) { } } - public boolean isBust() { - return hand.calculateScore().isMoreThan(BLACKJACK_SCORE); - } - public abstract boolean canDraw(); public abstract List getInitialHand(); @@ -72,6 +67,10 @@ public int getScore() { return hand.calculateScore().getScore(); } + public int getBustHandledScore() { + return hand.calculateBustHandledScore().getScore(); + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/src/main/java/blackjack/domain/user/hand/Hand.java b/src/main/java/blackjack/domain/user/hand/Hand.java index 693b934dca..3e184c951c 100644 --- a/src/main/java/blackjack/domain/user/hand/Hand.java +++ b/src/main/java/blackjack/domain/user/hand/Hand.java @@ -6,6 +6,8 @@ import blackjack.domain.card.Card; public class Hand { + private static final int BLACKJACK_SCORE = 21; + private final List cards; public Hand() { @@ -29,6 +31,15 @@ public Score calculateScore() { return score; } + public Score calculateBustHandledScore() { + Score score = calculateScore(); + + if (score.getScore() > BLACKJACK_SCORE) { + return Score.ZERO; + } + return score; + } + public List getCards() { return cards; } diff --git a/src/main/java/blackjack/domain/user/hand/Score.java b/src/main/java/blackjack/domain/user/hand/Score.java index 6aadb9f48f..bd753fe293 100644 --- a/src/main/java/blackjack/domain/user/hand/Score.java +++ b/src/main/java/blackjack/domain/user/hand/Score.java @@ -62,21 +62,4 @@ public boolean isMoreThan(Score score) { public int getScore() { return score; } - - @Override - public boolean equals(Object object) { - if (this == object) { - return true; - } - if (object == null || getClass() != object.getClass()) { - return false; - } - Score that = (Score)object; - return score == that.score; - } - - @Override - public int hashCode() { - return Objects.hash(score); - } } diff --git a/src/test/java/blackjack/domain/card/DeckTest.java b/src/test/java/blackjack/domain/card/DeckTest.java index a137e08757..89d1d04a06 100644 --- a/src/test/java/blackjack/domain/card/DeckTest.java +++ b/src/test/java/blackjack/domain/card/DeckTest.java @@ -2,6 +2,8 @@ import static org.assertj.core.api.Assertions.*; +import java.util.Arrays; +import java.util.List; import java.util.stream.Stream; import org.junit.jupiter.api.Test; @@ -15,6 +17,17 @@ void Deck_GetCardsFromCardRepository_GenerateInstance() { assertThat(new Deck()).isInstanceOf(Deck.class); } + @Test + void validate_DuplicateExistCards_InvalidDeckExceptionThrown() { + List cards = Arrays.asList( + new Card(Symbol.TEN, Type.CLUB), + new Card(Symbol.TEN, Type.CLUB)); + + assertThatThrownBy(() -> new Deck().refill(cards)) + .isInstanceOf(InvalidDeckException.class) + .hasMessage(InvalidDeckException.INVALID); + } + @Test void draw_UserDrawCardFromDeck_ReturnCard() { Deck deck = new Deck(); diff --git a/src/test/java/blackjack/domain/card/HandTest.java b/src/test/java/blackjack/domain/card/HandTest.java index 73794955a4..e6bb64422e 100644 --- a/src/test/java/blackjack/domain/card/HandTest.java +++ b/src/test/java/blackjack/domain/card/HandTest.java @@ -59,4 +59,17 @@ void calculateScore_WithAceCard_SumCards() { Score expected = Score.valueOf(19); assertThat(hand.calculateScore()).isEqualTo(expected); } + + @Test + void calculateBustHandledScore_BustScore_ReturnZeroScore() { + Hand hand = new Hand(); + List cards = Arrays.asList( + new Card(Symbol.JACK, Type.DIAMOND), + new Card(Symbol.QUEEN, Type.HEART), + new Card(Symbol.TWO, Type.SPADE)); + hand.add(cards); + + Score expected = Score.ZERO; + assertThat(hand.calculateBustHandledScore()).isEqualTo(expected); + } } diff --git a/src/test/java/blackjack/domain/result/ReportTest.java b/src/test/java/blackjack/domain/result/ReportTest.java index 013d7771fb..5ea06c85f5 100644 --- a/src/test/java/blackjack/domain/result/ReportTest.java +++ b/src/test/java/blackjack/domain/result/ReportTest.java @@ -24,16 +24,19 @@ class ReportTest { @BeforeEach void setUp() { - List dealerCards = Arrays.asList(new Card(Symbol.EIGHT, Type.HEART), new Card(Symbol.KING, Type.DIAMOND)); - List pobiCards = Arrays.asList(new Card(Symbol.QUEEN, Type.HEART), new Card(Symbol.KING, Type.DIAMOND)); - List sonyCards = Arrays.asList(new Card(Symbol.EIGHT, Type.HEART), new Card(Symbol.KING, Type.DIAMOND)); - List stitchCards = Arrays.asList(new Card(Symbol.SEVEN, Type.HEART), new Card(Symbol.KING, Type.DIAMOND)); + dealer = Dealer.valueOf("dealer", Arrays.asList( + new Card(Symbol.EIGHT, Type.HEART), + new Card(Symbol.KING, Type.DIAMOND))); - Player pobi = Player.valueOf("pobi", pobiCards); - Player sony = Player.valueOf("sony", sonyCards); - Player stitch = Player.valueOf("stitch", stitchCards); - - dealer = Dealer.valueOf("dealer", dealerCards); + Player pobi = Player.valueOf("pobi", Arrays.asList( + new Card(Symbol.QUEEN, Type.HEART), + new Card(Symbol.KING, Type.DIAMOND))); + Player sony = Player.valueOf("sony", Arrays.asList( + new Card(Symbol.EIGHT, Type.HEART), + new Card(Symbol.KING, Type.DIAMOND))); + Player stitch = Player.valueOf("stitch", Arrays.asList( + new Card(Symbol.SEVEN, Type.HEART), + new Card(Symbol.KING, Type.DIAMOND))); players = Arrays.asList(pobi, sony, stitch); } diff --git a/src/test/java/blackjack/domain/result/ResultTypeTest.java b/src/test/java/blackjack/domain/result/ResultTypeTest.java index 515d93589a..cae4419188 100644 --- a/src/test/java/blackjack/domain/result/ResultTypeTest.java +++ b/src/test/java/blackjack/domain/result/ResultTypeTest.java @@ -3,7 +3,6 @@ import static org.assertj.core.api.Assertions.*; import java.util.Arrays; -import java.util.List; import java.util.stream.Stream; import org.junit.jupiter.params.ParameterizedTest; @@ -25,19 +24,28 @@ void from_DealerScoreAndPlayerScore_ReturnResultType(User targetUser, User compa } private static Stream provideDealerAndPlayerScoreWithReturnType() { - List dealerCards = Arrays.asList(new Card(Symbol.EIGHT, Type.HEART), new Card(Symbol.KING, Type.DIAMOND)); - List pobiCards = Arrays.asList(new Card(Symbol.QUEEN, Type.HEART), new Card(Symbol.KING, Type.DIAMOND)); - List sonyCards = Arrays.asList(new Card(Symbol.EIGHT, Type.HEART), new Card(Symbol.KING, Type.DIAMOND)); - List stitchCards = Arrays.asList(new Card(Symbol.SEVEN, Type.HEART), new Card(Symbol.KING, Type.DIAMOND)); + Dealer dealer = Dealer.valueOf("dealer", Arrays.asList( + new Card(Symbol.EIGHT, Type.HEART), + new Card(Symbol.KING, Type.DIAMOND))); + + Player pobi = Player.valueOf("pobi", Arrays.asList( + new Card(Symbol.QUEEN, Type.HEART), + new Card(Symbol.KING, Type.DIAMOND))); + Player sony = Player.valueOf("sony", Arrays.asList( + new Card(Symbol.EIGHT, Type.HEART), + new Card(Symbol.KING, Type.DIAMOND))); + Player stitch = Player.valueOf("stitch", Arrays.asList( + new Card(Symbol.SEVEN, Type.HEART), + new Card(Symbol.KING, Type.DIAMOND))); + Player bustPlayer = Player.valueOf("bust", Arrays.asList( + new Card(Symbol.TEN, Type.CLUB), + new Card(Symbol.KING, Type.DIAMOND), + new Card(Symbol.TWO, Type.CLUB))); - Player pobi = Player.valueOf("pobi", pobiCards); - Player sony = Player.valueOf("sony", sonyCards); - Player stitch = Player.valueOf("stitch", stitchCards); - - Dealer dealer = Dealer.valueOf("dealer", dealerCards); return Stream.of( - Arguments.arguments(dealer, pobi, ResultType.LOSE), - Arguments.arguments(dealer, sony, ResultType.DRAW), - Arguments.arguments(dealer, stitch, ResultType.WIN)); + Arguments.of(dealer, pobi, ResultType.LOSE), + Arguments.of(dealer, sony, ResultType.DRAW), + Arguments.of(dealer, stitch, ResultType.WIN), + Arguments.of(dealer, bustPlayer, ResultType.WIN)); } } diff --git a/src/test/java/blackjack/domain/user/DealerTest.java b/src/test/java/blackjack/domain/user/DealerTest.java index 665d7ac752..a493d0c79c 100644 --- a/src/test/java/blackjack/domain/user/DealerTest.java +++ b/src/test/java/blackjack/domain/user/DealerTest.java @@ -57,4 +57,15 @@ private static Stream provideUndrawableDealer() { } return Stream.of(Arguments.of(dealer)); } + + @Test + void getInitialHand_DealerDrawInitialTwoCards_ReturnOneCard() { + List cards = Arrays.asList( + new Card(Symbol.SEVEN, Type.CLUB), + new Card(Symbol.TWO, Type.DIAMOND)); + Dealer dealer = Dealer.valueOf("dealer", cards); + + assertThat(dealer.getInitialHand()).hasSize(1) + .isEqualTo(Arrays.asList(new Card(Symbol.SEVEN, Type.CLUB))); + } } diff --git a/src/test/java/blackjack/domain/user/UserTest.java b/src/test/java/blackjack/domain/user/UserTest.java index ddee59d731..7e6f1755bd 100644 --- a/src/test/java/blackjack/domain/user/UserTest.java +++ b/src/test/java/blackjack/domain/user/UserTest.java @@ -2,12 +2,8 @@ import static org.assertj.core.api.Assertions.*; -import java.util.stream.Stream; - import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.NullAndEmptySource; import blackjack.domain.card.Card; @@ -60,22 +56,4 @@ void validateDrawNumber_InvalidNumberOfCards_InvalidUserExceptionThrown() { .isInstanceOf(InvalidUserException.class) .hasMessage(InvalidUserException.INVALID_DRAW_NUMBER); } - - @ParameterizedTest - @MethodSource("provideBustUser") - void isBust_BustScoreUser_ReturnTrue(User user) { - assertThat(user.isBust()).isTrue(); - } - - private static Stream provideBustUser() { - final int WORST_CASE_OF_DRAWABLE_COUNT = 12; - - User user = new Player("user"); - Deck deck = new Deck(); - - for (int i = 0; i < WORST_CASE_OF_DRAWABLE_COUNT; i++) { - user.draw(deck); - } - return Stream.of(Arguments.of(user)); - } } From aa986dabc926f515e8136a77d4bb0ee5e5dd33f5 Mon Sep 17 00:00:00 2001 From: junyoung lee Date: Sat, 14 Mar 2020 13:57:10 +0900 Subject: [PATCH 30/31] =?UTF-8?q?refactor=20:=20Score=20=EC=9D=B8=EC=8A=A4?= =?UTF-8?q?=ED=84=B4=EC=8A=A4=20=EC=9C=A0=ED=9A=A8=EC=84=B1=20=EA=B2=80?= =?UTF-8?q?=EC=82=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/user/hand/InvalidScoreException.java | 9 +++++++++ src/main/java/blackjack/domain/user/hand/Score.java | 7 +++++++ src/test/java/blackjack/domain/user/hand/ScoreTest.java | 7 +++++++ 3 files changed, 23 insertions(+) create mode 100644 src/main/java/blackjack/domain/user/hand/InvalidScoreException.java diff --git a/src/main/java/blackjack/domain/user/hand/InvalidScoreException.java b/src/main/java/blackjack/domain/user/hand/InvalidScoreException.java new file mode 100644 index 0000000000..2f5c63270e --- /dev/null +++ b/src/main/java/blackjack/domain/user/hand/InvalidScoreException.java @@ -0,0 +1,9 @@ +package blackjack.domain.user.hand; + +public class InvalidScoreException extends IllegalArgumentException { + public static final String INVALID = "점수는 0보다 작을 수 없습니다."; + + public InvalidScoreException(String s) { + super(s); + } +} diff --git a/src/main/java/blackjack/domain/user/hand/Score.java b/src/main/java/blackjack/domain/user/hand/Score.java index bd753fe293..a69164f34d 100644 --- a/src/main/java/blackjack/domain/user/hand/Score.java +++ b/src/main/java/blackjack/domain/user/hand/Score.java @@ -22,9 +22,16 @@ public class Score { } private Score(int score) { + validate(score); this.score = score; } + private void validate(int score) { + if (score < 0) { + throw new InvalidScoreException(InvalidScoreException.INVALID); + } + } + public static Score valueOf(int number) { Score score = CACHE.get(number); diff --git a/src/test/java/blackjack/domain/user/hand/ScoreTest.java b/src/test/java/blackjack/domain/user/hand/ScoreTest.java index c5997ba843..2c73eec046 100644 --- a/src/test/java/blackjack/domain/user/hand/ScoreTest.java +++ b/src/test/java/blackjack/domain/user/hand/ScoreTest.java @@ -12,6 +12,13 @@ import blackjack.domain.card.Type; class ScoreTest { + @Test + void validate_Negative_InvalidScoreExceptionThrown() { + assertThatThrownBy(() -> Score.valueOf(-1)) + .isInstanceOf(InvalidScoreException.class) + .hasMessage(InvalidScoreException.INVALID); + } + @Test void valueOf_InputInteger_ReturnInstance() { assertThat(Score.valueOf(10)).isInstanceOf(Score.class) From 709cee45c8fda8993228c79c2635cbfddeba06e3 Mon Sep 17 00:00:00 2001 From: junyoung lee Date: Mon, 16 Mar 2020 00:14:08 +0900 Subject: [PATCH 31/31] =?UTF-8?q?refactor=20:=20=ED=94=BC=EB=93=9C?= =?UTF-8?q?=EB=B0=B1=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/Application.java | 13 ++- .../controller/BlackjackController.java | 78 +++++++--------- .../java/blackjack/domain/BlackjackTable.java | 36 ++++++++ .../java/blackjack/domain/DrawOpinion.java | 4 + src/main/java/blackjack/domain/card/Card.java | 4 +- .../{CardRepository.java => CardFactory.java} | 17 ++-- src/main/java/blackjack/domain/card/Deck.java | 18 ++-- .../domain/card/InvalidDeckException.java | 2 + .../java/blackjack/domain/card/Symbol.java | 4 + .../domain/result/InvalidReportException.java | 9 ++ .../java/blackjack/domain/result/Report.java | 15 ++- .../blackjack/domain/result/ResultType.java | 21 ++--- .../blackjack/domain/user/PlayerFactory.java | 7 +- src/main/java/blackjack/domain/user/User.java | 23 ++++- .../java/blackjack/domain/user/hand/Hand.java | 8 +- .../user/hand/InvalidScoreException.java | 1 + .../blackjack/domain/user/hand/Score.java | 33 ++++--- src/main/java/blackjack/util/StringUtil.java | 18 +++- src/main/java/blackjack/view/OutputView.java | 6 ++ .../blackjack/domain/BlackjackTableTest.java | 91 +++++++++++++++++++ .../blackjack/domain/DrawOpinionTest.java | 6 ++ .../java/blackjack/domain/card/DeckTest.java | 40 ++++---- .../blackjack/domain/card/SymbolTest.java | 12 +++ .../blackjack/domain/result/ReportTest.java | 29 +++++- .../domain/result/ResultTypeTest.java | 12 +-- .../blackjack/domain/user/DealerTest.java | 25 ++--- .../domain/user/PlayerFactoryTest.java | 4 +- .../blackjack/domain/user/PlayerTest.java | 25 ++--- .../java/blackjack/domain/user/UserTest.java | 17 +++- .../blackjack/domain/user/hand/ScoreTest.java | 22 ++--- .../java/blackjack/util/StringUtilTest.java | 21 +++++ 31 files changed, 440 insertions(+), 181 deletions(-) create mode 100644 src/main/java/blackjack/domain/BlackjackTable.java rename src/main/java/blackjack/domain/card/{CardRepository.java => CardFactory.java} (58%) create mode 100644 src/main/java/blackjack/domain/result/InvalidReportException.java create mode 100644 src/test/java/blackjack/domain/BlackjackTableTest.java create mode 100644 src/test/java/blackjack/domain/card/SymbolTest.java diff --git a/src/main/java/blackjack/Application.java b/src/main/java/blackjack/Application.java index 8672b81669..de8ec8a4b1 100644 --- a/src/main/java/blackjack/Application.java +++ b/src/main/java/blackjack/Application.java @@ -1,8 +1,12 @@ package blackjack; +import static blackjack.util.StringUtil.*; + import java.util.List; import blackjack.controller.BlackjackController; +import blackjack.domain.BlackjackTable; +import blackjack.domain.card.CardFactory; import blackjack.domain.card.Deck; import blackjack.domain.user.Dealer; import blackjack.domain.user.Player; @@ -11,11 +15,12 @@ public class Application { public static void main(String[] args) { - Deck deck = new Deck(); + Deck deck = new Deck(CardFactory.create()); Dealer dealer = new Dealer(Dealer.NAME); - List players = PlayerFactory.create(InputView.inputPlayerNames()); + List players = PlayerFactory.create(parsingPlayerNames(InputView.inputPlayerNames())); + BlackjackTable blackjackTable = new BlackjackTable(deck); - BlackjackController blackjackController = new BlackjackController(deck, dealer, players); - blackjackController.playGame(); + BlackjackController blackjackController = new BlackjackController(blackjackTable); + blackjackController.playGame(dealer, players); } } diff --git a/src/main/java/blackjack/controller/BlackjackController.java b/src/main/java/blackjack/controller/BlackjackController.java index 6dbb866f33..ff933055a4 100644 --- a/src/main/java/blackjack/controller/BlackjackController.java +++ b/src/main/java/blackjack/controller/BlackjackController.java @@ -1,79 +1,67 @@ package blackjack.controller; -import java.util.ArrayList; +import static blackjack.view.InputView.*; +import static blackjack.view.OutputView.*; + import java.util.List; +import blackjack.domain.BlackjackTable; import blackjack.domain.DrawOpinion; -import blackjack.domain.card.Deck; import blackjack.domain.result.Report; import blackjack.domain.user.Dealer; import blackjack.domain.user.Player; import blackjack.domain.user.User; -import blackjack.view.InputView; -import blackjack.view.OutputView; public class BlackjackController { - private static final int INITIAL_DRAW_NUMBER = 2; - - private final Deck deck; - private final Dealer dealer; - private final List players; + private final BlackjackTable blackjackTable; - public BlackjackController(Deck deck, Dealer dealer, List players) { - this.deck = deck; - this.dealer = dealer; - this.players = players; + public BlackjackController(BlackjackTable blackjackTable) { + this.blackjackTable = blackjackTable; } - public void playGame() { - List users = generateUsers(); - drawInitialCardsEachUser(users); - OutputView.printUsersInitialDraw(INITIAL_DRAW_NUMBER, users); + public void playGame(Dealer dealer, List players) { + List users = blackjackTable.collectToUsersFrom(dealer, players); + + drawInitialCardsFrom(users); + continueDrawCardsEach(dealer, players); - drawCardsEachUsers(); printUsersCardsAndScore(users); Report blackJackReport = Report.from(dealer, players); - OutputView.printBlackjackReport(blackJackReport); - } - - private List generateUsers() { - List users = new ArrayList<>(); - users.add(dealer); - users.addAll(players); - return users; + printBlackjackReport(blackJackReport); } - private void drawInitialCardsEachUser(List users) { - users.forEach(user -> user.draw(deck, INITIAL_DRAW_NUMBER)); + private void drawInitialCardsFrom(List users) { + blackjackTable.drawInitialCards(users); + printUsersInitialDraw(BlackjackTable.INITIAL_DRAW_NUMBER, users); } - private void drawCardsEachUsers() { - players.forEach(this::drawCardsByOpinion); - drawCardsDealer(); + private void continueDrawCardsEach(Dealer dealer, List players) { + for (Player player : players) { + drawCardsFrom(player); + } + drawCardsFrom(dealer); } - private void drawCardsByOpinion(Player player) { - while (player.canDraw() && wantDraw(player)) { - player.draw(deck); - OutputView.printUserHand(player, player.getHand()); + private void drawCardsFrom(Player player) { + while (canDraw(player) && wantDraw(player)) { + blackjackTable.drawCardFrom(player); + printUserHand(player, player.getHand()); } } - private boolean wantDraw(Player player) { - return DrawOpinion.YES.equals(DrawOpinion.of(InputView.inputDrawOpinion(player))); + private boolean canDraw(User user) { + return user.canDraw(); } - private void drawCardsDealer() { - while (dealer.canDraw()) { - dealer.draw(deck); - OutputView.printDealerDrawCard(); - } + private boolean wantDraw(Player player) { + return DrawOpinion.of(inputDrawOpinion(player)).isYes(); } - private void printUsersCardsAndScore(List users) { - for (User user : users) { - OutputView.printUserHandAndScore(user); + private void drawCardsFrom(Dealer dealer) { + while (canDraw(dealer)) { + blackjackTable.drawCardFrom(dealer); + printDealerDrawCard(); } } } diff --git a/src/main/java/blackjack/domain/BlackjackTable.java b/src/main/java/blackjack/domain/BlackjackTable.java new file mode 100644 index 0000000000..eeb8aa1e60 --- /dev/null +++ b/src/main/java/blackjack/domain/BlackjackTable.java @@ -0,0 +1,36 @@ +package blackjack.domain; + +import java.util.ArrayList; +import java.util.List; + +import blackjack.domain.card.Deck; +import blackjack.domain.user.Dealer; +import blackjack.domain.user.Player; +import blackjack.domain.user.User; + +public class BlackjackTable { + public static final int INITIAL_DRAW_NUMBER = 2; + + private final Deck deck; + + public BlackjackTable(Deck deck) { + this.deck = deck; + } + + public List collectToUsersFrom(Dealer dealer, List players) { + List users = new ArrayList<>(); + users.add(dealer); + users.addAll(players); + return users; + } + + public void drawInitialCards(List users) { + for (User user : users) { + user.draw(deck, INITIAL_DRAW_NUMBER); + } + } + + public void drawCardFrom(User user) { + user.draw(deck); + } +} diff --git a/src/main/java/blackjack/domain/DrawOpinion.java b/src/main/java/blackjack/domain/DrawOpinion.java index fa0b3b404f..0ab7797ca8 100644 --- a/src/main/java/blackjack/domain/DrawOpinion.java +++ b/src/main/java/blackjack/domain/DrawOpinion.java @@ -18,4 +18,8 @@ public static DrawOpinion of(String opinion) { .findAny() .orElseThrow(() -> new InvalidDrawOpinionException(InvalidDrawOpinionException.INVALID)); } + + public boolean isYes() { + return this.equals(YES); + } } diff --git a/src/main/java/blackjack/domain/card/Card.java b/src/main/java/blackjack/domain/card/Card.java index 7b591d77af..5941be1914 100644 --- a/src/main/java/blackjack/domain/card/Card.java +++ b/src/main/java/blackjack/domain/card/Card.java @@ -12,10 +12,10 @@ public Card(Symbol symbol, Type type) { } public boolean isAce() { - return this.symbol.equals(Symbol.ACE); + return symbol.isAce(); } - public int getSymbolValue() { + public int getScore() { return symbol.getScore(); } diff --git a/src/main/java/blackjack/domain/card/CardRepository.java b/src/main/java/blackjack/domain/card/CardFactory.java similarity index 58% rename from src/main/java/blackjack/domain/card/CardRepository.java rename to src/main/java/blackjack/domain/card/CardFactory.java index 5e8a37c80a..c53fa0f61d 100644 --- a/src/main/java/blackjack/domain/card/CardRepository.java +++ b/src/main/java/blackjack/domain/card/CardFactory.java @@ -2,30 +2,27 @@ import static java.util.stream.Collectors.*; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Stream; -public class CardRepository { +public class CardFactory { private static final List CARDS; static { - CARDS = create(); - } - - private static List create() { - return Arrays.stream(Symbol.values()) - .flatMap(CardRepository::createByType) + CARDS = Arrays.stream(Symbol.values()) + .flatMap(CardFactory::createBy) .collect(collectingAndThen(toList(), Collections::unmodifiableList)); } - private static Stream createByType(Symbol symbol) { + private static Stream createBy(Symbol symbol) { return Arrays.stream(Type.values()) .map(type -> new Card(symbol, type)); } - public static List cards() { - return CARDS; + public static List create() { + return new ArrayList<>(CARDS); } } diff --git a/src/main/java/blackjack/domain/card/Deck.java b/src/main/java/blackjack/domain/card/Deck.java index 63c7e2cebe..2a1b3451c9 100644 --- a/src/main/java/blackjack/domain/card/Deck.java +++ b/src/main/java/blackjack/domain/card/Deck.java @@ -2,21 +2,17 @@ import java.util.Collections; import java.util.HashSet; -import java.util.LinkedList; import java.util.List; public class Deck { - private LinkedList cards; + private static final int TOP = 0; - public Deck() { - this.cards = refill(CardRepository.cards()); - } + private final List cards; - LinkedList refill(List cards) { + public Deck(List cards) { validate(cards); - LinkedList refillCards = new LinkedList<>(cards); - Collections.shuffle(refillCards); - return refillCards; + Collections.shuffle(cards); + this.cards = cards; } private void validate(List cards) { @@ -29,8 +25,8 @@ private void validate(List cards) { public Card draw() { if (cards.isEmpty()) { - cards = refill(CardRepository.cards()); + throw new InvalidDeckException(InvalidDeckException.DECK_EMPTY); } - return cards.poll(); + return cards.remove(TOP); } } diff --git a/src/main/java/blackjack/domain/card/InvalidDeckException.java b/src/main/java/blackjack/domain/card/InvalidDeckException.java index d720f0d8be..f1bf2fe3d8 100644 --- a/src/main/java/blackjack/domain/card/InvalidDeckException.java +++ b/src/main/java/blackjack/domain/card/InvalidDeckException.java @@ -2,6 +2,8 @@ public class InvalidDeckException extends IllegalArgumentException { public static final String INVALID = "카드 덱에 중복이 존재합니다."; + public static final String DECK_EMPTY = "덱의 모든 카드를 소진하였습니다."; + public static final String NULL = "덱이 존재하지 않습니다."; public InvalidDeckException(String s) { super(s); diff --git a/src/main/java/blackjack/domain/card/Symbol.java b/src/main/java/blackjack/domain/card/Symbol.java index f0a406cab0..0de7642c7b 100644 --- a/src/main/java/blackjack/domain/card/Symbol.java +++ b/src/main/java/blackjack/domain/card/Symbol.java @@ -23,6 +23,10 @@ public enum Symbol { this.symbol = symbol; } + public boolean isAce() { + return this.equals(ACE); + } + public int getScore() { return score; } diff --git a/src/main/java/blackjack/domain/result/InvalidReportException.java b/src/main/java/blackjack/domain/result/InvalidReportException.java new file mode 100644 index 0000000000..c469433295 --- /dev/null +++ b/src/main/java/blackjack/domain/result/InvalidReportException.java @@ -0,0 +1,9 @@ +package blackjack.domain.result; + +public class InvalidReportException extends IllegalArgumentException { + public static final String NULL = "게임 결과를 판단하기 위한 딜러 또는 플레이어가 존재하지 않습니다."; + + public InvalidReportException(String s) { + super(s); + } +} diff --git a/src/main/java/blackjack/domain/result/Report.java b/src/main/java/blackjack/domain/result/Report.java index f6e21bac3b..e8f35212f4 100644 --- a/src/main/java/blackjack/domain/result/Report.java +++ b/src/main/java/blackjack/domain/result/Report.java @@ -3,8 +3,10 @@ import static java.util.stream.Collectors.*; import java.util.EnumMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.function.Function; import blackjack.domain.user.Dealer; @@ -20,19 +22,28 @@ private Report(Map dealerResult, Map playe } public static Report from(Dealer dealer, List players) { + validateUser(dealer, players); return new Report( generateDealerResult(dealer, players), generatePlayersResult(dealer, players)); } + private static void validateUser(Dealer dealer, List players) { + if (Objects.isNull(dealer) || Objects.isNull(players) || players.isEmpty()) { + throw new InvalidReportException(InvalidReportException.NULL); + } + } + private static Map generatePlayersResult(Dealer dealer, List players) { return players.stream() - .collect(toMap(Function.identity(), player -> ResultType.from(player, dealer))); + .collect(collectingAndThen( + toMap(Function.identity(), player -> ResultType.of(player, dealer)), + LinkedHashMap::new)); } private static Map generateDealerResult(Dealer dealer, List players) { return players.stream() - .map(player -> ResultType.from(dealer, player)) + .map(player -> ResultType.of(dealer, player)) .collect(collectingAndThen(groupingBy(Function.identity(), counting()), EnumMap::new)); } diff --git a/src/main/java/blackjack/domain/result/ResultType.java b/src/main/java/blackjack/domain/result/ResultType.java index 6a9e60e6d8..21d36c6631 100644 --- a/src/main/java/blackjack/domain/result/ResultType.java +++ b/src/main/java/blackjack/domain/result/ResultType.java @@ -1,28 +1,27 @@ package blackjack.domain.result; import java.util.Arrays; -import java.util.function.Predicate; +import java.util.function.BiPredicate; import blackjack.domain.user.User; public enum ResultType { - WIN(value -> value > 0, "승"), - DRAW(value -> value == 0, "무"), - LOSE(value -> value < 0, "패"); + WIN((targetUser, compareUser) -> targetUser.compareTo(compareUser) > 0, "승"), + DRAW((targetUser, compareUser) -> targetUser.compareTo(compareUser) == 0, "무"), + LOSE((targetUser, compareUser) -> targetUser.compareTo(compareUser) < 0, "패"); - private Predicate compareResult; + private final BiPredicate judgementStandard; private final String alias; - ResultType(Predicate compareResult, String alias) { - this.compareResult = compareResult; + ResultType(BiPredicate judgementStandard, String alias) { + this.judgementStandard = judgementStandard; this.alias = alias; } - public static ResultType from(User targetUser, User compareUser) { + public static ResultType of(User targetUser, User compareUser) { return Arrays.stream(values()) - .filter(resultType -> resultType.compareResult - .test(Integer.compare(targetUser.getBustHandledScore(), compareUser.getBustHandledScore()))) - .findAny() + .filter(resultType -> resultType.judgementStandard.test(targetUser, compareUser)) + .findFirst() .orElseThrow(() -> new NullPointerException("유효하지 않은 결과 타입입니다.")); } diff --git a/src/main/java/blackjack/domain/user/PlayerFactory.java b/src/main/java/blackjack/domain/user/PlayerFactory.java index 67feb272cb..4090da6d8c 100644 --- a/src/main/java/blackjack/domain/user/PlayerFactory.java +++ b/src/main/java/blackjack/domain/user/PlayerFactory.java @@ -2,14 +2,11 @@ import static java.util.stream.Collectors.*; -import java.util.Arrays; import java.util.List; public class PlayerFactory { - private static final String DELIMITER = ","; - - public static List create(String playerNames) { - return Arrays.stream(playerNames.split(DELIMITER)) + public static List create(List playerNames) { + return playerNames.stream() .map(Player::new) .collect(toList()); } diff --git a/src/main/java/blackjack/domain/user/User.java b/src/main/java/blackjack/domain/user/User.java index b6ad991fc2..e4380a9426 100644 --- a/src/main/java/blackjack/domain/user/User.java +++ b/src/main/java/blackjack/domain/user/User.java @@ -8,11 +8,11 @@ import blackjack.domain.card.Card; import blackjack.domain.card.Deck; +import blackjack.domain.card.InvalidDeckException; import blackjack.domain.user.hand.Hand; -public abstract class User { +public abstract class User implements Comparable { private static final int DRAW_LOWER_BOUND = 1; - protected final String name; protected final Hand hand; @@ -34,10 +34,18 @@ private void validate(String name) { } public void draw(Deck deck) { + validateDeck(deck); hand.add(deck.draw()); } + private void validateDeck(Deck deck) { + if (Objects.isNull(deck)) { + throw new InvalidDeckException(InvalidDeckException.NULL); + } + } + public void draw(Deck deck, int drawNumber) { + validateDeck(deck); validateDrawNumber(drawNumber); List drawCards = IntStream.range(0, drawNumber) .mapToObj(e -> deck.draw()) @@ -53,12 +61,12 @@ protected void validateDrawNumber(int drawNumber) { public abstract boolean canDraw(); - public abstract List getInitialHand(); - public String getName() { return name; } + public abstract List getInitialHand(); + public List getHand() { return hand.getCards(); } @@ -67,10 +75,15 @@ public int getScore() { return hand.calculateScore().getScore(); } - public int getBustHandledScore() { + private int getBustHandledScore() { return hand.calculateBustHandledScore().getScore(); } + @Override + public int compareTo(User that) { + return Integer.compare(this.getBustHandledScore(), that.getBustHandledScore()); + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/src/main/java/blackjack/domain/user/hand/Hand.java b/src/main/java/blackjack/domain/user/hand/Hand.java index 3e184c951c..d52426e1d6 100644 --- a/src/main/java/blackjack/domain/user/hand/Hand.java +++ b/src/main/java/blackjack/domain/user/hand/Hand.java @@ -1,17 +1,15 @@ package blackjack.domain.user.hand; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; import blackjack.domain.card.Card; public class Hand { - private static final int BLACKJACK_SCORE = 21; - private final List cards; public Hand() { - this.cards = new LinkedList<>(); + this.cards = new ArrayList<>(); } public void add(Card card) { @@ -34,7 +32,7 @@ public Score calculateScore() { public Score calculateBustHandledScore() { Score score = calculateScore(); - if (score.getScore() > BLACKJACK_SCORE) { + if (score.isMoreThan(Score.BUST_SCORE)) { return Score.ZERO; } return score; diff --git a/src/main/java/blackjack/domain/user/hand/InvalidScoreException.java b/src/main/java/blackjack/domain/user/hand/InvalidScoreException.java index 2f5c63270e..140f896636 100644 --- a/src/main/java/blackjack/domain/user/hand/InvalidScoreException.java +++ b/src/main/java/blackjack/domain/user/hand/InvalidScoreException.java @@ -2,6 +2,7 @@ public class InvalidScoreException extends IllegalArgumentException { public static final String INVALID = "점수는 0보다 작을 수 없습니다."; + public static final String NULL = "점수를 계산할 카드가 존재하지 않습니다."; public InvalidScoreException(String s) { super(s); diff --git a/src/main/java/blackjack/domain/user/hand/Score.java b/src/main/java/blackjack/domain/user/hand/Score.java index a69164f34d..1f17bf1329 100644 --- a/src/main/java/blackjack/domain/user/hand/Score.java +++ b/src/main/java/blackjack/domain/user/hand/Score.java @@ -10,17 +10,17 @@ public class Score { public static final Score ZERO = new Score(0); + public static final int BUST_SCORE = 22; private static final Map CACHE = new HashMap<>(); private static final int ADDITIONAL_ACE_SCORE = 10; - private static final int BUST_SCORE = 22; - - private final int score; static { IntStream.range(Symbol.ACE.getScore(), BUST_SCORE) .forEach(score -> CACHE.put(score, new Score(score))); } + private final int score; + private Score(int score) { validate(score); this.score = score; @@ -42,16 +42,27 @@ public static Score valueOf(int number) { } public static Score valueOf(Card card) { - return CACHE.get(card.getSymbolValue()); + if (Objects.isNull(card)) { + throw new InvalidScoreException(InvalidScoreException.NULL); + } + return Score.valueOf(card.getScore()); } public Score add(Card card) { - int currentScore = score + card.getSymbolValue(); + if (Objects.isNull(card)) { + throw new InvalidScoreException(InvalidScoreException.NULL); + } - if (card.isAce() && currentScore + ADDITIONAL_ACE_SCORE < BUST_SCORE) { - return Score.valueOf(currentScore + ADDITIONAL_ACE_SCORE); + int calculatedScore = score + card.getScore(); + + if (card.isAce() && isNotBustFromBigAceWith(calculatedScore)) { + calculatedScore += ADDITIONAL_ACE_SCORE; } - return Score.valueOf(currentScore); + return Score.valueOf(calculatedScore); + } + + private boolean isNotBustFromBigAceWith(int calculatedScore) { + return calculatedScore + ADDITIONAL_ACE_SCORE < BUST_SCORE; } public boolean isLowerThan(int score) { @@ -59,11 +70,7 @@ public boolean isLowerThan(int score) { } public boolean isMoreThan(int score) { - return this.score > score; - } - - public boolean isMoreThan(Score score) { - return this.score > score.score; + return this.score >= score; } public int getScore() { diff --git a/src/main/java/blackjack/util/StringUtil.java b/src/main/java/blackjack/util/StringUtil.java index c65863286b..7334c37d32 100644 --- a/src/main/java/blackjack/util/StringUtil.java +++ b/src/main/java/blackjack/util/StringUtil.java @@ -2,8 +2,10 @@ import static java.util.stream.Collectors.*; +import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.Objects; import blackjack.domain.card.Card; import blackjack.domain.result.ResultType; @@ -11,20 +13,30 @@ import blackjack.domain.user.User; public class StringUtil { - private static final String DELIMITER = ", "; + private static final String SPLIT_DELIMITER = ","; + private static final String JOIN_DELIMITER = ", "; private static final String SPACE = " "; + public static List parsingPlayerNames(String inputPlayerNames) { + if (Objects.isNull(inputPlayerNames)) { + throw new NullPointerException("플레이어 입력이 존재하지 않습니다."); + } + return Arrays.stream(inputPlayerNames.split(SPLIT_DELIMITER)) + .map(String::trim) + .collect(toList()); + } + public static String joinPlayerNames(List players) { return players.stream() .filter(user -> user.getClass().equals(Player.class)) .map(User::getName) - .collect(joining(DELIMITER)); + .collect(joining(JOIN_DELIMITER)); } public static String joinCards(List cards) { return cards.stream() .map(Card::toString) - .collect(joining(DELIMITER)); + .collect(joining(JOIN_DELIMITER)); } public static String joinDealerResult(Map dealerResult) { diff --git a/src/main/java/blackjack/view/OutputView.java b/src/main/java/blackjack/view/OutputView.java index e14b1a05b7..bb27683037 100644 --- a/src/main/java/blackjack/view/OutputView.java +++ b/src/main/java/blackjack/view/OutputView.java @@ -35,6 +35,12 @@ public static void printDealerDrawCard() { System.out.println(String.format(DEALER_DRAW_FORMAT, Dealer.NAME, Dealer.DEALER_DRAWABLE_MAX_SCORE)); } + public static void printUsersCardsAndScore(List users) { + for (User user : users) { + OutputView.printUserHandAndScore(user); + } + } + public static void printUserHandAndScore(User user) { System.out.println(user.getName() + SEPARATOR + StringUtil.joinCards(user.getHand()) diff --git a/src/test/java/blackjack/domain/BlackjackTableTest.java b/src/test/java/blackjack/domain/BlackjackTableTest.java new file mode 100644 index 0000000000..65e0cf0651 --- /dev/null +++ b/src/test/java/blackjack/domain/BlackjackTableTest.java @@ -0,0 +1,91 @@ +package blackjack.domain; + +import static blackjack.domain.BlackjackTable.*; +import static org.assertj.core.api.Assertions.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Stream; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import blackjack.domain.card.CardFactory; +import blackjack.domain.card.Deck; +import blackjack.domain.user.Dealer; +import blackjack.domain.user.Player; +import blackjack.domain.user.User; + +class BlackjackTableTest { + private Deck deck; + private Dealer dealer; + private List players; + + @BeforeEach + void setUp() { + deck = new Deck(CardFactory.create()); + dealer = new Dealer(Dealer.NAME); + players = Arrays.asList( + new Player("pobi"), + new Player("sony"), + new Player("stitch")); + } + + @Test + void BlackjackTable_InputDeck_GenerateInstance() { + assertThat(new BlackjackTable(deck)).isInstanceOf(BlackjackTable.class); + } + + @Test + void collectToUsersFrom_DealerAndPlayers_ReturnUserList() { + BlackjackTable blackjackTable = new BlackjackTable(deck); + + List expected = new ArrayList<>(); + expected.add(dealer); + expected.addAll(players); + assertThat(blackjackTable.collectToUsersFrom(dealer, players)).isEqualTo(expected); + } + + @Test + void drawInitialCards_DealerAndPlayer_DrawInitialCardsForEachUsers() { + BlackjackTable blackjackTable = new BlackjackTable(deck); + List users = blackjackTable.collectToUsersFrom(dealer, players); + blackjackTable.drawInitialCards(users); + + // NOTE: 2020-03-15 테스트에서 forEach를 사용해도 괜찮은가 + for (User user : users) { + assertThat(user).extracting("hand").asList().hasSize(INITIAL_DRAW_NUMBER); + } + } + + @ParameterizedTest + @MethodSource("provideDealerOrPlayer") + void drawCardFrom_User_DrawOneCardAndReturnDrownCards(User user) { + BlackjackTable blackjackTable = new BlackjackTable(deck); + blackjackTable.drawCardFrom(user); + + assertThat(user).extracting("hand").asList().hasSize(INITIAL_DRAW_NUMBER + 1); + } + + private static Stream provideDealerOrPlayer() { + Deck deck = new Deck(CardFactory.create()); + + Dealer dealer = new Dealer(Dealer.NAME); + dealer.draw(deck, INITIAL_DRAW_NUMBER); + + Player pobi = new Player("pobi"); + pobi.draw(deck, INITIAL_DRAW_NUMBER); + + Player sony = new Player("sony"); + sony.draw(deck, INITIAL_DRAW_NUMBER); + + return Stream.of( + Arguments.of(dealer), + Arguments.of(pobi), + Arguments.of(sony)); + } +} diff --git a/src/test/java/blackjack/domain/DrawOpinionTest.java b/src/test/java/blackjack/domain/DrawOpinionTest.java index 3d805d981c..f8c194d9bd 100644 --- a/src/test/java/blackjack/domain/DrawOpinionTest.java +++ b/src/test/java/blackjack/domain/DrawOpinionTest.java @@ -16,4 +16,10 @@ void of_InvalidInputDrawOpinion_InvalidDrawOpinionExceptionThrown() { .isInstanceOf(InvalidDrawOpinionException.class) .hasMessage(InvalidDrawOpinionException.INVALID); } + + @Test + void isYes_PlayerDrawOpinionIsYes_ReturnTrue() { + DrawOpinion playerDrawOpinion = DrawOpinion.of("y"); + assertThat(playerDrawOpinion.isYes()).isTrue(); + } } diff --git a/src/test/java/blackjack/domain/card/DeckTest.java b/src/test/java/blackjack/domain/card/DeckTest.java index 89d1d04a06..40f0f16dae 100644 --- a/src/test/java/blackjack/domain/card/DeckTest.java +++ b/src/test/java/blackjack/domain/card/DeckTest.java @@ -3,7 +3,7 @@ import static org.assertj.core.api.Assertions.*; import java.util.Arrays; -import java.util.List; +import java.util.LinkedList; import java.util.stream.Stream; import org.junit.jupiter.api.Test; @@ -12,42 +12,44 @@ import org.junit.jupiter.params.provider.MethodSource; class DeckTest { + private static Stream provideEmptyDeck() { + int initDeckSize = CardFactory.create().size(); + Deck deck = new Deck(CardFactory.create()); + + for (int i = 0; i < initDeckSize; i++) { + deck.draw(); + } + return Stream.of(Arguments.of(deck)); + } + @Test - void Deck_GetCardsFromCardRepository_GenerateInstance() { - assertThat(new Deck()).isInstanceOf(Deck.class); + void Deck_GetCardsFromCardFactory_GenerateInstance() { + assertThat(new Deck(CardFactory.create())).isInstanceOf(Deck.class); } @Test void validate_DuplicateExistCards_InvalidDeckExceptionThrown() { - List cards = Arrays.asList( + LinkedList cards = new LinkedList<>(Arrays.asList( new Card(Symbol.TEN, Type.CLUB), - new Card(Symbol.TEN, Type.CLUB)); + new Card(Symbol.TEN, Type.CLUB))); - assertThatThrownBy(() -> new Deck().refill(cards)) + assertThatThrownBy(() -> new Deck(cards)) .isInstanceOf(InvalidDeckException.class) .hasMessage(InvalidDeckException.INVALID); } @Test void draw_UserDrawCardFromDeck_ReturnCard() { - Deck deck = new Deck(); + Deck deck = new Deck(CardFactory.create()); assertThat(deck.draw()).isInstanceOf(Card.class); } @ParameterizedTest @MethodSource("provideEmptyDeck") - void refill_EmptyDeck_RefillDeck(Deck deck) { - assertThat(deck.draw()).isInstanceOf(Card.class); - } - - private static Stream provideEmptyDeck() { - int initDeckSize = CardRepository.cards().size(); - Deck deck = new Deck(); - - for (int i = 0; i < initDeckSize; i++) { - deck.draw(); - } - return Stream.of(Arguments.of(deck)); + void draw_DrawFromEmptyDeck_InvalidDeckExceptionThrown(Deck deck) { + assertThatThrownBy(deck::draw) + .isInstanceOf(InvalidDeckException.class) + .hasMessage(InvalidDeckException.DECK_EMPTY); } } diff --git a/src/test/java/blackjack/domain/card/SymbolTest.java b/src/test/java/blackjack/domain/card/SymbolTest.java new file mode 100644 index 0000000000..d366eb6ac9 --- /dev/null +++ b/src/test/java/blackjack/domain/card/SymbolTest.java @@ -0,0 +1,12 @@ +package blackjack.domain.card; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class SymbolTest { + @Test + void isAce_ThisInstanceIsAce_ReturnTrue() { + assertThat(Symbol.valueOf("ACE").isAce()).isTrue(); + } +} diff --git a/src/test/java/blackjack/domain/result/ReportTest.java b/src/test/java/blackjack/domain/result/ReportTest.java index 5ea06c85f5..804fcb060c 100644 --- a/src/test/java/blackjack/domain/result/ReportTest.java +++ b/src/test/java/blackjack/domain/result/ReportTest.java @@ -7,9 +7,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Stream; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import blackjack.domain.card.Card; import blackjack.domain.card.Symbol; @@ -17,11 +21,24 @@ import blackjack.domain.user.Dealer; import blackjack.domain.user.Player; import blackjack.domain.user.PlayerFactory; +import blackjack.util.StringUtil; class ReportTest { private Dealer dealer; private List players; + private static Stream provideNullDealerOrNullPlayers() { + Dealer dealer = new Dealer(Dealer.NAME); + List players = Arrays.asList( + new Player("pobi"), + new Player("stitch")); + + return Stream.of( + Arguments.of(null, players), + Arguments.of(dealer, null), + Arguments.of(null, null)); + } + @BeforeEach void setUp() { dealer = Dealer.valueOf("dealer", Arrays.asList( @@ -42,10 +59,20 @@ void setUp() { @Test void from_DealerAndPlayers_GenerateInstance() { - assertThat(Report.from(new Dealer("dealer"), PlayerFactory.create("pobi, sony, stitch"))) + assertThat(Report.from(new Dealer("dealer"), + PlayerFactory.create(StringUtil.parsingPlayerNames("pobi, sony, stitch")))) .isInstanceOf(Report.class); } + @ParameterizedTest + @MethodSource("provideNullDealerOrNullPlayers") + void validateUser_DealerOrPlayersHaveNullValue_InvalidReportExceptionThrown(Dealer nullDealer, + List nullPlayers) { + assertThatThrownBy(() -> Report.from(nullDealer, nullPlayers)) + .isInstanceOf(InvalidReportException.class) + .hasMessage(InvalidReportException.NULL); + } + @Test void generatePlayersResult_dealerAndPlayers_MapOfPlayerAndResultType() { Report report = Report.from(dealer, players); diff --git a/src/test/java/blackjack/domain/result/ResultTypeTest.java b/src/test/java/blackjack/domain/result/ResultTypeTest.java index cae4419188..f16a416790 100644 --- a/src/test/java/blackjack/domain/result/ResultTypeTest.java +++ b/src/test/java/blackjack/domain/result/ResultTypeTest.java @@ -17,12 +17,6 @@ import blackjack.domain.user.User; class ResultTypeTest { - @ParameterizedTest - @MethodSource("provideDealerAndPlayerScoreWithReturnType") - void from_DealerScoreAndPlayerScore_ReturnResultType(User targetUser, User compareUser, ResultType expected) { - assertThat(ResultType.from(targetUser, compareUser)).isEqualTo(expected); - } - private static Stream provideDealerAndPlayerScoreWithReturnType() { Dealer dealer = Dealer.valueOf("dealer", Arrays.asList( new Card(Symbol.EIGHT, Type.HEART), @@ -48,4 +42,10 @@ private static Stream provideDealerAndPlayerScoreWithReturnType() { Arguments.of(dealer, stitch, ResultType.WIN), Arguments.of(dealer, bustPlayer, ResultType.WIN)); } + + @ParameterizedTest + @MethodSource("provideDealerAndPlayerScoreWithReturnType") + void of_DealerScoreAndPlayerScore_ReturnResultType(User targetUser, User compareUser, ResultType expected) { + assertThat(ResultType.of(targetUser, compareUser)).isEqualTo(expected); + } } diff --git a/src/test/java/blackjack/domain/user/DealerTest.java b/src/test/java/blackjack/domain/user/DealerTest.java index a493d0c79c..05aebf8857 100644 --- a/src/test/java/blackjack/domain/user/DealerTest.java +++ b/src/test/java/blackjack/domain/user/DealerTest.java @@ -12,11 +12,24 @@ import org.junit.jupiter.params.provider.MethodSource; import blackjack.domain.card.Card; +import blackjack.domain.card.CardFactory; import blackjack.domain.card.Deck; import blackjack.domain.card.Symbol; import blackjack.domain.card.Type; class DealerTest { + private static Stream provideUndrawableDealer() { + final int WORST_CASE_OF_DRAWABLE_COUNT = 12; + + Dealer dealer = new Dealer("dealer"); + Deck deck = new Deck(CardFactory.create()); + + for (int i = 0; i < WORST_CASE_OF_DRAWABLE_COUNT; i++) { + dealer.draw(deck); + } + return Stream.of(Arguments.of(dealer)); + } + @Test void Dealer_InputDealerName_GenerateInstance() { Dealer dealer = new Dealer("dealer"); @@ -46,18 +59,6 @@ void canDraw_CurrentScoreMoreThanDrawableMaxScore_ReturnFalse(Dealer dealer) { assertThat(dealer.canDraw()).isFalse(); } - private static Stream provideUndrawableDealer() { - final int WORST_CASE_OF_DRAWABLE_COUNT = 12; - - Dealer dealer = new Dealer("dealer"); - Deck deck = new Deck(); - - for (int i = 0; i < WORST_CASE_OF_DRAWABLE_COUNT; i++) { - dealer.draw(deck); - } - return Stream.of(Arguments.of(dealer)); - } - @Test void getInitialHand_DealerDrawInitialTwoCards_ReturnOneCard() { List cards = Arrays.asList( diff --git a/src/test/java/blackjack/domain/user/PlayerFactoryTest.java b/src/test/java/blackjack/domain/user/PlayerFactoryTest.java index 2385251da4..1d73420236 100644 --- a/src/test/java/blackjack/domain/user/PlayerFactoryTest.java +++ b/src/test/java/blackjack/domain/user/PlayerFactoryTest.java @@ -7,6 +7,8 @@ import org.junit.jupiter.api.Test; +import blackjack.util.StringUtil; + class PlayerFactoryTest { @Test void create_PlayerNames_GeneratePlayerList() { @@ -15,6 +17,6 @@ void create_PlayerNames_GeneratePlayerList() { new Player("sony"), new Player("stitch")); - assertThat(PlayerFactory.create("pobi, sony, stitch")).isEqualTo(players); + assertThat(PlayerFactory.create(StringUtil.parsingPlayerNames("pobi, sony, stitch"))).isEqualTo(players); } } diff --git a/src/test/java/blackjack/domain/user/PlayerTest.java b/src/test/java/blackjack/domain/user/PlayerTest.java index 1389922b22..d58ec50b15 100644 --- a/src/test/java/blackjack/domain/user/PlayerTest.java +++ b/src/test/java/blackjack/domain/user/PlayerTest.java @@ -12,11 +12,24 @@ import org.junit.jupiter.params.provider.MethodSource; import blackjack.domain.card.Card; +import blackjack.domain.card.CardFactory; import blackjack.domain.card.Deck; import blackjack.domain.card.Symbol; import blackjack.domain.card.Type; class PlayerTest { + private static Stream provideUndrawablePlayer() { + final int WORST_CASE_OF_DRAWABLE_COUNT = 12; + + Player player = new Player("player"); + Deck deck = new Deck(CardFactory.create()); + + for (int i = 0; i < WORST_CASE_OF_DRAWABLE_COUNT; i++) { + player.draw(deck); + } + return Stream.of(Arguments.of(player)); + } + @Test void Player_InputPlayerName_GenerateInstance() { assertThat(new Player("player")).isInstanceOf(Player.class); @@ -42,16 +55,4 @@ void canDraw_CurrentScoreLowerThanDrawableMaxScore_ReturnTrue() { void canDraw_CurrentScoreMoreThanDrawableMaxScore_ReturnFalse(Player player) { assertThat(player.canDraw()).isFalse(); } - - private static Stream provideUndrawablePlayer() { - final int WORST_CASE_OF_DRAWABLE_COUNT = 12; - - Player player = new Player("player"); - Deck deck = new Deck(); - - for (int i = 0; i < WORST_CASE_OF_DRAWABLE_COUNT; i++) { - player.draw(deck); - } - return Stream.of(Arguments.of(player)); - } } diff --git a/src/test/java/blackjack/domain/user/UserTest.java b/src/test/java/blackjack/domain/user/UserTest.java index 7e6f1755bd..976b465988 100644 --- a/src/test/java/blackjack/domain/user/UserTest.java +++ b/src/test/java/blackjack/domain/user/UserTest.java @@ -7,7 +7,9 @@ import org.junit.jupiter.params.provider.NullAndEmptySource; import blackjack.domain.card.Card; +import blackjack.domain.card.CardFactory; import blackjack.domain.card.Deck; +import blackjack.domain.card.InvalidDeckException; class UserTest { @Test @@ -28,7 +30,7 @@ void validate_InvalidUserName_InvalidUserExceptionThrown(String name) { @Test void draw_DrawCard_AddDrawCardToHand() { User user = new Dealer("user"); - Deck deck = new Deck(); + Deck deck = new Deck(CardFactory.create()); user.draw(deck); assertThat(user).extracting("hand").asList() @@ -36,10 +38,19 @@ void draw_DrawCard_AddDrawCardToHand() { .hasSize(1); } + @Test + void validateDeck_Null_InvalidDeckExceptionThrown() { + User user = new Dealer("user"); + + assertThatThrownBy(() -> user.draw(null)) + .isInstanceOf(InvalidDeckException.class) + .hasMessage(InvalidDeckException.NULL); + } + @Test void draw_DrawNumberOfCards_AddDrawCardsToHand() { User user = new Dealer("user"); - Deck deck = new Deck(); + Deck deck = new Deck(CardFactory.create()); user.draw(deck, 2); assertThat(user).extracting("hand").asList() @@ -50,7 +61,7 @@ void draw_DrawNumberOfCards_AddDrawCardsToHand() { @Test void validateDrawNumber_InvalidNumberOfCards_InvalidUserExceptionThrown() { User user = new Dealer("user"); - Deck deck = new Deck(); + Deck deck = new Deck(CardFactory.create()); assertThatThrownBy(() -> user.draw(deck, 0)) .isInstanceOf(InvalidUserException.class) diff --git a/src/test/java/blackjack/domain/user/hand/ScoreTest.java b/src/test/java/blackjack/domain/user/hand/ScoreTest.java index 2c73eec046..67467ead41 100644 --- a/src/test/java/blackjack/domain/user/hand/ScoreTest.java +++ b/src/test/java/blackjack/domain/user/hand/ScoreTest.java @@ -6,6 +6,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.EnumSource; +import org.junit.jupiter.params.provider.NullSource; import blackjack.domain.card.Card; import blackjack.domain.card.Symbol; @@ -31,7 +32,15 @@ void valueOf_InputCard_ReturnInstance(Symbol symbol) { Card card = new Card(symbol, Type.CLUB); assertThat(Score.valueOf(card)).isInstanceOf(Score.class) - .extracting("score").isEqualTo(card.getSymbolValue()); + .extracting("score").isEqualTo(card.getScore()); + } + + @ParameterizedTest + @NullSource + void valueOf_InputNull_NullPointerExceptionThrown(Card card) { + assertThatThrownBy(() -> Score.valueOf(card)) + .isInstanceOf(InvalidScoreException.class) + .hasMessage(InvalidScoreException.NULL); } @Test @@ -64,19 +73,10 @@ void isLowerThan_InputIntegerScore_ReturnCompareResult(int value, boolean expect } @ParameterizedTest - @CsvSource(value = {"8,true", "9,false"}) + @CsvSource(value = {"9,true", "10,false"}) void isMoreThan_InputIntegerScore_ReturnCompareResult(int value, boolean expected) { Score score = Score.valueOf(9); assertThat(score.isMoreThan(value)).isEqualTo(expected); } - - @ParameterizedTest - @CsvSource(value = {"8,true", "9,false"}) - void isMoreThan_InputScore_ReturnCompareResult(int value, boolean expected) { - Score score = Score.valueOf(9); - Score cmpScore = Score.valueOf(value); - - assertThat(score.isMoreThan(cmpScore)).isEqualTo(expected); - } } diff --git a/src/test/java/blackjack/util/StringUtilTest.java b/src/test/java/blackjack/util/StringUtilTest.java index 7d2d369775..177c20d81d 100644 --- a/src/test/java/blackjack/util/StringUtilTest.java +++ b/src/test/java/blackjack/util/StringUtilTest.java @@ -1,5 +1,6 @@ package blackjack.util; +import static blackjack.util.StringUtil.*; import static org.assertj.core.api.Assertions.*; import java.util.Arrays; @@ -8,6 +9,8 @@ import java.util.Map; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullSource; import blackjack.domain.card.Card; import blackjack.domain.card.Symbol; @@ -18,6 +21,24 @@ import blackjack.domain.user.User; class StringUtilTest { + @Test + void parsingPlayerNames_InputPlayerNames_PlayerNameList() { + String value = "pobi,sony,stitch"; + + List expected = Arrays.asList( + "pobi", + "sony", + "stitch"); + assertThat(parsingPlayerNames(value)).isEqualTo(expected); + } + + @ParameterizedTest + @NullSource + void parsingPlayerNames_InputNull_NullPointerExceptionThrown(String value) { + assertThatThrownBy(() -> parsingPlayerNames(value)) + .isInstanceOf(NullPointerException.class); + } + @Test void joinPlayerNames_PlayerList_JoinPlayerNamesToString() { List users = Arrays.asList(