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
๋งค์ง ๋„˜๋ฒ„ ๋ถ„๋ฆฌ์™€ ๋„ค์ด๋ฐ ์ปจ๋ฒค์…˜์„ ์ค€์ˆ˜ํ•˜์…จ๋„ค์š”! ๐Ÿ‘