code stringlengths 41 34.3k | lang stringclasses 8
values | review stringlengths 1 4.74k |
|---|---|---|
@@ -0,0 +1,67 @@
+package lotto.model;
+
+import camp.nextstep.edu.missionutils.Randoms;
+import lotto.controller.LottoController;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static lotto.constants.ErrorMessage.ISNOTINTEGER;
+import static lotto.constants.ErrorMessage.NOTTHOUSAND;
+
+public class LottoNumberMaker {
+ private static int buyPrice;
+ private static final List<List<Integer>> numbers = new ArrayList<>();
+ private static final LottoController lottoController = new LottoController();
+
+ public static int getBuyPrice() {
+ return buyPrice;
+ }
+
+ public void makeLottoNumber(Integer count) {
+ buyPrice = count;
+ checkThousand(count);
+ IntStream.range(0, count / 1000)
+ .forEach(i -> numbers.add(makeRandomNumber()));
+ }
+
+ private void checkThousand(Integer count) {
+ if(count%1000!=0){
+ throw new IllegalArgumentException(NOTTHOUSAND.getMessage());
+ }
+ return;
+ }
+
+ private List<Integer> makeRandomNumber() {
+ return Randoms.pickUniqueNumbersInRange(1, 45, 6).stream()
+ .sorted()
+ .collect(Collectors.toList());
+ }
+
+ public void checkInt() {
+ while (true) {
+ try {
+ int number = checkCount();
+ makeLottoNumber(number);
+ break;
+ } catch (IllegalArgumentException e) {
+ System.out.println(ISNOTINTEGER.getMessage());
+ System.out.println(NOTTHOUSAND.getMessage());
+ }
+ }
+ }
+
+ private int checkCount() {
+ try {
+ return Integer.parseInt(lottoController.askPrice());
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(ISNOTINTEGER.getMessage());
+ }
+ }
+
+ public List<List<Integer>> getLottoNumber() {
+ return numbers;
+ }
+} | Java | ์ด๋ถ๋ถ๋ง ๋ฐ๋ก ์ ์ญ ๋ฉ์๋๋ก ์ ์ธํ์ ์ด์ ๊ฐ ์์ผ์ค๊น์??? |
@@ -0,0 +1,67 @@
+package lotto.model;
+
+import camp.nextstep.edu.missionutils.Randoms;
+import lotto.controller.LottoController;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static lotto.constants.ErrorMessage.ISNOTINTEGER;
+import static lotto.constants.ErrorMessage.NOTTHOUSAND;
+
+public class LottoNumberMaker {
+ private static int buyPrice;
+ private static final List<List<Integer>> numbers = new ArrayList<>();
+ private static final LottoController lottoController = new LottoController();
+
+ public static int getBuyPrice() {
+ return buyPrice;
+ }
+
+ public void makeLottoNumber(Integer count) {
+ buyPrice = count;
+ checkThousand(count);
+ IntStream.range(0, count / 1000)
+ .forEach(i -> numbers.add(makeRandomNumber()));
+ }
+
+ private void checkThousand(Integer count) {
+ if(count%1000!=0){
+ throw new IllegalArgumentException(NOTTHOUSAND.getMessage());
+ }
+ return;
+ }
+
+ private List<Integer> makeRandomNumber() {
+ return Randoms.pickUniqueNumbersInRange(1, 45, 6).stream()
+ .sorted()
+ .collect(Collectors.toList());
+ }
+
+ public void checkInt() {
+ while (true) {
+ try {
+ int number = checkCount();
+ makeLottoNumber(number);
+ break;
+ } catch (IllegalArgumentException e) {
+ System.out.println(ISNOTINTEGER.getMessage());
+ System.out.println(NOTTHOUSAND.getMessage());
+ }
+ }
+ }
+
+ private int checkCount() {
+ try {
+ return Integer.parseInt(lottoController.askPrice());
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(ISNOTINTEGER.getMessage());
+ }
+ }
+
+ public List<List<Integer>> getLottoNumber() {
+ return numbers;
+ }
+} | Java | ํด๋น ํด๋์ค๊ฐ ๋๋ฌด ๋ง์ ์ฑ
์์ ๊ฐ์ง๊ณ ์๋๊ฒ ๊ฐ์ต๋๋ค. ๊ตฌ๋งค๊ธ์ก์ ๊ฒ์ฆํ๋ ๋ถ๋ถ๊ณผ ๋ก๋ ๋ฒํธ๋ฅผ ๊ฒ์ฆํ๋ ๋ถ๋ถ์ ๋ถ๋ฆฌํ๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ผ๊ฑฐ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,39 @@
+package lotto.model.constants;
+
+public enum LottoPrize {
+ FIRST_PRIZE(0,"6๊ฐ ์ผ์น (2,000,000,000์) - ","๊ฐ",2000000000),
+ SECOND_PRIZE(0,"5๊ฐ ์ผ์น, ๋ณด๋์ค ๋ณผ ์ผ์น (30,000,000์) - ","๊ฐ", 30000000),
+ THIRD_PRIZE(0,"5๊ฐ ์ผ์น (1,500,000์) - ","๊ฐ", 1500000),
+ FOURTH_PRIZE(0,"4๊ฐ ์ผ์น (50,000์) - ","๊ฐ", 50000),
+ FIFTH_PRIZE(0,"3๊ฐ ์ผ์น (5,000์) - ","๊ฐ", 5000),
+ LOOSE(0,"","๊ฐ", 0);
+
+
+ private int winCount;
+ private final String text;
+ private final String unit;
+ private int prizeMoney;
+
+ LottoPrize(int winCount, String text, String unit, int prizemoney) {
+ this.winCount = winCount;
+ this.text = text;
+ this.unit = unit;
+ this.prizeMoney = prizemoney;
+ }
+ public int getWinCount() {
+ return winCount;
+ }
+ public String getText() {
+ return text;
+ }
+ public String getUnit() {
+ return unit;
+ }
+ public int getPrizeMoney() {
+ return prizeMoney;
+ }
+
+ public void addWinCount() {
+ this.winCount ++;
+ }
+} | Java | ์ถ๋ ฅ ํ์๊ณผ ๊ด๋ จ๋ ๋ถ๋ถ์ view์์ ์ฒ๋ฆฌํด์ผํ ๊ฑฐ ๊ฐ์๋ฐ ์ด์ ๋ํด์ ์ด๋ป๊ฒ ์๊ฐํ์๋์? |
@@ -0,0 +1,100 @@
+# Domain ๊ตฌํ
+
+## ์งํ ํ๋ก์ธ์ค (์์คํ
์ฑ
์)
+
+1. ๊ฒ์ด๋จธ๋ ์๋ํ ๋ณด์ค ๋ชฌ์คํฐ์ HP๋ฅผ ์ค์ ํ๋ค.
+2. ๊ฒ์ด๋จธ๋ ํ๋ ์ดํ ํ๋ ์ด์ด์ ์ด๋ฆ๊ณผ HP, MP๋ฅผ ์ค์ ํ๋ค.
+3. ๊ฒ์์ด ์์๋๋ค.
+4. ๊ฒ์์ ํด์ ์ด๋ฉฐ, ๋งค ํด๋ง๋ค ๋ค์๊ณผ ๊ฐ์ ํ๋์ด ๋ฐ๋ณต๋๋ค.
+ 1) ๋ณด์ค ๋ชฌ์คํฐ์ ํ๋ ์ด์ด์ ํ์ฌ ์ํ๋ฅผ ๋ณด์ฌ์ค๋ค.
+ 2) ํ๋ ์ด์ด๊ฐ ๊ณต๊ฒฉํ ๋ฐฉ์์ ์ ํํ๋ค.
+ 3) ํ๋ ์ด์ด๊ฐ ์ ํ๋ ๋ฐฉ์์ผ๋ก ๊ณต๊ฒฉํ๋ค. (๋ณด์ค ๋ชฌ์คํฐ์๊ฒ ๋ฐ๋ฏธ์ง๋ฅผ ์
ํ)
+ 4) ๋ฐ๋ฏธ์ง๋ฅผ ์
์ ๋ณด์ค ๋ชฌ์คํฐ์ ์ํ๋ฅผ ํ์ธํ๋ค.
+ 5) ๋ณด์ค ๋ชฌ์คํฐ๊ฐ ์ฃฝ์๋ค๋ฉด ๊ฒ์์ด ์ข
๋ฃ๋๋ค.
+ 6) ๋ณด์ค ๋ชฌ์คํฐ๊ฐ ์ด์์๋ค๋ฉด ํ๋ ์ด์ด๋ฅผ ๊ณต๊ฒฉํ๋ค.
+ 7) ๋ฐ๋ฏธ์ง๋ฅผ ์
์ ํ๋ ์ด์ด์ ์ํ๋ฅผ ํ์ธํ๋ค.
+ 8) ํ๋ ์ด์ด๊ฐ ์ฃฝ์๋ค๋ฉด ๊ฒ์์ด ์ข
๋ฃ๋๋ค.
+5. ๊ฒ์ ์ข
๋ฃ ํ ์น์๋ฅผ ์๋ดํ๋ค.
+ - ํ๋ ์ด์ด๊ฐ ์ด๊ฒผ์ ๊ฒฝ์ฐ : ์งํํ ํด ์๋ฅผ ์๋ ค์ค๋ค.
+ - ๋ณด์ค ๋ชฌ์คํฐ๊ฐ ์ด๊ฒผ์ ๊ฒฝ์ฐ : ํ๋ ์ด์ด์ ๋ณด์ค ๋ชฌ์คํฐ์ ํ์ฌ ์ํ๋ฅผ ๋ณด์ฌ์ค๋ค.
+
+## Domain ๊ฐ์ฒด ์ ์ ๋ฐ ์ฑ
์๊ณผ ํ๋ ฅ ํ ๋น
+
+1. **Raid Game**
+ - ๊ฒ์ ์์ ์ ์ฃผ์ ์ธ๋ฌผ์ ๋ํด ์
ํ
ํ ์ฑ
์์ ๊ฐ์ง
+ - ๊ฒ์ ์ธ๋ฌผ์ธ **Boss Monster**๋ฅผ ์์ฑํ ์ฑ
์์ ๊ฐ์ง
+ - ๊ฒ์ ์ธ๋ฌผ์ธ **Player**๋ฅผ ์์ฑํ ์ฑ
์์ ๊ฐ์ง
+ - ๋งค ํด์ ์คํํ ์ฑ
์์ ๊ฐ์ง
+ - ๊ฒ์ ์ํ๋ฅผ ํ์ธํ ์ฑ
์์ ๊ฐ์ง (๊ฒ์ ์ํ๊ฐ '์ข
๋ฃ'์ผ ๊ฒฝ์ฐ ํด์ ์คํํ์ง ์์)
+ - ํด ์๋ฅผ ์ฆ๊ฐ์ํฌ ์ฑ
์์ ๊ฐ์ง
+ - **Player**๊ฐ **Boss Monster**๋ฅผ ์ ํ๋ ๊ณต๊ฒฉ ๋ฐฉ์์ผ๋ก ๊ณต๊ฒฉํ๋๋ก ํ ์ฑ
์์ ๊ฐ์ง
+ - **Player**์ ๊ณต๊ฒฉ ํ๋์ ๋ํ ๊ฒฐ๊ณผ๋ฅผ ์ ๊ณต๋ฐ๊ณ , **Boss Monster**์๊ฒ ์ ๋ฌํ ์ฑ
์์ ๊ฐ์ง (**Player**์ ๊ณต๊ฒฉ๊ณผ **Boss Monster**์ ํผํด ํ๋์ ๋ํ ํ๋ ฅ ํ์)
+ - **Boss Monster**์ ์์กด์ฌ๋ถ์ ๋ฐ๋ผ ๊ฒ์ ์ํ๋ฅผ ๋ณ๊ฒฝํ ์ฑ
์์ ๊ฐ์ง (**Boss Monster**์ ์ํ๋ฅผ ์ ๊ณต๋ฐ๋ ํ๋ ฅ ํ์)
+ - **Boss Monster**๊ฐ **Player**๋ฅผ ๊ณต๊ฒฉํ๋๋ก ํ ์ฑ
์์ ๊ฐ์ง (**Boss Monster**์ ๊ณต๊ฒฉ๊ณผ **Player**์ ํผํด ํ๋์ ๋ํ ํ๋ ฅ ํ์)
+ - **Boss Monster**์ ๊ณต๊ฒฉ ํ๋์ ๋ํ ๊ฒฐ๊ณผ๋ฅผ ์ ๊ณต๋ฐ๊ณ , **Player**์๊ฒ ์ ๋ฌํ ์ฑ
์์ ๊ฐ์ง (**Boss Monster**์ ๊ณต๊ฒฉ๊ณผ **Player**์ ํผํด ํ๋์ ๋ํ ํ๋ ฅ ํ์)
+ - **Player**์ ์์กด์ฌ๋ถ์ ๋ฐ๋ผ ๊ฒ์ ์ํ๋ฅผ ๋ณ๊ฒฝํ ์ฑ
์์ ๊ฐ์ง (**Player**์ ์ํ๋ฅผ ์ ๊ณต๋ฐ๋ ํ๋ ฅ ํ์)
+ - ํด๋น ํด์ ๋ํ ๊ฒ์ ๊ธฐ๋ก์ ์์ฑํ ์ฑ
์์ ๊ฐ์ง (**GameHistory**์๊ฒ ํด์ ๋ํ ๊ธฐ๋ก์ ์์ฑํ๋ ํ๋ ฅ ํ์)
+
+2. **Boss Monster**
+ - ํ์ฌ ์ํ๋ฅผ ์ ๊ณตํด์ค ์ฑ
์์ ๊ฐ์ง (์ด HP, ํ์ฌ HP)
+ - ๊ณต๊ฒฉ ๋ช
๋ น์ ์ํํ ์ฑ
์์ ๊ฐ์ง (0~20์ ๋๋ค ๋ฐ๋ฏธ์ง)
+ - ๊ณต๊ฒฉ ํผํด ์
์์ ์ํํ ์ฑ
์์ ๊ฐ์ง (ํ์ฌ HP ๊ฐ์)
+
+3. **Player**
+ - ํ์ฌ ์ํ๋ฅผ ์ ๊ณตํด์ค ์ฑ
์์ ๊ฐ์ง (์ด๋ฆ, ์ด HP, ํ์ฌ HP, ์ด MP, ํ์ฌ MP)
+ - ๊ณต๊ฒฉ ๋ช
๋ น์ ์ํํ ์ฑ
์์ ๊ฐ์ง (**Attack Type**์ ๋ํ ์ ๋ณด๋ฅผ ์ ๊ณต๋ฐ๋ ํ๋ ฅ ํ์, MP ๊ฐ์)
+ - ๊ณต๊ฒฉ ํผํด ์
์์ ์ํํ ์ฑ
์์ ๊ฐ์ง (ํ์ฌ HP ๊ฐ์)
+
+4. **Attack Type**
+ - ๊ณต๊ฒฉ ๋ฐฉ์ ์ข
๋ฅ๋ฅผ ์ ๊ณตํด์ค ์ฑ
์์ ๊ฐ์ง (๊ณต๊ฒฉ ๋ณํธ, ๊ณต๊ฒฉ ๋ฐฉ์ ์ด๋ฆ)
+ - ๊ณต๊ฒฉ ๋ฐฉ์ ์ํ์ ๋ํ ์ ๋ณด๋ฅผ ์ ๊ณตํด์ค ์ฑ
์์ ๊ฐ์ง (๋ฐ๋ฏธ์ง, MP ์๋ชจ๋, MP ํ๋ณต๋)
+
+5. **Game History**
+ - ๋งค ํด์ ๋ํ ๊ธฐ๋ก์ ์ ์ฅํ ์ฑ
์์ ๊ฐ์ง
+ - ์ถํ Controller์ Veiw๋ฅผ ์ํ ์์ฒญ์ ์๋ต ๊ฐ๋ฅ
+
+## ์ฃผ์ ๋ฉ์์ง ๊ตฌ์ฑ (๊ณต์ฉ ์ธํฐํ์ด์ค)
+
+1. ๋ณด์ค ๋ชฌ์คํฐ ์ค์ ์ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Create Boss Monster
+ - ์ธ์ : int hp
+ - ์๋ต : void, Boss Monster ๊ฐ์ฒด ์์ฑ
+
+2. ํ๋ ์ด์ด ์ค์ ์ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Create Player
+ - ์ธ์ : String name, int hp, int mp
+ - ์๋ต : void, Player ๊ฐ์ฒด ์์ฑ
+
+3. ํด ์งํ์ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Start Game
+ - ์ธ์ : -
+ - ์๋ต : ๊ตฌ์ฑ๋ ํด ๋ก์ง ์คํ
+
+4. ๊ฒ์ ์ํ ํ์ธ์ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Check Game Status
+ - ์ธ์ : -
+ - ์๋ต : return Game Status (true or False)
+
+5. ํด ํ์ ์ฆ๊ฐ๋ฅผ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Increase Turns
+ - ์ธ์ : -
+ - ์๋ต : void, Turn 1 ์ฆ๊ฐ
+
+6. ๊ณต๊ฒฉ ํ๋ ์ํ์ ์์ฒญ
+ - ์์ ์ : Player, Boss Monster
+ - ์ด๋ฆ : Attack
+ - ์ธ์ : -
+ - ์๋ต : return ๋ฐ๋ฏธ์ง, ๊ณต๊ฒฉ ๋ฐฉ์์ ๋ฐ๋ผ MP ๊ฐ์(Player)
+
+7. ๊ณต๊ฒฉ ํผํด ์ ๋ฌ์ ์์ฒญ
+ - ์์ ์ : Player, Boss Monster
+ - ์ด๋ฆ : take Damage
+ - ์ธ์ : int damage
+ - ์๋ต : return ํ์ฌ HP
+
+ | Unknown | ์์ฒญ ์ฌํญ์ ๊ต์ฅํ ๊น๋ํ๊ฒ ์ ์ด์ฃผ์ ๊ฒ ๊ฐ์์!
ํ์ง๋ง ์ฐ๋ ค๋๋ ์ ๋ ์๋๋ฐ์. ์ ์ฑ์ค๋ ์์ฑํด์ฃผ์ ๋ฌธ์์ง๋ง ๋๋ฃ ์
์ฅ์์๋ ํจ์ ์ด๋ฆ์ด๋ ์ธ์ ๋ฑ์ ๊ธ๋ก ํํํ๋ ๊ฒ๋ณด๋ค ํจ์ ํํ๋ฅผ ๋ณด์ฌ์ฃผ๋ ๊ฒ์ด ๊ฐ๋
์ฑ์ด ๋ ์ข์ ์๋ ์๊ฒ ๋ค๋ ์๊ฐ์ ํ์ด์!
์ฑ
๋ค๋ ๋ณด๋ฉด ๊ธ๋ณด๋ค๋ ์ฝ๋๋ก ๋จผ์ ๋ณด์ฌ์ฃผ์์์:)
```java
public void start() {
// ๊ฒ์ ์์ ๋ก์ง
}
```
์ด๋ฐ ๋๋์ผ๋ก์!
์ด๋ป๊ฒ ์๊ฐํ์๋์? |
@@ -0,0 +1,17 @@
+package bossmonster.exception;
+
+import bossmonster.view.OutputView;
+import java.util.function.Supplier;
+
+public class ExceptionHandler {
+
+ public static <T> T retryInput(Supplier<T> supplier) {
+ while (true) {
+ try {
+ return supplier.get();
+ } catch (Exception exception) {
+ OutputView.printError(exception);
+ }
+ }
+ }
+} | Java | ์ด๋ ๊ฒ ๋ง๋ค ์๊ฐ์ ํ์ง ๋ชปํ๋๋ฐ, ์ ๋ค๋ฆญ์ ์ ๋ง ์ ํ์ฉํ์ ๊ฒ ๊ฐ์์!
ํ์ง๋ง ํจ์๋ช
์์ ์คํด์ ์์ง๊ฐ ์๋ค๊ณ ์๊ฐํด์. ์ฒซ ์๋๋ฅผ ํ ๋๋ ํด๋น ํจ์๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ธ๋ฐ์. ์ค๋ฅ๊ฐ ๋์ง ์์๋๊น์ง ๊ณ์ ์ฌ์
๋ ฅ ๋ฐ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๋ค๋ฅธ ๋ค์ด๋ฐ์ด ์ด์ธ๋ฆด ๊ฒ ๊ฐ์์! ์... ๋ง๋
ํ ๋ ์ค๋ฅด๋ ๋ค์ด๋ฐ์ ์๋ค์ใ
ใ
|
@@ -0,0 +1,17 @@
+package bossmonster.exception;
+
+import bossmonster.view.OutputView;
+import java.util.function.Supplier;
+
+public class ExceptionHandler {
+
+ public static <T> T retryInput(Supplier<T> supplier) {
+ while (true) {
+ try {
+ return supplier.get();
+ } catch (Exception exception) {
+ OutputView.printError(exception);
+ }
+ }
+ }
+} | Java | ์ถ๊ฐ์ ์ผ๋ก ํด๋์ค๋ช
์ด ์ด์ธ๋ฆฌ์ง ์๋๋ฏํ ๋๋์ ๋ฐ์์ด์. ์คํ๋ง์ ์๊ฐํด์ ๋ค์ด๋ฐ์ ์ ํ์ ๊ฒ ๊ฐ์์ ์คํ๋ง์ ExceptionHandler์ ๋น๊ตํด์ ๋ง์๋๋ฆฌ๊ฒ ์ต๋๋ค!
`ExceptionHandler`์ ๊ฒฝ์ฐ ๋ง ๊ทธ๋๋ก ์์ธ๋ฅผ ํธ๋ค๋งํด์ฃผ๋ ์ญํ ์
๋๋ค. A๋ผ๋ ์์ธ๊ฐ ๋ฐ์ํ๋ฉด ExceptionHandler๊ฐ ๊ทธ์ ๋ง๊ฒ ์ฒ๋ฆฌํด์ฃผ๊ณ , B๋ผ๋ ์์ธ๊ฐ ๋ฐ์ํ๋ฉด ๊ทธ๊ฒ๋ ๊ทธ๊ฒ์ ExceptionHandler์ ๋ง๊ฒ ์ฒ๋ฆฌํด์ฃผ๋ ์ญํ ์ด์ฃ . ์คํ๋ง์ ExceptionHandler์ฒ๋ผ ์์ธ๋ฅผ ํธ๋ค๋งํ๋ ๊ทธ ์์ฒด์๋ง ๋ชฉ์ ์ด ์์ด์ผํ๋ค๊ณ ์๊ฐํด์.
ํ์ง๋ง ์์ฑํ์ ์ฝ๋์์๋ ์์ธ๊ฐ ๋ฐ์ํ์ ๋ ์ด๋ฒคํธ๊ฐ ์์ธ๋ฅผ ๊ฐ์งํด ์์ธ๋ฅผ ํธ๋ค๋งํ๋ ๊ฒ์ด ์๋๋ผ, ๋ง์น ์์ธ๊ฐ ๋ฐ์ํ ๋ฒํ ํจ์๋ค์ ๋์ ์คํํด์ฃผ๋ ์ญํ ๊ฐ์์. ๋ต, ๊ทธ์ ํจ์ํ ์ธํฐํ์ด์ค์ฃ . ๊ทธ๋ ๋ค๋ฉด ์ด๊ฒ์ ๋ง๋ ๋ค์ด๋ฐ์ผ๋ก ํ๋ ๊ฒ์ด ์ข์ง ์์๊น์?
๋ง์ฝ `ExceptionHandler`๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ผ๋ฉด ์๋์ ๊ฐ์ด ๋์ด์ผํ ๊ฒ ๊ฐ์ต๋๋ค.
```java
public static T handle(Exception e) {
// ํธ๋ค๋ง
}
``` |
@@ -0,0 +1,78 @@
+package bossmonster.exception;
+
+import bossmonster.view.constants.Message;
+
+public class Validator {
+
+ public static String validateInputOfNumber(String inputValue) {
+ validateEmpty(inputValue);
+ validateInteger(inputValue);
+ return inputValue;
+ }
+
+ public static String validatePlayerName(String inputValue) {
+ validateEmpty(inputValue);
+ validateRangeOfPlayerName(inputValue);
+ return inputValue;
+ }
+
+ public static String[] validateInputOfNumbers(String inputValue) {
+ validateSeparated(inputValue);
+ String[] separatedValue = inputValue.split(",");
+ validateArray(separatedValue);
+ for (String value : separatedValue) {
+ validateEmpty(value);
+ validateInteger(value);
+ }
+ return separatedValue;
+ }
+
+ public static int validateBossMonster(int hp) {
+ return validateRangeOfMonster(hp);
+ }
+
+ public static void validatePlayerStatus(int hp, int mp) {
+ if (hp + mp != 200) {
+ throw new IllegalArgumentException(Message.ERROR_PLAYER_STATUS.getErrorMessage());
+ }
+ }
+
+ private static void validateEmpty(String inputValue) {
+ if (inputValue.isEmpty()) {
+ throw new IllegalArgumentException(Message.ERROR_EMPTY.getErrorMessage());
+ }
+ }
+
+ private static void validateInteger(String inputValue) {
+ try {
+ Integer.parseInt(inputValue);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(Message.ERROR_NOT_INTEGER.getErrorMessage());
+ }
+ }
+
+ private static void validateSeparated(String inputValue) {
+ if (!inputValue.contains(",")) {
+ throw new IllegalArgumentException(Message.ERROR_NOT_COMMA.getErrorMessage());
+ }
+ }
+
+ private static void validateArray(String[] inputValue) {
+ if (inputValue.length != 2) {
+ throw new IllegalArgumentException(Message.ERROR_ARRAY_SIZE.getErrorMessage());
+ }
+ }
+
+ private static int validateRangeOfMonster(int hp) {
+ if (hp < 100 || hp > 300) {
+ throw new IllegalArgumentException(Message.ERROR_BOSS_MONSTER_HP.getErrorMessage());
+ }
+ return hp;
+ }
+
+ private static void validateRangeOfPlayerName(String name) {
+ if (name.length() > 5) {
+ throw new IllegalArgumentException(Message.ERROR_PLAYER_NAME.getErrorMessage());
+ }
+ }
+} | Java | ํด๋น ํด๋์ค๋ฅผ exception ํจํค์ง์ ๋ฃ์ผ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค!
hp + mp๋ 200์ด์ด์ผํ๋ค ๋ฑ... ๋น์ฆ๋์ค ๋ก์ง์ด ๋ด๊ธด ๋ถ๋ถ์ด ์๋๋ฐ domain ํจํค์ง์ ๋ถ๋ฆฌ๋์ด ์์ด์์! |
@@ -0,0 +1,61 @@
+package bossmonster.domain;
+
+import bossmonster.exception.Validator;
+
+public class Player {
+
+ private String name;
+ private PlayerStatus status;
+
+ public Player(String name, int maxHP, int maxMP) {
+ this.name = Validator.validatePlayerName(name);
+ Validator.validatePlayerStatus(maxHP, maxMP);
+ this.status = new PlayerStatus(maxHP, maxMP);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public boolean isAlive() {
+ return status.isAlive();
+ }
+
+ public int getMaxHP() {
+ return status.getMaxHP();
+ }
+
+ public int getCurrentHP() {
+ return status.getCurrentHP();
+ }
+
+ public int getMaxMP() {
+ return status.getMaxMP();
+ }
+
+ public int getCurrentMP() {
+ return status.getCurrentMP();
+ }
+
+ public int attack(AttackType attackType) {
+ if (attackType.getName().equals(AttackType.ATTACK_TYPE_NORMAL.getName())) {
+ recoverMP(attackType.getMpRecover());
+ }
+ if (attackType.getName().equals(AttackType.ATTACK_TYPE_MAGIC.getName())) {
+ consumeMP(attackType.getMpUsage());
+ }
+ return attackType.getDamage();
+ }
+
+ public void takeDamage(int damage) {
+ status.setCurrentHP(status.getCurrentHP() - damage);
+ }
+
+ private void recoverMP(int mp) {
+ status.setCurrentMP(status.getCurrentMP() + mp);
+ }
+
+ private void consumeMP(int mp) {
+ status.setCurrentMP(status.getCurrentMP() - mp);
+ }
+} | Java | controller์์ Validator๋ฅผ ์ฌ์ฉํ ๋๋ ์๊ณ , domain์์ ์ฌ์ฉํ ๋๋ ์๊ตฐ์.
์ด๋ค ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌํ์
จ๋์ง ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,70 @@
+package bossmonster.domain;
+
+import bossmonster.domain.dto.GameHistoryDto;
+import java.util.ArrayList;
+import java.util.List;
+
+public class GameHistory {
+
+ private class Turns {
+
+ private int turnCount;
+ private String playerName;
+ private String playerAttackType;
+ private int playerAttackDamage;
+ private int monsterAttackDamage;
+ private int playerMaxHP;
+ private int playerCurrentHP;
+ private int playerMaxMP;
+ private int playerCurrentMP;
+ private int monsterMaxHP;
+ private int monsterCurrentHP;
+ private boolean gameStatus;
+
+ public Turns(int turnCount, String playerAttackType, int playerAttackDamage,
+ int monsterAttackDamage, boolean gameStatus, Player player, BossMonster bossMonster) {
+ this.turnCount = turnCount;
+ this.playerName = player.getName();
+ this.playerAttackType = playerAttackType;
+ this.playerAttackDamage = playerAttackDamage;
+ this.monsterAttackDamage = monsterAttackDamage;
+ this.playerMaxHP = player.getMaxHP();
+ this.playerCurrentHP = player.getCurrentHP();
+ this.playerMaxMP = player.getMaxMP();
+ this.playerCurrentMP = player.getCurrentMP();
+ this.monsterMaxHP = bossMonster.getMaxHP();
+ this.monsterCurrentHP = bossMonster.getCurrentHP();
+ this.gameStatus = gameStatus;
+ }
+ }
+
+ private static final String DEFAULT_ATTACK_TYPE = "";
+ private static final int DEFAULT_DAMAGE = 0;
+ private int recentRecord;
+ private List<Turns> raidHistory = new ArrayList<>();
+
+ public void addHistory(int turnCount, boolean gameStatus, Player player, BossMonster bossMonster) {
+ raidHistory.add(
+ new Turns(turnCount, DEFAULT_ATTACK_TYPE, DEFAULT_DAMAGE, DEFAULT_DAMAGE, gameStatus, player,
+ bossMonster));
+ }
+
+ public void addHistory(int turnCount, String playerAttackType, int playerAttackDamage,
+ int monsterAttackDamage, boolean gameStatus, Player player, BossMonster bossMonster) {
+ raidHistory.add(
+ new Turns(turnCount, playerAttackType, playerAttackDamage, monsterAttackDamage, gameStatus, player,
+ bossMonster));
+ }
+
+ public GameHistoryDto requestRaidHistory() {
+ setRecentRecord();
+ Turns turns = raidHistory.get(recentRecord);
+ return new GameHistoryDto(turns.turnCount, turns.playerName, turns.playerAttackType, turns.playerAttackDamage,
+ turns.monsterAttackDamage, turns.playerMaxHP, turns.playerCurrentHP, turns.playerMaxMP,
+ turns.playerCurrentMP, turns.monsterMaxHP, turns.monsterCurrentHP, turns.gameStatus);
+ }
+
+ private void setRecentRecord() {
+ recentRecord = raidHistory.size() - 1;
+ }
+} | Java | ํด๋น ๊ฐ์ฒด๋ฅผ ๋ง๋์ ๋ชฉ์ ์ด ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,82 @@
+package bossmonster.domain.dto;
+
+public class GameHistoryDto {
+
+ private int turnCount;
+ private String playerName;
+ private String playerAttackType;
+ private int playerAttackDamage;
+ private int monsterAttackDamage;
+ private int playerMaxHP;
+ private int playerCurrentHP;
+ private int playerMaxMP;
+ private int playerCurrentMP;
+ private int monsterMaxHP;
+ private int monsterCurrentHP;
+ private boolean gameStatus;
+
+ public GameHistoryDto(int turnCount, String playerName, String playerAttackType, int playerAttackDamage,
+ int monsterAttackDamage, int playerMaxHP, int playerCurrentHP, int playerMaxMP,
+ int playerCurrentMP, int monsterMaxHP, int monsterCurrentHP, boolean gameStatus) {
+ this.turnCount = turnCount;
+ this.playerName = playerName;
+ this.playerAttackType = playerAttackType;
+ this.playerAttackDamage = playerAttackDamage;
+ this.monsterAttackDamage = monsterAttackDamage;
+ this.playerMaxHP = playerMaxHP;
+ this.playerCurrentHP = playerCurrentHP;
+ this.playerMaxMP = playerMaxMP;
+ this.playerCurrentMP = playerCurrentMP;
+ this.monsterMaxHP = monsterMaxHP;
+ this.monsterCurrentHP = monsterCurrentHP;
+ this.gameStatus = gameStatus;
+ }
+
+ public int getTurnCount() {
+ return turnCount;
+ }
+
+ public String getPlayerName() {
+ return playerName;
+ }
+
+ public String getPlayerAttackType() {
+ return playerAttackType;
+ }
+
+ public int getPlayerAttackDamage() {
+ return playerAttackDamage;
+ }
+
+ public int getMonsterAttackDamage() {
+ return monsterAttackDamage;
+ }
+
+ public int getPlayerMaxHP() {
+ return playerMaxHP;
+ }
+
+ public int getPlayerCurrentHP() {
+ return playerCurrentHP;
+ }
+
+ public int getPlayerMaxMP() {
+ return playerMaxMP;
+ }
+
+ public int getPlayerCurrentMP() {
+ return playerCurrentMP;
+ }
+
+ public int getMonsterMaxHP() {
+ return monsterMaxHP;
+ }
+
+ public int getMonsterCurrentHP() {
+ return monsterCurrentHP;
+ }
+
+ public boolean isGameStatus() {
+ return gameStatus;
+ }
+} | Java | `isFinish()`๋ `isPlaying()`์ ๊ฐ์ ๊ฒ์ ์ด๋จ๊น์? |
@@ -0,0 +1,119 @@
+package bossmonster.controller;
+
+import bossmonster.domain.AttackType;
+import bossmonster.domain.BossMonster;
+import bossmonster.domain.Player;
+import bossmonster.domain.RaidGame;
+import bossmonster.domain.dto.GameHistoryDto;
+import bossmonster.exception.ExceptionHandler;
+import bossmonster.exception.ManaShortageException;
+import bossmonster.exception.Validator;
+import bossmonster.view.InputView;
+import bossmonster.view.OutputView;
+import bossmonster.view.constants.Message;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.NoSuchElementException;
+
+public class Controller {
+
+ public void startGame() {
+ RaidGame raidGame = createRaid();
+ OutputView.printMessage(Message.OUTPUT_START_RAID);
+
+ while (isGamePossible(raidGame)) {
+ showGameStatus(raidGame);
+ raidGame.executeTurn(selectAttackType(raidGame));
+ showTurnResult(raidGame);
+ }
+ showGameOver(raidGame);
+ }
+
+ private RaidGame createRaid() {
+ OutputView.printMessage(Message.INPUT_MONSTER_HP);
+ BossMonster bossMonster = ExceptionHandler.retryInput(this::createBossMonster);
+
+ OutputView.printMessage(Message.INPUT_PLAYER_NAME);
+ String playerName = ExceptionHandler.retryInput(this::requestPlayerName);
+
+ OutputView.printMessage(Message.INPUT_PLAYER_HP_MP);
+ Player player = ExceptionHandler.retryInput(() -> createPlayer(playerName));
+
+ return new RaidGame(bossMonster, player);
+ }
+
+ private BossMonster createBossMonster() {
+ int bossMonsterHP = Integer.parseInt(Validator.validateInputOfNumber(InputView.readLine()));
+ return new BossMonster(bossMonsterHP);
+ }
+
+ private String requestPlayerName() {
+ return Validator.validatePlayerName(InputView.readLine());
+ }
+
+ private Player createPlayer(String playerName) {
+ String[] playerStatus = Validator.validateInputOfNumbers(InputView.readLine());
+ int playerHP = Integer.parseInt(playerStatus[0]);
+ int playerMP = Integer.parseInt(playerStatus[1]);
+ return new Player(playerName, playerHP, playerMP);
+ }
+
+ private boolean isGamePossible(RaidGame raidGame) {
+ return raidGame.getGameHistory().isGameStatus();
+ }
+
+ private void showGameStatus(RaidGame raidGame) {
+ OutputView.printGameStatus(raidGame.getGameHistory());
+ }
+
+ private AttackType selectAttackType(RaidGame raidGame) {
+ OutputView.printAttackType(provideAttackType());
+ return ExceptionHandler.retryInput(() -> requestAttackType(raidGame));
+ }
+
+ private LinkedHashMap<Integer, String> provideAttackType() {
+ LinkedHashMap<Integer, String> attackType = new LinkedHashMap<>();
+ for (AttackType type : AttackType.values()) {
+ if (type.getNumber() != 0) {
+ attackType.put(type.getNumber(), type.getName());
+ }
+ }
+ return attackType;
+ }
+
+ private AttackType requestAttackType(RaidGame raidGame) {
+ int valueInput = Integer.parseInt(Validator.validateInputOfNumber(InputView.readLine()));
+ AttackType attackType = Arrays.stream(AttackType.values())
+ .filter(type -> type.getNumber() == valueInput && type.getNumber() != 0)
+ .findFirst()
+ .orElseThrow(() -> new NoSuchElementException(Message.ERROR_PLAYER_ATTACK_TYPE.getErrorMessage()));
+ checkPlayerMP(raidGame, attackType);
+ return attackType;
+ }
+
+ private void checkPlayerMP(RaidGame raidGame, AttackType attackType) {
+ GameHistoryDto gameHistoryDto = raidGame.getGameHistory();
+ if (gameHistoryDto.getPlayerCurrentMP() < attackType.getMpUsage()) {
+ throw new ManaShortageException(Message.ERROR_PLAYER_MANA_SHORTAGE.getErrorMessage());
+ }
+ }
+
+ private void showTurnResult(RaidGame raidGame) {
+ GameHistoryDto gameHistoryDto = raidGame.getGameHistory();
+ OutputView.printPlayerTurnResult(gameHistoryDto);
+ if (gameHistoryDto.getMonsterCurrentHP() != 0) {
+ OutputView.printBossMonsterTurnResult(gameHistoryDto);
+ }
+ }
+
+ private void showGameOver(RaidGame raidGame) {
+ GameHistoryDto gameHistoryDto = raidGame.getGameHistory();
+ if (gameHistoryDto.getMonsterCurrentHP() == 0) {
+ OutputView.printPlayerWin(gameHistoryDto);
+ }
+ if (gameHistoryDto.getPlayerCurrentHP() == 0) {
+ OutputView.printGameStatus(gameHistoryDto);
+ OutputView.printPlayerLose(gameHistoryDto);
+ }
+ }
+} | Java | DTO๋ก ๋ฐ์๋ค๋ฉด ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ํ๋ ์ด์ด ์ ๋ณด๋ฅผ ๋ฐ์์ ์ ์์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,73 @@
+package bossmonster.domain;
+
+import bossmonster.domain.dto.GameHistoryDto;
+
+public class RaidGame {
+
+ private static final int DEFAULT_VALUE = 0;
+
+ private BossMonster bossMonster;
+ private Player player;
+ private GameHistory gameHistory;
+ private boolean status;
+ private int turns;
+
+ public RaidGame(BossMonster bossMonster, Player player) {
+ this.bossMonster = bossMonster;
+ this.player = player;
+ this.gameHistory = new GameHistory();
+ this.status = true;
+ this.turns = DEFAULT_VALUE;
+
+ gameHistory.addHistory(turns, status, player, bossMonster);
+ }
+
+ public void executeTurn(AttackType attackType) {
+ if (isGameProgress()) {
+ increaseTurn();
+ int playerAttackDamage = DEFAULT_VALUE;
+ int monsterAttackDamage = DEFAULT_VALUE;
+ playerAttackDamage = executePlayerTurn(attackType);
+ if (bossMonster.isAlive()) {
+ monsterAttackDamage = executeBossMonsterTurn();
+ }
+ setStatus();
+ createTurnHistory(attackType, playerAttackDamage, monsterAttackDamage);
+ }
+ }
+
+ public GameHistoryDto getGameHistory() {
+ return gameHistory.requestRaidHistory();
+ }
+
+ private boolean isGameProgress() {
+ setStatus();
+ return status;
+ }
+
+ private void setStatus() {
+ status = player.isAlive() && bossMonster.isAlive();
+ }
+
+ private void increaseTurn() {
+ turns ++;
+ }
+
+ private int executePlayerTurn(AttackType attackType) {
+ int playerAttackDamage = player.attack(attackType);
+ bossMonster.takeDamage(playerAttackDamage);
+ return playerAttackDamage;
+ }
+
+ private int executeBossMonsterTurn() {
+ int monsterAttackDamage = bossMonster.attack();
+ player.takeDamage(monsterAttackDamage);
+ return monsterAttackDamage;
+ }
+
+ private void createTurnHistory(AttackType playerAttackType, int playerAttackDamage, int monsterAttackDamage) {
+ gameHistory.addHistory(turns, playerAttackType.getName(), playerAttackDamage, monsterAttackDamage, status,
+ player,
+ bossMonster);
+ }
+} | Java | ์ปจํธ๋กค๋ฌ์ ๋๋ฉ์ธ์ ์ ๋ง ์ ๋ถ๋ฆฌํ์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,61 @@
+package bossmonster.domain;
+
+import bossmonster.exception.Validator;
+
+public class Player {
+
+ private String name;
+ private PlayerStatus status;
+
+ public Player(String name, int maxHP, int maxMP) {
+ this.name = Validator.validatePlayerName(name);
+ Validator.validatePlayerStatus(maxHP, maxMP);
+ this.status = new PlayerStatus(maxHP, maxMP);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public boolean isAlive() {
+ return status.isAlive();
+ }
+
+ public int getMaxHP() {
+ return status.getMaxHP();
+ }
+
+ public int getCurrentHP() {
+ return status.getCurrentHP();
+ }
+
+ public int getMaxMP() {
+ return status.getMaxMP();
+ }
+
+ public int getCurrentMP() {
+ return status.getCurrentMP();
+ }
+
+ public int attack(AttackType attackType) {
+ if (attackType.getName().equals(AttackType.ATTACK_TYPE_NORMAL.getName())) {
+ recoverMP(attackType.getMpRecover());
+ }
+ if (attackType.getName().equals(AttackType.ATTACK_TYPE_MAGIC.getName())) {
+ consumeMP(attackType.getMpUsage());
+ }
+ return attackType.getDamage();
+ }
+
+ public void takeDamage(int damage) {
+ status.setCurrentHP(status.getCurrentHP() - damage);
+ }
+
+ private void recoverMP(int mp) {
+ status.setCurrentMP(status.getCurrentMP() + mp);
+ }
+
+ private void consumeMP(int mp) {
+ status.setCurrentMP(status.getCurrentMP() - mp);
+ }
+} | Java | ์ด ๋ถ๋ถ ์ด๋ ค์ฐ์
จ์ํ
๋ฐ ์ญํ ๋ถ๋ฐฐ ์ํ์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,63 @@
+package bossmonster.view.constants;
+
+public enum Message {
+
+ /* Input Request */
+ INPUT_MONSTER_HP("๋ณด์ค ๋ชฌ์คํฐ์ HP๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."),
+ INPUT_PLAYER_NAME("\n" + "ํ๋ ์ด์ด์ ์ด๋ฆ์ ์
๋ ฅํด์ฃผ์ธ์"),
+ INPUT_PLAYER_HP_MP("\n" + "ํ๋ ์ด์ด์ HP์ MP๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.(,๋ก ๊ตฌ๋ถ)"),
+ INPUT_ATTACK_TYPE_SELECT("\n" + "์ด๋ค ๊ณต๊ฒฉ์ ํ์๊ฒ ์ต๋๊น?"),
+ INPUT_ATTACK_TYPE("%d. %s"),
+
+ /* Output Message */
+ OUTPUT_START_RAID("\n" + "๋ณด์ค ๋ ์ด๋๋ฅผ ์์ํฉ๋๋ค!"),
+ OUTPUT_LINE_ONE("============================"),
+ OUTPUT_BOSS_STATUS("BOSS HP [%d/%d]"),
+ OUTPUT_LINE_TWO("____________________________"),
+ OUTPUT_BOSS_DEFAULT(" ^-^" + "\n" +
+ " / 0 0 \\" + "\n" +
+ "( \" )" + "\n" +
+ " \\ - /" + "\n" +
+ " - ^ -"),
+ OUTPUT_BOSS_DAMAGED(" ^-^" + "\n" +
+ " / x x \\" + "\n" +
+ "( \"\\ )" + "\n" +
+ " \\ ^ /" + "\n" +
+ " - ^ -"),
+ OUTPUT_BOSS_WIN(" ^-^" + "\n" +
+ " / ^ ^ \\" + "\n" +
+ "( \" )" + "\n" +
+ " \\ 3 /" + "\n" +
+ " - ^ -"),
+ OUTPUT_PLAYER_STATUS("\n" + "%s HP [%d/%d] MP [%d/%d]"),
+ OUTPUT_TURN_RESULT_PLAYER("\n" + "%s์ ํ์ต๋๋ค. (์
ํ ๋ฐ๋ฏธ์ง: %d)"),
+ OUTPUT_TURN_RESULT_BOSS("๋ณด์ค๊ฐ ๊ณต๊ฒฉ ํ์ต๋๋ค. (์
ํ ๋ฐ๋ฏธ์ง: %d)"),
+ OUTPUT_GAME_OVER_PLAYER_WIN("\n" + "%s ๋์ด %d๋ฒ์ ์ ํฌ ๋์ ๋ณด์ค ๋ชฌ์คํฐ๋ฅผ ์ก์์ต๋๋ค."),
+ OUTPUT_GAME_OVER_PLAYER_LOSE("\n" + "%s์ HP๊ฐ %d์ด ๋์์ต๋๋ค." + "\n" + "๋ณด์ค ๋ ์ด๋์ ์คํจํ์ต๋๋ค."),
+
+ /* Error Message */
+ ERROR_EMPTY("์
๋ ฅ๊ฐ์ด ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_NOT_INTEGER("์ซ์๋ง ์
๋ ฅ ๊ฐ๋ฅํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_NOT_COMMA("์ฝค๋ง(',')๋ฅผ ํตํด ๊ตฌ๋ถํด์ ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_ARRAY_SIZE("์
๋ ฅ๋ ํญ๋ชฉ์ด ๋ง์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_BOSS_MONSTER_HP("๋ณด์ค๋ชฌ์คํฐ์ HP๋ 100 ์ด์ 300 ์ดํ๋ง ์ค์ ๊ฐ๋ฅํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_PLAYER_NAME("ํ๋ ์ด์ด์ ์ด๋ฆ์ 5์ ์ดํ๋ง ๊ฐ๋ฅํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_PLAYER_STATUS("ํ๋ ์ด์ด์ HP์ MP์ ํฉ์ 200์ผ๋ก๋ง ์ค์ ๊ฐ๋ฅํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_PLAYER_ATTACK_TYPE("์กด์ฌํ์ง ์๋ ๊ณต๊ฒฉ ๋ฐฉ์์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_PLAYER_MANA_SHORTAGE("๋ง๋๊ฐ ๋ถ์กฑํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+
+ public static final String ERROR_PREFIX = "[ERROR] ";
+ private final String message;
+
+ Message(String message) {
+ this.message = message;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public String getErrorMessage() {
+ return ERROR_PREFIX + message;
+ }
+} | Java | ์ ๋ ์์๋ฅผ ํ ํ์ผ๋ก ๊ด๋ฆฌํ์๋๋ฐ์.
์์๋ฅผ ํ ํ์ผ๋ก ๊ด๋ฆฌํ๊ฒ ๋๋ฉด ํ ํ๋ก์ ํธ๋ฅผ ์งํํ ๋ ์์ ํ์ผ์์ ์ถฉ๋์ด ๋ง์ด ์ผ์ด๋๋๋ผ๊ตฌ์.
์ด๋ฐ ๋ฌธ์ ๋ฅผ ์ด๋ป๊ฒ ํด๊ฒฐํ๋ฉด ์ข์๊น์? |
@@ -0,0 +1,63 @@
+package bossmonster.view.constants;
+
+public enum Message {
+
+ /* Input Request */
+ INPUT_MONSTER_HP("๋ณด์ค ๋ชฌ์คํฐ์ HP๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."),
+ INPUT_PLAYER_NAME("\n" + "ํ๋ ์ด์ด์ ์ด๋ฆ์ ์
๋ ฅํด์ฃผ์ธ์"),
+ INPUT_PLAYER_HP_MP("\n" + "ํ๋ ์ด์ด์ HP์ MP๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.(,๋ก ๊ตฌ๋ถ)"),
+ INPUT_ATTACK_TYPE_SELECT("\n" + "์ด๋ค ๊ณต๊ฒฉ์ ํ์๊ฒ ์ต๋๊น?"),
+ INPUT_ATTACK_TYPE("%d. %s"),
+
+ /* Output Message */
+ OUTPUT_START_RAID("\n" + "๋ณด์ค ๋ ์ด๋๋ฅผ ์์ํฉ๋๋ค!"),
+ OUTPUT_LINE_ONE("============================"),
+ OUTPUT_BOSS_STATUS("BOSS HP [%d/%d]"),
+ OUTPUT_LINE_TWO("____________________________"),
+ OUTPUT_BOSS_DEFAULT(" ^-^" + "\n" +
+ " / 0 0 \\" + "\n" +
+ "( \" )" + "\n" +
+ " \\ - /" + "\n" +
+ " - ^ -"),
+ OUTPUT_BOSS_DAMAGED(" ^-^" + "\n" +
+ " / x x \\" + "\n" +
+ "( \"\\ )" + "\n" +
+ " \\ ^ /" + "\n" +
+ " - ^ -"),
+ OUTPUT_BOSS_WIN(" ^-^" + "\n" +
+ " / ^ ^ \\" + "\n" +
+ "( \" )" + "\n" +
+ " \\ 3 /" + "\n" +
+ " - ^ -"),
+ OUTPUT_PLAYER_STATUS("\n" + "%s HP [%d/%d] MP [%d/%d]"),
+ OUTPUT_TURN_RESULT_PLAYER("\n" + "%s์ ํ์ต๋๋ค. (์
ํ ๋ฐ๋ฏธ์ง: %d)"),
+ OUTPUT_TURN_RESULT_BOSS("๋ณด์ค๊ฐ ๊ณต๊ฒฉ ํ์ต๋๋ค. (์
ํ ๋ฐ๋ฏธ์ง: %d)"),
+ OUTPUT_GAME_OVER_PLAYER_WIN("\n" + "%s ๋์ด %d๋ฒ์ ์ ํฌ ๋์ ๋ณด์ค ๋ชฌ์คํฐ๋ฅผ ์ก์์ต๋๋ค."),
+ OUTPUT_GAME_OVER_PLAYER_LOSE("\n" + "%s์ HP๊ฐ %d์ด ๋์์ต๋๋ค." + "\n" + "๋ณด์ค ๋ ์ด๋์ ์คํจํ์ต๋๋ค."),
+
+ /* Error Message */
+ ERROR_EMPTY("์
๋ ฅ๊ฐ์ด ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_NOT_INTEGER("์ซ์๋ง ์
๋ ฅ ๊ฐ๋ฅํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_NOT_COMMA("์ฝค๋ง(',')๋ฅผ ํตํด ๊ตฌ๋ถํด์ ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_ARRAY_SIZE("์
๋ ฅ๋ ํญ๋ชฉ์ด ๋ง์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_BOSS_MONSTER_HP("๋ณด์ค๋ชฌ์คํฐ์ HP๋ 100 ์ด์ 300 ์ดํ๋ง ์ค์ ๊ฐ๋ฅํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_PLAYER_NAME("ํ๋ ์ด์ด์ ์ด๋ฆ์ 5์ ์ดํ๋ง ๊ฐ๋ฅํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_PLAYER_STATUS("ํ๋ ์ด์ด์ HP์ MP์ ํฉ์ 200์ผ๋ก๋ง ์ค์ ๊ฐ๋ฅํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_PLAYER_ATTACK_TYPE("์กด์ฌํ์ง ์๋ ๊ณต๊ฒฉ ๋ฐฉ์์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_PLAYER_MANA_SHORTAGE("๋ง๋๊ฐ ๋ถ์กฑํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+
+ public static final String ERROR_PREFIX = "[ERROR] ";
+ private final String message;
+
+ Message(String message) {
+ this.message = message;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public String getErrorMessage() {
+ return ERROR_PREFIX + message;
+ }
+} | Java | ์
์ถ๋ ฅ์ ์ฌ์ฉ๋๋ ๋ฉ์์ง๋ค์ InputView์ OutputView์์ ๊ฐ๊ฐ ์ฌ์ฉํ๋ค ๋ณด๋ ๋ฉ์์ง๊ฐ ๋ง์ ๊ฒฝ์ฐ์๋ ์๋ฆฌ๋ฅผ ๋ง์ด ์ฐจ์งํ๋ ๊ฒ ๊ฐ์์ ํ ํ์ผ๋ก ๊ด๋ฆฌํ๊ณ ๊ฐ์ ธ๋ค ์ฐ๋ ํํ๋ก ํด์ผ๊ฒ ๋ค๊ณ ์๊ฐํ์ต๋๋ค. ์ ๊ฐ ๊ณต๋ถํ์ง ์ผ๋ง ๋์ง ์์ ํ ํ๋ก์ ํธ๋ฅผ ๊ฒฝํํด๋ณธ ์ ์ด ์์ง ์์ด ์ด๋ค ๋ถ๋ถ์์ ์ถฉ๋์ด ์ผ์ด๋๋์ง๋ ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. ๋ถ๋ช
์์๋ฅผ ํ ํ์ผ๋ก ๊ด๋ฆฌํ๋ค๋ณด๋ฉด ๋ฌธ์ ๊ฐ ์๊ธธ ์๋ ์์ ๊ฒ ๊ฐ์๋ฐ์. ์ด ๋ถ๋ถ์ ์ฌ๋ฌ ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉด์ ์กฐ๊ธ ๋ ๊ณ ๋ฏผํด ๋ด์ผ ํ ๋ถ๋ถ์ธ ๊ฒ ๊ฐ์ต๋๋ค. ์๊ฒฌ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,78 @@
+package bossmonster.exception;
+
+import bossmonster.view.constants.Message;
+
+public class Validator {
+
+ public static String validateInputOfNumber(String inputValue) {
+ validateEmpty(inputValue);
+ validateInteger(inputValue);
+ return inputValue;
+ }
+
+ public static String validatePlayerName(String inputValue) {
+ validateEmpty(inputValue);
+ validateRangeOfPlayerName(inputValue);
+ return inputValue;
+ }
+
+ public static String[] validateInputOfNumbers(String inputValue) {
+ validateSeparated(inputValue);
+ String[] separatedValue = inputValue.split(",");
+ validateArray(separatedValue);
+ for (String value : separatedValue) {
+ validateEmpty(value);
+ validateInteger(value);
+ }
+ return separatedValue;
+ }
+
+ public static int validateBossMonster(int hp) {
+ return validateRangeOfMonster(hp);
+ }
+
+ public static void validatePlayerStatus(int hp, int mp) {
+ if (hp + mp != 200) {
+ throw new IllegalArgumentException(Message.ERROR_PLAYER_STATUS.getErrorMessage());
+ }
+ }
+
+ private static void validateEmpty(String inputValue) {
+ if (inputValue.isEmpty()) {
+ throw new IllegalArgumentException(Message.ERROR_EMPTY.getErrorMessage());
+ }
+ }
+
+ private static void validateInteger(String inputValue) {
+ try {
+ Integer.parseInt(inputValue);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(Message.ERROR_NOT_INTEGER.getErrorMessage());
+ }
+ }
+
+ private static void validateSeparated(String inputValue) {
+ if (!inputValue.contains(",")) {
+ throw new IllegalArgumentException(Message.ERROR_NOT_COMMA.getErrorMessage());
+ }
+ }
+
+ private static void validateArray(String[] inputValue) {
+ if (inputValue.length != 2) {
+ throw new IllegalArgumentException(Message.ERROR_ARRAY_SIZE.getErrorMessage());
+ }
+ }
+
+ private static int validateRangeOfMonster(int hp) {
+ if (hp < 100 || hp > 300) {
+ throw new IllegalArgumentException(Message.ERROR_BOSS_MONSTER_HP.getErrorMessage());
+ }
+ return hp;
+ }
+
+ private static void validateRangeOfPlayerName(String name) {
+ if (name.length() > 5) {
+ throw new IllegalArgumentException(Message.ERROR_PLAYER_NAME.getErrorMessage());
+ }
+ }
+} | Java | ์ฒ์์๋ ์
๋ ฅ ๋จ๊ณ์์ ๊ฒ์ฆํ๋ ๋ถ๋ถ๋ง ๋ฐ๋ก ๋ฃ๊ณ , ๋๋ฉ์ธ ๋ถ๋ถ์์ ๊ฒ์ฆํ ๋ก์ง์ ๊ฐ ๋๋ฉ์ธ ํด๋์ค์์ ์งํํ๋ ๊ฒ์ผ๋ก ํ์๋๋ฐ์. ํ๋ค๋ณด๋ ๊ฒน์น๋ ๋ถ๋ถ์ด ์๋ ๊ฒ ๊ฐ์์ ์ ํ์ฉํ์ง ๋ชปํ์ต๋๋ค. ์์์ ์๊ฒฌ์ ์ฃผ์ ๊ฒ ์ฒ๋ผ DTO๋ฅผ ํ์ฉํ๋ฉด ๋ ๊น๋ํ๊ฒ ๊ฒ์ฆ ๋ก์ง์ ๋ถ๋ฐฐํ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,17 @@
+package bossmonster.exception;
+
+import bossmonster.view.OutputView;
+import java.util.function.Supplier;
+
+public class ExceptionHandler {
+
+ public static <T> T retryInput(Supplier<T> supplier) {
+ while (true) {
+ try {
+ return supplier.get();
+ } catch (Exception exception) {
+ OutputView.printError(exception);
+ }
+ }
+ }
+} | Java | ์ ๋ ์๋ฌ ๋ฐ์ ์, ์ฌ์
๋ ฅ์ ๋ฐ์์ผ ํ๋ ๋ถ๋ถ์ ๋ํด์ ๊ณ ๋ฏผ์ ๋ง์ด ํ๋ค๊ฐ.. ๋ค๋ฅธ ๋ถ๋ค์ ์ฝ๋ ๋ฆฌ๋ทฐ๋ฅผ ๋ณด๊ณ ์ฌ๋ฌ๊ฐ์ง ์ฐพ์๋ณด๋ค ์ด๋ฐ ๋ฐฉ๋ฒ๋ ์๋ค๋ ๊ฒ์ ์๊ฒ ๋์์ต๋๋ค. ํ๋ ๋ง๋ค์ด ๋๋ฉด ํ์ฉ์ฑ์ด ์ข์ ๊ฒ ๊ฐ์์. ๋ค์ด๋ฐ์ ๋ํด์๋ ๋ง์ ์ฃผ์ ๋ถ๋ถ์ฒ๋ผ ๊ฐ๋
์ฑ์ด ์ ์ข์ ์ง ์ ์๊ธฐ ๋๋ฌธ์ ๋ ๊ณ ๋ฏผ์ด ํ์ํ ๊ฒ ๊ฐ์ต๋๋ค. ๋ง์ ๋ค์ด๋ฐ์ ๋ํด์๋ ๋ง์ด ์ ๊ฒฝ์ ์ฐ์ง ๋ชปํ๋ ๊ฒ ๊ฐ๋ค์.
ํด๋์ค๋ช
์ ๊ดํด ์๊ฒฌ ์ฃผ์ ๋ถ๋ถ์ ๋ํด์๋ ๊ฐ์ฌํฉ๋๋ค. ์คํ๋ง์ ๋ํ ๊ฐ๋
์ด ์์ง ์์ด์ ์คํ๋ง์ ExceptionHandler๊ฐ ์ด๋ป๊ฒ ์ฌ์ฉ๋๋์ง ์ฒ์ ์์๋ค์.. ์ ๋ ๋จ์ํ ์์ธ๋ฅผ ๊ด๋ฆฌํ๋ค๋ ๋ถ๋ถ์์ ์ ๋ ๊ฒ ์ฌ์ฉํ๋๋ฐ ๋ค๋ฅธ ๋ถ๋ค์ด ์ฝ๊ธฐ์๋ ํท๊ฐ๋ฆด ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค. ์ด ๋ถ๋ถ๋ ์ฐธ๊ณ ํ๊ฒ ์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,82 @@
+package bossmonster.domain.dto;
+
+public class GameHistoryDto {
+
+ private int turnCount;
+ private String playerName;
+ private String playerAttackType;
+ private int playerAttackDamage;
+ private int monsterAttackDamage;
+ private int playerMaxHP;
+ private int playerCurrentHP;
+ private int playerMaxMP;
+ private int playerCurrentMP;
+ private int monsterMaxHP;
+ private int monsterCurrentHP;
+ private boolean gameStatus;
+
+ public GameHistoryDto(int turnCount, String playerName, String playerAttackType, int playerAttackDamage,
+ int monsterAttackDamage, int playerMaxHP, int playerCurrentHP, int playerMaxMP,
+ int playerCurrentMP, int monsterMaxHP, int monsterCurrentHP, boolean gameStatus) {
+ this.turnCount = turnCount;
+ this.playerName = playerName;
+ this.playerAttackType = playerAttackType;
+ this.playerAttackDamage = playerAttackDamage;
+ this.monsterAttackDamage = monsterAttackDamage;
+ this.playerMaxHP = playerMaxHP;
+ this.playerCurrentHP = playerCurrentHP;
+ this.playerMaxMP = playerMaxMP;
+ this.playerCurrentMP = playerCurrentMP;
+ this.monsterMaxHP = monsterMaxHP;
+ this.monsterCurrentHP = monsterCurrentHP;
+ this.gameStatus = gameStatus;
+ }
+
+ public int getTurnCount() {
+ return turnCount;
+ }
+
+ public String getPlayerName() {
+ return playerName;
+ }
+
+ public String getPlayerAttackType() {
+ return playerAttackType;
+ }
+
+ public int getPlayerAttackDamage() {
+ return playerAttackDamage;
+ }
+
+ public int getMonsterAttackDamage() {
+ return monsterAttackDamage;
+ }
+
+ public int getPlayerMaxHP() {
+ return playerMaxHP;
+ }
+
+ public int getPlayerCurrentHP() {
+ return playerCurrentHP;
+ }
+
+ public int getPlayerMaxMP() {
+ return playerMaxMP;
+ }
+
+ public int getPlayerCurrentMP() {
+ return playerCurrentMP;
+ }
+
+ public int getMonsterMaxHP() {
+ return monsterMaxHP;
+ }
+
+ public int getMonsterCurrentHP() {
+ return monsterCurrentHP;
+ }
+
+ public boolean isGameStatus() {
+ return gameStatus;
+ }
+} | Java | ๋จ์ํ ํ์ฌ ๊ฒ์ ์ํ๋ฅผ ๋ฌป๊ธฐ ์ํด ์ผ๋ ๊ฒ ๊ฐ์๋ฐ ์๊ฒฌ ์ฃผ์ ๋ฉ์๋๋ช
์ด ๋ฐํ๋๋ ๊ฐ์ ์ดํดํ๊ธฐ์ ๋ ์ง๊ด์ ์ด๋ค์! |
@@ -0,0 +1,73 @@
+package bossmonster.domain;
+
+import bossmonster.domain.dto.GameHistoryDto;
+
+public class RaidGame {
+
+ private static final int DEFAULT_VALUE = 0;
+
+ private BossMonster bossMonster;
+ private Player player;
+ private GameHistory gameHistory;
+ private boolean status;
+ private int turns;
+
+ public RaidGame(BossMonster bossMonster, Player player) {
+ this.bossMonster = bossMonster;
+ this.player = player;
+ this.gameHistory = new GameHistory();
+ this.status = true;
+ this.turns = DEFAULT_VALUE;
+
+ gameHistory.addHistory(turns, status, player, bossMonster);
+ }
+
+ public void executeTurn(AttackType attackType) {
+ if (isGameProgress()) {
+ increaseTurn();
+ int playerAttackDamage = DEFAULT_VALUE;
+ int monsterAttackDamage = DEFAULT_VALUE;
+ playerAttackDamage = executePlayerTurn(attackType);
+ if (bossMonster.isAlive()) {
+ monsterAttackDamage = executeBossMonsterTurn();
+ }
+ setStatus();
+ createTurnHistory(attackType, playerAttackDamage, monsterAttackDamage);
+ }
+ }
+
+ public GameHistoryDto getGameHistory() {
+ return gameHistory.requestRaidHistory();
+ }
+
+ private boolean isGameProgress() {
+ setStatus();
+ return status;
+ }
+
+ private void setStatus() {
+ status = player.isAlive() && bossMonster.isAlive();
+ }
+
+ private void increaseTurn() {
+ turns ++;
+ }
+
+ private int executePlayerTurn(AttackType attackType) {
+ int playerAttackDamage = player.attack(attackType);
+ bossMonster.takeDamage(playerAttackDamage);
+ return playerAttackDamage;
+ }
+
+ private int executeBossMonsterTurn() {
+ int monsterAttackDamage = bossMonster.attack();
+ player.takeDamage(monsterAttackDamage);
+ return monsterAttackDamage;
+ }
+
+ private void createTurnHistory(AttackType playerAttackType, int playerAttackDamage, int monsterAttackDamage) {
+ gameHistory.addHistory(turns, playerAttackType.getName(), playerAttackDamage, monsterAttackDamage, status,
+ player,
+ bossMonster);
+ }
+} | Java | ๊ฐ์ฌํฉ๋๋ค. ๊ฐ์ฒด์งํฅ์ ๊ณต๋ถํ๋ฉด์ MVC ํจํด์ ๊ตฌ์ ๋ฐ์ง ์๊ณ ๋๋ฉ์ธ ๋ก์ง์ ๋๋ฉ์ธ ๋ก์ง์์๋ง์ผ๋ก๋ ๋์๊ฐ ์ ์๋๋ก ๊ตฌํํ๊ณ ์ ์ฐ์ตํ๊ณ ์์ต๋๋ค. ์ปจํธ๋กค๋ฌ๊ฐ ์์ด๋ ๋๋ฉ์ธ ๋ด์์๋ ๋๋ฉ์ธ ๋ก์ง์ด ์ ๋์๊ฐ์ผ ํ๋ค๊ณ ์๊ฐํ๋ฉด์ ๊ณ ๋ฏผํ๋ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,61 @@
+package bossmonster.domain;
+
+import bossmonster.exception.Validator;
+
+public class Player {
+
+ private String name;
+ private PlayerStatus status;
+
+ public Player(String name, int maxHP, int maxMP) {
+ this.name = Validator.validatePlayerName(name);
+ Validator.validatePlayerStatus(maxHP, maxMP);
+ this.status = new PlayerStatus(maxHP, maxMP);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public boolean isAlive() {
+ return status.isAlive();
+ }
+
+ public int getMaxHP() {
+ return status.getMaxHP();
+ }
+
+ public int getCurrentHP() {
+ return status.getCurrentHP();
+ }
+
+ public int getMaxMP() {
+ return status.getMaxMP();
+ }
+
+ public int getCurrentMP() {
+ return status.getCurrentMP();
+ }
+
+ public int attack(AttackType attackType) {
+ if (attackType.getName().equals(AttackType.ATTACK_TYPE_NORMAL.getName())) {
+ recoverMP(attackType.getMpRecover());
+ }
+ if (attackType.getName().equals(AttackType.ATTACK_TYPE_MAGIC.getName())) {
+ consumeMP(attackType.getMpUsage());
+ }
+ return attackType.getDamage();
+ }
+
+ public void takeDamage(int damage) {
+ status.setCurrentHP(status.getCurrentHP() - damage);
+ }
+
+ private void recoverMP(int mp) {
+ status.setCurrentMP(status.getCurrentMP() + mp);
+ }
+
+ private void consumeMP(int mp) {
+ status.setCurrentMP(status.getCurrentMP() - mp);
+ }
+} | Java | ์ ๋์๋ค๋ ๋คํ์ด๋ค์ ์ข๊ฒ ๋ด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,61 @@
+package bossmonster.domain;
+
+import bossmonster.exception.Validator;
+
+public class Player {
+
+ private String name;
+ private PlayerStatus status;
+
+ public Player(String name, int maxHP, int maxMP) {
+ this.name = Validator.validatePlayerName(name);
+ Validator.validatePlayerStatus(maxHP, maxMP);
+ this.status = new PlayerStatus(maxHP, maxMP);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public boolean isAlive() {
+ return status.isAlive();
+ }
+
+ public int getMaxHP() {
+ return status.getMaxHP();
+ }
+
+ public int getCurrentHP() {
+ return status.getCurrentHP();
+ }
+
+ public int getMaxMP() {
+ return status.getMaxMP();
+ }
+
+ public int getCurrentMP() {
+ return status.getCurrentMP();
+ }
+
+ public int attack(AttackType attackType) {
+ if (attackType.getName().equals(AttackType.ATTACK_TYPE_NORMAL.getName())) {
+ recoverMP(attackType.getMpRecover());
+ }
+ if (attackType.getName().equals(AttackType.ATTACK_TYPE_MAGIC.getName())) {
+ consumeMP(attackType.getMpUsage());
+ }
+ return attackType.getDamage();
+ }
+
+ public void takeDamage(int damage) {
+ status.setCurrentHP(status.getCurrentHP() - damage);
+ }
+
+ private void recoverMP(int mp) {
+ status.setCurrentMP(status.getCurrentMP() + mp);
+ }
+
+ private void consumeMP(int mp) {
+ status.setCurrentMP(status.getCurrentMP() - mp);
+ }
+} | Java | ๋ฐ์ Validator์ ์ฌ์ฉ์ ๋ํด ์ฃผ์ ์๊ฒฌ๊ณผ ๋์ผํ ๋ฌธ์ ๋ค์.. ์
์ถ๋ ฅ์ ๊ด๋ จ๋ ๊ฒ์ฆ๊ณผ ๋๋ฉ์ธ ๋ถ๋ถ์ ๊ฒ์ฆ์ ๋ถ๋ฆฌํด์ ์ฌ์ฉํ๋๊ฒ ๋ง์ ๊ฒ ๊ฐ์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,70 @@
+package bossmonster.domain;
+
+import bossmonster.domain.dto.GameHistoryDto;
+import java.util.ArrayList;
+import java.util.List;
+
+public class GameHistory {
+
+ private class Turns {
+
+ private int turnCount;
+ private String playerName;
+ private String playerAttackType;
+ private int playerAttackDamage;
+ private int monsterAttackDamage;
+ private int playerMaxHP;
+ private int playerCurrentHP;
+ private int playerMaxMP;
+ private int playerCurrentMP;
+ private int monsterMaxHP;
+ private int monsterCurrentHP;
+ private boolean gameStatus;
+
+ public Turns(int turnCount, String playerAttackType, int playerAttackDamage,
+ int monsterAttackDamage, boolean gameStatus, Player player, BossMonster bossMonster) {
+ this.turnCount = turnCount;
+ this.playerName = player.getName();
+ this.playerAttackType = playerAttackType;
+ this.playerAttackDamage = playerAttackDamage;
+ this.monsterAttackDamage = monsterAttackDamage;
+ this.playerMaxHP = player.getMaxHP();
+ this.playerCurrentHP = player.getCurrentHP();
+ this.playerMaxMP = player.getMaxMP();
+ this.playerCurrentMP = player.getCurrentMP();
+ this.monsterMaxHP = bossMonster.getMaxHP();
+ this.monsterCurrentHP = bossMonster.getCurrentHP();
+ this.gameStatus = gameStatus;
+ }
+ }
+
+ private static final String DEFAULT_ATTACK_TYPE = "";
+ private static final int DEFAULT_DAMAGE = 0;
+ private int recentRecord;
+ private List<Turns> raidHistory = new ArrayList<>();
+
+ public void addHistory(int turnCount, boolean gameStatus, Player player, BossMonster bossMonster) {
+ raidHistory.add(
+ new Turns(turnCount, DEFAULT_ATTACK_TYPE, DEFAULT_DAMAGE, DEFAULT_DAMAGE, gameStatus, player,
+ bossMonster));
+ }
+
+ public void addHistory(int turnCount, String playerAttackType, int playerAttackDamage,
+ int monsterAttackDamage, boolean gameStatus, Player player, BossMonster bossMonster) {
+ raidHistory.add(
+ new Turns(turnCount, playerAttackType, playerAttackDamage, monsterAttackDamage, gameStatus, player,
+ bossMonster));
+ }
+
+ public GameHistoryDto requestRaidHistory() {
+ setRecentRecord();
+ Turns turns = raidHistory.get(recentRecord);
+ return new GameHistoryDto(turns.turnCount, turns.playerName, turns.playerAttackType, turns.playerAttackDamage,
+ turns.monsterAttackDamage, turns.playerMaxHP, turns.playerCurrentHP, turns.playerMaxMP,
+ turns.playerCurrentMP, turns.monsterMaxHP, turns.monsterCurrentHP, turns.gameStatus);
+ }
+
+ private void setRecentRecord() {
+ recentRecord = raidHistory.size() - 1;
+ }
+} | Java | ์ปจํธ๋กค๋ฌ์์ ๊ฒ์์ ์ ์ดํ๋ ๊ฒ ๋ณด๋ค RaidGame ํด๋์ค์์ ์ ์ฒด์ ์ธ ๊ฒ์ ๋ก์ง์ด ๋์๊ฐ๋ ํํ๋ก ์๊ฐํ๊ณ ๊ตฌํํ๋ค ๋ณด๋ ๊ฒ์์ด ์คํ๋๋ฉด์ ํ์คํ ๋ฆฌ๋ฅผ ๋จ๊ธฐ๊ณ ์ปจํธ๋กค๋ฌ์์ ์ถ๋ ฅ์ ์ํด์๋ ๊ธฐ๋ก์ ๋จ๊ฒจ์ง ๋ถ๋ถ์ ๊ฐ์ ธ๋ค๊ฐ ๋ณด์ฌ์ค์ผ ์์กด์ฑ์ด ๋จ์ด์ง ๊ฒ ๊ฐ๋ค๊ณ ์๊ฐํ์ต๋๋ค. ์ค์ ๋ก ํด ์ ๊ฒ์์ ํ๋ฉด ๋งค ํด๋ง๋ค ํด์ ๋ํ ๊ธฐ๋ก์ด ๊ฒ์์์๋ ๋จ๊ฒ ๋๋๋ฐ ์ด๊ฑธ ์๊ฐํด์ ๋ง๋ค์๋ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,119 @@
+package bossmonster.controller;
+
+import bossmonster.domain.AttackType;
+import bossmonster.domain.BossMonster;
+import bossmonster.domain.Player;
+import bossmonster.domain.RaidGame;
+import bossmonster.domain.dto.GameHistoryDto;
+import bossmonster.exception.ExceptionHandler;
+import bossmonster.exception.ManaShortageException;
+import bossmonster.exception.Validator;
+import bossmonster.view.InputView;
+import bossmonster.view.OutputView;
+import bossmonster.view.constants.Message;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.NoSuchElementException;
+
+public class Controller {
+
+ public void startGame() {
+ RaidGame raidGame = createRaid();
+ OutputView.printMessage(Message.OUTPUT_START_RAID);
+
+ while (isGamePossible(raidGame)) {
+ showGameStatus(raidGame);
+ raidGame.executeTurn(selectAttackType(raidGame));
+ showTurnResult(raidGame);
+ }
+ showGameOver(raidGame);
+ }
+
+ private RaidGame createRaid() {
+ OutputView.printMessage(Message.INPUT_MONSTER_HP);
+ BossMonster bossMonster = ExceptionHandler.retryInput(this::createBossMonster);
+
+ OutputView.printMessage(Message.INPUT_PLAYER_NAME);
+ String playerName = ExceptionHandler.retryInput(this::requestPlayerName);
+
+ OutputView.printMessage(Message.INPUT_PLAYER_HP_MP);
+ Player player = ExceptionHandler.retryInput(() -> createPlayer(playerName));
+
+ return new RaidGame(bossMonster, player);
+ }
+
+ private BossMonster createBossMonster() {
+ int bossMonsterHP = Integer.parseInt(Validator.validateInputOfNumber(InputView.readLine()));
+ return new BossMonster(bossMonsterHP);
+ }
+
+ private String requestPlayerName() {
+ return Validator.validatePlayerName(InputView.readLine());
+ }
+
+ private Player createPlayer(String playerName) {
+ String[] playerStatus = Validator.validateInputOfNumbers(InputView.readLine());
+ int playerHP = Integer.parseInt(playerStatus[0]);
+ int playerMP = Integer.parseInt(playerStatus[1]);
+ return new Player(playerName, playerHP, playerMP);
+ }
+
+ private boolean isGamePossible(RaidGame raidGame) {
+ return raidGame.getGameHistory().isGameStatus();
+ }
+
+ private void showGameStatus(RaidGame raidGame) {
+ OutputView.printGameStatus(raidGame.getGameHistory());
+ }
+
+ private AttackType selectAttackType(RaidGame raidGame) {
+ OutputView.printAttackType(provideAttackType());
+ return ExceptionHandler.retryInput(() -> requestAttackType(raidGame));
+ }
+
+ private LinkedHashMap<Integer, String> provideAttackType() {
+ LinkedHashMap<Integer, String> attackType = new LinkedHashMap<>();
+ for (AttackType type : AttackType.values()) {
+ if (type.getNumber() != 0) {
+ attackType.put(type.getNumber(), type.getName());
+ }
+ }
+ return attackType;
+ }
+
+ private AttackType requestAttackType(RaidGame raidGame) {
+ int valueInput = Integer.parseInt(Validator.validateInputOfNumber(InputView.readLine()));
+ AttackType attackType = Arrays.stream(AttackType.values())
+ .filter(type -> type.getNumber() == valueInput && type.getNumber() != 0)
+ .findFirst()
+ .orElseThrow(() -> new NoSuchElementException(Message.ERROR_PLAYER_ATTACK_TYPE.getErrorMessage()));
+ checkPlayerMP(raidGame, attackType);
+ return attackType;
+ }
+
+ private void checkPlayerMP(RaidGame raidGame, AttackType attackType) {
+ GameHistoryDto gameHistoryDto = raidGame.getGameHistory();
+ if (gameHistoryDto.getPlayerCurrentMP() < attackType.getMpUsage()) {
+ throw new ManaShortageException(Message.ERROR_PLAYER_MANA_SHORTAGE.getErrorMessage());
+ }
+ }
+
+ private void showTurnResult(RaidGame raidGame) {
+ GameHistoryDto gameHistoryDto = raidGame.getGameHistory();
+ OutputView.printPlayerTurnResult(gameHistoryDto);
+ if (gameHistoryDto.getMonsterCurrentHP() != 0) {
+ OutputView.printBossMonsterTurnResult(gameHistoryDto);
+ }
+ }
+
+ private void showGameOver(RaidGame raidGame) {
+ GameHistoryDto gameHistoryDto = raidGame.getGameHistory();
+ if (gameHistoryDto.getMonsterCurrentHP() == 0) {
+ OutputView.printPlayerWin(gameHistoryDto);
+ }
+ if (gameHistoryDto.getPlayerCurrentHP() == 0) {
+ OutputView.printGameStatus(gameHistoryDto);
+ OutputView.printPlayerLose(gameHistoryDto);
+ }
+ }
+} | Java | ์ ๊ฐ ์์ฑํ ์ฝ๋์ InputView๋ ๋จ์ํ ์
๋ ฅ์ ๋ฐ๋ ๋ฉ์๋๋ง ์กด์ฌ ํ๋๋ฐ์. ์ด๋ ๊ฒ ๊ตฌ์ฑํ๋ค ๋ณด๋ ์ปจํธ๋กค๋ฌ์์ ์
๋ ฅ๋ฐ์ ๋ถ๋ถ์ ๋ํด์ ๊ฒ์ฆ์ ํ๊ณ ๋ณํํด์ ๋๋ฉ์ธ์ ์์ฑํ๊ฑฐ๋ ๋ฐ์ดํฐ๋ฅผ ๋๊ฒจ์ค์ผ ํด์ ํ ์ผ์ด ๋ง์์ง๋ ๋จ์ ์ด ์๋ค์. ๋ง์์ฃผ์ ๊ฒ์ฒ๋ผ InputView ์ชฝ์์ ์
๋ ฅ์ ๋ฐ๊ณ ๋ฐ๋ก ์
๋ ฅ์ ๋ํ ๋ถ๋ถ์ ๊ฒ์ฆํด์ DTO๋ฅผ ์์ฑํ์ฌ ๊ฑด๋ค์ฃผ๋ ๋ฐฉ์์ ์ฐ์ตํด๋ด์ผ ํ ๊ฒ ๊ฐ์ต๋๋ค. ์ข์ ์๊ฒฌ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,100 @@
+# Domain ๊ตฌํ
+
+## ์งํ ํ๋ก์ธ์ค (์์คํ
์ฑ
์)
+
+1. ๊ฒ์ด๋จธ๋ ์๋ํ ๋ณด์ค ๋ชฌ์คํฐ์ HP๋ฅผ ์ค์ ํ๋ค.
+2. ๊ฒ์ด๋จธ๋ ํ๋ ์ดํ ํ๋ ์ด์ด์ ์ด๋ฆ๊ณผ HP, MP๋ฅผ ์ค์ ํ๋ค.
+3. ๊ฒ์์ด ์์๋๋ค.
+4. ๊ฒ์์ ํด์ ์ด๋ฉฐ, ๋งค ํด๋ง๋ค ๋ค์๊ณผ ๊ฐ์ ํ๋์ด ๋ฐ๋ณต๋๋ค.
+ 1) ๋ณด์ค ๋ชฌ์คํฐ์ ํ๋ ์ด์ด์ ํ์ฌ ์ํ๋ฅผ ๋ณด์ฌ์ค๋ค.
+ 2) ํ๋ ์ด์ด๊ฐ ๊ณต๊ฒฉํ ๋ฐฉ์์ ์ ํํ๋ค.
+ 3) ํ๋ ์ด์ด๊ฐ ์ ํ๋ ๋ฐฉ์์ผ๋ก ๊ณต๊ฒฉํ๋ค. (๋ณด์ค ๋ชฌ์คํฐ์๊ฒ ๋ฐ๋ฏธ์ง๋ฅผ ์
ํ)
+ 4) ๋ฐ๋ฏธ์ง๋ฅผ ์
์ ๋ณด์ค ๋ชฌ์คํฐ์ ์ํ๋ฅผ ํ์ธํ๋ค.
+ 5) ๋ณด์ค ๋ชฌ์คํฐ๊ฐ ์ฃฝ์๋ค๋ฉด ๊ฒ์์ด ์ข
๋ฃ๋๋ค.
+ 6) ๋ณด์ค ๋ชฌ์คํฐ๊ฐ ์ด์์๋ค๋ฉด ํ๋ ์ด์ด๋ฅผ ๊ณต๊ฒฉํ๋ค.
+ 7) ๋ฐ๋ฏธ์ง๋ฅผ ์
์ ํ๋ ์ด์ด์ ์ํ๋ฅผ ํ์ธํ๋ค.
+ 8) ํ๋ ์ด์ด๊ฐ ์ฃฝ์๋ค๋ฉด ๊ฒ์์ด ์ข
๋ฃ๋๋ค.
+5. ๊ฒ์ ์ข
๋ฃ ํ ์น์๋ฅผ ์๋ดํ๋ค.
+ - ํ๋ ์ด์ด๊ฐ ์ด๊ฒผ์ ๊ฒฝ์ฐ : ์งํํ ํด ์๋ฅผ ์๋ ค์ค๋ค.
+ - ๋ณด์ค ๋ชฌ์คํฐ๊ฐ ์ด๊ฒผ์ ๊ฒฝ์ฐ : ํ๋ ์ด์ด์ ๋ณด์ค ๋ชฌ์คํฐ์ ํ์ฌ ์ํ๋ฅผ ๋ณด์ฌ์ค๋ค.
+
+## Domain ๊ฐ์ฒด ์ ์ ๋ฐ ์ฑ
์๊ณผ ํ๋ ฅ ํ ๋น
+
+1. **Raid Game**
+ - ๊ฒ์ ์์ ์ ์ฃผ์ ์ธ๋ฌผ์ ๋ํด ์
ํ
ํ ์ฑ
์์ ๊ฐ์ง
+ - ๊ฒ์ ์ธ๋ฌผ์ธ **Boss Monster**๋ฅผ ์์ฑํ ์ฑ
์์ ๊ฐ์ง
+ - ๊ฒ์ ์ธ๋ฌผ์ธ **Player**๋ฅผ ์์ฑํ ์ฑ
์์ ๊ฐ์ง
+ - ๋งค ํด์ ์คํํ ์ฑ
์์ ๊ฐ์ง
+ - ๊ฒ์ ์ํ๋ฅผ ํ์ธํ ์ฑ
์์ ๊ฐ์ง (๊ฒ์ ์ํ๊ฐ '์ข
๋ฃ'์ผ ๊ฒฝ์ฐ ํด์ ์คํํ์ง ์์)
+ - ํด ์๋ฅผ ์ฆ๊ฐ์ํฌ ์ฑ
์์ ๊ฐ์ง
+ - **Player**๊ฐ **Boss Monster**๋ฅผ ์ ํ๋ ๊ณต๊ฒฉ ๋ฐฉ์์ผ๋ก ๊ณต๊ฒฉํ๋๋ก ํ ์ฑ
์์ ๊ฐ์ง
+ - **Player**์ ๊ณต๊ฒฉ ํ๋์ ๋ํ ๊ฒฐ๊ณผ๋ฅผ ์ ๊ณต๋ฐ๊ณ , **Boss Monster**์๊ฒ ์ ๋ฌํ ์ฑ
์์ ๊ฐ์ง (**Player**์ ๊ณต๊ฒฉ๊ณผ **Boss Monster**์ ํผํด ํ๋์ ๋ํ ํ๋ ฅ ํ์)
+ - **Boss Monster**์ ์์กด์ฌ๋ถ์ ๋ฐ๋ผ ๊ฒ์ ์ํ๋ฅผ ๋ณ๊ฒฝํ ์ฑ
์์ ๊ฐ์ง (**Boss Monster**์ ์ํ๋ฅผ ์ ๊ณต๋ฐ๋ ํ๋ ฅ ํ์)
+ - **Boss Monster**๊ฐ **Player**๋ฅผ ๊ณต๊ฒฉํ๋๋ก ํ ์ฑ
์์ ๊ฐ์ง (**Boss Monster**์ ๊ณต๊ฒฉ๊ณผ **Player**์ ํผํด ํ๋์ ๋ํ ํ๋ ฅ ํ์)
+ - **Boss Monster**์ ๊ณต๊ฒฉ ํ๋์ ๋ํ ๊ฒฐ๊ณผ๋ฅผ ์ ๊ณต๋ฐ๊ณ , **Player**์๊ฒ ์ ๋ฌํ ์ฑ
์์ ๊ฐ์ง (**Boss Monster**์ ๊ณต๊ฒฉ๊ณผ **Player**์ ํผํด ํ๋์ ๋ํ ํ๋ ฅ ํ์)
+ - **Player**์ ์์กด์ฌ๋ถ์ ๋ฐ๋ผ ๊ฒ์ ์ํ๋ฅผ ๋ณ๊ฒฝํ ์ฑ
์์ ๊ฐ์ง (**Player**์ ์ํ๋ฅผ ์ ๊ณต๋ฐ๋ ํ๋ ฅ ํ์)
+ - ํด๋น ํด์ ๋ํ ๊ฒ์ ๊ธฐ๋ก์ ์์ฑํ ์ฑ
์์ ๊ฐ์ง (**GameHistory**์๊ฒ ํด์ ๋ํ ๊ธฐ๋ก์ ์์ฑํ๋ ํ๋ ฅ ํ์)
+
+2. **Boss Monster**
+ - ํ์ฌ ์ํ๋ฅผ ์ ๊ณตํด์ค ์ฑ
์์ ๊ฐ์ง (์ด HP, ํ์ฌ HP)
+ - ๊ณต๊ฒฉ ๋ช
๋ น์ ์ํํ ์ฑ
์์ ๊ฐ์ง (0~20์ ๋๋ค ๋ฐ๋ฏธ์ง)
+ - ๊ณต๊ฒฉ ํผํด ์
์์ ์ํํ ์ฑ
์์ ๊ฐ์ง (ํ์ฌ HP ๊ฐ์)
+
+3. **Player**
+ - ํ์ฌ ์ํ๋ฅผ ์ ๊ณตํด์ค ์ฑ
์์ ๊ฐ์ง (์ด๋ฆ, ์ด HP, ํ์ฌ HP, ์ด MP, ํ์ฌ MP)
+ - ๊ณต๊ฒฉ ๋ช
๋ น์ ์ํํ ์ฑ
์์ ๊ฐ์ง (**Attack Type**์ ๋ํ ์ ๋ณด๋ฅผ ์ ๊ณต๋ฐ๋ ํ๋ ฅ ํ์, MP ๊ฐ์)
+ - ๊ณต๊ฒฉ ํผํด ์
์์ ์ํํ ์ฑ
์์ ๊ฐ์ง (ํ์ฌ HP ๊ฐ์)
+
+4. **Attack Type**
+ - ๊ณต๊ฒฉ ๋ฐฉ์ ์ข
๋ฅ๋ฅผ ์ ๊ณตํด์ค ์ฑ
์์ ๊ฐ์ง (๊ณต๊ฒฉ ๋ณํธ, ๊ณต๊ฒฉ ๋ฐฉ์ ์ด๋ฆ)
+ - ๊ณต๊ฒฉ ๋ฐฉ์ ์ํ์ ๋ํ ์ ๋ณด๋ฅผ ์ ๊ณตํด์ค ์ฑ
์์ ๊ฐ์ง (๋ฐ๋ฏธ์ง, MP ์๋ชจ๋, MP ํ๋ณต๋)
+
+5. **Game History**
+ - ๋งค ํด์ ๋ํ ๊ธฐ๋ก์ ์ ์ฅํ ์ฑ
์์ ๊ฐ์ง
+ - ์ถํ Controller์ Veiw๋ฅผ ์ํ ์์ฒญ์ ์๋ต ๊ฐ๋ฅ
+
+## ์ฃผ์ ๋ฉ์์ง ๊ตฌ์ฑ (๊ณต์ฉ ์ธํฐํ์ด์ค)
+
+1. ๋ณด์ค ๋ชฌ์คํฐ ์ค์ ์ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Create Boss Monster
+ - ์ธ์ : int hp
+ - ์๋ต : void, Boss Monster ๊ฐ์ฒด ์์ฑ
+
+2. ํ๋ ์ด์ด ์ค์ ์ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Create Player
+ - ์ธ์ : String name, int hp, int mp
+ - ์๋ต : void, Player ๊ฐ์ฒด ์์ฑ
+
+3. ํด ์งํ์ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Start Game
+ - ์ธ์ : -
+ - ์๋ต : ๊ตฌ์ฑ๋ ํด ๋ก์ง ์คํ
+
+4. ๊ฒ์ ์ํ ํ์ธ์ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Check Game Status
+ - ์ธ์ : -
+ - ์๋ต : return Game Status (true or False)
+
+5. ํด ํ์ ์ฆ๊ฐ๋ฅผ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Increase Turns
+ - ์ธ์ : -
+ - ์๋ต : void, Turn 1 ์ฆ๊ฐ
+
+6. ๊ณต๊ฒฉ ํ๋ ์ํ์ ์์ฒญ
+ - ์์ ์ : Player, Boss Monster
+ - ์ด๋ฆ : Attack
+ - ์ธ์ : -
+ - ์๋ต : return ๋ฐ๋ฏธ์ง, ๊ณต๊ฒฉ ๋ฐฉ์์ ๋ฐ๋ผ MP ๊ฐ์(Player)
+
+7. ๊ณต๊ฒฉ ํผํด ์ ๋ฌ์ ์์ฒญ
+ - ์์ ์ : Player, Boss Monster
+ - ์ด๋ฆ : take Damage
+ - ์ธ์ : int damage
+ - ์๋ต : return ํ์ฌ HP
+
+ | Unknown | ์์ง์ ๊ธฐ๋ฅ ๋ชฉ๋ก ๋ฌธ์์ ๋ํด์ ๋๊ตฐ๊ฐ ๋ณธ๋ค๋ ์๊ฐ๋ณด๋ค๋ ์ ๊ฐ ๊ตฌํ์ ์์์ ๊ณต๋ถํ ๋ด์ฉ์ผ๋ก ์ดํดํ๊ธฐ ์ฝ๊ฒ ์ ๋ฆฌํ๋ ๊ฒ์ ๋ชฉ์ ์ผ๋ก ์์ฑํ์๋๋ฐ์. ๊ฐ๋ฐ์ ํผ์ ํ๋ ๊ฒ์ด ์๋๊ธฐ์ ๋ง์ ์ฃผ์ ๋ถ๋ถ์ ๋ํด์๋ ์ ๊ฒฝ์ ์ฐ๋ฉด์ ์์ฑ์ ํด์ผ ํ ๊ฒ ๊ฐ์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค.
์ ๊ฐ ๋ฐ์์๋ถํฐ ๋ฆฌ๋ทฐ์ ๋ํ ๋ต๊ธ์ ๋จ๊ฒผ์ต๋๋ค. ๋ฆฌ๋ทฐ๋ฅผ ์์ฒญํ๊ณ ๋ฐ์ ๋ณด๋๊ฒ ์ฒ์์ด๋ผ ์ด๋ ๊ฒ ๋์์ด ๋ง์ด ๋๋์ง ๋ชฐ๋๋ค์. ์์ง ๊ณต๋ถ๋ฅผ ์์ํ์ง ์ผ๋ง ๋์ง ์์ ์ฝ๋ ์ฝ๊ธฐ๊ฐ ๋ง์ด ํ๋์
จ์ํ
๋ฐ ๋ฆ์ ์๊ฐ์๋ ๋ฆฌ๋ทฐ ํด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค. ์ฃผ์ ์๊ฒฌ๋ค ๋ชจ๋ ์ฐธ๊ณ ํด์ ์ ๊ฐ ๊ฐ์ ํด ๋๊ฐ์ผ ํ ๋ฐฉํฅ์ธ ๊ฒ ๊ฐ์ต๋๋ค. ๋ฆฌ๋ทฐ ๊ฐ์ฌ๋๋ฆฝ๋๋ค. ์ข์ ํ๋ฃจ ๋์ธ์! |
@@ -0,0 +1,28 @@
+package com.eureka.spartaonetoone.common.client;
+
+import com.eureka.spartaonetoone.common.client.dto.ReviewAggregateRequest;
+import com.eureka.spartaonetoone.common.client.dto.ReviewAggregateResponse;
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.web.reactive.function.client.WebClient;
+
+@Component
+@RequiredArgsConstructor
+public class ReviewAggregateClient { // ์ด๊ทธ๋ฆฌ ๋นผ์.
+
+ private final WebClient webClient;
+
+ // ์: http://review-service/api/v1/reviews/_aggregate
+ @Value("${review.aggregate.url}")
+ private String reviewAggregateUrl;
+
+ public ReviewAggregateResponse getReviewAggregate(ReviewAggregateRequest request) {
+ return webClient.post()
+ .uri(reviewAggregateUrl)
+ .bodyValue(request)
+ .retrieve()
+ .bodyToMono(ReviewAggregateResponse.class)
+ .block(); // ๋๊ธฐ ํธ์ถ
+ }
+} | Java | ์ฃผ์ ํ์ธํ์์ต๋๋ค ~! :) |
@@ -0,0 +1,28 @@
+package com.eureka.spartaonetoone.common.client;
+
+import com.eureka.spartaonetoone.common.client.dto.ReviewAggregateRequest;
+import com.eureka.spartaonetoone.common.client.dto.ReviewAggregateResponse;
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.web.reactive.function.client.WebClient;
+
+@Component
+@RequiredArgsConstructor
+public class ReviewAggregateClient { // ์ด๊ทธ๋ฆฌ ๋นผ์.
+
+ private final WebClient webClient;
+
+ // ์: http://review-service/api/v1/reviews/_aggregate
+ @Value("${review.aggregate.url}")
+ private String reviewAggregateUrl;
+
+ public ReviewAggregateResponse getReviewAggregate(ReviewAggregateRequest request) {
+ return webClient.post()
+ .uri(reviewAggregateUrl)
+ .bodyValue(request)
+ .retrieve()
+ .bodyToMono(ReviewAggregateResponse.class)
+ .block(); // ๋๊ธฐ ํธ์ถ
+ }
+} | Java | ์ ๋ถ๋ถ์์๋ Aggregate ์ ๊ฑฐํด์ฃผ์ธ์ ~~! |
@@ -0,0 +1,28 @@
+package com.eureka.spartaonetoone.common.client;
+
+import com.eureka.spartaonetoone.common.client.dto.ReviewAggregateRequest;
+import com.eureka.spartaonetoone.common.client.dto.ReviewAggregateResponse;
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.web.reactive.function.client.WebClient;
+
+@Component
+@RequiredArgsConstructor
+public class ReviewAggregateClient { // ์ด๊ทธ๋ฆฌ ๋นผ์.
+
+ private final WebClient webClient;
+
+ // ์: http://review-service/api/v1/reviews/_aggregate
+ @Value("${review.aggregate.url}")
+ private String reviewAggregateUrl;
+
+ public ReviewAggregateResponse getReviewAggregate(ReviewAggregateRequest request) {
+ return webClient.post()
+ .uri(reviewAggregateUrl)
+ .bodyValue(request)
+ .retrieve()
+ .bodyToMono(ReviewAggregateResponse.class)
+ .block(); // ๋๊ธฐ ํธ์ถ
+ }
+} | Java | ์ ๋ถ๋ถ์์๋ Aggregate ์ ๊ฑฐํด์ฃผ์ธ์ :) |
@@ -0,0 +1,15 @@
+package com.eureka.spartaonetoone.common.client.dto;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+
+import java.util.UUID;
+
+@Getter
+@NoArgsConstructor
+@AllArgsConstructor
+public class ReviewAggregateRequest {
+ // ํด๋น ๊ฐ๊ฒ์ ์๋ณ์
+ private UUID storeId;
+} | Java | ์ ๋ถ๋ถ์์๋ Aggregate ์ ๊ฑฐํด์ฃผ์ธ์ ~~! |
@@ -0,0 +1,6 @@
+package racingcar;
+
+public interface RacingNumberGenerator {
+
+ int generate();
+} | Java | ๋ค๋ฆฌ ๋ฏธ์
์์ ์ด ์ธํฐํ์ด์ค๋ฅผ ํจ์ํ์ธํฐํ์ด์ค๋ก ๊ตฌํ์ ํ๋๋ผ๊ตฌ์.
`@FunctionalInterface` ์ด๋
ธํ
์ด์
์ ๋ถ์ฌ์
ํ๋ฒ์ฏค, ๊ณ ๋ คํด๋ณผ๋ง ํ ๊ฒ ๊ฐ์, ๊ด๋ จ ํฌ์คํ
๋งํฌ ๋จ๊ฒจ๋์ต๋๋ค!
https://codechacha.com/ko/java8-functional-interface/ |
@@ -0,0 +1,47 @@
+package racingcar.model;
+
+public class Car implements Comparable<Car> {
+ private static final int MOVABLE_LOWER_BOUND = 4;
+
+ private final CarName name;
+ private final Position position = Position.init();
+
+ private Car(String name) {
+ this.name = CarName.from(name);
+ }
+
+ public static Car from(String name) {
+ return new Car(name);
+ }
+
+ public void move(int command) {
+ if (isMovable(command)) {
+ position.increase();
+ }
+ }
+
+ private static boolean isMovable(int command) {
+ return command >= MOVABLE_LOWER_BOUND;
+ }
+
+ public boolean isAt(Position expectedPosition) {
+ return position.equals(expectedPosition);
+ }
+
+ public int getPosition() {
+ return position.getPosition();
+ }
+
+ public CarName getName() {
+ return name;
+ }
+
+ public boolean isSamePosition(Car other) {
+ return this.position.equals(other.position);
+ }
+
+ @Override
+ public int compareTo(Car other) {
+ return this.position.compareTo(other.position);
+ }
+} | Java | ์ ์ด๊ฑฐ ์ถฉ๊ฒฉ์ ์ธ๋ฐ์...
Comparable<Car>๋ฅผ ํตํด Car ํด๋์ค๋ฅผ ์ ์ํ์ ๊ฑฐ ์ง์ง ์์ด๋์ด ์ข๋ค์.
๋ฐฐ์ ๊ฐ๋๋ค! |
@@ -0,0 +1,48 @@
+package racingcar.model;
+
+import java.util.Objects;
+
+public class Position implements Comparable<Position> {
+
+ private static final int STARTING_POSITION = 0;
+
+ private int position;
+
+ public Position(int position) {
+ this.position = position;
+ }
+
+ public static Position from(int position) {
+ return new Position(position);
+ }
+
+ public static Position init() {
+ return new Position(STARTING_POSITION);
+ }
+
+ public void increase() {
+ position++;
+ }
+
+ public int getPosition() {
+ return position;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ Position position1 = (Position) o;
+ return position == position1.position;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(position);
+ }
+
+ @Override
+ public int compareTo(Position other) {
+ return this.position - other.position;
+ }
+} | Java | Position์ ์ผ๊ธ๊ฐ์ฒดํํ ํ์๊ฐ ์๋?์ ๋ํ ์๋ฌธ์ด ๋ญ๋๋ค. Car ๊ฐ์ฒด์ int ํ๋๋ก ์ ์ธํด๋ ์ถฉ๋ถํด ๋ณด์ฌ์์.
์์ฑ์ validation์ด ํ์ํ ๊ฒ๋ ์๋๊ณ , ์ด๊ธฐ๊ฐ ์ค์ ๋ Car ๊ฐ์ฒด์์ ์ถฉ๋ถํ ๊ฐ๋ฅํ๊ณ ,
Car์์ compareTo๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉํ ๋, Car์ ๋ํ getter๋ง ์ค์ ํ๋ค๋ฉด ๋ฉ์๋ ๊ตฌํ์ด ๊ฐ๋ฅํ๋ค๊ณ ์๊ฐํฉ๋๋ค.
ํน ๋ค๋ฅธ ์๋๊ฐ ์๊ฑฐ๋, ์ ์๊ฐ์ด ํ๋ ธ๋ค๋ฉด ํผ๋๋ฐฑ ๋ถํ๋๋ฆฝ๋๋ค:) |
@@ -0,0 +1,65 @@
+package racingcar.model;
+
+import racingcar.RacingNumberGenerator;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class Cars {
+
+ private static final String DUPLICATED_CAR_NAMES = "์๋์ฐจ๋ค์ ์ด๋ฆ์ ์๋ก ์ค๋ณต๋ ์ ์์ต๋๋ค.";
+
+ private final List<Car> cars;
+
+ private Cars(List<String> names) {
+ validate(names);
+ cars = toCars(names);
+ }
+
+ private List<Car> toCars(List<String> names) {
+ return names.stream()
+ .map(Car::from)
+ .collect(Collectors.toList());
+ }
+
+ private void validate(List<String> names) {
+ if (isDuplicated(names)) {
+ throw new IllegalArgumentException(DUPLICATED_CAR_NAMES);
+ }
+ }
+
+ private boolean isDuplicated(List<String> names) {
+ Set<String> uniqueNames = new HashSet<>(names);
+
+ return uniqueNames.size() != names.size();
+ }
+
+ public static Cars from(List<String> names) {
+ return new Cars(names);
+ }
+
+ public void race(RacingNumberGenerator numberGenerator) {
+ cars.forEach(car -> car.move(numberGenerator.generate()));
+ }
+
+ public List<CarName> findWinners() {
+ Car maxPositionCar = getCarWithMaxPosition();
+ return cars.stream()
+ .filter(maxPositionCar::isSamePosition)
+ .map(Car::getName)
+ .collect(Collectors.toList());
+ }
+
+ private Car getCarWithMaxPosition() {
+ return cars.stream()
+ .max(Car::compareTo)
+ .orElseThrow(() -> new IllegalArgumentException("์ฐจ๋ ๋ฆฌ์คํธ๊ฐ ๋น์ด์์ต๋๋ค."));
+ }
+
+ public List<Car> get() {
+ return Collections.unmodifiableList(cars);
+ }
+} | Java | from์ด๋ผ๋ ๋ฉ์๋๊ฐ ๋ชจ๋ ํด๋์ค์์ ๋ฐ๋ณต๋๋๋ฐ,
Cars.from(names)์ด๋ฐ ์์ผ๋ก ์ฐ๊ฒฐ๋ ๊ฒฝ์ฐ ์๋ฏธ๊ฐ ๋๋ฌ๋์ง๋ง
๋ฉ์๋ ์ด๋ฆ์ด๋ ๊ฒฐ๊ตญ ๊ฐ์ฒด์๊ฒ ๋ณด๋ด๋ ๋ฉ์ธ์ง๋ผ๋ ๊ด์ ์์๋ 'from'์ด๋ผ๋ ๋ฉ์๋ ์ด๋ฆ๋ง ๋ณผ ๊ฒฝ์ฐ ์ด๋ค ์ฉ๋์ธ์ง ์๊ธฐ ํ๋ ๊ฒ ์ฌ์ค์ธ๊ฒ ๊ฐ์ต๋๋ค.
`setNewCars()`๋ฑ๋ฑ ์ ์ ํ๊ฒ ๋ฉ์๋๋ช
์ ๋ฐ๊ฟ๋ณด์๋ฉด ์ด๋จ๊น ์๊ฐํด๋ณด์์ต๋๋ค.ใ
ใ
|
@@ -0,0 +1,24 @@
+package racingcar.model;
+
+public class AttemptCount {
+
+ private static final int IS_OVER = 0;
+
+ private int count;
+
+ public AttemptCount(int count) {
+ this.count = count;
+ }
+
+ public static AttemptCount from(int count) {
+ return new AttemptCount(count);
+ }
+
+ public boolean isPlayable() {
+ return count > IS_OVER;
+ }
+
+ public void decrease() {
+ count--;
+ }
+} | Java | AttemptCount ํด๋์ค๋ ์๋ ํ์์ ๋ํ ์ผ๊ธ ๊ฐ์ฒด๋ก์์ ์ฑ๊ฒฉ๋ณด๋ค,
RacingGame์ ์ ์ฒด ์ฝ๋ ํ๋ฆ์ ์ ์ดํ๋ ์ญํ ์ ํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
isPlayable ๋ฉ์๋๋ฅผ ํตํด ๊ฒ์ ์งํ์ฌ๋ถ๋ฅผ ํ๋จํด looping์ ์ด๋๊น์ง ๋ ์ง ํ๋จํด์ฃผ๋ ๊ฒ์ด ํต์ฌ์ด๋๊น์.
`RacingGame`๋ฑ์ผ๋ก ํด๋์ค๋ช
์ ๋ฐ๊พธ๊ณ , attemptCount๋ฅผ ์ธ์คํด์ค ํ๋ํ ํด๋ณด๋ฉด ์ด๋จ๊น์?ใ
ใ
|
@@ -0,0 +1,24 @@
+package racingcar.model;
+
+public class AttemptCount {
+
+ private static final int IS_OVER = 0;
+
+ private int count;
+
+ public AttemptCount(int count) {
+ this.count = count;
+ }
+
+ public static AttemptCount from(int count) {
+ return new AttemptCount(count);
+ }
+
+ public boolean isPlayable() {
+ return count > IS_OVER;
+ }
+
+ public void decrease() {
+ count--;
+ }
+} | Java | ์ ๋ ์ฌ์ฉํ๋ฉด์ ์ฐ์ฐํ๋๋ฐ `RacingGame` ๊ฐ์ฒด๋ก ๊ฒ์์ ์งํํ๋ ์ญํ ์ ๋ถ๋ฆฌํด๋ด์ผ๊ฒ ๋ค์...!
์ข์ ์ง์ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,47 @@
+package racingcar.model;
+
+public class Car implements Comparable<Car> {
+ private static final int MOVABLE_LOWER_BOUND = 4;
+
+ private final CarName name;
+ private final Position position = Position.init();
+
+ private Car(String name) {
+ this.name = CarName.from(name);
+ }
+
+ public static Car from(String name) {
+ return new Car(name);
+ }
+
+ public void move(int command) {
+ if (isMovable(command)) {
+ position.increase();
+ }
+ }
+
+ private static boolean isMovable(int command) {
+ return command >= MOVABLE_LOWER_BOUND;
+ }
+
+ public boolean isAt(Position expectedPosition) {
+ return position.equals(expectedPosition);
+ }
+
+ public int getPosition() {
+ return position.getPosition();
+ }
+
+ public CarName getName() {
+ return name;
+ }
+
+ public boolean isSamePosition(Car other) {
+ return this.position.equals(other.position);
+ }
+
+ @Override
+ public int compareTo(Car other) {
+ return this.position.compareTo(other.position);
+ }
+} | Java | ์์ ์ ์์ด๋์ด๋ ์๋๊ตฌ... **Tecoble** ์์ `getter()` ์ ๊ด๋ จ๋ ๋ด์ฉ์ ์ฝ๋ค๊ฐ ์๊ฒ ๋ ๋ด์ฉ์ ๊ทธ๋๋ก ์ ์ฉํด๋ณธ ๊ฑฐ์์... ํํ... ์ ๋ ์ฒ์ ์ด ์์ด๋์ด๋ฅผ ์ ํ๊ณ ์ถฉ๊ฒฉ์ ๋ง์ด ๋ฐ์์ต๋๋ค ใ
- [getter() ๋ฅผ ์ฌ์ฉํ๋ ๋์ ๊ฐ์ฒด์ ๋ฉ์์ง๋ฅผ ๋ณด๋ด์ง](https://tecoble.techcourse.co.kr/post/2020-04-28-ask-instead-of-getter/)
- [Comparator ์ Comparable ์ ์ดํด](https://st-lab.tistory.com/243)
๋๋ฌด ์ข์ ์๋ฃ๋ค์ด๋ผ ํ๋ฒ ์ฝ์ด๋ณด์๋ ๊ฑฐ ๊ฐ๋ ฅํ ์ถ์ฒ๋๋ฆฝ๋๋ค!! |
@@ -0,0 +1,65 @@
+package racingcar.model;
+
+import racingcar.RacingNumberGenerator;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class Cars {
+
+ private static final String DUPLICATED_CAR_NAMES = "์๋์ฐจ๋ค์ ์ด๋ฆ์ ์๋ก ์ค๋ณต๋ ์ ์์ต๋๋ค.";
+
+ private final List<Car> cars;
+
+ private Cars(List<String> names) {
+ validate(names);
+ cars = toCars(names);
+ }
+
+ private List<Car> toCars(List<String> names) {
+ return names.stream()
+ .map(Car::from)
+ .collect(Collectors.toList());
+ }
+
+ private void validate(List<String> names) {
+ if (isDuplicated(names)) {
+ throw new IllegalArgumentException(DUPLICATED_CAR_NAMES);
+ }
+ }
+
+ private boolean isDuplicated(List<String> names) {
+ Set<String> uniqueNames = new HashSet<>(names);
+
+ return uniqueNames.size() != names.size();
+ }
+
+ public static Cars from(List<String> names) {
+ return new Cars(names);
+ }
+
+ public void race(RacingNumberGenerator numberGenerator) {
+ cars.forEach(car -> car.move(numberGenerator.generate()));
+ }
+
+ public List<CarName> findWinners() {
+ Car maxPositionCar = getCarWithMaxPosition();
+ return cars.stream()
+ .filter(maxPositionCar::isSamePosition)
+ .map(Car::getName)
+ .collect(Collectors.toList());
+ }
+
+ private Car getCarWithMaxPosition() {
+ return cars.stream()
+ .max(Car::compareTo)
+ .orElseThrow(() -> new IllegalArgumentException("์ฐจ๋ ๋ฆฌ์คํธ๊ฐ ๋น์ด์์ต๋๋ค."));
+ }
+
+ public List<Car> get() {
+ return Collections.unmodifiableList(cars);
+ }
+} | Java | ์ ์ ํฉํ ๋ฆฌ ๋ฉ์๋ ๋ค์ด๋ฐ์ ์ปจ๋ฒค์
์ด ์กด์ฌํ๋ค๊ณ ์๊ณ ์์ต๋๋ค!
์ ๋ ๊ฐ์ธ์ ์ผ๋ก๋ ์ ์ ํ ์ด๋ฆ์ ์ง์ด์ฃผ๋ ๊ฒ์ด ์ข๋ค๊ณ ์๊ฐํฉ๋๋ค.
ํ์ง๋ง ์ปจ๋ฒค์
์ด๋๊น...! ์ปจ๋ฒค์
์ ์งํค๋ ๋ฐฉํฅ์ผ๋ก ์งํํ๊ณ ์์ด์...
- [์ ์ ํฉํ ๋ฆฌ ๋ฉ์๋์ ๋ค์ด๋ฐ ๋ฐฉ์](https://velog.io/@saint6839/%EC%A0%95%EC%A0%81-%ED%8C%A9%ED%86%A0%EB%A6%AC-%EB%A9%94%EC%84%9C%EB%93%9C-%EB%84%A4%EC%9D%B4%EB%B0%8D-%EB%B0%A9%EC%8B%9D) |
@@ -0,0 +1,48 @@
+package racingcar.model;
+
+import java.util.Objects;
+
+public class Position implements Comparable<Position> {
+
+ private static final int STARTING_POSITION = 0;
+
+ private int position;
+
+ public Position(int position) {
+ this.position = position;
+ }
+
+ public static Position from(int position) {
+ return new Position(position);
+ }
+
+ public static Position init() {
+ return new Position(STARTING_POSITION);
+ }
+
+ public void increase() {
+ position++;
+ }
+
+ public int getPosition() {
+ return position;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ Position position1 = (Position) o;
+ return position == position1.position;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(position);
+ }
+
+ @Override
+ public int compareTo(Position other) {
+ return this.position - other.position;
+ }
+} | Java | ๋ง์ํด์ฃผ์ ๋๋ก ํ์ฌ ๋ฏธ์
์์๋ ๊ตณ์ด ์ผ๊ธ ๊ฐ์ฒดํํ ํ์๊ฐ ์๋ค๋ ๊ฒ์ ๋์ํฉ๋๋ค.
๋์ดํด์ฃผ์ ๊ทผ๊ฑฐ๋ค๋ ๋ชจ๋ ํ๋นํ๋ค๊ณ ์๊ฐํด์..!
๋ค๋ง ๋ฏธ์
์ ์งํํ๋ฉด์ ์ต๋ํ ๊ฐ์ฒด์งํฅ ์ํ ์ฒด์กฐ ์์น๋ค์ ์งํค๋ฉด์ ๊ตฌํํด๋ณด๋ ๊ฒ์ ๋ชฉํ๋ก ํ๊ณ ์์ด์. (๊ฐ์ฒด์งํฅ ์ํ ์ฒด์กฐ ์์น์ **๋ชจ๋ ์์ ๊ฐ๊ณผ ๋ฌธ์์ด์ ํฌ์ฅํ๋ค.** ๋ผ๋ ํญ๋ชฉ์ด ์๋๋ผ๊ตฌ์!) ์ผ์ข
์ ์ฐ์ต์ด๋ผ๊ณ ๋ด์ฃผ์๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค! ์์ ๊ฐ์ ํฌ์ฅํ๋ ๊ฒ์ด ์ถํ์ ์๊ตฌ์ฌํญ์ด ๋ณํ์ ๋ ์ ์ง ๋ณด์์๋ ํฐ ์ด์ ์ด ์๋ค๋ ๊ฒ์ ๊ณ ๋ คํด์ ์ต์ง๋ก๋ผ๋ Wrapping ํ๋ ค๊ณ ๋
ธ๋ ฅํ๊ณ ์์ต๋๋ค...
- [์์ ํ์
์ ํฌ์ฅํด์ผ ํ๋ ์ด์ ](https://tecoble.techcourse.co.kr/post/2020-05-29-wrap-primitive-type/) |
@@ -0,0 +1,6 @@
+package racingcar;
+
+public interface RacingNumberGenerator {
+
+ int generate();
+} | Java | ์ ์ข์ ์๋ฃ ๊ฐ์ฌํฉ๋๋ค! ์ต๊ทผ์ ๋๋ค๋ฅผ ํ์ตํ๋ฉด์ ํจ์ํ ์ธํฐํ์ด์ค๋ ํจ๊ป ๊ณต๋ถํ์๋๋ฐ
์ง์ ํ์ฉํด๋ณผ ์๊ฐ์ ๋ชปํด๋ดค๋ค์. ์ง๊ธ ๋น์ฅ ๋ฆฌํฉํ ๋ง ํด๋ด์ผ๊ฒ ์ต๋๋ค! ๊ฐ์ฌํฉ๋๋ค!! |
@@ -0,0 +1,57 @@
+package racingcar.view;
+
+import racingcar.model.Car;
+import racingcar.model.CarName;
+import racingcar.model.Cars;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class OutputView {
+
+ private static final String NEW_LINE = "\n";
+ private static final String SCORE_UNIT = "-";
+ private static final String SCORE_BOARD = "%s : %s";
+ private static final String WINNER_SEPARATOR = ", ";
+ private static final String WINNERS_ARE = "์ต์ข
์ฐ์น์ : ";
+ private static final String ERROR_PREFIX = "[ERROR] ";
+ private static final String RESULT_INTRO = "์คํ ๊ฒฐ๊ณผ";
+
+ public void printResult(Cars cars) {
+ System.out.println(getScore(cars));
+ }
+
+ private String getScore(Cars cars) {
+ return cars.get().stream()
+ .map(this::toScoreBoard)
+ .collect(Collectors.joining(NEW_LINE));
+ }
+
+ private String toScoreBoard(Car car) {
+ return String.format(SCORE_BOARD, car.getName(), convertToScore(car.getPosition()));
+ }
+
+ private String convertToScore(int position) {
+ return SCORE_UNIT.repeat(position);
+ }
+
+ public void printWinners(List<CarName> winners) {
+ System.out.print(WINNERS_ARE);
+ System.out.println(getWinnerNames(winners));
+ }
+
+ private String getWinnerNames(List<CarName> winners) {
+ return winners.stream()
+ .map(CarName::get)
+ .collect(Collectors.joining(WINNER_SEPARATOR));
+ }
+
+ public void printError(IllegalArgumentException error) {
+ System.out.print(ERROR_PREFIX);
+ System.out.println(error.getMessage());
+ }
+
+ public void printRaceIntro() {
+ System.out.println(RESULT_INTRO);
+ }
+} | Java | ์ด ๋ถ๋ถ์ ๋ฉ์ธ์ง ๋ถ๋ถ์ด๋ผ view์์ ์ฒ๋ฆฌํ์ ๊ฒ ๊ฐ์๋ฐ, ์ด ๊ฒฝ์ฐ `cars`๊ฐ ์ง์ ์ฒ๋ฆฌํ๋ ๊ฒ์ด ์๋ ์ง์ ๊บผ๋ด์์ ์ฒ๋ฆฌํ๋ ๊ฒ์ธ ๊ฒ ๊ฐ์์ ์ฐจ๋ผ๋ฆฌ ๋ฉ์์ง๋ฅผ `cars`์์ ์ฒ๋ฆฌํ๋ ๊ฒ ๋ ๊ฐ์ฒด๋ฅผ ๋
๋ฆฝ์ ์ผ๋ก ๋ง๋๋ ๊ฒ ์๋๊น ํ๋ ์๊ฐ์ด ๋๋ค์! |
@@ -0,0 +1,37 @@
+package racingcar.view;
+
+import camp.nextstep.edu.missionutils.Console;
+import racingcar.model.AttemptCount;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class InputView {
+
+ private static final String SPLIT_REGEX = ",";
+ private static final String NON_NUMERIC_INPUT_MESSAGE = "์ซ์๋ง ์
๋ ฅํ ์ ์์ต๋๋ค.";
+ private static final String REQUEST_ATTEMPT_COUNT_MESSAGE = "์๋ํ ํ์๋ ๋ชํ์ธ๊ฐ์?";
+
+ public List<String> readNames() {
+ String names = Console.readLine();
+
+ return Arrays.stream(names.split(SPLIT_REGEX))
+ .collect(Collectors.toList());
+ }
+
+ public AttemptCount readAttemptCount() {
+ System.out.println(REQUEST_ATTEMPT_COUNT_MESSAGE);
+ String attemptCount = Console.readLine();
+
+ return AttemptCount.from(parseInt(attemptCount));
+ }
+
+ private int parseInt(String input) {
+ try {
+ return Integer.parseInt(input);
+ } catch (NumberFormatException error) {
+ throw new IllegalArgumentException(NON_NUMERIC_INPUT_MESSAGE);
+ }
+ }
+} | Java | ์ด๋ฌํ ๋ฉ์์ง๋ค์ ์ผ์ผ์ด ์์์ฒ๋ฆฌํ๋ฉด ๋์ค์ ๊ท๋ชจ๊ฐ ์ปค์ก์ ๋, ๊ด๋ฆฌํ๊ธฐ ํ๋ค์ด์ง๋ค๊ณ ํฉ๋๋ค. ์ฌ์ฌ์ฉ๋์ง ์๋ ๋ฉ์์ง๋ค์ ์ง์ ๊ฐ์ ๋ด์๋ณด๋ ๊ฑด ์ด๋จ๊น์? |
@@ -0,0 +1,24 @@
+package racingcar.model;
+
+public class AttemptCount {
+
+ private static final int IS_OVER = 0;
+
+ private int count;
+
+ public AttemptCount(int count) {
+ this.count = count;
+ }
+
+ public static AttemptCount from(int count) {
+ return new AttemptCount(count);
+ }
+
+ public boolean isPlayable() {
+ return count > IS_OVER;
+ }
+
+ public void decrease() {
+ count--;
+ }
+} | Java | ์ ๋ ๋ง์ด ๋ฐฐ์๊ฐ๋ค์ ๊ฐ์ฌํฉ๋๋ค :D |
@@ -0,0 +1,47 @@
+package racingcar.model;
+
+public class Car implements Comparable<Car> {
+ private static final int MOVABLE_LOWER_BOUND = 4;
+
+ private final CarName name;
+ private final Position position = Position.init();
+
+ private Car(String name) {
+ this.name = CarName.from(name);
+ }
+
+ public static Car from(String name) {
+ return new Car(name);
+ }
+
+ public void move(int command) {
+ if (isMovable(command)) {
+ position.increase();
+ }
+ }
+
+ private static boolean isMovable(int command) {
+ return command >= MOVABLE_LOWER_BOUND;
+ }
+
+ public boolean isAt(Position expectedPosition) {
+ return position.equals(expectedPosition);
+ }
+
+ public int getPosition() {
+ return position.getPosition();
+ }
+
+ public CarName getName() {
+ return name;
+ }
+
+ public boolean isSamePosition(Car other) {
+ return this.position.equals(other.position);
+ }
+
+ @Override
+ public int compareTo(Car other) {
+ return this.position.compareTo(other.position);
+ }
+} | Java | ์ ๋ ์ฌ๊ธฐ์ ๋๋๋๋ฐ ๋๋ถ์ ์ข์ ์ปจํ
์ธ ๋ค์ ๋ฐฐ์๊ฐ๋ค์ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,44 @@
+package racingcar.model;
+
+import org.junit.jupiter.api.DisplayNameGeneration;
+import org.junit.jupiter.api.DisplayNameGenerator;
+import org.junit.jupiter.api.Test;
+import racingcar.RacingNumberGenerator;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.util.Lists.newArrayList;
+
+@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
+public class CarsTest {
+
+ @Test
+ void ์๋์ฐจ๋ค์_์ด๋ฆ์_์๋ก_์ค๋ณต๋ _์_์๋ค() {
+ assertThatIllegalArgumentException()
+ .isThrownBy(() -> Cars.from(List.of("pobi,jun,pobi")));
+ }
+
+ @Test
+ void ๊ฐ์ฅ_๋ฉ๋ฆฌ_์ด๋ํ_์ฌ๋ฆผ์ด_์ฐ์นํ๋ค() {
+ Cars cars = Cars.from(List.of("pobi", "jun", "khj"));
+ TestRacingNumberGenerator numberGenerator = new TestRacingNumberGenerator();
+
+ cars.race(numberGenerator);
+ cars.race(numberGenerator);
+
+ assertThat(cars.findWinners())
+ .contains(CarName.from("pobi"), CarName.from("khj"));
+ }
+
+ static class TestRacingNumberGenerator implements RacingNumberGenerator {
+
+ private final List<Integer> numbers = newArrayList(4, 1, 4, 4, 1, 4);
+
+ @Override
+ public int generate() {
+ return numbers.remove(0);
+ }
+ }
+} | Java | ์ด ๋ถ๋ถ์ `RacingNumberGenerator`๋ผ๊ณ ํจ์ํ ์ธํฐํ์ด์ค๋ก ์ด๋ฏธ ์ ์ํด ๋์
จ์ผ๋ ์ง์ ๊ตฌํํ ํ์ ์์ด ๊ฐ๋จํ๊ฒ ๋๋ค๋ก ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,44 @@
+package racingcar.model;
+
+import org.junit.jupiter.api.DisplayNameGeneration;
+import org.junit.jupiter.api.DisplayNameGenerator;
+import org.junit.jupiter.api.Test;
+import racingcar.RacingNumberGenerator;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.util.Lists.newArrayList;
+
+@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
+public class CarsTest {
+
+ @Test
+ void ์๋์ฐจ๋ค์_์ด๋ฆ์_์๋ก_์ค๋ณต๋ _์_์๋ค() {
+ assertThatIllegalArgumentException()
+ .isThrownBy(() -> Cars.from(List.of("pobi,jun,pobi")));
+ }
+
+ @Test
+ void ๊ฐ์ฅ_๋ฉ๋ฆฌ_์ด๋ํ_์ฌ๋ฆผ์ด_์ฐ์นํ๋ค() {
+ Cars cars = Cars.from(List.of("pobi", "jun", "khj"));
+ TestRacingNumberGenerator numberGenerator = new TestRacingNumberGenerator();
+
+ cars.race(numberGenerator);
+ cars.race(numberGenerator);
+
+ assertThat(cars.findWinners())
+ .contains(CarName.from("pobi"), CarName.from("khj"));
+ }
+
+ static class TestRacingNumberGenerator implements RacingNumberGenerator {
+
+ private final List<Integer> numbers = newArrayList(4, 1, 4, 4, 1, 4);
+
+ @Override
+ public int generate() {
+ return numbers.remove(0);
+ }
+ }
+} | Java | ```java
cars.race(() -> new Integer[]{4, 1, 4, 4, 1, 4})
```
์ด๋ฐ ๋๋์ผ๋ก์! |
@@ -0,0 +1,65 @@
+package racingcar.model;
+
+import racingcar.RacingNumberGenerator;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class Cars {
+
+ private static final String DUPLICATED_CAR_NAMES = "์๋์ฐจ๋ค์ ์ด๋ฆ์ ์๋ก ์ค๋ณต๋ ์ ์์ต๋๋ค.";
+
+ private final List<Car> cars;
+
+ private Cars(List<String> names) {
+ validate(names);
+ cars = toCars(names);
+ }
+
+ private List<Car> toCars(List<String> names) {
+ return names.stream()
+ .map(Car::from)
+ .collect(Collectors.toList());
+ }
+
+ private void validate(List<String> names) {
+ if (isDuplicated(names)) {
+ throw new IllegalArgumentException(DUPLICATED_CAR_NAMES);
+ }
+ }
+
+ private boolean isDuplicated(List<String> names) {
+ Set<String> uniqueNames = new HashSet<>(names);
+
+ return uniqueNames.size() != names.size();
+ }
+
+ public static Cars from(List<String> names) {
+ return new Cars(names);
+ }
+
+ public void race(RacingNumberGenerator numberGenerator) {
+ cars.forEach(car -> car.move(numberGenerator.generate()));
+ }
+
+ public List<CarName> findWinners() {
+ Car maxPositionCar = getCarWithMaxPosition();
+ return cars.stream()
+ .filter(maxPositionCar::isSamePosition)
+ .map(Car::getName)
+ .collect(Collectors.toList());
+ }
+
+ private Car getCarWithMaxPosition() {
+ return cars.stream()
+ .max(Car::compareTo)
+ .orElseThrow(() -> new IllegalArgumentException("์ฐจ๋ ๋ฆฌ์คํธ๊ฐ ๋น์ด์์ต๋๋ค."));
+ }
+
+ public List<Car> get() {
+ return Collections.unmodifiableList(cars);
+ }
+} | Java | ์ซ์ ์์ฑํ๋ ๋ถ๋ถ์ ์์ฑ์๊ฐ ์๋ `race`์์ ์ฃผ์
๋ฐ์ ์๋ ์๋ค์! ํ๋ ๋ฐฐ์๊ฐ๋๋ค |
@@ -0,0 +1,44 @@
+package racingcar.model;
+
+import org.junit.jupiter.api.DisplayNameGeneration;
+import org.junit.jupiter.api.DisplayNameGenerator;
+import org.junit.jupiter.api.Test;
+import racingcar.RacingNumberGenerator;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.util.Lists.newArrayList;
+
+@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
+public class CarsTest {
+
+ @Test
+ void ์๋์ฐจ๋ค์_์ด๋ฆ์_์๋ก_์ค๋ณต๋ _์_์๋ค() {
+ assertThatIllegalArgumentException()
+ .isThrownBy(() -> Cars.from(List.of("pobi,jun,pobi")));
+ }
+
+ @Test
+ void ๊ฐ์ฅ_๋ฉ๋ฆฌ_์ด๋ํ_์ฌ๋ฆผ์ด_์ฐ์นํ๋ค() {
+ Cars cars = Cars.from(List.of("pobi", "jun", "khj"));
+ TestRacingNumberGenerator numberGenerator = new TestRacingNumberGenerator();
+
+ cars.race(numberGenerator);
+ cars.race(numberGenerator);
+
+ assertThat(cars.findWinners())
+ .contains(CarName.from("pobi"), CarName.from("khj"));
+ }
+
+ static class TestRacingNumberGenerator implements RacingNumberGenerator {
+
+ private final List<Integer> numbers = newArrayList(4, 1, 4, 4, 1, 4);
+
+ @Override
+ public int generate() {
+ return numbers.remove(0);
+ }
+ }
+} | Java | ์ ๋ ํผ๋๋ฐฑ ๋ฐ๊ณ ๋ ๋ค ๋๋ค๋ก ํํํด๋ณด๋ ค๊ณ ํ๋๋ฐ `TestRacingNumberGenerator` ์ฒ๋ผ ๋ด๋ถ์ ์ํ๊ฐ ํ์ํ ๊ฒฝ์ฐ์ ์จ์ ํ ๋๋ค๋ก ํํํ๋๊ฒ ํ๋ค๋๋ผ๊ตฌ์!
๋ง์ฝ ๋๋ค๋ก ์ฒ๋ฆฌํ๋ ค๋ฉด
```java
cars.race(() -> newArrayList(4, 1, 4, 4, 1, 4).remove(0));
```
๊ณผ ๊ฐ์ ํํ๋ก ์ฌ์ฉํด์ผ ํ๋๋ฐ ์ด๋ ๊ฒ ๋๋ฉด ์ผ๋จ ๊ฐ๋
์ฑ์ด ๋๋ฌด ๋จ์ด์ง๋๋ผ๊ตฌ์.
๋ฌด์๋ณด๋ค๋ `race()` ๋ฅผ ์ฌ๋ฌ ๋ฒ ๋ฐ๋ณตํ๋๋ผ๋ ํญ์ *List*๊ฐ ์ด๊ธฐํ ๋๊ธฐ ๋๋ฌธ์ ํญ์ ๊ฐ์ ๊ฐ๋ง ๋ฐ๋ณตํ๊ฒ ๋๊ธฐ๋ ํ๊ตฌ์.
```java
cars.race(() -> newArrayList(4, 1, 4, 4, 1, 4).remove(0)); // 4 ๋ฐํ
cars.race(() -> newArrayList(4, 1, 4, 4, 1, 4).remove(0)); // 4 ๋ฐํ
```
[Functional Interface ๋?](https://tecoble.techcourse.co.kr/post/2020-07-17-Functional-Interface/) ์ ์ฝ์ด๋ณด๋ ๋ค์๊ณผ ๊ฐ์ ๋๋ชฉ์ด ์๋๋ผ๊ตฌ์!
```
ํจ์ํ ๊ฐ๋ฐ ๋ฐฉ์์ ํ์์ ํด๋นํ๋ ๋ถ๋ถ๋ ๊ฐ์ผ๋ก ์ทจ๊ธ์ด ๊ฐ๋ฅํด ์ก๋ค๋ ๊ฒ์ธ๋ฐ
์๋ฐ์์ ์๋ฏธํ๋ ๊ธฐ๋ณธํ์ ๋ฐ์ดํฐ(Integer ๋ String)๋ง ๊ฐ์ด ์๋๋ผ ํ์(๋ก์ง)๋
๊ฐ์ผ๋ก ์ทจ๊ธํ ์ ์๊ฒ ๋์๋ค๋ ์ด์ผ๊ธฐ์
๋๋ค.
์ด๊ฒ์ ์๋ฐ๊ฐ ์ฝ๋์ ์ฌํ์ฉ ๋จ์๊ฐ ํด๋์ค์๋ ๊ฒ์ด ํจ์ ๋จ์๋ก ์ฌ์ฌ์ฉ์ด ๊ฐ๋ฅํด ์ง๋ฉด์
์กฐ๊ธ ๋ ๊ฐ๋ฐ์ ์ ์ฐํ๊ฒ ํ ์ ์๊ฒ ๋ ์ ์ด๋ผ๊ณ ํ ์ ์์ต๋๋ค.
```
ํจ์ํ ์ธํฐํ์ด์ค์ ๋์์ด ๋๋ ๊ฒ์
ํด๋์ค์ ์ข
์ ๋์ด์๋ (์ ๊ฐ ๋๋ผ๊ธฐ์ ์ํ์ ๋ฌถ์ฌ์๋) **๋ฉ์๋**๊ฐ ์๋๋ผ
์ค๋ก์ง ํ์ ๋ง์ ๋ค๋ฃจ๊ณ ์๋ **ํจ์** ๋ฟ์ธ ๊ฒ ๊ฐ์ต๋๋ค.
(๊ทธ๋ฌ๊ณ ๋ณด๋ ์ด๋ฆ๋ **ํจ์ํ** ์ธํฐํ์ด์ค๋ค์...)
๋ถ๋ช
์ ์ ํจ์ํ ์ธํฐํ์ด์ค๋ฅผ ํ์ตํ์๋๋ฐ ํจ์์ ๋ฉ์๋์ ์ฐจ์ด๋ฅผ ์๊ฐํ์ง ์๊ณ ์ฌ์ฉํ๋ ค๊ณ ํ๋ค์ ํ...
์๊ฒฝ๋ ๋๋ถ์ ์ ๋ ํจ์ํ ์ธํฐํ์ด์ค์ ๋ํด์ ์ข ๋ ๋ช
๋ฃํ๊ฒ ์ดํดํ๊ฒ ๋๋ ๊ณ๊ธฐ๊ฐ ๋์์ต๋๋ค.
์ข์ ์ง์ ๊ฐ์ฌ๋๋ ค์!!! :) |
@@ -0,0 +1,57 @@
+package racingcar.view;
+
+import racingcar.model.Car;
+import racingcar.model.CarName;
+import racingcar.model.Cars;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class OutputView {
+
+ private static final String NEW_LINE = "\n";
+ private static final String SCORE_UNIT = "-";
+ private static final String SCORE_BOARD = "%s : %s";
+ private static final String WINNER_SEPARATOR = ", ";
+ private static final String WINNERS_ARE = "์ต์ข
์ฐ์น์ : ";
+ private static final String ERROR_PREFIX = "[ERROR] ";
+ private static final String RESULT_INTRO = "์คํ ๊ฒฐ๊ณผ";
+
+ public void printResult(Cars cars) {
+ System.out.println(getScore(cars));
+ }
+
+ private String getScore(Cars cars) {
+ return cars.get().stream()
+ .map(this::toScoreBoard)
+ .collect(Collectors.joining(NEW_LINE));
+ }
+
+ private String toScoreBoard(Car car) {
+ return String.format(SCORE_BOARD, car.getName(), convertToScore(car.getPosition()));
+ }
+
+ private String convertToScore(int position) {
+ return SCORE_UNIT.repeat(position);
+ }
+
+ public void printWinners(List<CarName> winners) {
+ System.out.print(WINNERS_ARE);
+ System.out.println(getWinnerNames(winners));
+ }
+
+ private String getWinnerNames(List<CarName> winners) {
+ return winners.stream()
+ .map(CarName::get)
+ .collect(Collectors.joining(WINNER_SEPARATOR));
+ }
+
+ public void printError(IllegalArgumentException error) {
+ System.out.print(ERROR_PREFIX);
+ System.out.println(error.getMessage());
+ }
+
+ public void printRaceIntro() {
+ System.out.println(RESULT_INTRO);
+ }
+} | Java | ๋ง์ํด์ฃผ์ ๋๋ก *UI* ๋ฅผ ๋ค๋ฃจ๋ ๋ก์ง์ ๋๋ฉ์ธ ๋ด๋ถ์์ ์ฒ๋ฆฌํ๋ ๊ฒ์ด ๋ง์์ ๊ฑธ๋ฆฌ๋๋ผ๊ตฌ์...
*UI* ์๊ตฌ์ฌํญ์ด ๋ณํ๋ฉด ๋๋ฉ์ธ ๋ด๋ถ์๋ ๋ณํ๊ฐ ๊ฐ์ ๋๋ค ๋ ๊ฒ ์คํ๋ ค ๊ฐ์ฒด ๋
๋ฆฝ์ฑ์ ํด์น๋ค๊ณ ์๊ฐํ์ต๋๋ค.
๊ทธ๋์ ๊ฐ์ฒด๋ฅผ ์ง์ ๊บผ๋ด์ค๋ ๋์ ๋ค์๊ณผ ๊ฐ์ด ๋ถ๋ณ ๊ฐ์ฒด๋ก ๋ฆฌ์คํธ๋ฅผ ๋ฐํํ๋๋ก ์ฒ๋ฆฌํด์ฃผ์์ต๋๋ค.
```java
public List<Car> get() {
return Collections.unmodifiableList(cars);
}
```
๊ทธ๋๋ ์ญ์ ์ผ๊ธ ์ปฌ๋ ์
๋ด์ ์ํ๋ฅผ ์ง์ ๊บผ๋ด์์ ์ฌ์ฉํ๋ ๊ฒ ๋๋ฌด ์ฐ์ฐํ๊ธด ํ๋๋ผ๊ตฌ์...
์ ๊ฐ์ธ์ ์ธ ์๊ฐ์ ๋ ์์ธํ๊ฒ ๋ง์๋๋ฆฌ๊ฒ ์ต๋๋ค.
์ ๋ *UI* ๋ก์ง์ ๋๋ฉ์ธ์ ํตํฉํ๋ ๊ฒ ๋ณด๋ค๋ `getter()` ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์คํ๋ ค ๋ ๋ซ๋ค๊ณ ํ๋จํ์ต๋๋ค.
๋ฏธ์
์ ์งํํ๋ฉด์ ์ ๋ `getter()`๋ฅผ ๋ฌด์กฐ๊ฑด์ ์ผ๋ก ๋ฐฐ์ ํ๋๊ฒ ๋๋ฌด ํ๋ค๋๋ผ๊ตฌ์...
๋ ๊ฐ์ฒด๋ฅผ ๋น๊ตํ๊ฑฐ๋, ์ง์ ์ฌ๋ถ๋ฅผ ํ๋จํ๋ ๋ก์ง์์๋ ๋ฉ์์ง๋ง์ผ๋ก๋ ์ถฉ๋ถํ ๊ตฌํํ ์ ์์์ง๋ง,
๋ ๊ฐ์ฒด์ ๊ฐ์ผ๋ก ์ฐ์ฐ์ ํด์ผ ํ๋ค๊ฑฐ๋, ๊ฐ์ฒด ๋ด๋ถ์ ๊ฐ์ ์ถ๋ ฅํด์ผ ํ๋ ๊ฒฝ์ฐ์ `getter()`๋ฅผ ์ฌ์ฉํ๋ ๊ฒ
์คํ๋ ค ๋ ํจ์จ์ ์ด๋ผ๊ณ ์๊ฐํ์ต๋๋ค.
๊ทธ๋์ `getter()`๋ฅผ ์ต๋ํ ์ง์ํ๋ ์ด์ ์ฆ, ์บก์ํ๋ฅผ ์๋ฐํ์ง ์๋์ง, ๋ถ๋ณ์ฑ์ ํด์น์ง ์๋์ง๋ฅผ ๊ณ์ ์์ํ๋ฉด์ ์์ ํ๊ฒ `getter()`๋ฅผ ์ฌ์ฉํด๋ณด๋ ค๊ณ ๋
ธ๋ ฅํ๊ณ ์์ต๋๋ค.
ํน์ ์๊ฒฝ๋์ `getter()`๋ฅผ ์ฌ์ฉํ๋ ๊ฒ ๋ณด๋ค UI ๋ก์ง์ ๋๋ฉ์ธ ๋ด๋ถ์์ ๊ตฌํํ๋ ๊ฒ ์ด ๋ ํฉ๋ฆฌ์ ์ด๋ผ๊ณ ์๊ฐํ์
๋ ๋ค๋ฅธ ์ด์ ๊ฐ ์๋์? ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,37 @@
+package racingcar.view;
+
+import camp.nextstep.edu.missionutils.Console;
+import racingcar.model.AttemptCount;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class InputView {
+
+ private static final String SPLIT_REGEX = ",";
+ private static final String NON_NUMERIC_INPUT_MESSAGE = "์ซ์๋ง ์
๋ ฅํ ์ ์์ต๋๋ค.";
+ private static final String REQUEST_ATTEMPT_COUNT_MESSAGE = "์๋ํ ํ์๋ ๋ชํ์ธ๊ฐ์?";
+
+ public List<String> readNames() {
+ String names = Console.readLine();
+
+ return Arrays.stream(names.split(SPLIT_REGEX))
+ .collect(Collectors.toList());
+ }
+
+ public AttemptCount readAttemptCount() {
+ System.out.println(REQUEST_ATTEMPT_COUNT_MESSAGE);
+ String attemptCount = Console.readLine();
+
+ return AttemptCount.from(parseInt(attemptCount));
+ }
+
+ private int parseInt(String input) {
+ try {
+ return Integer.parseInt(input);
+ } catch (NumberFormatException error) {
+ throw new IllegalArgumentException(NON_NUMERIC_INPUT_MESSAGE);
+ }
+ }
+} | Java | ์ ๊ฐ ์๊ฐํ์ ๋ ์์๋ฅผ ์ฌ์ฉํ๋ ์ด์ ์๋
- ํด๋์ค ๊ฐ, ๋ฉ์๋ ๊ฐ ๊ฐ์ ์ฌ์ฌ์ฉํ๋ ์ธก๋ฉด๋ ์์ง๋ง
- **์ธ์คํด์ค ๊ฐ์ ๊ฐ์ ๊ณต์ **ํ๋ ์ธก๋ฉด๋ ์๋ค๊ณ ์๊ณ ์์ต๋๋ค!
- (์ด๋ฆ์ ์ง์ด์ค์ผ๋ก์จ ๊ฐ๋
์ฑ์ ๊ณ ๋ คํ๋ ์ธก๋ฉด๋ ์๊ตฌ์!)
`InputView` ์์ ์ฌ์ฉ๋๋ ์๋ฌ ๋ฉ์์ง๋ค ๊ฐ์ ๊ฒฝ์ฐ ์ธ์คํด์ค์ ์ํ์ ๋ฌด๊ดํ๊ฒ ํญ์ ๊ฐ์ด ๊ณ ์ ๋์ด ์์ต๋๋ค.
์ด๋ฐ ๊ฒฝ์ฐ ํ๋์ฝ๋ฉ๋ ๊ฐ์ `static final` ๋ก ์ ์ธํด์ฃผ๋ฉด ํ๋ก๊ทธ๋จ ์คํ ๋จ๊ณ์์ ๋ฑ ํ๋ฒ๋ง ์ด๊ธฐํ๋๊ธฐ ๋๋ฌธ์
์๋ก ๋ค๋ฅธ ์ธ์คํด์ค๋ฅผ ์ฌ๋ฌ ๋ฒ ํธ์ถํ๋๋ผ๋ ์ธ์คํด์ค์ ํจ๊ป ๋งค๋ฒ ์ด๊ธฐํํด ์ค ํ์๊ฐ ์์ด์ง๊ฒ ๋ฉ๋๋ค.
๊ทธ๋์ ๋ฉ๋ชจ๋ฆฌ ์ ์ผ๋ก๋ ๋ ํจ์จ์ ์ด๋ผ๊ณ ์๊ฐํด์!
(์ซ์ ์ผ๊ตฌ ํผ๋๋ฐฑ ์์์์ ํด๋์ค ๋ณ์์ ์ธ์คํด์ค ๋ณ์์ ์ฐจ์ด๋ฅผ ๋ ์ฌ๋ฆฌ์๋ฉด ๋ ๊ฒ ๊ฐ์์!!)
์ด๋ฌํ ์ด์ ์์ ์ ๋ ํ๋์ฝ๋ฉ๋ ๊ฐ์ ๋ฌด์กฐ๊ฑด ์์ ์ฒ๋ฆฌ ํด๋ฒ๋ฆฌ์! ๋ผ๋ ์ฃผ์๋ก ํ๋ก๊ทธ๋๋ฐ ํ๊ณ ์์ต๋๋ค.
์๋์ฐจ ๊ฒฝ์ฃผ ๋ฏธ์
์์๋ `InputView` ๊ฐ ์ฌ๋ฌ๋ฒ ์ด๊ธฐํ ๋ ๊ฐ๋ฅ์ฑ์ด ๊ฑฐ์ ์์ง๋ง ๊ทธ๋ ๋ค ํ๋๋ผ๋
ํ๋์ฝ๋ฉ ๋ ๊ฐ์ ์ต๋ํ ์์๋ก ์ฒ๋ฆฌํ๋ ์ต๊ด์ ๋ค์ด๋ ค๊ณ ๋
ธ๋ ฅ์ด๋ผ๊ณ ์ดํดํด์ฃผ์๋ฉด ๋ ๊ฒ ๊ฐ์์!
ํน์ฌ๋ ๊ท๋ชจ๊ฐ ์ปค์ง๋ค๋ฉด `InputView` ํด๋์ค๋ฅผ ์ธ๋ถ ํด๋์ค๋ค๋ก ๋๋๋ ๊ฒ๋ ๊ด์ฐฎ์ ๋ฐฉ๋ฒ์ผ ๊ฒ ๊ฐ์์!
๊ทธ๋ ๊ฒ ๋๋ฉด ํด๋์ค๊ฐ ๊ด๋ จ๋ ์์๋ฅผ ํจ๊ป ๊ด๋ฆฌํ ์ ์๊ธฐ ๋๋ฌธ์ ์ ์ง๋ณด์ ์ธก๋ฉด์์๋ ๋ ์ ๋ฆฌํ๋ค๊ณ ์๊ฐํฉ๋๋ค! |
@@ -0,0 +1,37 @@
+package racingcar.view;
+
+import camp.nextstep.edu.missionutils.Console;
+import racingcar.model.AttemptCount;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class InputView {
+
+ private static final String SPLIT_REGEX = ",";
+ private static final String NON_NUMERIC_INPUT_MESSAGE = "์ซ์๋ง ์
๋ ฅํ ์ ์์ต๋๋ค.";
+ private static final String REQUEST_ATTEMPT_COUNT_MESSAGE = "์๋ํ ํ์๋ ๋ชํ์ธ๊ฐ์?";
+
+ public List<String> readNames() {
+ String names = Console.readLine();
+
+ return Arrays.stream(names.split(SPLIT_REGEX))
+ .collect(Collectors.toList());
+ }
+
+ public AttemptCount readAttemptCount() {
+ System.out.println(REQUEST_ATTEMPT_COUNT_MESSAGE);
+ String attemptCount = Console.readLine();
+
+ return AttemptCount.from(parseInt(attemptCount));
+ }
+
+ private int parseInt(String input) {
+ try {
+ return Integer.parseInt(input);
+ } catch (NumberFormatException error) {
+ throw new IllegalArgumentException(NON_NUMERIC_INPUT_MESSAGE);
+ }
+ }
+} | Java | ์คํธ ์ข์ ์ต๊ด์ธ ๊ฒ ๊ฐ์์ ใ
ใ
ํ๋์ฝ๋ฉ๋ ๊ฐ์ ์ฌ์ฉํ์ง ์๋ค๊ฐ ์ฌ์ฉํ๋ ๊ฑด ์ผ์ผ์ด ์์ํ๊ธฐ ํ๋ค์ง๋ง, ์์๋ฅผ ์ฌ์ฉํ๋ ์์ผ๋ก ์ฝ๋ฉํ๋ค๊ฐ ๊ฐ์ ์ง์ ์ฐ๋ ๋ฐฉ์์ผ๋ก ์ ํํ๊ธฐ ์ฌ์ธ ํ
๋๊น์
์ด๋ฌํ ํผ๋๋ฐฑ์ ๋๋ฆฐ ์ด์ ๋ ํ๋ฆฌ์ฝ์ค ์ธ์ ์ธํดํ๋ ํ์ฌ์์ `์์ธ ๋ฉ์ธ์ง์ ์์ ๊ฐ์ ์ง์ ๋ค ๋ฐ์ ๊ฒฝ์ฐ ์์๋ฅผ ๋ฌด๋ถ๋ณํ๊ฒ ๋ง์ด ์ฌ์ฉํ๊ฒ ๋ผ์ ์ฝ๋๋ฅผ ์ฝ์ ๋ ์ ์ ๋ฌด์์ด ์ค์ํ์ง ํ๋ณํ๊ธฐ ํ๋ค์ด์ง๊ณ ์ฝ๋๊ฐ ๊ธธ์ด์ง๋ค๋ ๋ฌธ์ ์ ์ด ๋ฐ์ํ๋ค` ๋ผ๋ ํผ๋๋ฐฑ์ ๋ฐ์๊ธฐ ๋๋ฌธ์ธ๋ฐ์
๋ง์ํ์ ๊ฒ์ฒ๋ผ ์ฐ์ต์ ๊ณผ์ ์ด๋ผ๋ฉด ์คํ๋ ค ์ข์ ์ ํธ๊ฒ ๋ค์! ๐ |
@@ -0,0 +1,44 @@
+package racingcar.model;
+
+import org.junit.jupiter.api.DisplayNameGeneration;
+import org.junit.jupiter.api.DisplayNameGenerator;
+import org.junit.jupiter.api.Test;
+import racingcar.RacingNumberGenerator;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.util.Lists.newArrayList;
+
+@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
+public class CarsTest {
+
+ @Test
+ void ์๋์ฐจ๋ค์_์ด๋ฆ์_์๋ก_์ค๋ณต๋ _์_์๋ค() {
+ assertThatIllegalArgumentException()
+ .isThrownBy(() -> Cars.from(List.of("pobi,jun,pobi")));
+ }
+
+ @Test
+ void ๊ฐ์ฅ_๋ฉ๋ฆฌ_์ด๋ํ_์ฌ๋ฆผ์ด_์ฐ์นํ๋ค() {
+ Cars cars = Cars.from(List.of("pobi", "jun", "khj"));
+ TestRacingNumberGenerator numberGenerator = new TestRacingNumberGenerator();
+
+ cars.race(numberGenerator);
+ cars.race(numberGenerator);
+
+ assertThat(cars.findWinners())
+ .contains(CarName.from("pobi"), CarName.from("khj"));
+ }
+
+ static class TestRacingNumberGenerator implements RacingNumberGenerator {
+
+ private final List<Integer> numbers = newArrayList(4, 1, 4, 4, 1, 4);
+
+ @Override
+ public int generate() {
+ return numbers.remove(0);
+ }
+ }
+} | Java | ๊ทธ๋ ๊ตฐ์! ์ฌ์ค ์ด ๋ถ๋ถ์ ์ทจํฅ์ด๋ผ ๋ ๊ฐ๋จํ๊ฒ ํํ์ด ๊ฐ๋ฅํ ๊ฒ ๊ฐ์์ ๋ง์๋๋ฆฐ ๋ถ๋ถ์ด์์ต๋๋ค ใ
ใ
์ ๊ฐ์ ๊ฒฝ์ฐ์๋ js๋ง ์ฌ์ฉํ๋ค๊ฐ ์ด๋ฒ ํ๋ฆฌ์ฝ์ค์์ ์๋ฐ๋ฅผ ์์ํ๋๋ฐ ์คํ๋ ค ๋ฉ์๋์ ์ฌ๊ณ ํ๋๊ฒ ํ๋ค๋๋ผ๊ตฌ์ ์์ด๋ ๊ฒ ์ง์ ์ ๋๋ ๋ถ๋ถ๋ค์ด ๋ง์์ง...ใ
ใ
์ฒจ๋ถํด์ฃผ์ ๋ด์ฉ์ ๋ํด ๋ง๋ถ์ด์๋ฉด ์ ์ค๋ช
์์ ๋งํ๊ณ ์๋๊ฑด ํจ์ํ ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ฉด ์ธ์๋ก ํจ์๋ฅผ ๋๊ธธ ์ ์๊ฒ ๋๋ค๋ ๊ฑธ ๋งํ๋ ๊ฒ ๊ฐ์ต๋๋ค. (js์ฒ๋ผ์!) ์ด๋ฐ ํน์ง์ `์ผ๊ธํจ์`๋ผ๊ณ ๋ถ๋ฅด๋๋ฐ์, ์ด ์ผ๊ธํจ์๋ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ ์ํํ๊ธฐ ์ํ ์ต์ํ์ ์กฐ๊ฑด์ด ๋ฉ๋๋ค.
๋ฐ๋ผ์, ํจ์ํ ์ธํฐํ์ด์ค๋ฅผ ๋๊ธฐ๋ฉด ์ฌ์ค์ ์ต๋ช
ํด๋์ค์ง๋ง ํจ์๋ฅผ ๋๊ธฐ๋ ๊ฒ์ฒ๋ผ ๋ง๋ค์ด ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์ ํ๋ด๋ด๋ ๋ฐฉ์์ ์ฝ๋๋ฅผ ์งค ์ ์๋ค! ๋ผ๋๊ฑฐ์ฃ |
@@ -0,0 +1,57 @@
+package racingcar.view;
+
+import racingcar.model.Car;
+import racingcar.model.CarName;
+import racingcar.model.Cars;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class OutputView {
+
+ private static final String NEW_LINE = "\n";
+ private static final String SCORE_UNIT = "-";
+ private static final String SCORE_BOARD = "%s : %s";
+ private static final String WINNER_SEPARATOR = ", ";
+ private static final String WINNERS_ARE = "์ต์ข
์ฐ์น์ : ";
+ private static final String ERROR_PREFIX = "[ERROR] ";
+ private static final String RESULT_INTRO = "์คํ ๊ฒฐ๊ณผ";
+
+ public void printResult(Cars cars) {
+ System.out.println(getScore(cars));
+ }
+
+ private String getScore(Cars cars) {
+ return cars.get().stream()
+ .map(this::toScoreBoard)
+ .collect(Collectors.joining(NEW_LINE));
+ }
+
+ private String toScoreBoard(Car car) {
+ return String.format(SCORE_BOARD, car.getName(), convertToScore(car.getPosition()));
+ }
+
+ private String convertToScore(int position) {
+ return SCORE_UNIT.repeat(position);
+ }
+
+ public void printWinners(List<CarName> winners) {
+ System.out.print(WINNERS_ARE);
+ System.out.println(getWinnerNames(winners));
+ }
+
+ private String getWinnerNames(List<CarName> winners) {
+ return winners.stream()
+ .map(CarName::get)
+ .collect(Collectors.joining(WINNER_SEPARATOR));
+ }
+
+ public void printError(IllegalArgumentException error) {
+ System.out.print(ERROR_PREFIX);
+ System.out.println(error.getMessage());
+ }
+
+ public void printRaceIntro() {
+ System.out.println(RESULT_INTRO);
+ }
+} | Java | ์ ์ฐ์ ์ ์ ๋ ๋ฉ์ธ์ง๋ฅผ ๋ง๋๋ ๋ถ๋ถ ์์ฒด๋ฅผ UI ๋ก์ง์ด๋ผ๊ณ ์๊ฐํ๊ธฐ ๋ณด๋ค๋ ๋๋ฉ์ธ์์ ํด๊ฒฐ ๊ฐ๋ฅํ ์์ญ์ด๋ผ๊ณ ์๊ฐํ์ต๋๋ค. ์๋ํ๋ฉด ์ด๋ฏธ `Car`์์ ํด๋น ์ํ์ ๋ํด ๋ชจ๋ ๊ฑธ ์๊ณ ์๋๋ฐ ์ด๋ฅผ ๊ตณ์ด ๋ฐ์ผ๋ก ๊บผ๋ธ๋ค๋ฉด ์บก์ํ๊ฐ ๊นจ์ง๋ค๊ณ ์๊ฐํ๊ฑฐ๋ ์. ๊ทธ๋ฐ๋ฐ ๋ง์ํด์ฃผ์ ๋ด์ฉ์ ์ฝ์ด๋ณด๋ ์ด๋ฅผ UI๋ผ๊ณ ์๊ฐํ๋ฉด ์ ์ด์ ๋ค๋ฅธ ์์ญ์ผ๋ก ๋์ ธ์ฃผ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๊ทธ๋๋ก ๊บผ๋ธ๋ค๊ณ ํด์ ์บก์ํ๊ฐ ๊นจ์ง๋ ๊ฑด ์๋๊ฐ? ํ๋ ์๊ฐ๋ ๋ค๊ณ ์ด๋ ต๋ค์ ใ
ใ
์ฌ์ค ์ ๋ `getter`๋ฅผ ๋ฌด์กฐ๊ฑด์ ์ผ๋ก ๋ฐฐ์ ํ๋ ๊ฒ์ ์ณ์ง ์๋ค๊ณ ์๊ฐํ๋ ์ฃผ์๊ธฐ๋ ํ๊ตฌ์! ๋ค ํ์ํ๋๊น ์ฐ๋ ๊ฒ์ธ๋ฐ ํ๋ ์๊ฐ๋ ๋ค๊ณ ์ :D ๋จ๊ฒจ์ฃผ์ ์๊ฒฌ ๋ชจ๋ ์ฆ๊ฑฐ์ด ์ ๋ค์์ต๋๋ค ใ
ใ
ใ
๋ค์ ์๊ฐํด๋ณผ ์ ์๋ ๊ณ๊ธฐ๋ ๋์๊ตฌ์ |
@@ -0,0 +1,57 @@
+package racingcar.view;
+
+import racingcar.model.Car;
+import racingcar.model.CarName;
+import racingcar.model.Cars;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class OutputView {
+
+ private static final String NEW_LINE = "\n";
+ private static final String SCORE_UNIT = "-";
+ private static final String SCORE_BOARD = "%s : %s";
+ private static final String WINNER_SEPARATOR = ", ";
+ private static final String WINNERS_ARE = "์ต์ข
์ฐ์น์ : ";
+ private static final String ERROR_PREFIX = "[ERROR] ";
+ private static final String RESULT_INTRO = "์คํ ๊ฒฐ๊ณผ";
+
+ public void printResult(Cars cars) {
+ System.out.println(getScore(cars));
+ }
+
+ private String getScore(Cars cars) {
+ return cars.get().stream()
+ .map(this::toScoreBoard)
+ .collect(Collectors.joining(NEW_LINE));
+ }
+
+ private String toScoreBoard(Car car) {
+ return String.format(SCORE_BOARD, car.getName(), convertToScore(car.getPosition()));
+ }
+
+ private String convertToScore(int position) {
+ return SCORE_UNIT.repeat(position);
+ }
+
+ public void printWinners(List<CarName> winners) {
+ System.out.print(WINNERS_ARE);
+ System.out.println(getWinnerNames(winners));
+ }
+
+ private String getWinnerNames(List<CarName> winners) {
+ return winners.stream()
+ .map(CarName::get)
+ .collect(Collectors.joining(WINNER_SEPARATOR));
+ }
+
+ public void printError(IllegalArgumentException error) {
+ System.out.print(ERROR_PREFIX);
+ System.out.println(error.getMessage());
+ }
+
+ public void printRaceIntro() {
+ System.out.println(RESULT_INTRO);
+ }
+} | Java | ์์์ฒ๋ฆฌ๊ฐ ์ ๋ง ๊น๋ํ๋ค์!
ํนํ SCORE_BOARD ๋ถ๋ถ ๋ฐฐ์๊ฐ๋๋ค. |
@@ -0,0 +1,57 @@
+package racingcar.view;
+
+import racingcar.model.Car;
+import racingcar.model.CarName;
+import racingcar.model.Cars;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class OutputView {
+
+ private static final String NEW_LINE = "\n";
+ private static final String SCORE_UNIT = "-";
+ private static final String SCORE_BOARD = "%s : %s";
+ private static final String WINNER_SEPARATOR = ", ";
+ private static final String WINNERS_ARE = "์ต์ข
์ฐ์น์ : ";
+ private static final String ERROR_PREFIX = "[ERROR] ";
+ private static final String RESULT_INTRO = "์คํ ๊ฒฐ๊ณผ";
+
+ public void printResult(Cars cars) {
+ System.out.println(getScore(cars));
+ }
+
+ private String getScore(Cars cars) {
+ return cars.get().stream()
+ .map(this::toScoreBoard)
+ .collect(Collectors.joining(NEW_LINE));
+ }
+
+ private String toScoreBoard(Car car) {
+ return String.format(SCORE_BOARD, car.getName(), convertToScore(car.getPosition()));
+ }
+
+ private String convertToScore(int position) {
+ return SCORE_UNIT.repeat(position);
+ }
+
+ public void printWinners(List<CarName> winners) {
+ System.out.print(WINNERS_ARE);
+ System.out.println(getWinnerNames(winners));
+ }
+
+ private String getWinnerNames(List<CarName> winners) {
+ return winners.stream()
+ .map(CarName::get)
+ .collect(Collectors.joining(WINNER_SEPARATOR));
+ }
+
+ public void printError(IllegalArgumentException error) {
+ System.out.print(ERROR_PREFIX);
+ System.out.println(error.getMessage());
+ }
+
+ public void printRaceIntro() {
+ System.out.println(RESULT_INTRO);
+ }
+} | Java | ์ ๋ @khj1 ๋๊ณผ ๊ฐ์ ์ด์ ๋ก getter๋ฅผ ์ฌ์ฉํ๋๋ฐ์,
๊ฐ์ธ์ ์ผ๋ก๋ `์ถ๋ ฅ์ ์ํ getter๋ ์บก์ํ๋ฅผ ์๋ฐฐํ๋ ๊ฒ ์๋๋ค` ๋ผ๊ณ ์๊ฐํฉ๋๋ค.
๋ฌผ๋ก ์๋์ ๋ค๋ฅด๊ฒ ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ ์บก์ํ๋ฅผ ์ ํด์ํฌ ์ ์์ต๋๋ค. ์ด ๋ถ๋ถ์ ์ด์ฉ ์ ์๋ ๋จ์ ์ด๋ผ๊ณ ์๊ฐํด์.
๊ฐ์ธ์ ์ผ๋ก ํ๋ฆฌ์ฝ์ค ๊ธฐ๊ฐ ๋์ '๊ฐ์ฒด์ ์์จ์ฑ์ ์ผ๋ง๋ ํ์ฉํด์ค์ผ ํ๋์ง' ๋ฅผ ๊ณ์ ๊ณ ๋ฏผํ๋๋ฐ, ์ ๊ฐ ๋ค๋ค๋ฅธ ๊ฒฐ๋ก ์ '๊ฐ์ฒด๋ UI ๋ก์ง ์ธ์ ๊ฒฝ์ฐ์์ ์์จ์ฑ์ ๋ณด์ฅํด์ค์ผ ํ๋ค' ์์ต๋๋ค.
๊ฐ์ฒด์งํฅ์ ์ฌ์ฉํ๋ ์ด์ ๋ ๊ฒฐ๊ตญ '์์จ์ฑ'๋ณด๋ค๋ '์ ์ฐ์ฑ'์ด๋๊น์! UI ๋ก์ง์ ๋ณ๊ฒฝ์ด ๋๋ฉ์ธ ์์ญ์ ์นจ๋ฒํ๋ ๊ฒ์ ์๋๋ค๊ณ ํ๋จํ์ต๋๋ค.
์ ๋ ์ด๋ ๊ฒ ์๊ฐํ๋๋ฐ,
@JerryK026 ๋ ๋ง์ ๋ค์ด๋ณด๋ ์ด ๋ถ๋ถ์ ์คํ์ผ์ ์ฐจ์ด์ผ ์๋ ์๊ฒ ๋ค์!
์
๋ฌด ํ๊ฒฝ์ด๋ ํ์ ์ฝ๋ ์คํ์ผ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง ๊ฒ ๊ฐ๊ธฐ๋ ํฉ๋๋ค. ํ์คํ ์ด ๋ถ์ผ์ ์ ๋ต์ ์๋ ๊ฒ ๊ฐ๋ค์.. ใ
ใ
|
@@ -0,0 +1,57 @@
+package racingcar.view;
+
+import racingcar.model.Car;
+import racingcar.model.CarName;
+import racingcar.model.Cars;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class OutputView {
+
+ private static final String NEW_LINE = "\n";
+ private static final String SCORE_UNIT = "-";
+ private static final String SCORE_BOARD = "%s : %s";
+ private static final String WINNER_SEPARATOR = ", ";
+ private static final String WINNERS_ARE = "์ต์ข
์ฐ์น์ : ";
+ private static final String ERROR_PREFIX = "[ERROR] ";
+ private static final String RESULT_INTRO = "์คํ ๊ฒฐ๊ณผ";
+
+ public void printResult(Cars cars) {
+ System.out.println(getScore(cars));
+ }
+
+ private String getScore(Cars cars) {
+ return cars.get().stream()
+ .map(this::toScoreBoard)
+ .collect(Collectors.joining(NEW_LINE));
+ }
+
+ private String toScoreBoard(Car car) {
+ return String.format(SCORE_BOARD, car.getName(), convertToScore(car.getPosition()));
+ }
+
+ private String convertToScore(int position) {
+ return SCORE_UNIT.repeat(position);
+ }
+
+ public void printWinners(List<CarName> winners) {
+ System.out.print(WINNERS_ARE);
+ System.out.println(getWinnerNames(winners));
+ }
+
+ private String getWinnerNames(List<CarName> winners) {
+ return winners.stream()
+ .map(CarName::get)
+ .collect(Collectors.joining(WINNER_SEPARATOR));
+ }
+
+ public void printError(IllegalArgumentException error) {
+ System.out.print(ERROR_PREFIX);
+ System.out.println(error.getMessage());
+ }
+
+ public void printRaceIntro() {
+ System.out.println(RESULT_INTRO);
+ }
+} | Java | ์ ๋ ๊ฐ์ธ์ ์ผ๋ก MVC ์์ View๋ ์ปจํธ๋กค๋ฌ๋ก๋ถํฐ ๋์ ์ธ ์ ๋ณด๋ง ๋ฐ์์ผ ํ๋ค๊ณ ์๊ฐํ๋๋ฐ์! ํน์ ์ด๋ฐ ์ ๊ทผ ๋ฐฉ๋ฒ์ ์ด๋ ์ค๊น์?
์ ๊ฐ ์ด๋ ๊ฒ ์๊ฐํ๋ ์ด์ ๋, ๊ด์ฌ์ฌ๊ฐ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์
๋๋ค.
์๋ฌ ์ฒ๋ฆฌ์ ๊ดํ ์ถ๋ ฅ์ ํด๋น try-catch ๋ธ๋ก์ ์ฑ
์์ด ์๋ค๊ณ ์๊ฐํด์.
์ฌ์ค ์ ๋ต์ ์๋ ๊ฒ ๊ฐ์ผ๋, ์ ๊ฐ ๊ณ ์ํ๋ ๋ฐฉ๋ฒ์ ์ด๋ค์ง ์ ์๋๋ ค ๋ด
๋๋ค. |
@@ -0,0 +1,47 @@
+package racingcar.model;
+
+public class Car implements Comparable<Car> {
+ private static final int MOVABLE_LOWER_BOUND = 4;
+
+ private final CarName name;
+ private final Position position = Position.init();
+
+ private Car(String name) {
+ this.name = CarName.from(name);
+ }
+
+ public static Car from(String name) {
+ return new Car(name);
+ }
+
+ public void move(int command) {
+ if (isMovable(command)) {
+ position.increase();
+ }
+ }
+
+ private static boolean isMovable(int command) {
+ return command >= MOVABLE_LOWER_BOUND;
+ }
+
+ public boolean isAt(Position expectedPosition) {
+ return position.equals(expectedPosition);
+ }
+
+ public int getPosition() {
+ return position.getPosition();
+ }
+
+ public CarName getName() {
+ return name;
+ }
+
+ public boolean isSamePosition(Car other) {
+ return this.position.equals(other.position);
+ }
+
+ @Override
+ public int compareTo(Car other) {
+ return this.position.compareTo(other.position);
+ }
+} | Java | ์ ๋ ๋ฐฐ์๊ฐ๋๋ค!! ์ด๋ฐ ์์ผ๋ก ๋น๊ต๋ ๊ฐ๋ฅํ๊ตฐ์.. |
@@ -0,0 +1,52 @@
+package racingcar.model;
+
+import java.util.Objects;
+
+public class CarName {
+
+ private static final String INVALID_NAME_LENGTH = "์๋์ฐจ ์ด๋ฆ์ 5์๋ฅผ ์ด๊ณผํ ์ ์์ต๋๋ค.";
+ private static final int NAME_LENGTH_UPPER_BOUND = 5;
+
+ private final String name;
+
+ public CarName(String name) {
+ validate(name);
+ this.name = name;
+ }
+
+ private void validate(String name) {
+ if (isInvalidLength(name)) {
+ throw new IllegalArgumentException(INVALID_NAME_LENGTH);
+ }
+ }
+
+ private boolean isInvalidLength(String name) {
+ return name.length() > NAME_LENGTH_UPPER_BOUND;
+ }
+
+ public static CarName from(String name) {
+ return new CarName(name);
+ }
+
+ public String get() {
+ return name;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ CarName carName = (CarName) o;
+ return Objects.equals(name, carName.name);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(name);
+ }
+
+ @Override
+ public String toString() {
+ return name;
+ }
+} | Java | hashCode๊ฐ ์ฌ์ฉ๋์ง ์๋ ๊ฒ ๊ฐ์, ๋ฐ๋ก ์ค๋ฒ๋ผ์ด๋ฉ ํ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,52 @@
+package racingcar.model;
+
+import java.util.Objects;
+
+public class CarName {
+
+ private static final String INVALID_NAME_LENGTH = "์๋์ฐจ ์ด๋ฆ์ 5์๋ฅผ ์ด๊ณผํ ์ ์์ต๋๋ค.";
+ private static final int NAME_LENGTH_UPPER_BOUND = 5;
+
+ private final String name;
+
+ public CarName(String name) {
+ validate(name);
+ this.name = name;
+ }
+
+ private void validate(String name) {
+ if (isInvalidLength(name)) {
+ throw new IllegalArgumentException(INVALID_NAME_LENGTH);
+ }
+ }
+
+ private boolean isInvalidLength(String name) {
+ return name.length() > NAME_LENGTH_UPPER_BOUND;
+ }
+
+ public static CarName from(String name) {
+ return new CarName(name);
+ }
+
+ public String get() {
+ return name;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ CarName carName = (CarName) o;
+ return Objects.equals(name, carName.name);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(name);
+ }
+
+ @Override
+ public String toString() {
+ return name;
+ }
+} | Java | get ๋ฉ์๋๊ฐ toString๊ณผ ์ค๋ณต๋๋ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,63 @@
+package racingcar.cotroller;
+
+import racingcar.RandomRacingNumberGenerator;
+import racingcar.model.AttemptCount;
+import racingcar.model.Cars;
+import racingcar.view.InputView;
+import racingcar.view.OutputView;
+
+import java.util.List;
+import java.util.function.Supplier;
+
+public class RacingCarController {
+
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final Cars cars;
+
+ public RacingCarController() {
+ inputView = new InputView();
+ outputView = new OutputView();
+ cars = checkError(this::getCars);
+ }
+
+ public void run() {
+ progressGame();
+ printWinners();
+ }
+
+ private void progressGame() {
+ AttemptCount attemptCount = checkError(inputView::readAttemptCount);
+
+ outputView.printRaceIntro();
+ while (attemptCount.isPlayable()) {
+ attempt();
+ attemptCount.decrease();
+ }
+ }
+
+ private void printWinners() {
+ outputView.printWinners(cars.findWinners());
+ }
+
+ private void attempt() {
+ RandomRacingNumberGenerator numberGenerator = new RandomRacingNumberGenerator();
+ cars.race(numberGenerator);
+
+ outputView.printResult(cars);
+ }
+
+ private Cars getCars() {
+ List<String> names = inputView.readNames();
+ return Cars.from(names);
+ }
+
+ private <T> T checkError(Supplier<T> reader) {
+ try {
+ return reader.get();
+ } catch (IllegalArgumentException error) {
+ outputView.printError(error);
+ return checkError(reader);
+ }
+ }
+} | Java | ๋ฐ๋ก ์ปจํธ๋กค๋ฌ์์ RandomRacingNumberGenerator๋ฅผ ์ฃผ์
ํด์ฃผ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค.
์ ์๊ฐ์๋, RandomRacingNumberGenerator๋ฅผ race ๋ฉ์๋ ๋ด์์ ์ ์ธํ๋ฉด ์ปจํธ๋กค๋ฌ์์ ์์์ผ ํ ๊ด์ฌ์ฌ๊ฐ ํ๋ ์ค์ด๋๋ ์ด์ ์ด ์์ ๊ฒ ๊ฐ์์์! |
@@ -1,29 +1,51 @@
package lotto.domain;
-import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
+import java.util.stream.Collectors;
public class Lotto {
- private List<Integer> lotto;
+ private List<LottoNumber> lotto;
- public Lotto(List<Integer> list) {
- lotto = new ArrayList<>();
- lotto = list;
+ public Lotto(List<LottoNumber> lotto) {
+ Collections.sort(lotto);
+ this.lotto = lotto;
+ }
+
+ public Lotto() {
}
- public List<Integer> getLotto() {
+ public List<LottoNumber> getLotto() {
return lotto;
}
- public void setLotto(List<Integer> lotto) {
+ public void setLotto(List<LottoNumber> lotto) {
this.lotto = lotto;
}
- public boolean isContain(int number) {
- return lotto.contains(number);
+ public int countMatch(List<LottoNumber> winningNumber) {
+ int count = 0;
+ for (LottoNumber lottoNumber : winningNumber) {
+ count = increaseCount(lottoNumber, count);
+ }
+ return count;
+ }
+
+ public int increaseCount(LottoNumber lottoNumber, int count) {
+ if (this.hasThisNumber(lottoNumber)) {
+ count++;
+ }
+ return count;
+ }
+
+ public boolean hasThisNumber(LottoNumber number) {
+ return lotto.stream()
+ .filter(lottoNo -> lottoNo.equals(number)).count() != 0;
}
public String toString() {
- return lotto.toString();
+ return lotto.stream()
+ .map(LottoNumber::toString)
+ .collect(Collectors.joining(", ","[","]"));
}
-}
+}
\ No newline at end of file | Java | ๋ญ ํฌํจํ๊ณ ์๋ค๋๊ฑฐ์ฃ ? |
@@ -1,29 +1,51 @@
package lotto.domain;
-import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
+import java.util.stream.Collectors;
public class Lotto {
- private List<Integer> lotto;
+ private List<LottoNumber> lotto;
- public Lotto(List<Integer> list) {
- lotto = new ArrayList<>();
- lotto = list;
+ public Lotto(List<LottoNumber> lotto) {
+ Collections.sort(lotto);
+ this.lotto = lotto;
+ }
+
+ public Lotto() {
}
- public List<Integer> getLotto() {
+ public List<LottoNumber> getLotto() {
return lotto;
}
- public void setLotto(List<Integer> lotto) {
+ public void setLotto(List<LottoNumber> lotto) {
this.lotto = lotto;
}
- public boolean isContain(int number) {
- return lotto.contains(number);
+ public int countMatch(List<LottoNumber> winningNumber) {
+ int count = 0;
+ for (LottoNumber lottoNumber : winningNumber) {
+ count = increaseCount(lottoNumber, count);
+ }
+ return count;
+ }
+
+ public int increaseCount(LottoNumber lottoNumber, int count) {
+ if (this.hasThisNumber(lottoNumber)) {
+ count++;
+ }
+ return count;
+ }
+
+ public boolean hasThisNumber(LottoNumber number) {
+ return lotto.stream()
+ .filter(lottoNo -> lottoNo.equals(number)).count() != 0;
}
public String toString() {
- return lotto.toString();
+ return lotto.stream()
+ .map(LottoNumber::toString)
+ .collect(Collectors.joining(", ","[","]"));
}
-}
+}
\ No newline at end of file | Java | Builder ํจํด๊ณผ ๋ฉ์๋ ์ฒด์ด๋ ๊ณต๋ถํ์๊ณ stringBuilder ๋ค์ ์จ๋ณด์ธ์ |
@@ -1,29 +1,51 @@
package lotto.domain;
-import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
+import java.util.stream.Collectors;
public class Lotto {
- private List<Integer> lotto;
+ private List<LottoNumber> lotto;
- public Lotto(List<Integer> list) {
- lotto = new ArrayList<>();
- lotto = list;
+ public Lotto(List<LottoNumber> lotto) {
+ Collections.sort(lotto);
+ this.lotto = lotto;
+ }
+
+ public Lotto() {
}
- public List<Integer> getLotto() {
+ public List<LottoNumber> getLotto() {
return lotto;
}
- public void setLotto(List<Integer> lotto) {
+ public void setLotto(List<LottoNumber> lotto) {
this.lotto = lotto;
}
- public boolean isContain(int number) {
- return lotto.contains(number);
+ public int countMatch(List<LottoNumber> winningNumber) {
+ int count = 0;
+ for (LottoNumber lottoNumber : winningNumber) {
+ count = increaseCount(lottoNumber, count);
+ }
+ return count;
+ }
+
+ public int increaseCount(LottoNumber lottoNumber, int count) {
+ if (this.hasThisNumber(lottoNumber)) {
+ count++;
+ }
+ return count;
+ }
+
+ public boolean hasThisNumber(LottoNumber number) {
+ return lotto.stream()
+ .filter(lottoNo -> lottoNo.equals(number)).count() != 0;
}
public String toString() {
- return lotto.toString();
+ return lotto.stream()
+ .map(LottoNumber::toString)
+ .collect(Collectors.joining(", ","[","]"));
}
-}
+}
\ No newline at end of file | Java | ๊ทธ๋ฆฌ๊ณ ์ ๋ง toString์ ์ด๋ ๊ฒ ๊ตฌํํ์๊ฒ ์ต๋๊น? |
@@ -3,77 +3,78 @@
import lotto.utils.Splitter;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
public class LottoGame {
- private List<Lotto> lottos = new ArrayList<Lotto>();
+ private static final int LOTTO_PRICE = 1000;
+ private List<Lotto> lottos;
+ private List<WinningLotto> winningLottos;
+
+ public LottoGame() {
+ lottos = new ArrayList<Lotto>();
+ winningLottos = new ArrayList<WinningLotto>();
+ }
public List<Lotto> getLottos() {
return lottos;
}
- public void setLottos(List<Lotto> lottos) {
- this.lottos = lottos;
+ public List<WinningLotto> getWinningLottos() {
+ return winningLottos;
}
- public List<Lotto> purchase(int money) {
- for (int numbers = 0; numbers < changeUnit(money); numbers++) {
- List<Integer> randomNumber = generateRandomNumbers();
- Collections.sort(randomNumber);
- Lotto lotto = new Lotto(randomNumber);
- lottos.add(lotto);
+ public void purchaseManual(String[] continuousNumbers) {
+ for (String continuousNumber : continuousNumbers) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.manual(continuousNumber));
}
- return lottos;
}
- public static List<Integer> generateRandomNumbers() {
- List<Integer> randomNumbers = new ArrayList<Integer>();
- for (int number = 1; number <= 45; number++) {
- randomNumbers.add(number);
+ public void setWinningLottos(String continuousWinningNumbers, LottoNumber bonusNumber) {
+ List<LottoNumber> winningNumbers = Splitter.splitNumber(continuousWinningNumbers);
+ winningLottos = new ArrayList<>();
+ for (Lotto lotto : lottos) {
+ setWinningLotto(lotto, lotto.countMatch(winningNumbers), bonusNumber);
}
- Collections.shuffle(randomNumbers);
- return randomNumbers.subList(0, 6);
}
- public int[] saveLottoResult(String text) {
- List<Integer> winnerNumber = Splitter.splitNumber(text);
- int[] result = new int[NumberOfHits.values().length];
- for (int index = 0; index < lottos.size(); index++) {
- result[countMatch(winnerNumber, index)]++;
+ public void setWinningLotto(Lotto lotto, int count, LottoNumber bonusNumber) {
+ if (Rank.lookUpRank(count, lotto.hasThisNumber(bonusNumber)) != Rank.MISS) {
+ winningLottos.add(new WinningLotto(lotto, count, bonusNumber));
}
- return result;
}
- public int countMatch(List<Integer> winnerNumber, int index) {
- int count = 0;
- for (int number : winnerNumber) {
- count = increaseCount(number, index, count);
+ public List<Lotto> purchaseAuto(int money, int numberOfManual) {
+ for (int numbers = 0; numbers < numberOfAutoLotto(money, numberOfManual); numbers++) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.auto());
}
- return count;
+ return lottos;
}
- public int increaseCount(int number, int index, int count) {
- if (lottos.get(index).isContain(number)) {
- count++;
- }
- return count;
+ public int countSameRank(Rank rank) {
+ return (int) winningLottos.stream()
+ .filter(winningLotto -> winningLotto.findRank().equals(rank))
+ .count();
}
- public double calculateRate(int[] result, int outcome) {
- int income = calculateReward(result);
- return Math.round((double)income / outcome * 1000.0) / 10.0 ;
+ public double calculateRate(int outcome) {
+ return (double) calculateIncome() / outcome * 100.0;
}
- public int calculateReward(int[] result) {
- int total = 0;
- for (int index = 0; index < result.length; index++) {
- total += result[index] * NumberOfHits.values()[index].getReward();
+ public int calculateIncome() {
+ int income = 0;
+ for (WinningLotto winningLotto : winningLottos) {
+ income = winningLotto.findRank().plusReward(income);
}
- return total;
+ return income;
}
public int changeUnit(int totalPrice) {
- return totalPrice / 1000;
+ return totalPrice / LOTTO_PRICE;
+ }
+
+ public int numberOfAutoLotto(int money, int numberOfManual) {
+ return changeUnit(money) - numberOfManual;
}
-}
+}
\ No newline at end of file | Java | text ๋ณด๋ค ์ข์ ๋ค์ด๋ฐ์ด ์์ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -3,77 +3,78 @@
import lotto.utils.Splitter;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
public class LottoGame {
- private List<Lotto> lottos = new ArrayList<Lotto>();
+ private static final int LOTTO_PRICE = 1000;
+ private List<Lotto> lottos;
+ private List<WinningLotto> winningLottos;
+
+ public LottoGame() {
+ lottos = new ArrayList<Lotto>();
+ winningLottos = new ArrayList<WinningLotto>();
+ }
public List<Lotto> getLottos() {
return lottos;
}
- public void setLottos(List<Lotto> lottos) {
- this.lottos = lottos;
+ public List<WinningLotto> getWinningLottos() {
+ return winningLottos;
}
- public List<Lotto> purchase(int money) {
- for (int numbers = 0; numbers < changeUnit(money); numbers++) {
- List<Integer> randomNumber = generateRandomNumbers();
- Collections.sort(randomNumber);
- Lotto lotto = new Lotto(randomNumber);
- lottos.add(lotto);
+ public void purchaseManual(String[] continuousNumbers) {
+ for (String continuousNumber : continuousNumbers) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.manual(continuousNumber));
}
- return lottos;
}
- public static List<Integer> generateRandomNumbers() {
- List<Integer> randomNumbers = new ArrayList<Integer>();
- for (int number = 1; number <= 45; number++) {
- randomNumbers.add(number);
+ public void setWinningLottos(String continuousWinningNumbers, LottoNumber bonusNumber) {
+ List<LottoNumber> winningNumbers = Splitter.splitNumber(continuousWinningNumbers);
+ winningLottos = new ArrayList<>();
+ for (Lotto lotto : lottos) {
+ setWinningLotto(lotto, lotto.countMatch(winningNumbers), bonusNumber);
}
- Collections.shuffle(randomNumbers);
- return randomNumbers.subList(0, 6);
}
- public int[] saveLottoResult(String text) {
- List<Integer> winnerNumber = Splitter.splitNumber(text);
- int[] result = new int[NumberOfHits.values().length];
- for (int index = 0; index < lottos.size(); index++) {
- result[countMatch(winnerNumber, index)]++;
+ public void setWinningLotto(Lotto lotto, int count, LottoNumber bonusNumber) {
+ if (Rank.lookUpRank(count, lotto.hasThisNumber(bonusNumber)) != Rank.MISS) {
+ winningLottos.add(new WinningLotto(lotto, count, bonusNumber));
}
- return result;
}
- public int countMatch(List<Integer> winnerNumber, int index) {
- int count = 0;
- for (int number : winnerNumber) {
- count = increaseCount(number, index, count);
+ public List<Lotto> purchaseAuto(int money, int numberOfManual) {
+ for (int numbers = 0; numbers < numberOfAutoLotto(money, numberOfManual); numbers++) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.auto());
}
- return count;
+ return lottos;
}
- public int increaseCount(int number, int index, int count) {
- if (lottos.get(index).isContain(number)) {
- count++;
- }
- return count;
+ public int countSameRank(Rank rank) {
+ return (int) winningLottos.stream()
+ .filter(winningLotto -> winningLotto.findRank().equals(rank))
+ .count();
}
- public double calculateRate(int[] result, int outcome) {
- int income = calculateReward(result);
- return Math.round((double)income / outcome * 1000.0) / 10.0 ;
+ public double calculateRate(int outcome) {
+ return (double) calculateIncome() / outcome * 100.0;
}
- public int calculateReward(int[] result) {
- int total = 0;
- for (int index = 0; index < result.length; index++) {
- total += result[index] * NumberOfHits.values()[index].getReward();
+ public int calculateIncome() {
+ int income = 0;
+ for (WinningLotto winningLotto : winningLottos) {
+ income = winningLotto.findRank().plusReward(income);
}
- return total;
+ return income;
}
public int changeUnit(int totalPrice) {
- return totalPrice / 1000;
+ return totalPrice / LOTTO_PRICE;
+ }
+
+ public int numberOfAutoLotto(int money, int numberOfManual) {
+ return changeUnit(money) - numberOfManual;
}
-}
+}
\ No newline at end of file | Java | contrivedNumber ๋ณด๋ค ์ข์ ๋ค์ด๋ฐ์ด ์์ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -3,77 +3,78 @@
import lotto.utils.Splitter;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
public class LottoGame {
- private List<Lotto> lottos = new ArrayList<Lotto>();
+ private static final int LOTTO_PRICE = 1000;
+ private List<Lotto> lottos;
+ private List<WinningLotto> winningLottos;
+
+ public LottoGame() {
+ lottos = new ArrayList<Lotto>();
+ winningLottos = new ArrayList<WinningLotto>();
+ }
public List<Lotto> getLottos() {
return lottos;
}
- public void setLottos(List<Lotto> lottos) {
- this.lottos = lottos;
+ public List<WinningLotto> getWinningLottos() {
+ return winningLottos;
}
- public List<Lotto> purchase(int money) {
- for (int numbers = 0; numbers < changeUnit(money); numbers++) {
- List<Integer> randomNumber = generateRandomNumbers();
- Collections.sort(randomNumber);
- Lotto lotto = new Lotto(randomNumber);
- lottos.add(lotto);
+ public void purchaseManual(String[] continuousNumbers) {
+ for (String continuousNumber : continuousNumbers) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.manual(continuousNumber));
}
- return lottos;
}
- public static List<Integer> generateRandomNumbers() {
- List<Integer> randomNumbers = new ArrayList<Integer>();
- for (int number = 1; number <= 45; number++) {
- randomNumbers.add(number);
+ public void setWinningLottos(String continuousWinningNumbers, LottoNumber bonusNumber) {
+ List<LottoNumber> winningNumbers = Splitter.splitNumber(continuousWinningNumbers);
+ winningLottos = new ArrayList<>();
+ for (Lotto lotto : lottos) {
+ setWinningLotto(lotto, lotto.countMatch(winningNumbers), bonusNumber);
}
- Collections.shuffle(randomNumbers);
- return randomNumbers.subList(0, 6);
}
- public int[] saveLottoResult(String text) {
- List<Integer> winnerNumber = Splitter.splitNumber(text);
- int[] result = new int[NumberOfHits.values().length];
- for (int index = 0; index < lottos.size(); index++) {
- result[countMatch(winnerNumber, index)]++;
+ public void setWinningLotto(Lotto lotto, int count, LottoNumber bonusNumber) {
+ if (Rank.lookUpRank(count, lotto.hasThisNumber(bonusNumber)) != Rank.MISS) {
+ winningLottos.add(new WinningLotto(lotto, count, bonusNumber));
}
- return result;
}
- public int countMatch(List<Integer> winnerNumber, int index) {
- int count = 0;
- for (int number : winnerNumber) {
- count = increaseCount(number, index, count);
+ public List<Lotto> purchaseAuto(int money, int numberOfManual) {
+ for (int numbers = 0; numbers < numberOfAutoLotto(money, numberOfManual); numbers++) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.auto());
}
- return count;
+ return lottos;
}
- public int increaseCount(int number, int index, int count) {
- if (lottos.get(index).isContain(number)) {
- count++;
- }
- return count;
+ public int countSameRank(Rank rank) {
+ return (int) winningLottos.stream()
+ .filter(winningLotto -> winningLotto.findRank().equals(rank))
+ .count();
}
- public double calculateRate(int[] result, int outcome) {
- int income = calculateReward(result);
- return Math.round((double)income / outcome * 1000.0) / 10.0 ;
+ public double calculateRate(int outcome) {
+ return (double) calculateIncome() / outcome * 100.0;
}
- public int calculateReward(int[] result) {
- int total = 0;
- for (int index = 0; index < result.length; index++) {
- total += result[index] * NumberOfHits.values()[index].getReward();
+ public int calculateIncome() {
+ int income = 0;
+ for (WinningLotto winningLotto : winningLottos) {
+ income = winningLotto.findRank().plusReward(income);
}
- return total;
+ return income;
}
public int changeUnit(int totalPrice) {
- return totalPrice / 1000;
+ return totalPrice / LOTTO_PRICE;
+ }
+
+ public int numberOfAutoLotto(int money, int numberOfManual) {
+ return changeUnit(money) - numberOfManual;
}
-}
+}
\ No newline at end of file | Java | ์ด๋์๋ menual์ด๊ณ ์ฌ๊ธฐ์๋ manual์ด๊ณ ์คํ ๋ง์ด ๋๋์ฒด ๋ญ์ฃ ? |
@@ -0,0 +1,67 @@
+package lotto.domain;
+
+import java.util.Arrays;
+
+public enum Rank {
+ FIRST(6, 2000000000),
+ SECOND(5, 30000000),
+ THIRD(5, 1500000),
+ FOURTH(4, 50000),
+ FIFTH(3, 5000),
+ MISS(0, 0);
+
+ private static final int PRIZE_MIN_NUMBER = 3;
+
+ private int matchCount;
+ private int winningPrice;
+
+ private Rank(int matchCount, int winningPrice) {
+ this.matchCount = matchCount;
+ this.winningPrice = winningPrice;
+ }
+
+ public int getMatchCount() {
+ return matchCount;
+ }
+
+ public int getWinningPrice() {
+ return winningPrice;
+ }
+
+ public static Rank lookUpRank(int matchCount, boolean hasBonusNumber) {
+ if (matchCount < PRIZE_MIN_NUMBER) {
+ return MISS;
+ }
+
+ if (SECOND.isRightCount(matchCount) && hasBonusNumber) {
+ return SECOND;
+ }
+
+ if (THIRD.isRightCount(matchCount)) {
+ return THIRD;
+ }
+
+ return (Rank) Arrays.stream(values()).filter(rank -> rank.isRightCount(matchCount)).toArray()[0];
+ }
+
+ public boolean isRightCount(int matchCount) {
+ return this.matchCount == matchCount;
+ }
+
+ public int plusReward(int income) {
+ return income + winningPrice;
+ }
+
+ public String toString() {
+ StringBuilder stringBuilder = new StringBuilder();
+ stringBuilder.append(this.matchCount)
+ .append("๊ฐ ์ผ์น");
+ if (this.winningPrice == Rank.SECOND.winningPrice) {
+ stringBuilder.append(", ๋ณด๋์ค ๋ณผ ์ผ์น");
+ }
+ stringBuilder.append("(")
+ .append(this.winningPrice)
+ .append(")-");
+ return stringBuilder.toString();
+ }
+}
\ No newline at end of file | Java | ์ข ๋ ์ข์ ๋ฉ์๋ ๋ค์ด๋ฐ |
@@ -0,0 +1,67 @@
+package lotto.domain;
+
+import java.util.Arrays;
+
+public enum Rank {
+ FIRST(6, 2000000000),
+ SECOND(5, 30000000),
+ THIRD(5, 1500000),
+ FOURTH(4, 50000),
+ FIFTH(3, 5000),
+ MISS(0, 0);
+
+ private static final int PRIZE_MIN_NUMBER = 3;
+
+ private int matchCount;
+ private int winningPrice;
+
+ private Rank(int matchCount, int winningPrice) {
+ this.matchCount = matchCount;
+ this.winningPrice = winningPrice;
+ }
+
+ public int getMatchCount() {
+ return matchCount;
+ }
+
+ public int getWinningPrice() {
+ return winningPrice;
+ }
+
+ public static Rank lookUpRank(int matchCount, boolean hasBonusNumber) {
+ if (matchCount < PRIZE_MIN_NUMBER) {
+ return MISS;
+ }
+
+ if (SECOND.isRightCount(matchCount) && hasBonusNumber) {
+ return SECOND;
+ }
+
+ if (THIRD.isRightCount(matchCount)) {
+ return THIRD;
+ }
+
+ return (Rank) Arrays.stream(values()).filter(rank -> rank.isRightCount(matchCount)).toArray()[0];
+ }
+
+ public boolean isRightCount(int matchCount) {
+ return this.matchCount == matchCount;
+ }
+
+ public int plusReward(int income) {
+ return income + winningPrice;
+ }
+
+ public String toString() {
+ StringBuilder stringBuilder = new StringBuilder();
+ stringBuilder.append(this.matchCount)
+ .append("๊ฐ ์ผ์น");
+ if (this.winningPrice == Rank.SECOND.winningPrice) {
+ stringBuilder.append(", ๋ณด๋์ค ๋ณผ ์ผ์น");
+ }
+ stringBuilder.append("(")
+ .append(this.winningPrice)
+ .append(")-");
+ return stringBuilder.toString();
+ }
+}
\ No newline at end of file | Java | set์ด๋ผ๋ ๋ช
์นญ์ ํด๋์ค ํ๋๋ค์ ๊ฐ์ ์ธํ
ํ ๋ ์ฌ์ฉํ๋๊น ๋ค๋ฅธ ๋ช
์นญ์ด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -3,77 +3,78 @@
import lotto.utils.Splitter;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
public class LottoGame {
- private List<Lotto> lottos = new ArrayList<Lotto>();
+ private static final int LOTTO_PRICE = 1000;
+ private List<Lotto> lottos;
+ private List<WinningLotto> winningLottos;
+
+ public LottoGame() {
+ lottos = new ArrayList<Lotto>();
+ winningLottos = new ArrayList<WinningLotto>();
+ }
public List<Lotto> getLottos() {
return lottos;
}
- public void setLottos(List<Lotto> lottos) {
- this.lottos = lottos;
+ public List<WinningLotto> getWinningLottos() {
+ return winningLottos;
}
- public List<Lotto> purchase(int money) {
- for (int numbers = 0; numbers < changeUnit(money); numbers++) {
- List<Integer> randomNumber = generateRandomNumbers();
- Collections.sort(randomNumber);
- Lotto lotto = new Lotto(randomNumber);
- lottos.add(lotto);
+ public void purchaseManual(String[] continuousNumbers) {
+ for (String continuousNumber : continuousNumbers) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.manual(continuousNumber));
}
- return lottos;
}
- public static List<Integer> generateRandomNumbers() {
- List<Integer> randomNumbers = new ArrayList<Integer>();
- for (int number = 1; number <= 45; number++) {
- randomNumbers.add(number);
+ public void setWinningLottos(String continuousWinningNumbers, LottoNumber bonusNumber) {
+ List<LottoNumber> winningNumbers = Splitter.splitNumber(continuousWinningNumbers);
+ winningLottos = new ArrayList<>();
+ for (Lotto lotto : lottos) {
+ setWinningLotto(lotto, lotto.countMatch(winningNumbers), bonusNumber);
}
- Collections.shuffle(randomNumbers);
- return randomNumbers.subList(0, 6);
}
- public int[] saveLottoResult(String text) {
- List<Integer> winnerNumber = Splitter.splitNumber(text);
- int[] result = new int[NumberOfHits.values().length];
- for (int index = 0; index < lottos.size(); index++) {
- result[countMatch(winnerNumber, index)]++;
+ public void setWinningLotto(Lotto lotto, int count, LottoNumber bonusNumber) {
+ if (Rank.lookUpRank(count, lotto.hasThisNumber(bonusNumber)) != Rank.MISS) {
+ winningLottos.add(new WinningLotto(lotto, count, bonusNumber));
}
- return result;
}
- public int countMatch(List<Integer> winnerNumber, int index) {
- int count = 0;
- for (int number : winnerNumber) {
- count = increaseCount(number, index, count);
+ public List<Lotto> purchaseAuto(int money, int numberOfManual) {
+ for (int numbers = 0; numbers < numberOfAutoLotto(money, numberOfManual); numbers++) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.auto());
}
- return count;
+ return lottos;
}
- public int increaseCount(int number, int index, int count) {
- if (lottos.get(index).isContain(number)) {
- count++;
- }
- return count;
+ public int countSameRank(Rank rank) {
+ return (int) winningLottos.stream()
+ .filter(winningLotto -> winningLotto.findRank().equals(rank))
+ .count();
}
- public double calculateRate(int[] result, int outcome) {
- int income = calculateReward(result);
- return Math.round((double)income / outcome * 1000.0) / 10.0 ;
+ public double calculateRate(int outcome) {
+ return (double) calculateIncome() / outcome * 100.0;
}
- public int calculateReward(int[] result) {
- int total = 0;
- for (int index = 0; index < result.length; index++) {
- total += result[index] * NumberOfHits.values()[index].getReward();
+ public int calculateIncome() {
+ int income = 0;
+ for (WinningLotto winningLotto : winningLottos) {
+ income = winningLotto.findRank().plusReward(income);
}
- return total;
+ return income;
}
public int changeUnit(int totalPrice) {
- return totalPrice / 1000;
+ return totalPrice / LOTTO_PRICE;
+ }
+
+ public int numberOfAutoLotto(int money, int numberOfManual) {
+ return changeUnit(money) - numberOfManual;
}
-}
+}
\ No newline at end of file | Java | ์คํ์
๋๋ค |
@@ -3,77 +3,78 @@
import lotto.utils.Splitter;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
public class LottoGame {
- private List<Lotto> lottos = new ArrayList<Lotto>();
+ private static final int LOTTO_PRICE = 1000;
+ private List<Lotto> lottos;
+ private List<WinningLotto> winningLottos;
+
+ public LottoGame() {
+ lottos = new ArrayList<Lotto>();
+ winningLottos = new ArrayList<WinningLotto>();
+ }
public List<Lotto> getLottos() {
return lottos;
}
- public void setLottos(List<Lotto> lottos) {
- this.lottos = lottos;
+ public List<WinningLotto> getWinningLottos() {
+ return winningLottos;
}
- public List<Lotto> purchase(int money) {
- for (int numbers = 0; numbers < changeUnit(money); numbers++) {
- List<Integer> randomNumber = generateRandomNumbers();
- Collections.sort(randomNumber);
- Lotto lotto = new Lotto(randomNumber);
- lottos.add(lotto);
+ public void purchaseManual(String[] continuousNumbers) {
+ for (String continuousNumber : continuousNumbers) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.manual(continuousNumber));
}
- return lottos;
}
- public static List<Integer> generateRandomNumbers() {
- List<Integer> randomNumbers = new ArrayList<Integer>();
- for (int number = 1; number <= 45; number++) {
- randomNumbers.add(number);
+ public void setWinningLottos(String continuousWinningNumbers, LottoNumber bonusNumber) {
+ List<LottoNumber> winningNumbers = Splitter.splitNumber(continuousWinningNumbers);
+ winningLottos = new ArrayList<>();
+ for (Lotto lotto : lottos) {
+ setWinningLotto(lotto, lotto.countMatch(winningNumbers), bonusNumber);
}
- Collections.shuffle(randomNumbers);
- return randomNumbers.subList(0, 6);
}
- public int[] saveLottoResult(String text) {
- List<Integer> winnerNumber = Splitter.splitNumber(text);
- int[] result = new int[NumberOfHits.values().length];
- for (int index = 0; index < lottos.size(); index++) {
- result[countMatch(winnerNumber, index)]++;
+ public void setWinningLotto(Lotto lotto, int count, LottoNumber bonusNumber) {
+ if (Rank.lookUpRank(count, lotto.hasThisNumber(bonusNumber)) != Rank.MISS) {
+ winningLottos.add(new WinningLotto(lotto, count, bonusNumber));
}
- return result;
}
- public int countMatch(List<Integer> winnerNumber, int index) {
- int count = 0;
- for (int number : winnerNumber) {
- count = increaseCount(number, index, count);
+ public List<Lotto> purchaseAuto(int money, int numberOfManual) {
+ for (int numbers = 0; numbers < numberOfAutoLotto(money, numberOfManual); numbers++) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.auto());
}
- return count;
+ return lottos;
}
- public int increaseCount(int number, int index, int count) {
- if (lottos.get(index).isContain(number)) {
- count++;
- }
- return count;
+ public int countSameRank(Rank rank) {
+ return (int) winningLottos.stream()
+ .filter(winningLotto -> winningLotto.findRank().equals(rank))
+ .count();
}
- public double calculateRate(int[] result, int outcome) {
- int income = calculateReward(result);
- return Math.round((double)income / outcome * 1000.0) / 10.0 ;
+ public double calculateRate(int outcome) {
+ return (double) calculateIncome() / outcome * 100.0;
}
- public int calculateReward(int[] result) {
- int total = 0;
- for (int index = 0; index < result.length; index++) {
- total += result[index] * NumberOfHits.values()[index].getReward();
+ public int calculateIncome() {
+ int income = 0;
+ for (WinningLotto winningLotto : winningLottos) {
+ income = winningLotto.findRank().plusReward(income);
}
- return total;
+ return income;
}
public int changeUnit(int totalPrice) {
- return totalPrice / 1000;
+ return totalPrice / LOTTO_PRICE;
+ }
+
+ public int numberOfAutoLotto(int money, int numberOfManual) {
+ return changeUnit(money) - numberOfManual;
}
-}
+}
\ No newline at end of file | Java | 1000์ด๋ ๊ฐ์ ํ๋์ฝ๋ฉ์ค |
@@ -0,0 +1,17 @@
+package lotto.domain;
+
+public class WinningLotto extends Lotto {
+ private Lotto lotto;
+ private int matchCount;
+ private boolean hasbonusNumber;
+
+ public WinningLotto(Lotto lotto, int matchCount, LottoNumber bonusNumber) {
+ this.hasbonusNumber = lotto.hasThisNumber(bonusNumber);
+ this.lotto = lotto;
+ this.matchCount = matchCount;
+ }
+
+ public Rank findRank() {
+ return Rank.lookUpRank(matchCount, hasbonusNumber);
+ }
+}
\ No newline at end of file | Java | get set ์ง์๋ณด๊ณ ์๊ฐํด๋ณด์ญ์ผ (๊ฐ์ฒด์งํฅ ์ํ์ฒด์กฐ ๊ท์น get set ์ฌ์ฉ์ ์์ ํ๋ค) |
@@ -3,77 +3,78 @@
import lotto.utils.Splitter;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
public class LottoGame {
- private List<Lotto> lottos = new ArrayList<Lotto>();
+ private static final int LOTTO_PRICE = 1000;
+ private List<Lotto> lottos;
+ private List<WinningLotto> winningLottos;
+
+ public LottoGame() {
+ lottos = new ArrayList<Lotto>();
+ winningLottos = new ArrayList<WinningLotto>();
+ }
public List<Lotto> getLottos() {
return lottos;
}
- public void setLottos(List<Lotto> lottos) {
- this.lottos = lottos;
+ public List<WinningLotto> getWinningLottos() {
+ return winningLottos;
}
- public List<Lotto> purchase(int money) {
- for (int numbers = 0; numbers < changeUnit(money); numbers++) {
- List<Integer> randomNumber = generateRandomNumbers();
- Collections.sort(randomNumber);
- Lotto lotto = new Lotto(randomNumber);
- lottos.add(lotto);
+ public void purchaseManual(String[] continuousNumbers) {
+ for (String continuousNumber : continuousNumbers) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.manual(continuousNumber));
}
- return lottos;
}
- public static List<Integer> generateRandomNumbers() {
- List<Integer> randomNumbers = new ArrayList<Integer>();
- for (int number = 1; number <= 45; number++) {
- randomNumbers.add(number);
+ public void setWinningLottos(String continuousWinningNumbers, LottoNumber bonusNumber) {
+ List<LottoNumber> winningNumbers = Splitter.splitNumber(continuousWinningNumbers);
+ winningLottos = new ArrayList<>();
+ for (Lotto lotto : lottos) {
+ setWinningLotto(lotto, lotto.countMatch(winningNumbers), bonusNumber);
}
- Collections.shuffle(randomNumbers);
- return randomNumbers.subList(0, 6);
}
- public int[] saveLottoResult(String text) {
- List<Integer> winnerNumber = Splitter.splitNumber(text);
- int[] result = new int[NumberOfHits.values().length];
- for (int index = 0; index < lottos.size(); index++) {
- result[countMatch(winnerNumber, index)]++;
+ public void setWinningLotto(Lotto lotto, int count, LottoNumber bonusNumber) {
+ if (Rank.lookUpRank(count, lotto.hasThisNumber(bonusNumber)) != Rank.MISS) {
+ winningLottos.add(new WinningLotto(lotto, count, bonusNumber));
}
- return result;
}
- public int countMatch(List<Integer> winnerNumber, int index) {
- int count = 0;
- for (int number : winnerNumber) {
- count = increaseCount(number, index, count);
+ public List<Lotto> purchaseAuto(int money, int numberOfManual) {
+ for (int numbers = 0; numbers < numberOfAutoLotto(money, numberOfManual); numbers++) {
+ LottoGenerator lottoGenerator = new LottoGenerator();
+ lottos.add(lottoGenerator.auto());
}
- return count;
+ return lottos;
}
- public int increaseCount(int number, int index, int count) {
- if (lottos.get(index).isContain(number)) {
- count++;
- }
- return count;
+ public int countSameRank(Rank rank) {
+ return (int) winningLottos.stream()
+ .filter(winningLotto -> winningLotto.findRank().equals(rank))
+ .count();
}
- public double calculateRate(int[] result, int outcome) {
- int income = calculateReward(result);
- return Math.round((double)income / outcome * 1000.0) / 10.0 ;
+ public double calculateRate(int outcome) {
+ return (double) calculateIncome() / outcome * 100.0;
}
- public int calculateReward(int[] result) {
- int total = 0;
- for (int index = 0; index < result.length; index++) {
- total += result[index] * NumberOfHits.values()[index].getReward();
+ public int calculateIncome() {
+ int income = 0;
+ for (WinningLotto winningLotto : winningLottos) {
+ income = winningLotto.findRank().plusReward(income);
}
- return total;
+ return income;
}
public int changeUnit(int totalPrice) {
- return totalPrice / 1000;
+ return totalPrice / LOTTO_PRICE;
+ }
+
+ public int numberOfAutoLotto(int money, int numberOfManual) {
+ return changeUnit(money) - numberOfManual;
}
-}
+}
\ No newline at end of file | Java | ์ด ํด๋์ค๊ฐ ๋๋ฌด ๋ฌด๊ฑฐ์๋ณด์
๋๋ค. ๋๋ฌด ๋ง์ ์ฑ
์์ ๊ฐ๊ณ ์๋๊ฑด ์๋์ง ํ๋ฒ ๋ด๋ณด์ธ์ |
@@ -0,0 +1,32 @@
+package lotto.domain;
+
+import lotto.utils.Splitter;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class LottoGenerator {
+ List<LottoNumber> randomNumbers;
+
+ public LottoGenerator() {
+ randomNumbers = new ArrayList<>();
+ }
+
+ public Lotto manual(String continuousNumber) {
+ randomNumbers = Splitter.splitNumber(continuousNumber);
+ return new Lotto(randomNumbers);
+ }
+
+ public Lotto auto() {
+ return new Lotto(generateRandomNumbers());
+ }
+
+ public List<LottoNumber> generateRandomNumbers() {
+ for (int number = 1; number <= 45; number++) {
+ randomNumbers.add(new LottoNumber(number));
+ }
+ Collections.shuffle(randomNumbers);
+ return randomNumbers.subList(0, 6);
+ }
+}
\ No newline at end of file | Java | generateAuto() ๊ฐ ๋ซ์ง ์์๊น์ |
@@ -0,0 +1,32 @@
+package lotto.domain;
+
+import lotto.utils.Splitter;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class LottoGenerator {
+ List<LottoNumber> randomNumbers;
+
+ public LottoGenerator() {
+ randomNumbers = new ArrayList<>();
+ }
+
+ public Lotto manual(String continuousNumber) {
+ randomNumbers = Splitter.splitNumber(continuousNumber);
+ return new Lotto(randomNumbers);
+ }
+
+ public Lotto auto() {
+ return new Lotto(generateRandomNumbers());
+ }
+
+ public List<LottoNumber> generateRandomNumbers() {
+ for (int number = 1; number <= 45; number++) {
+ randomNumbers.add(new LottoNumber(number));
+ }
+ Collections.shuffle(randomNumbers);
+ return randomNumbers.subList(0, 6);
+ }
+}
\ No newline at end of file | Java | generateMenual ์ด ๋ซ์ง ์์๊น์. menual์ธ์ง manual์ธ์ง ํต์ผ ์์ผ๋ฌ๋ผ๋๊น ๋ฌด์๋นํ๋ค์ |
@@ -0,0 +1,79 @@
+import React, { useState } from "react";
+import { useNavigate } from 'react-router-dom';
+import "../../../style/reset.scss";
+import "../../../style/common.scss";
+import "../../../style/variables.scss"
+import "./Login.scss";
+
+
+const Login = () => {
+
+ const [userInfo, setUserInfo] = useState({
+ userId:"์์ด๋",
+ userPw:"ํจ์ค์๋",
+ });
+ const navigate = useNavigate();
+ const goToMain = () => {
+ fetch("http://10.58.52.144:3000/users/signup", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ email: userInfo.userId,
+ password: userInfo.userPw,
+ }),
+ })
+ .then((response) => response.json())
+ .then((result) => {
+ if(result.accessToken) {
+ localStorage.setItem("token", result.accessToken);
+ navigate("/jinheekim-main");
+ }
+ if(result.message === "invalid password") {
+ alert ("๋น๋ฐ๋ฒํธ ํ๋ ธ์");
+ };
+ if(result.message === "specified user does not exist") {
+ alert ("์์ด๋ ํ๋ ธ์");
+ }
+ })
+
+ }
+
+ const handleInput = (event) => {
+ const {value, id} = event.target;
+ setUserInfo({...userInfo, [id]:value});
+ };
+
+ const isValue = userInfo.userId.includes('@') && userInfo.userPw.length >= 5;
+
+ return (
+ <div className="login">
+ <div className="box">
+ <h1>Westagram</h1>
+ <div className="inputTxt">
+ <input
+ id="userId"
+ onChange={handleInput}
+ type="text"
+ className="id"
+ placeholder="์ ํ๋ฒํธ, ์ฌ์ฉ์ ์ด๋ฆ ๋๋ ์ด๋ฉ์ผ"
+ />
+ <input
+ id="userPw"
+ onChange={handleInput}
+ type="password"
+ className="pw"
+ placeholder="๋น๋ฐ๋ฒํธ"
+ />
+ <button type="button" onClick={goToMain} className={isValue ? "abled-button" : 'disabled-button'} disabled={!isValue}>๋ก๊ทธ์ธ</button>
+ <div className="findPw">
+ <a href="#">๋น๋ฐ๋ฒํธ๋ฅผ ์์ผ์
จ๋์?</a>
+ </div>
+ </div>
+ </div>
+ </div>
+ );
+};
+
+export default Login;
\ No newline at end of file | JavaScript | ๐ ์ข์ต๋๋ค! ๊ณ์ํด์ ์ด๋ ๊ฒ ์ธ์
๋ด์ฉ ๋ฐ๋ก๋ฐ๋ก ์ ์ฉํด์ฃผ๋ฉด์ ๋ณธ์ธ์ ์ง์์ผ๋ก ๊ฐ์ ธ๊ฐ์ฃผ์๋ฉด ์ข๊ฒ ์ต๋๋ค! |
@@ -0,0 +1,79 @@
+import React, { useState } from "react";
+import { useNavigate } from 'react-router-dom';
+import "../../../style/reset.scss";
+import "../../../style/common.scss";
+import "../../../style/variables.scss"
+import "./Login.scss";
+
+
+const Login = () => {
+
+ const [userInfo, setUserInfo] = useState({
+ userId:"์์ด๋",
+ userPw:"ํจ์ค์๋",
+ });
+ const navigate = useNavigate();
+ const goToMain = () => {
+ fetch("http://10.58.52.144:3000/users/signup", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ email: userInfo.userId,
+ password: userInfo.userPw,
+ }),
+ })
+ .then((response) => response.json())
+ .then((result) => {
+ if(result.accessToken) {
+ localStorage.setItem("token", result.accessToken);
+ navigate("/jinheekim-main");
+ }
+ if(result.message === "invalid password") {
+ alert ("๋น๋ฐ๋ฒํธ ํ๋ ธ์");
+ };
+ if(result.message === "specified user does not exist") {
+ alert ("์์ด๋ ํ๋ ธ์");
+ }
+ })
+
+ }
+
+ const handleInput = (event) => {
+ const {value, id} = event.target;
+ setUserInfo({...userInfo, [id]:value});
+ };
+
+ const isValue = userInfo.userId.includes('@') && userInfo.userPw.length >= 5;
+
+ return (
+ <div className="login">
+ <div className="box">
+ <h1>Westagram</h1>
+ <div className="inputTxt">
+ <input
+ id="userId"
+ onChange={handleInput}
+ type="text"
+ className="id"
+ placeholder="์ ํ๋ฒํธ, ์ฌ์ฉ์ ์ด๋ฆ ๋๋ ์ด๋ฉ์ผ"
+ />
+ <input
+ id="userPw"
+ onChange={handleInput}
+ type="password"
+ className="pw"
+ placeholder="๋น๋ฐ๋ฒํธ"
+ />
+ <button type="button" onClick={goToMain} className={isValue ? "abled-button" : 'disabled-button'} disabled={!isValue}>๋ก๊ทธ์ธ</button>
+ <div className="findPw">
+ <a href="#">๋น๋ฐ๋ฒํธ๋ฅผ ์์ผ์
จ๋์?</a>
+ </div>
+ </div>
+ </div>
+ </div>
+ );
+};
+
+export default Login;
\ No newline at end of file | JavaScript | ์ฝ๋์ ์ฃผ์์ด ๋ง์ต๋๋ค!
์ถํ์ ๋ค์ ํ์ตํ ์ฉ๋๋ก ๋จ๊ฒจ๋์ ์ฃผ์์ด๋ผ๋ฉด, ํด๋น๋ด์ฉ์ ๋ฐ๋ก ๋ธ๋ก๊ทธ๋ ๊ฐ์ธ ๋ฉ๋ชจ์ ์์ฑ์ ํด ์ฃผ์๊ณ , ์ฝ๋๋ฅผ github์ ํตํด ์ฌ๋ ค์ฃผ์ค๋๋ ๋ถํ์ํ ์ฝ๋๋ฅผ ์ต๋ํ ์ค์ฌ์ฃผ์ธ์! |
@@ -0,0 +1,79 @@
+import React, { useState } from "react";
+import { useNavigate } from 'react-router-dom';
+import "../../../style/reset.scss";
+import "../../../style/common.scss";
+import "../../../style/variables.scss"
+import "./Login.scss";
+
+
+const Login = () => {
+
+ const [userInfo, setUserInfo] = useState({
+ userId:"์์ด๋",
+ userPw:"ํจ์ค์๋",
+ });
+ const navigate = useNavigate();
+ const goToMain = () => {
+ fetch("http://10.58.52.144:3000/users/signup", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ email: userInfo.userId,
+ password: userInfo.userPw,
+ }),
+ })
+ .then((response) => response.json())
+ .then((result) => {
+ if(result.accessToken) {
+ localStorage.setItem("token", result.accessToken);
+ navigate("/jinheekim-main");
+ }
+ if(result.message === "invalid password") {
+ alert ("๋น๋ฐ๋ฒํธ ํ๋ ธ์");
+ };
+ if(result.message === "specified user does not exist") {
+ alert ("์์ด๋ ํ๋ ธ์");
+ }
+ })
+
+ }
+
+ const handleInput = (event) => {
+ const {value, id} = event.target;
+ setUserInfo({...userInfo, [id]:value});
+ };
+
+ const isValue = userInfo.userId.includes('@') && userInfo.userPw.length >= 5;
+
+ return (
+ <div className="login">
+ <div className="box">
+ <h1>Westagram</h1>
+ <div className="inputTxt">
+ <input
+ id="userId"
+ onChange={handleInput}
+ type="text"
+ className="id"
+ placeholder="์ ํ๋ฒํธ, ์ฌ์ฉ์ ์ด๋ฆ ๋๋ ์ด๋ฉ์ผ"
+ />
+ <input
+ id="userPw"
+ onChange={handleInput}
+ type="password"
+ className="pw"
+ placeholder="๋น๋ฐ๋ฒํธ"
+ />
+ <button type="button" onClick={goToMain} className={isValue ? "abled-button" : 'disabled-button'} disabled={!isValue}>๋ก๊ทธ์ธ</button>
+ <div className="findPw">
+ <a href="#">๋น๋ฐ๋ฒํธ๋ฅผ ์์ผ์
จ๋์?</a>
+ </div>
+ </div>
+ </div>
+ </div>
+ );
+};
+
+export default Login;
\ No newline at end of file | JavaScript | ```suggestion
<div className="inputTxt">
```
์ ์ฝ๋ ์ฒ๋ผ js์ ๊ธฐ๋ณธ ์ปจ๋ฒค์
์ `camelCase`์
๋๋ค! className์ ์์ฑํ ๋์๋ ์ ๊ฒฝ์จ์ฃผ์
์ผํฉ๋๋ค! |
@@ -0,0 +1,79 @@
+import React, { useState } from "react";
+import { useNavigate } from 'react-router-dom';
+import "../../../style/reset.scss";
+import "../../../style/common.scss";
+import "../../../style/variables.scss"
+import "./Login.scss";
+
+
+const Login = () => {
+
+ const [userInfo, setUserInfo] = useState({
+ userId:"์์ด๋",
+ userPw:"ํจ์ค์๋",
+ });
+ const navigate = useNavigate();
+ const goToMain = () => {
+ fetch("http://10.58.52.144:3000/users/signup", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ email: userInfo.userId,
+ password: userInfo.userPw,
+ }),
+ })
+ .then((response) => response.json())
+ .then((result) => {
+ if(result.accessToken) {
+ localStorage.setItem("token", result.accessToken);
+ navigate("/jinheekim-main");
+ }
+ if(result.message === "invalid password") {
+ alert ("๋น๋ฐ๋ฒํธ ํ๋ ธ์");
+ };
+ if(result.message === "specified user does not exist") {
+ alert ("์์ด๋ ํ๋ ธ์");
+ }
+ })
+
+ }
+
+ const handleInput = (event) => {
+ const {value, id} = event.target;
+ setUserInfo({...userInfo, [id]:value});
+ };
+
+ const isValue = userInfo.userId.includes('@') && userInfo.userPw.length >= 5;
+
+ return (
+ <div className="login">
+ <div className="box">
+ <h1>Westagram</h1>
+ <div className="inputTxt">
+ <input
+ id="userId"
+ onChange={handleInput}
+ type="text"
+ className="id"
+ placeholder="์ ํ๋ฒํธ, ์ฌ์ฉ์ ์ด๋ฆ ๋๋ ์ด๋ฉ์ผ"
+ />
+ <input
+ id="userPw"
+ onChange={handleInput}
+ type="password"
+ className="pw"
+ placeholder="๋น๋ฐ๋ฒํธ"
+ />
+ <button type="button" onClick={goToMain} className={isValue ? "abled-button" : 'disabled-button'} disabled={!isValue}>๋ก๊ทธ์ธ</button>
+ <div className="findPw">
+ <a href="#">๋น๋ฐ๋ฒํธ๋ฅผ ์์ผ์
จ๋์?</a>
+ </div>
+ </div>
+ </div>
+ </div>
+ );
+};
+
+export default Login;
\ No newline at end of file | JavaScript | ํด๋น scssํ์ผ๋ค์ Index.js์์ import ํด์์ ์ ์ญ์ผ๋ก ์ ์ฉ์ํค๋๊ฒ ์ข์ต๋๋ค. |
@@ -0,0 +1,87 @@
+@import '../../../style/variables.scss';
+@import '../../../style/reset.scss';
+
+
+body {
+ font-size: 14px;
+}
+input {
+ outline: none;
+}
+
+.login {
+ width: 100%;
+ height: 100vh;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ flex-direction: column;
+ .box {
+ width: 400px;
+ padding: 40px 0 20px;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ flex-direction: column;
+ border: 1px solid rgb(219, 219, 219);
+
+ h1 {
+ font-size:40px;
+ font-family: 'Lobster';
+ }
+
+ .inputTxt {
+ width: 300px;
+
+ input {
+ display: block;
+ width: 100%;
+ margin: 5px 0;
+ padding: 10px 0 10px 5px;
+ background-color: $grey-color-bg;
+ border-radius: 3px;
+ border: 1px solid $grey-color-border;
+
+ &::placeholder{
+ font-size: 12px;
+ }
+ }
+ }
+ }
+}
+
+@mixin button-style {
+ margin-top: 20px;
+ display: inline-block;
+ width: 100%;
+ text-align: center;
+ text-decoration-line: none;
+ padding: 8px 0;
+ border-radius: 8px;
+ border: 0px;
+ font-weight: bold;
+ color: #fff;
+ cursor: pointer;
+ }
+
+.abled-button {
+ @include button-style;
+ background-color: $point-color-blue;
+}
+
+.disabled-button {
+ @include button-style;
+ background-color: #666;
+}
+
+
+.findPw {
+ text-align: center;
+ margin: 60px 0;
+
+ a {
+ font-size: 9px;
+ color: rgb(0, 55, 107);
+ text-decoration: none;
+ }
+}
\ No newline at end of file | Unknown | ๋ง์ฐฌ๊ฐ์ง ์
๋๋ค! font๊ฐ์ ์์ฑ๋ค๋ common.scss์ ์์ฑํด์ ์ ์ญ์ผ๋ก ๊ด๋ฆฌํ ์ ์๊ฒ ์ต๋๋ค! |
@@ -0,0 +1,87 @@
+@import '../../../style/variables.scss';
+@import '../../../style/reset.scss';
+
+
+body {
+ font-size: 14px;
+}
+input {
+ outline: none;
+}
+
+.login {
+ width: 100%;
+ height: 100vh;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ flex-direction: column;
+ .box {
+ width: 400px;
+ padding: 40px 0 20px;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ flex-direction: column;
+ border: 1px solid rgb(219, 219, 219);
+
+ h1 {
+ font-size:40px;
+ font-family: 'Lobster';
+ }
+
+ .inputTxt {
+ width: 300px;
+
+ input {
+ display: block;
+ width: 100%;
+ margin: 5px 0;
+ padding: 10px 0 10px 5px;
+ background-color: $grey-color-bg;
+ border-radius: 3px;
+ border: 1px solid $grey-color-border;
+
+ &::placeholder{
+ font-size: 12px;
+ }
+ }
+ }
+ }
+}
+
+@mixin button-style {
+ margin-top: 20px;
+ display: inline-block;
+ width: 100%;
+ text-align: center;
+ text-decoration-line: none;
+ padding: 8px 0;
+ border-radius: 8px;
+ border: 0px;
+ font-weight: bold;
+ color: #fff;
+ cursor: pointer;
+ }
+
+.abled-button {
+ @include button-style;
+ background-color: $point-color-blue;
+}
+
+.disabled-button {
+ @include button-style;
+ background-color: #666;
+}
+
+
+.findPw {
+ text-align: center;
+ margin: 60px 0;
+
+ a {
+ font-size: 9px;
+ color: rgb(0, 55, 107);
+ text-decoration: none;
+ }
+}
\ No newline at end of file | Unknown | variables.scssํ์ผ์ ์์ผ๋ฉด ๋ ๋ณ์๋ค์ด๋ค์! |
@@ -0,0 +1,163 @@
+import React, { useState, useEffect } from "react";
+import MainFeeds from "./MainFeeds";
+import "./Main.scss";
+import compass from "../assets/icon/compass.png";
+import heart from "../assets/icon/heart.png";
+import instagram from "../assets/icon/instagram.png";
+import search from "../assets/icon/search.png";
+import user from "../assets/icon/user.png";
+import userimg from "../assets/images/test.jpg";
+import feedimg from "../assets/images/test.jpg";
+
+const INFO_LIST = [
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+];
+
+const Main = () => {
+ return (
+ <>
+ <nav className="nav">
+ <div className="navLeft">
+ <img className="navIcon iconInstagram" alt="." src={instagram} />
+ <span className="logo">Westagram</span>
+ </div>
+ <div className="searchBar">
+ <img className="iconSearch" alt="." src={search} />
+ <input
+ className="searchBox"
+ type="text"
+ aria-label="๊ฒ์์ฐฝ"
+ placeholder="๊ฒ์"
+ />
+ </div>
+ <div className="navRight">
+ <img className="navIcon" alt="." src={compass} />
+ <img className="navIcon" alt="." src={heart} />
+ <img className="navIcon" alt="." src={user} />
+ </div>
+ </nav>
+ <div className="main">
+ <MainFeeds />
+ <div className="mainRight">
+ <div className="mainRightTop">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="stories">
+ <div className="storiesTop">
+ <p className="story">์คํ ๋ฆฌ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ </div>
+ <div className="recommend">
+ <div className="storiesTop">
+ <p className="story">ํ์๋์ ์ํ ์ถ์ฒ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ </div>
+ <div>
+ <ul>
+ {INFO_LIST.map((info) => {
+ return (
+ <li key={info.id}>
+ <a href="{info.link}">{info.text}</a>
+ </li>
+ );
+ })}
+ </ul>
+ <p className="copyright">โ 2023 WESTAGRAM</p>
+ </div>
+ </div>
+ </div>
+ </>
+ );
+};
+
+export default Main; | JavaScript | index.js์์ ์ ์ญ์ผ๋ก importํ๋ค๋ฉด ์ด๋ ๊ฒ ํ์ผ๋ง๋ค ๋งค๋ฒ import ํ์ง์์๋ ๋ฉ๋๋ค! |
@@ -0,0 +1,163 @@
+import React, { useState, useEffect } from "react";
+import MainFeeds from "./MainFeeds";
+import "./Main.scss";
+import compass from "../assets/icon/compass.png";
+import heart from "../assets/icon/heart.png";
+import instagram from "../assets/icon/instagram.png";
+import search from "../assets/icon/search.png";
+import user from "../assets/icon/user.png";
+import userimg from "../assets/images/test.jpg";
+import feedimg from "../assets/images/test.jpg";
+
+const INFO_LIST = [
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+];
+
+const Main = () => {
+ return (
+ <>
+ <nav className="nav">
+ <div className="navLeft">
+ <img className="navIcon iconInstagram" alt="." src={instagram} />
+ <span className="logo">Westagram</span>
+ </div>
+ <div className="searchBar">
+ <img className="iconSearch" alt="." src={search} />
+ <input
+ className="searchBox"
+ type="text"
+ aria-label="๊ฒ์์ฐฝ"
+ placeholder="๊ฒ์"
+ />
+ </div>
+ <div className="navRight">
+ <img className="navIcon" alt="." src={compass} />
+ <img className="navIcon" alt="." src={heart} />
+ <img className="navIcon" alt="." src={user} />
+ </div>
+ </nav>
+ <div className="main">
+ <MainFeeds />
+ <div className="mainRight">
+ <div className="mainRightTop">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="stories">
+ <div className="storiesTop">
+ <p className="story">์คํ ๋ฆฌ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ </div>
+ <div className="recommend">
+ <div className="storiesTop">
+ <p className="story">ํ์๋์ ์ํ ์ถ์ฒ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ </div>
+ <div>
+ <ul>
+ {INFO_LIST.map((info) => {
+ return (
+ <li key={info.id}>
+ <a href="{info.link}">{info.text}</a>
+ </li>
+ );
+ })}
+ </ul>
+ <p className="copyright">โ 2023 WESTAGRAM</p>
+ </div>
+ </div>
+ </div>
+ </>
+ );
+};
+
+export default Main; | JavaScript | `comment`๋ผ๋ ๋ณ์๋ช
์ด ์์ฑ๋์ด์๋ comment๋ค์ ๋ชจ์์ธ์ง, ๋ด๊ฐ input์ ์
๋ ฅํ ๊ฐ์ ๊ด๋ฆฌํ๋ ๋ณ์์ธ์ง ์ด๋ฆ๋ง ๋ดค์ ๋ ๋ฐ๋ก ์ถ์ธกํ๊ธฐ ์ด๋ ต์ต๋๋ค.
์๋์ commentArray๋ผ๋ ๋ณ์๋ ๋ง์ฐฌ๊ฐ์ง๋ก ์กฐ๊ธ ๋ ๋ณ์๊ฐ ์ด๋ค state๋ฅผ ๊ฐ์ง๊ณ ์๋์ง ๋ช
ํํ ์ด๋ฆ์ผ๋ก ์์ฑํด์ฃผ์๋ฉด ์ข๊ฒ ์ต๋๋ค |
@@ -0,0 +1,163 @@
+import React, { useState, useEffect } from "react";
+import MainFeeds from "./MainFeeds";
+import "./Main.scss";
+import compass from "../assets/icon/compass.png";
+import heart from "../assets/icon/heart.png";
+import instagram from "../assets/icon/instagram.png";
+import search from "../assets/icon/search.png";
+import user from "../assets/icon/user.png";
+import userimg from "../assets/images/test.jpg";
+import feedimg from "../assets/images/test.jpg";
+
+const INFO_LIST = [
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+];
+
+const Main = () => {
+ return (
+ <>
+ <nav className="nav">
+ <div className="navLeft">
+ <img className="navIcon iconInstagram" alt="." src={instagram} />
+ <span className="logo">Westagram</span>
+ </div>
+ <div className="searchBar">
+ <img className="iconSearch" alt="." src={search} />
+ <input
+ className="searchBox"
+ type="text"
+ aria-label="๊ฒ์์ฐฝ"
+ placeholder="๊ฒ์"
+ />
+ </div>
+ <div className="navRight">
+ <img className="navIcon" alt="." src={compass} />
+ <img className="navIcon" alt="." src={heart} />
+ <img className="navIcon" alt="." src={user} />
+ </div>
+ </nav>
+ <div className="main">
+ <MainFeeds />
+ <div className="mainRight">
+ <div className="mainRightTop">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="stories">
+ <div className="storiesTop">
+ <p className="story">์คํ ๋ฆฌ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ </div>
+ <div className="recommend">
+ <div className="storiesTop">
+ <p className="story">ํ์๋์ ์ํ ์ถ์ฒ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ </div>
+ <div>
+ <ul>
+ {INFO_LIST.map((info) => {
+ return (
+ <li key={info.id}>
+ <a href="{info.link}">{info.text}</a>
+ </li>
+ );
+ })}
+ </ul>
+ <p className="copyright">โ 2023 WESTAGRAM</p>
+ </div>
+ </div>
+ </div>
+ </>
+ );
+};
+
+export default Main; | JavaScript | ```suggestion
setCommentArray(commentList => [ ...commentList,comment]);
```
์ด ๋๊ฐ์ ์ฐจ์ด์ ์ ํ์คํ ์๊ณ ๋์ด๊ฐ์ฃผ์
จ์ผ๋ฉด ์ข๊ฒ ์ต๋๋ค!! |
@@ -0,0 +1,163 @@
+import React, { useState, useEffect } from "react";
+import MainFeeds from "./MainFeeds";
+import "./Main.scss";
+import compass from "../assets/icon/compass.png";
+import heart from "../assets/icon/heart.png";
+import instagram from "../assets/icon/instagram.png";
+import search from "../assets/icon/search.png";
+import user from "../assets/icon/user.png";
+import userimg from "../assets/images/test.jpg";
+import feedimg from "../assets/images/test.jpg";
+
+const INFO_LIST = [
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+];
+
+const Main = () => {
+ return (
+ <>
+ <nav className="nav">
+ <div className="navLeft">
+ <img className="navIcon iconInstagram" alt="." src={instagram} />
+ <span className="logo">Westagram</span>
+ </div>
+ <div className="searchBar">
+ <img className="iconSearch" alt="." src={search} />
+ <input
+ className="searchBox"
+ type="text"
+ aria-label="๊ฒ์์ฐฝ"
+ placeholder="๊ฒ์"
+ />
+ </div>
+ <div className="navRight">
+ <img className="navIcon" alt="." src={compass} />
+ <img className="navIcon" alt="." src={heart} />
+ <img className="navIcon" alt="." src={user} />
+ </div>
+ </nav>
+ <div className="main">
+ <MainFeeds />
+ <div className="mainRight">
+ <div className="mainRightTop">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="stories">
+ <div className="storiesTop">
+ <p className="story">์คํ ๋ฆฌ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ </div>
+ <div className="recommend">
+ <div className="storiesTop">
+ <p className="story">ํ์๋์ ์ํ ์ถ์ฒ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ </div>
+ <div>
+ <ul>
+ {INFO_LIST.map((info) => {
+ return (
+ <li key={info.id}>
+ <a href="{info.link}">{info.text}</a>
+ </li>
+ );
+ })}
+ </ul>
+ <p className="copyright">โ 2023 WESTAGRAM</p>
+ </div>
+ </div>
+ </div>
+ </>
+ );
+};
+
+export default Main; | JavaScript | Input์ ๊ฐ์ด ๋น์ด์์๋ ํจ์๊ฐ ๊ทธ๋ฅ ์๋ฌด๋ฐ ๋์์ ํ์ง ์๋๋ค๋ฉด ๊ธฐ๋ฅ์ ์๋ฌ์ธ์ง, ์๋๋ ๋์์ธ์ง ํ์
ํ๊ธฐ ์ด๋ ต์ต๋๋ค.
`comment` ๊ฐ์ด ๋น์ด์๋ค๋ฉด ํจ์๊ฐ ํธ์ถ๋ ๋ `alert`๋ฅผ ํ์ํด์ฃผ๋ ๋ฑ์ ์ถ๊ฐ์ ์ธ ์กฐ์น๊ฐ ์์ผ๋ฉด ์ข์ต๋๋ค |
@@ -0,0 +1,163 @@
+import React, { useState, useEffect } from "react";
+import MainFeeds from "./MainFeeds";
+import "./Main.scss";
+import compass from "../assets/icon/compass.png";
+import heart from "../assets/icon/heart.png";
+import instagram from "../assets/icon/instagram.png";
+import search from "../assets/icon/search.png";
+import user from "../assets/icon/user.png";
+import userimg from "../assets/images/test.jpg";
+import feedimg from "../assets/images/test.jpg";
+
+const INFO_LIST = [
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+];
+
+const Main = () => {
+ return (
+ <>
+ <nav className="nav">
+ <div className="navLeft">
+ <img className="navIcon iconInstagram" alt="." src={instagram} />
+ <span className="logo">Westagram</span>
+ </div>
+ <div className="searchBar">
+ <img className="iconSearch" alt="." src={search} />
+ <input
+ className="searchBox"
+ type="text"
+ aria-label="๊ฒ์์ฐฝ"
+ placeholder="๊ฒ์"
+ />
+ </div>
+ <div className="navRight">
+ <img className="navIcon" alt="." src={compass} />
+ <img className="navIcon" alt="." src={heart} />
+ <img className="navIcon" alt="." src={user} />
+ </div>
+ </nav>
+ <div className="main">
+ <MainFeeds />
+ <div className="mainRight">
+ <div className="mainRightTop">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="stories">
+ <div className="storiesTop">
+ <p className="story">์คํ ๋ฆฌ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ </div>
+ <div className="recommend">
+ <div className="storiesTop">
+ <p className="story">ํ์๋์ ์ํ ์ถ์ฒ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ </div>
+ <div>
+ <ul>
+ {INFO_LIST.map((info) => {
+ return (
+ <li key={info.id}>
+ <a href="{info.link}">{info.text}</a>
+ </li>
+ );
+ })}
+ </ul>
+ <p className="copyright">โ 2023 WESTAGRAM</p>
+ </div>
+ </div>
+ </div>
+ </>
+ );
+};
+
+export default Main; | JavaScript | map ๋ฉ์๋์ ์ฝ๋ฐฑํจ์์ ๋๋ฒ ์งธ ์ธ์, ์ฆ ์งํฌ๋์ ์ฝ๋์์ user๋ผ๊ณ ํ๋ ๋ณ์๋ ๊ธฐ์ค๋ฐฐ์ด(commnetArray)์ value(๋ฐฐ์ด์ ๊ฐ๊ฐ์ ์์)๋ง๋ค์ index(๋ฐฐ์ด๋ด์ ์์)๋ฅผ ๋ํ๋
๋๋ค. ๋ฆฌ์กํธ์์ ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํด์ key ์์ฑ์ ๊ผญ ๋ถ์ฌํ๋ผ๊ณ ํ๋ ์ด์ ๋ ํด๋น์์๋ฅผ ๋ฆฌ์กํธ๊ฐ ๋ช
ํ์ด ์ธ์ํ๊ธฐ ์ํจ์ธ๋ฐ, ๋ฐฐ์ด์ index๋ฅผ key๊ฐ์ผ๋ก ๋ถ์ฌํ๋๊ฒ์ ์กฐ๊ธ ์ง์ํ๋ ๋ฐฉ๋ฒ์
๋๋ค. ์ด๋ค ๊ฐ์ผ๋ก key ์์ฑ์ ๋ถ์ฌํด์ผํ ์ง ์กฐ๊ธ ๋ ๊ณ ๋ฏผํด๋ณด์ธ์! |
@@ -0,0 +1,163 @@
+import React, { useState, useEffect } from "react";
+import MainFeeds from "./MainFeeds";
+import "./Main.scss";
+import compass from "../assets/icon/compass.png";
+import heart from "../assets/icon/heart.png";
+import instagram from "../assets/icon/instagram.png";
+import search from "../assets/icon/search.png";
+import user from "../assets/icon/user.png";
+import userimg from "../assets/images/test.jpg";
+import feedimg from "../assets/images/test.jpg";
+
+const INFO_LIST = [
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+ { id: 1, link: "https://instargram.com", text: "์๊ฐ" },
+];
+
+const Main = () => {
+ return (
+ <>
+ <nav className="nav">
+ <div className="navLeft">
+ <img className="navIcon iconInstagram" alt="." src={instagram} />
+ <span className="logo">Westagram</span>
+ </div>
+ <div className="searchBar">
+ <img className="iconSearch" alt="." src={search} />
+ <input
+ className="searchBox"
+ type="text"
+ aria-label="๊ฒ์์ฐฝ"
+ placeholder="๊ฒ์"
+ />
+ </div>
+ <div className="navRight">
+ <img className="navIcon" alt="." src={compass} />
+ <img className="navIcon" alt="." src={heart} />
+ <img className="navIcon" alt="." src={user} />
+ </div>
+ </nav>
+ <div className="main">
+ <MainFeeds />
+ <div className="mainRight">
+ <div className="mainRightTop">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="stories">
+ <div className="storiesTop">
+ <p className="story">์คํ ๋ฆฌ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ </div>
+ </div>
+ <div className="recommend">
+ <div className="storiesTop">
+ <p className="story">ํ์๋์ ์ํ ์ถ์ฒ</p>
+ <p>๋ชจ๋ ๋ณด๊ธฐ</p>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ <div className="storyPeople">
+ <div className="photo">
+ <img src={userimg} alt="."></img>
+ </div>
+ <div className="name">
+ <p className="username">jini</p>
+ <p className="username2">์ง๋์ง๋</p>
+ </div>
+ <button>ํ๋ก์ฐ</button>
+ </div>
+ </div>
+ <div>
+ <ul>
+ {INFO_LIST.map((info) => {
+ return (
+ <li key={info.id}>
+ <a href="{info.link}">{info.text}</a>
+ </li>
+ );
+ })}
+ </ul>
+ <p className="copyright">โ 2023 WESTAGRAM</p>
+ </div>
+ </div>
+ </div>
+ </>
+ );
+};
+
+export default Main; | JavaScript | ๊ตณ์ด ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ง์๋๋ผ๋ `<textarea></textarea>`ํ๊ทธ๋ฅผ ์ฌ์ฉํ๋ฉด multiline input์ ํ์ฉํ ์ ์์ต๋๋ค. |
@@ -0,0 +1,264 @@
+@import '../../../style/reset.scss';
+@import '../../../style/variables.scss';
+
+body {
+ font-size: 14px;
+}
+.logo {
+ font-family: "Lobster";
+ src: url(../assets/Lobster-Regular.ttf);
+}
+input {
+ outline: none;
+}
+
+.navIcon {
+ width: 24px;
+ margin-left: 20px;
+}
+
+.navLeft{
+ .iconInstagram {
+ margin-left: 0;
+ }
+}
+.nav {
+ display: flex;
+ width: 860px;
+ margin: 0 auto;
+ justify-content: space-between;
+ align-items: center;
+ padding: 40px 0;
+
+ .logo {
+ font-size: 24px;
+ margin-left: 20px;
+ border-left: 1px solid black;
+ padding-left: 20px;
+ }
+}
+.navLeft, .searchBar, .navRight{
+ display: flex;
+ align-items: center;
+}
+
+.searchBar {
+ width: 300px;
+ background-color: $grey-color-bg;
+ border: 1px solid $grey-color-border;
+ border-radius: 5px;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+
+ .searchBox {
+ width: 50px;
+ padding : 8px 0;
+ border: none;
+ background-color: $grey-color-bg;
+ text-align: center;
+ margin-left: 10px;
+ overflow: auto;
+ outline: none;
+ &:focus {
+ outline: none;
+ }
+ }
+
+ .iconSearch {
+ width: 16px;
+ }
+}
+
+.bold {font-weight: bold;}
+.main {
+ width: 100%;
+ height: 100vh;
+ display: flex;
+ justify-content: center;
+ padding: 80px 40px;
+ background-color: $grey-color-bg;
+ border-top: 2px solid black;
+
+ .username {
+ font-weight: bold;
+ }
+
+ .feeds {
+ border: 1px solid $grey-color-border;
+ background-color: white;
+
+ .feedImg img {
+ width: 468px;
+ }
+ }
+ .feedsTop, .mainRightTop {
+ display: flex;
+ align-items: stretch;
+ padding: 15px;
+ }
+
+ .name {
+ display: flex;
+ margin-left: 15px;
+ justify-content: center;
+ flex-direction: column;
+ }
+ .location, .username2 {
+ font-size: 12px;
+ color: gray;
+ }
+
+ .photo img{
+ width: 50px;
+ border-radius: 100%;
+ }
+
+ .stories{
+ .photo {
+ background: radial-gradient(circle at bottom left, #F58529 20%, #C42D91);
+ border-radius: 100%;
+ height: 57px;
+ width: 57px;
+
+ img {
+ box-sizing: content-box;
+ border: 2px solid #fff;
+ margin: 1px;
+ }
+ }
+ }
+
+ .feedsBottom {
+ padding: 10px;
+
+ .photo img {
+ width: 24px;
+ margin-right: 10px;
+ padding: 1px;
+ border:1px solid rgb(188, 188, 188);
+ }
+
+ .like {
+ margin-top:10px;
+ display: inline-flex;
+ }
+
+ .feedsBottomIcons {
+ display: flex;
+ justify-content: space-between;
+
+ .bottomRight > .navIcon {
+ margin-left:0;
+ }
+
+ .bottomLeft .navIcon:first-child {
+ margin-left: 0;
+ }
+ }
+
+ .comment {
+ margin-top: 10px;
+
+ .writeTime {
+ color: grey;
+ margin-top: 10px;
+ }
+ .commentContainer{
+ position: relative;
+ }
+ .commentButton {
+ position: absolute;
+ right: 0;
+ top: 20px;
+ border: none;
+ background-color: inherit;
+ color: grey;
+ }
+ }
+
+ .description {
+ margin-top: 20px;
+ }
+
+ .commentBox {
+ width: 100%;
+ resize: none;
+ margin-top: 20px;
+ border-style: none;
+ border-bottom: 1px solid grey;
+
+ &:focus {
+ outline: none;
+ }
+ }
+ }
+
+ .mainRight {
+ width: 300px;
+ padding: 0 20px;
+
+ .stories, .recommend {
+ border: 1px solid $grey-color-border;
+ background-color: white;
+ padding: 15px;
+ height: 250px;
+ overflow: hidden;
+ }
+
+ .storiesTop {
+ display: flex;
+ justify-content: space-between;
+ font-size: 12px;
+ }
+
+ .recommend {
+ margin-top: 20px;
+ }
+
+ .story {
+ color: grey;
+ }
+
+ .storyPeople {
+ display: flex;
+ position: relative;
+ margin-top: 15px;
+
+ button {
+ position: absolute;
+ right: 0;
+ margin-top: 15px;
+ border: none;
+ background-color: inherit;
+ color: $point-color-blue;
+ font-weight: bold;
+
+ }
+ }
+ }
+
+ ul {
+ padding: 0;
+
+ li {
+ display: inline-block;
+ list-style-type: none;
+ color: $grey-color-193;
+ margin:5px 5px 0;
+ }
+ li:first-child {
+ margin-left: 0;
+ }
+
+ a {
+ text-decoration: none;
+ color: $grey-color-193;
+ }
+ }
+
+ .copyright {
+ color: $grey-color-193;
+ margin-top: 10px;
+ }
+}
\ No newline at end of file | Unknown | ์์ ๋จ๊ฒจ๋๋ฆฐ ๋ด์ฉ์ ๋ฆฌ๋ทฐ์ ๋์ผํฉ๋๋ค |
@@ -1,37 +1,20 @@
import java.util.List;
+import java.util.Set;
-import domain.LottoGame;
-import domain.LottoResults;
-import domain.WinningAnalyzer;
-import domain.WinningStatistics;
+import domain.ManualRequest;
import view.LottoInputView;
-import view.LottoOutputView;
public class LottoController {
- private LottoGame lottoGame;
- private WinningAnalyzer winningAnalyzer;
- public void playLottoGames(int money) {
- lottoGame = new LottoGame();
- lottoGame.generateLottoResultsFromMoney(money);
- }
-
- public void getLottoResults() {
- List<List<Integer>> results = lottoGame.getLottoResults().lottoNumbersToInt();
- int gameCount = lottoGame.getCount();
- LottoOutputView.printLottoResults(gameCount, results);
- }
-
- public void getWinningStatistics() {
- LottoResults lottoResults = lottoGame.getLottoResults();
- winningAnalyzer = new WinningAnalyzer(lottoResults, LottoInputView.getWinningNumbers(), LottoInputView.getBonusNumber());
- WinningStatistics winningStatistics = winningAnalyzer.calculateWinningStatistics();
- LottoOutputView.printBeforeWinnings();
- LottoOutputView.printWinnings(winningStatistics);
- }
-
- public void getReturnOnInvestment() {
- int money = lottoGame.getMoney();
- LottoOutputView.printReturnOnInvestment(winningAnalyzer.getReturnOnInvestment(money));
+ public static void main(String[] args) {
+ int money = LottoInputView.getMoney();
+ int manualCount = LottoInputView.getManualCount();
+ List<Set<Integer>> manualNumbers = LottoInputView.getManualLottos(manualCount);
+ LottoService lottoService = new LottoService();
+ ManualRequest manualRequest = new ManualRequest(manualCount, manualNumbers);
+ lottoService.playLottoGames(manualRequest, money);
+ lottoService.lottoResults();
+ lottoService.winningStatistics();
+ lottoService.returnOnInvestment();
}
} | Java | ์ผ๊ธ ์ปฌ๋ ์
์ ์จ๋ณด๋ฉด ์ด๋จ๊น์? |
@@ -1,13 +0,0 @@
-import view.LottoInputView;
-
-public class LottoMain {
-
- public static void main(String[] args) {
- int money = LottoInputView.getMoney();
- LottoController lottoController = new LottoController();
- lottoController.playLottoGames(money);
- lottoController.getLottoResults();
- lottoController.getWinningStatistics();
- lottoController.getReturnOnInvestment();
- }
-} | Java | - ๊ฐ ๋์
, ๋ฉ์๋ ํธ์ถ๊ณผ ๊ฐ์ด ๊ฐ๋
์ ์ ์ฌ์ฑ์ด ๋ค๋ฅธ ์ฝ๋๋ค๊ฐ์๋ ๊ฐํ์ผ๋ก ๊ฐ๋
์ฑ์ ๋ํ์ค ์ ์์ต๋๋ค.
- ์ปจํธ๋กค๋ฌ์๊ฒ ์ธ๋ถ ๊ธฐ๋ฅ๋ค์ ํธ์ถํ๊ณ ์์ต๋๋ค. ์ด๊ฑด ๋น์ฆ๋์ค๋ก์ง ์ค์ผ์คํธ๋ ์ด์
์ผ๋ก ๋ณด์ฌ์. ๊ทธ๋ฅ ์ ์ ํ Request ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ๊ณ ์ต์ข
๊ฒฐ๊ณผ๋ง ๋ฐํ๋ฐ์์ ์ถ๋ ฅ๊ณ์ธต์๊ฒ ์ ๋ฌํ๋ฉด ๋ ๊ฒ ๊ฐ์์.
- ํ์ฌ ๊ตฌ์กฐ๋ฅผ ๋ณด๋ฉด LottoMain์ด ์ปจํธ๋กค๋ฌ์ ์ญํ ์ ํ๊ณ ์๊ณ , LottoController๊ฐ Service Layer์ ์ญํ ์ ํ๊ณ ์์ต๋๋ค. ๊ทธ๋ผ ์ฐจ๋ผ๋ฆฌ Controller์์ View Layer๋ฅผ ๋ถ๋ฆฌํด LottoMain์ผ๋ก ์ฎ๊ธฐ์๊ณ LottoController์ ์ด๋ฆ์ LottoService๋ก ๋ฐ๊พธ๋๊ฑด ์ด๋จ๊น์ |
@@ -0,0 +1,15 @@
+package domain;
+
+public class GameCount {
+ private final int manualCount;
+ private final int automaticCount;
+
+ public GameCount(int totalCount, int manualCount) {
+ this.manualCount = manualCount;
+ this.automaticCount = totalCount - manualCount;
+ }
+
+ public int getAutomaticCount() {
+ return automaticCount;
+ }
+} | Java | ๋ด๋ถ์ ์ผ๋ก ๊ฐ์ด ๋ณ๊ฒฝ๋์ง ์๋๋ค๋ฉด final ํค์๋๋ฅผ ํตํด ๋ถ๋ณ์ฑ ํ๋ณด๋ฅผ ํ ์ ์์ ๊ฒ ๊ฐ๋ค์. |
@@ -1,23 +1,47 @@
package domain;
+import java.util.List;
+import java.util.Set;
+
public class LottoGame {
private LottoResults lottoResults;
-
private Money money;
+ private WinningAnalyzer winningAnalyzer;
public LottoGame() {
this.lottoResults = new LottoResults();
}
- public void generateLottoResultsFromMoney(int money) {
- this.money = new Money(money);
- for (int i = 0; i < getCount(); i++) {
- lottoResults.add(LottoNumGenerator.generateResult());
+ public void generateLottoResultsFromMoney(ManualRequest manualRequest, int money) {
+ int manualCount = manualRequest.getManualCount();
+ List<Set<Integer>> manualNumbers = manualRequest.getManualNumbers();
+ this.money = new Money(money, manualCount);
+ if (manualCount != manualNumbers.size()) {
+ throw new IllegalArgumentException("์๋ ๊ตฌ๋งค๊ฐ ์๋ชป๋์์ต๋๋ค.");
+ }
+ // ์๋ ๊ตฌ๋งค
+ generateManualResults(manualCount, manualNumbers);
+ generateAutomaticResults();
+ }
+
+ private void generateAutomaticResults() {
+ for (int i = 0; i < getAutomaticCount(); i++) {
+ lottoResults.add(LottoNumGenerator.generateAutomaticResults());
+ }
+ }
+
+ private void generateManualResults(int manualCount, List<Set<Integer>> manualNumbers) {
+ for (int i = 0; i < manualCount; i++) {
+ lottoResults.add(LottoNumGenerator.generateManualResults(manualNumbers.get(i)));
}
}
public int getCount() {
- return money.getCount();
+ return money.getTotalCount();
+ }
+
+ public int getAutomaticCount() {
+ return money.getAutomaticCount();
}
public int getMoney() {
@@ -27,5 +51,19 @@ public int getMoney() {
public LottoResults getLottoResults() {
return lottoResults;
}
+
+ public List<List<Integer>> getLottoResultsToInt() {
+ return lottoResults.lottoResultsToInt();
+ }
+
+ public WinningStatistics calculateWinningStatistics(Set<Integer> winningNumbers, int bonusNumber) {
+ winningAnalyzer = new WinningAnalyzer(lottoResults, winningNumbers, bonusNumber);
+ return winningAnalyzer.calculateWinningStatistics();
+ }
+
+ public float getReturnOnInvestment() {
+ return winningAnalyzer.getReturnOnInvestment(money.getMoney());
+ }
+
}
| Java | ์ด๋ฐ ์ฐธ์กฐํ์
๋ ์ผ๊ธ ์ปฌ๋ ์
์ ์ด์ฉํด์ ๋ด๋ถ ์ธ๋ฑ์ค ์ ์ด๋ฅผ ์ฑ
์ ๋ถ๋ฆฌํ ์ ์์ ๊ฒ ๊ฐ์์. |
@@ -1,23 +1,47 @@
package domain;
+import java.util.List;
+import java.util.Set;
+
public class LottoGame {
private LottoResults lottoResults;
-
private Money money;
+ private WinningAnalyzer winningAnalyzer;
public LottoGame() {
this.lottoResults = new LottoResults();
}
- public void generateLottoResultsFromMoney(int money) {
- this.money = new Money(money);
- for (int i = 0; i < getCount(); i++) {
- lottoResults.add(LottoNumGenerator.generateResult());
+ public void generateLottoResultsFromMoney(ManualRequest manualRequest, int money) {
+ int manualCount = manualRequest.getManualCount();
+ List<Set<Integer>> manualNumbers = manualRequest.getManualNumbers();
+ this.money = new Money(money, manualCount);
+ if (manualCount != manualNumbers.size()) {
+ throw new IllegalArgumentException("์๋ ๊ตฌ๋งค๊ฐ ์๋ชป๋์์ต๋๋ค.");
+ }
+ // ์๋ ๊ตฌ๋งค
+ generateManualResults(manualCount, manualNumbers);
+ generateAutomaticResults();
+ }
+
+ private void generateAutomaticResults() {
+ for (int i = 0; i < getAutomaticCount(); i++) {
+ lottoResults.add(LottoNumGenerator.generateAutomaticResults());
+ }
+ }
+
+ private void generateManualResults(int manualCount, List<Set<Integer>> manualNumbers) {
+ for (int i = 0; i < manualCount; i++) {
+ lottoResults.add(LottoNumGenerator.generateManualResults(manualNumbers.get(i)));
}
}
public int getCount() {
- return money.getCount();
+ return money.getTotalCount();
+ }
+
+ public int getAutomaticCount() {
+ return money.getAutomaticCount();
}
public int getMoney() {
@@ -27,5 +51,19 @@ public int getMoney() {
public LottoResults getLottoResults() {
return lottoResults;
}
+
+ public List<List<Integer>> getLottoResultsToInt() {
+ return lottoResults.lottoResultsToInt();
+ }
+
+ public WinningStatistics calculateWinningStatistics(Set<Integer> winningNumbers, int bonusNumber) {
+ winningAnalyzer = new WinningAnalyzer(lottoResults, winningNumbers, bonusNumber);
+ return winningAnalyzer.calculateWinningStatistics();
+ }
+
+ public float getReturnOnInvestment() {
+ return winningAnalyzer.getReturnOnInvestment(money.getMoney());
+ }
+
}
| Java | Money๊ฐ์ฒด์ธ๋ฐ count์ ๋ณด๊น์ง ๋ด์๋ฒ๋ฆฌ๋ฉด ์ผ๊ธ ๊ฐ์ฒด๋ก์จ์ ํจ์ฉ์ด ๋จ์ด์ง๊ณ ๊ฐ์ฒด ์๊ทธ๋์ฒ๊ฐ ๋ถ๋ถ๋ช
ํด์ง ๊ฒ ๊ฐ๋ค์ ๐ค |
@@ -1,23 +1,47 @@
package domain;
+import java.util.List;
+import java.util.Set;
+
public class LottoGame {
private LottoResults lottoResults;
-
private Money money;
+ private WinningAnalyzer winningAnalyzer;
public LottoGame() {
this.lottoResults = new LottoResults();
}
- public void generateLottoResultsFromMoney(int money) {
- this.money = new Money(money);
- for (int i = 0; i < getCount(); i++) {
- lottoResults.add(LottoNumGenerator.generateResult());
+ public void generateLottoResultsFromMoney(ManualRequest manualRequest, int money) {
+ int manualCount = manualRequest.getManualCount();
+ List<Set<Integer>> manualNumbers = manualRequest.getManualNumbers();
+ this.money = new Money(money, manualCount);
+ if (manualCount != manualNumbers.size()) {
+ throw new IllegalArgumentException("์๋ ๊ตฌ๋งค๊ฐ ์๋ชป๋์์ต๋๋ค.");
+ }
+ // ์๋ ๊ตฌ๋งค
+ generateManualResults(manualCount, manualNumbers);
+ generateAutomaticResults();
+ }
+
+ private void generateAutomaticResults() {
+ for (int i = 0; i < getAutomaticCount(); i++) {
+ lottoResults.add(LottoNumGenerator.generateAutomaticResults());
+ }
+ }
+
+ private void generateManualResults(int manualCount, List<Set<Integer>> manualNumbers) {
+ for (int i = 0; i < manualCount; i++) {
+ lottoResults.add(LottoNumGenerator.generateManualResults(manualNumbers.get(i)));
}
}
public int getCount() {
- return money.getCount();
+ return money.getTotalCount();
+ }
+
+ public int getAutomaticCount() {
+ return money.getAutomaticCount();
}
public int getMoney() {
@@ -27,5 +51,19 @@ public int getMoney() {
public LottoResults getLottoResults() {
return lottoResults;
}
+
+ public List<List<Integer>> getLottoResultsToInt() {
+ return lottoResults.lottoResultsToInt();
+ }
+
+ public WinningStatistics calculateWinningStatistics(Set<Integer> winningNumbers, int bonusNumber) {
+ winningAnalyzer = new WinningAnalyzer(lottoResults, winningNumbers, bonusNumber);
+ return winningAnalyzer.calculateWinningStatistics();
+ }
+
+ public float getReturnOnInvestment() {
+ return winningAnalyzer.getReturnOnInvestment(money.getMoney());
+ }
+
}
| Java | ์๋,์๋ ๋ก๋๋ฉด ๊ทธ๋ฅ LottoNumber, Lotto, Lottos ์ ๋๋ก ๋ค์ด๋ฐ์ ์ง์ด๋ ๋ ๊ฒ ๊ฐ๋ค์ Result๋ผ๊ณ ํ๋ ๋ญ๊ฐ ๋น๊ต๊น์ง ๋ค ๋๋ ์ต์ข
๊ฒฐ๊ณผ๋ฅผ ์๋ฏธํ๋ ๊ฑธ๋ก ๋ณด์
๋๋ค. |
@@ -1,23 +1,47 @@
package domain;
+import java.util.List;
+import java.util.Set;
+
public class LottoGame {
private LottoResults lottoResults;
-
private Money money;
+ private WinningAnalyzer winningAnalyzer;
public LottoGame() {
this.lottoResults = new LottoResults();
}
- public void generateLottoResultsFromMoney(int money) {
- this.money = new Money(money);
- for (int i = 0; i < getCount(); i++) {
- lottoResults.add(LottoNumGenerator.generateResult());
+ public void generateLottoResultsFromMoney(ManualRequest manualRequest, int money) {
+ int manualCount = manualRequest.getManualCount();
+ List<Set<Integer>> manualNumbers = manualRequest.getManualNumbers();
+ this.money = new Money(money, manualCount);
+ if (manualCount != manualNumbers.size()) {
+ throw new IllegalArgumentException("์๋ ๊ตฌ๋งค๊ฐ ์๋ชป๋์์ต๋๋ค.");
+ }
+ // ์๋ ๊ตฌ๋งค
+ generateManualResults(manualCount, manualNumbers);
+ generateAutomaticResults();
+ }
+
+ private void generateAutomaticResults() {
+ for (int i = 0; i < getAutomaticCount(); i++) {
+ lottoResults.add(LottoNumGenerator.generateAutomaticResults());
+ }
+ }
+
+ private void generateManualResults(int manualCount, List<Set<Integer>> manualNumbers) {
+ for (int i = 0; i < manualCount; i++) {
+ lottoResults.add(LottoNumGenerator.generateManualResults(manualNumbers.get(i)));
}
}
public int getCount() {
- return money.getCount();
+ return money.getTotalCount();
+ }
+
+ public int getAutomaticCount() {
+ return money.getAutomaticCount();
}
public int getMoney() {
@@ -27,5 +51,19 @@ public int getMoney() {
public LottoResults getLottoResults() {
return lottoResults;
}
+
+ public List<List<Integer>> getLottoResultsToInt() {
+ return lottoResults.lottoResultsToInt();
+ }
+
+ public WinningStatistics calculateWinningStatistics(Set<Integer> winningNumbers, int bonusNumber) {
+ winningAnalyzer = new WinningAnalyzer(lottoResults, winningNumbers, bonusNumber);
+ return winningAnalyzer.calculateWinningStatistics();
+ }
+
+ public float getReturnOnInvestment() {
+ return winningAnalyzer.getReturnOnInvestment(money.getMoney());
+ }
+
}
| Java | ์๋์์ฑ๊ณผ ์๋์์ฑ์ ๋ฉ์๋๋ก ๊ตฌ๋ถํ๊ณ ์๋๋ฐ LottoGenerator -> LottoAutoGenerator, LottoManualGenerator ๋ฑ์ผ๋ก ์ ๋ตํจํด์ ์ฌ์ฉํ ์๋ ์์ ๊ฒ ๊ฐ๋ค์ |
@@ -1,23 +1,47 @@
package domain;
+import java.util.List;
+import java.util.Set;
+
public class LottoGame {
private LottoResults lottoResults;
-
private Money money;
+ private WinningAnalyzer winningAnalyzer;
public LottoGame() {
this.lottoResults = new LottoResults();
}
- public void generateLottoResultsFromMoney(int money) {
- this.money = new Money(money);
- for (int i = 0; i < getCount(); i++) {
- lottoResults.add(LottoNumGenerator.generateResult());
+ public void generateLottoResultsFromMoney(ManualRequest manualRequest, int money) {
+ int manualCount = manualRequest.getManualCount();
+ List<Set<Integer>> manualNumbers = manualRequest.getManualNumbers();
+ this.money = new Money(money, manualCount);
+ if (manualCount != manualNumbers.size()) {
+ throw new IllegalArgumentException("์๋ ๊ตฌ๋งค๊ฐ ์๋ชป๋์์ต๋๋ค.");
+ }
+ // ์๋ ๊ตฌ๋งค
+ generateManualResults(manualCount, manualNumbers);
+ generateAutomaticResults();
+ }
+
+ private void generateAutomaticResults() {
+ for (int i = 0; i < getAutomaticCount(); i++) {
+ lottoResults.add(LottoNumGenerator.generateAutomaticResults());
+ }
+ }
+
+ private void generateManualResults(int manualCount, List<Set<Integer>> manualNumbers) {
+ for (int i = 0; i < manualCount; i++) {
+ lottoResults.add(LottoNumGenerator.generateManualResults(manualNumbers.get(i)));
}
}
public int getCount() {
- return money.getCount();
+ return money.getTotalCount();
+ }
+
+ public int getAutomaticCount() {
+ return money.getAutomaticCount();
}
public int getMoney() {
@@ -27,5 +51,19 @@ public int getMoney() {
public LottoResults getLottoResults() {
return lottoResults;
}
+
+ public List<List<Integer>> getLottoResultsToInt() {
+ return lottoResults.lottoResultsToInt();
+ }
+
+ public WinningStatistics calculateWinningStatistics(Set<Integer> winningNumbers, int bonusNumber) {
+ winningAnalyzer = new WinningAnalyzer(lottoResults, winningNumbers, bonusNumber);
+ return winningAnalyzer.calculateWinningStatistics();
+ }
+
+ public float getReturnOnInvestment() {
+ return winningAnalyzer.getReturnOnInvestment(money.getMoney());
+ }
+
}
| Java | ์ด ๋ฉ์๋๋ ProfitCalculator -> DefaultProfiltCalculator๋ฑ์ผ๋ก ๋ถ๋ฆฌํด์ ์ด์จ ๊ณ์ฐ๋ ์ถ์ํํ๋ฉด ์ ์ง๋ณด์์ฑ์ด ๋์์ง ๊ฒ ๊ฐ์์ |
@@ -1,7 +1,9 @@
package domain;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
+import java.util.Set;
public class LottoResult {
private List<LottoNumber> lottoResult;
@@ -14,10 +16,11 @@ public LottoResult(List<LottoNumber> lottoNumbers) {
this.lottoResult = lottoNumbers;
}
- public static LottoResult fromIntegers(List<Integer> lottoIntegers) {
+ public static LottoResult fromIntegers(Set<Integer> lottoIntegers) {
LottoResult lottoResult = new LottoResult();
- for (int i = 0; i < lottoIntegers.size(); i++) {
- lottoResult.lottoResult.add(LottoNumber.from(lottoIntegers.get(i)));
+ Iterator<Integer> iterator = lottoIntegers.iterator();
+ while (iterator.hasNext()) {
+ lottoResult.lottoResult.add(LottoNumber.from(iterator.next()));
}
return lottoResult;
}
@@ -38,4 +41,14 @@ public List<Integer> getLottoResult() {
}
return lottoNumbers;
}
+
+ public int calculateCount(WinningNumbers winningNumbers) {
+ int count = 0;
+ return lottoResult.stream().mapToInt(num -> num.addCountIfContain(count, winningNumbers)).sum();
+ }
+
+ public boolean isBonusMatch(BonusNumber bonusNumber) {
+ return lottoResult.stream()
+ .anyMatch(lottoNumber -> bonusNumber.isBonusMatch(lottoNumber));
+ }
} | Java | ๋๋ค์์์ count๋ผ๋ ์ธ๋ถ ๊ฐ์ ์ํฅ์ ์ฃผ๊ณ ์์ด์. ๐ค
๋๋ค์์ ์ธ๋ถ๊ฐ์ ๋ํ ์ํฅ์ ์ฃผ์ง ์์์ผ ํ๊ธฐ์ ์์ ํจ์๋ก ์ค๊ณ๋์ผํฉ๋๋ค. ๋ก์ง๋ด์์ ์ง์ count๊ฐ์ ์ง์ญ๋ณ์๋ฅผ ๋ณ๊ฒฝํ๋ ค๊ณ ํ๋ฉด effective final variable์ ์์ ํ๋ คํ๋ค๊ณ ์ปดํ์ผ ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒ์ธ๋ฐ, ๋ฉ์๋๋ฅผ ๋ค์ ํธ์ถํด์ ๊ทธ ๋ฌธ์ ๋ฅผ ํผํ์ง๋ง, ๊ทธ๋ ๋ค๊ณ ๋ฌธ์ ๊ฐ ๋์ง ์๋๊ฑด ์๋๋๋ค.
filter์ count๋ฑ์ ์ด์ฉํด์๊ตฌํํ ์๋ ์์ ๊ฒ ๊ฐ๋ค์ |
@@ -1,68 +1,75 @@
package domain;
+import static domain.WinningStatistics.THRESHOLD;
+
import java.util.HashMap;
import java.util.Map;
+import java.util.function.Function;
public enum WinningPrizes {
- MISS(0, 0) {
-
- },
- FIFTH_PRIZE(5, 5_000),
- FOURTH_PRIZE(4, 50_000),
- THIRD_PRIZE(3, 1_500_000),
- SECOND_PRIZE(2, 3_000_000),
- FIRST_PRIZE(1, 2_000_000_000);
-
- public static final int OFFSET = 3;
+ ZERO(0, false, 0, 0, count -> THRESHOLD),
+ ONE(1, false,0, 0, count -> THRESHOLD),
+ TWO(2, false, 5, 5_000, count -> THRESHOLD),
+ THREE(3, false, 5, 5_000, count -> count),
+ FOUR(4, false, 4, 50_000, count -> count),
+ FIVE(5, false, 3, 1_500_000, count -> count),
+ FIVE_BONUS(5, true, 2, 3_000_000, count -> count),
+ SIX(6, false, 1, 2_000_000_000, count -> count);
- private static final Map<Integer, WinningPrizes> WINNING_PRIZES_MAP = new HashMap<>();
+ private static final int HASH_GENERATION_NUMBER = 31;
+ private static final Map<Integer, WinningPrizes> WINNING_PRIZE_MATCHERS_MAP = new HashMap<>();
- static {
- for (WinningPrizes value : values()) {
- WINNING_PRIZES_MAP.put(value.rank, value);
- }
- }
+ private int numberOfCount;
+ private boolean isBonusMatch;
private int rank;
- private final int prizeMoney;
+ private int prizeMoney;
+ private Function<Integer, Integer> countSupplier;
- WinningPrizes(int rank, int prizeMoney) {
+
+ WinningPrizes(int numberOfCount, boolean isBonusMatch, int rank, int prizeMoney, Function<Integer, Integer> countSupplier) {
+ this.numberOfCount = numberOfCount;
+ this.isBonusMatch = isBonusMatch;
this.rank = rank;
this.prizeMoney = prizeMoney;
+ this.countSupplier = countSupplier;
}
- public int calculatePrizeMoney(int count) {
- return prizeMoney * count;
+ static {
+ for (WinningPrizes value : WinningPrizes.values()) {
+ WINNING_PRIZE_MATCHERS_MAP.put(getHashCode(value.numberOfCount, value.isBonusMatch), value);
+ }
}
- public int getPrizeMoney() {
- return prizeMoney;
+ public static int getHashCode(int numberOfCount, boolean isBonusMatch) {
+ int bonusNum = 0;
+ if (isBonusMatch) {
+ bonusNum = 1;
+ }
+ return numberOfCount * HASH_GENERATION_NUMBER + bonusNum;
}
- public int getRank() {
- return rank;
+ public static WinningPrizes valueOf(int numberOfCount, boolean isBonusMatch) {
+ isBonusMatch = checkBonus(numberOfCount, isBonusMatch);
+ return WINNING_PRIZE_MATCHERS_MAP.get(getHashCode(numberOfCount, isBonusMatch));
}
- public static WinningPrizes valueOf(int countOfMatch, boolean matchBonus) {
- if (countOfMatch < OFFSET) {
- return WinningPrizes.MISS;
- }
-
- if (WinningPrizes.SECOND_PRIZE.rank == countOfMatch) {
- return decideSecondOrThirdPrizes(matchBonus);
+ private static boolean checkBonus(int numberOfCount, boolean isBonusMatch) {
+ if (FIVE_BONUS.getNumberOfCount() != numberOfCount && isBonusMatch) {
+ isBonusMatch = false;
}
+ return isBonusMatch;
+ }
- return WINNING_PRIZES_MAP.get(countOfMatch);
+ public int calculatePrizeMoney(int count) {
+ return prizeMoney * count;
}
- public static WinningPrizes valueOf(int rank) {
- return WINNING_PRIZES_MAP.get(rank);
+ public int getPrizeMoney() {
+ return prizeMoney;
}
- private static WinningPrizes decideSecondOrThirdPrizes(boolean matchBonus) {
- if (matchBonus) {
- return WinningPrizes.SECOND_PRIZE;
- }
- return WinningPrizes.THIRD_PRIZE;
+ public int getNumberOfCount() {
+ return countSupplier.apply(numberOfCount);
}
} | Java | IntFunction์ ํ์ฉํด๋ ์ข์ ๊ฒ ๊ฐ์์. |
@@ -2,25 +2,62 @@
import static org.assertj.core.api.Assertions.assertThat;
+import java.util.ArrayList;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
import org.junit.jupiter.api.Test;
public class LottoGameTest {
@Test
- public void ๋ก๋_๊ตฌ์
๊ธ์ก์_์
๋ ฅํ๋ฉด_๊ตฌ์
๊ธ์ก์_ํด๋นํ๋_๋ก๋๋ฅผ_๋ฐํํ๋ค() {
+ public void ๋ก๋_๊ตฌ์
๊ธ์ก๊ณผ_๋ฒํธ๋ฅผ_์๋์ผ๋ก_์
๋ ฅํ๋ฉด_๊ตฌ์
๊ธ์ก์_ํด๋นํ๋_๋ก๋๋ฅผ_๋ฐํํ๋ค() {
//given
int money = 14000;
+ Set<Integer> manualLottoNumber = new HashSet<>();
+ manualLottoNumber.add(2);
+ manualLottoNumber.add(4);
+ manualLottoNumber.add(6);
+ manualLottoNumber.add(7);
+ manualLottoNumber.add(10);
+ manualLottoNumber.add(12);
+
+ int manualCount = 1;
LottoGame lottoGenerator = new LottoGame();
+ List<Set<Integer>> manualNumbers = new ArrayList<>();
+ manualNumbers.add(manualLottoNumber);
+ ManualRequest manualRequest = new ManualRequest(manualCount, manualNumbers);
+
//when
- lottoGenerator.generateLottoResultsFromMoney(money);
+ lottoGenerator.generateLottoResultsFromMoney(manualRequest, money);
LottoResults lottoResults = lottoGenerator.getLottoResults();
- List<List<Integer>> lottoResultList = lottoResults.lottoNumbersToInt();
+ List<List<Integer>> lottoResultList = lottoResults.lottoResultsToInt();
//then
assertThat(lottoResultList).hasSize(14);
for (List<Integer> lottoNum : lottoResultList) {
assertThat(lottoNum).hasSize(6);
}
}
+
+// @Test
+// public void ๋ก๋_๋ฒํธ๋ฅผ_์๋์ผ๋ก_์
๋ ฅํ _๋_์ค๋ณต๋_๋ฒํธ๋ฅผ_์
๋ ฅํ๋ฉด_์์ธ๋ฅผ_๋์ง๋ค() {
+// //given
+// int money = 1000;
+// List<Integer> manualLottoNumber = Arrays.asList(2, 2, 6, 7, 10, 12);
+// int manualCount = 1;
+// LottoGame lottoGenerator = new LottoGame();
+// List<List<Integer>> manualNumbers = new ArrayList<>();
+// manualNumbers.add(manualLottoNumber);
+// ManualRequest manualRequest = new ManualRequest(manualCount, manualNumbers);
+//
+// //when
+//
+//
+// //then
+//
+// }
+
+
+
} | Java | ์ฌ์ฉํ์ง ์๋ ์ฃผ์(์ฝ๋)์ ์ ๊ฑฐํด์ฃผ์ธ์! |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | ์๋ก ์ฑ๊ฒฉ์ด ๋ค๋ฅธ ๋ณ์ ์ ์ธ ๊ฐ์๋ ์ค๋ฐ๊ฟ์ ํ ๋ฒ ๋ฃ์ด์ฃผ๋ ๊ฒ ๋ ๋ณด๊ธฐ ์ข์ง ์์๊น์??๐ค |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | ์กฐ๊ฑด๋ฌธ์ ์ซ์์ ๋งค๊ฐ๋ณ์๊ฐ ์ผ์นํ๋ค์!
์ด๋ฐ ์์ผ๋ก ์ฝ๋๋ฅผ ์ค์ผ ์ ์์ง ์์๊น์??๐
```suggestion
answer = randomNumber(numberOfDigit[0]);
``` |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | JS์์ ์ ๊ณตํ๋ ํจ์๋ฅผ ์ ํ์ฉํ์
จ๋ค์! ๐ |
@@ -0,0 +1,96 @@
+const MAX_RANDOM_NUMBER = 9;
+const MIN_RANDOM_NUMBER = 1;
+const $inputNumber = document.querySelector('.input-number');
+const $gameTable = document.querySelector(".game-table");
+
+let answer;
+
+function inputDigit(numberOfDigit){
+ $gameTable.innerHTML = "<tr><td>์์</td><td>์ซ์</td><td>๊ฒฐ๊ณผ</td></tr>";
+ if(numberOfDigit === "3์๋ฆฌ"){
+ answer = randomNumber(3);
+ } else if(numberOfDigit === "4์๋ฆฌ"){
+ answer = randomNumber(4);
+ } else if(numberOfDigit === "5์๋ฆฌ"){
+ answer = randomNumber(5);
+ }
+}
+
+function randomNumber(numberOfDigit){
+ let result = 0;
+ let existingNumbers = [];
+ for (step = 0; step < numberOfDigit; step++) {
+ let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ while (existingNumbers.includes(randomNumber)){
+ randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);
+ }
+ existingNumbers.push(randomNumber);
+ }
+
+ let weight = 1;
+ for(j = 0; j < numberOfDigit; j++){
+ result += existingNumbers[j] * weight;
+ weight *= 10;
+ }
+
+ console.log(result);
+ return result;
+}
+
+let index = 1;
+function addValue(){
+ const newRow = $gameTable.insertRow();
+ const pitchResult = checkBallAndStrike(answer);
+ const convertedAnswer = answer.toString();
+
+ let isValid = 0;
+ if($inputNumber.value.length === convertedAnswer.length){
+ for(let i = 0; i < $inputNumber.value.length; i++){
+ for(let j = 0; j < $inputNumber.value.length; j++){
+ if(i !== j){
+ if($inputNumber.value[i] === $inputNumber.value[j]){
+ isValid = 1;
+ }
+ }
+ }
+ }
+ if(isValid === 0){
+ const turn = newRow.insertCell(0);
+ const number = newRow.insertCell(1);
+ const result = newRow.insertCell(2);
+ turn.innerText = `${index++}`;
+ number.innerText = `${$inputNumber.value}`
+ if(pitchResult[1] === convertedAnswer.length){
+ result.innerText = `O.K.`;
+ } else{
+ result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`;
+ }
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+ } else{
+ alert("์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ์
๋๋ค.");
+ }
+
+}
+
+function checkBallAndStrike(answer){
+ const convertedAnswer = answer.toString();
+ const convertedInputNumber = $inputNumber.value.toString();
+ let k = 0;
+ let score = [0, 0];
+ while(k < convertedAnswer.length){
+ if(convertedAnswer[k] === convertedInputNumber[k]){
+ score[1] += 1;
+ } else{
+ for(let item of convertedAnswer){
+ if(item === convertedInputNumber[k]){
+ score[0] += 1;
+ }
+ }
+ }
+ k += 1;
+ }
+ return score;
+}
\ No newline at end of file | JavaScript | ๋งค์ง ๋๋ฒ ๋ถ๋ฆฌ์ ๋ค์ด๋ฐ ์ปจ๋ฒค์
์ ์ค์ํ์
จ๋ค์! ๐ |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.