From b7039fd62ad5b80580b0bdd595995ed844dedb32 Mon Sep 17 00:00:00 2001 From: jihong Date: Mon, 15 Aug 2022 00:04:39 +0900 Subject: [PATCH] =?UTF-8?q?=EC=88=98=EB=8F=99=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../study/lotto/controller/Controller.java | 7 +++- .../study/lotto/domain/LotteryMachine.java | 36 +++++++++++++++++-- .../study/lotto/domain/LotteryTicket.java | 8 +++++ .../study/lotto/domain/LotteryTickets.java | 6 +++- .../org/sscc/study/lotto/view/InputView.java | 16 +++++++++ .../org/sscc/study/lotto/view/ResultView.java | 7 ++-- .../lotto/domain/LotteryMachineTest.java | 27 ++++++++++++-- .../study/lotto/domain/LotteryTicketTest.java | 25 +++++++++++++ .../lotto/domain/LotteryTicketsTest.java | 6 ++-- 9 files changed, 125 insertions(+), 13 deletions(-) create mode 100644 src/test/java/org/sscc/study/lotto/domain/LotteryTicketTest.java diff --git a/src/main/java/org/sscc/study/lotto/controller/Controller.java b/src/main/java/org/sscc/study/lotto/controller/Controller.java index b76f66a..7cb7189 100644 --- a/src/main/java/org/sscc/study/lotto/controller/Controller.java +++ b/src/main/java/org/sscc/study/lotto/controller/Controller.java @@ -7,6 +7,8 @@ import org.sscc.study.lotto.view.InputView; import org.sscc.study.lotto.view.ResultView; +import java.util.List; + public class Controller { public static void main(String[] args) { InputView inputView = new InputView(); @@ -15,9 +17,12 @@ public static void main(String[] args) { InputMoney money = inputView.inputPrice(); lotteryMachine.pay(money.getMoney()); + lotteryMachine.setManualNumber(inputView.inputManualNumber()); + List manualNumbers = inputView.inputLotteryNumbers(lotteryMachine.getManualNumber()); + lotteryMachine.buyManualTicket(manualNumbers); lotteryMachine.drawNumbers(); - resultView.printNumber(lotteryMachine.getNumber()); + resultView.printNumber(lotteryMachine.getNumber(), lotteryMachine.getManualNumber()); resultView.printLottoNumbers(lotteryMachine.getString()); WinningNumbers winningNumbers = inputView.inputWinningNumbers(); diff --git a/src/main/java/org/sscc/study/lotto/domain/LotteryMachine.java b/src/main/java/org/sscc/study/lotto/domain/LotteryMachine.java index fed77f7..14ef1ea 100644 --- a/src/main/java/org/sscc/study/lotto/domain/LotteryMachine.java +++ b/src/main/java/org/sscc/study/lotto/domain/LotteryMachine.java @@ -1,8 +1,14 @@ package org.sscc.study.lotto.domain; +import java.util.Arrays; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + public class LotteryMachine { private static final int PRICE = 1000; private int number; + private int manualNumber; private LotteryTickets lotteryTickets; private WinningStat winningStat = new WinningStat(); private final NumberStrategy numberStrategy = new RandomLottoNumberGenerator(); @@ -13,9 +19,31 @@ public void pay(int money) { winningStat = new WinningStat(); } + public void setManualNumber(int number) { + this.manualNumber = number; + validate(); + } + + private void validate() { + if (number - manualNumber < 0) { + throw new IllegalArgumentException("입급된 금액보다 수동으로 입력된 티켓이 더 많습니다."); + } + } + + public void buyManualTicket(List givenNumbers) { + givenNumbers.forEach(x -> lotteryTickets.addManualTicket(new LotteryTicket(getNumbers(x)))); + } + + public Set getNumbers(String givenNumber) { + return Arrays.stream(givenNumber.split(", ")) + .map(x -> new LottoNumber(Integer.parseInt(x))) + .collect(Collectors.toSet()); + } + + // 자동 public void drawNumbers() { - for (int i = 0; i < number; i++) { - lotteryTickets.addLotteryTicket(numberStrategy); + for (int i = 0; i < number - manualNumber; i++) { + lotteryTickets.addAutoTicket(numberStrategy); } } @@ -27,6 +55,10 @@ public int getNumber() { return number; } + public int getManualNumber() { + return manualNumber; + } + public WinningStat getStat(WinningNumbers winningNumbers) { lotteryTickets.duplicateNumbers(winningNumbers).forEach(winningStat::add); return winningStat; diff --git a/src/main/java/org/sscc/study/lotto/domain/LotteryTicket.java b/src/main/java/org/sscc/study/lotto/domain/LotteryTicket.java index 8b75e61..84098d8 100644 --- a/src/main/java/org/sscc/study/lotto/domain/LotteryTicket.java +++ b/src/main/java/org/sscc/study/lotto/domain/LotteryTicket.java @@ -5,12 +5,20 @@ import java.util.stream.Collectors; public class LotteryTicket { + private static final int NUMBER = 6; private final Set lotteryNumbers = new HashSet<>(); public LotteryTicket(Set numbers) { + validate(numbers); lotteryNumbers.addAll(numbers); } + private void validate(Set numbers) { + if (numbers.size() != NUMBER) { + throw new IllegalArgumentException("로또 번호는 6개여야 합니다."); + } + } + public String getString() { return "[" + lotteryNumbers.stream() .sorted() diff --git a/src/main/java/org/sscc/study/lotto/domain/LotteryTickets.java b/src/main/java/org/sscc/study/lotto/domain/LotteryTickets.java index 29e22a7..37b01cf 100644 --- a/src/main/java/org/sscc/study/lotto/domain/LotteryTickets.java +++ b/src/main/java/org/sscc/study/lotto/domain/LotteryTickets.java @@ -11,7 +11,11 @@ public LotteryTickets() { lotteryTickets = new ArrayList<>(); } - public void addLotteryTicket(NumberStrategy numberStrategy) { + public void addManualTicket(LotteryTicket lotteryTicket) { + lotteryTickets.add(lotteryTicket); + } + + public void addAutoTicket(NumberStrategy numberStrategy) { lotteryTickets.add(new LotteryTicket(numberStrategy.getNumber())); } diff --git a/src/main/java/org/sscc/study/lotto/view/InputView.java b/src/main/java/org/sscc/study/lotto/view/InputView.java index 8c244de..47c179d 100644 --- a/src/main/java/org/sscc/study/lotto/view/InputView.java +++ b/src/main/java/org/sscc/study/lotto/view/InputView.java @@ -3,6 +3,8 @@ import org.sscc.study.lotto.domain.InputMoney; import org.sscc.study.lotto.domain.WinningNumbers; +import java.util.ArrayList; +import java.util.List; import java.util.Scanner; public class InputView { @@ -13,6 +15,20 @@ public InputMoney inputPrice() { return new InputMoney(Integer.parseInt(sc.nextLine())); } + public int inputManualNumber() { + System.out.println("수동으로 구매할 로또 수를 입력해 주세요."); + return Integer.parseInt(sc.nextLine()); + } + + public List inputLotteryNumbers(int number) { + System.out.println("수동으로 구매할 번호를 입력해 주세요."); + List get = new ArrayList<>(); + for (int i = 0; i < number; i++) { + get.add(sc.nextLine()); + } + return get; + } + public WinningNumbers inputWinningNumbers() { System.out.println("지난 주 당첨 번호를 입력해 주세요."); String s = sc.nextLine(); diff --git a/src/main/java/org/sscc/study/lotto/view/ResultView.java b/src/main/java/org/sscc/study/lotto/view/ResultView.java index 8cd9023..4008cbb 100644 --- a/src/main/java/org/sscc/study/lotto/view/ResultView.java +++ b/src/main/java/org/sscc/study/lotto/view/ResultView.java @@ -4,14 +4,15 @@ import org.sscc.study.lotto.domain.WinningStat; public class ResultView { - public void printNumber(int number) { - System.out.println(number + "개를 구매했습니다."); - } public void printLottoNumbers(String result) { System.out.println(result); } + public void printNumber(int manual, int auto) { + System.out.println("수동으로 " + manual + "장, 자동으로 " + auto + "개를 구매했습니다."); + } + public void printStat(WinningStat winningStat) { System.out.println("당첨 통계"); System.out.println("---------"); diff --git a/src/test/java/org/sscc/study/lotto/domain/LotteryMachineTest.java b/src/test/java/org/sscc/study/lotto/domain/LotteryMachineTest.java index be9adc3..76a6b20 100644 --- a/src/test/java/org/sscc/study/lotto/domain/LotteryMachineTest.java +++ b/src/test/java/org/sscc/study/lotto/domain/LotteryMachineTest.java @@ -5,8 +5,9 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.*; class LotteryMachineTest { @Test @@ -41,4 +42,24 @@ public void testLottoNumber(String input) { WinningNumbers winningNumbers = new WinningNumbers(input, 7); }).isInstanceOf(IllegalArgumentException.class); } -} \ No newline at end of file + + @DisplayName("금액보다 더 많은 개수의 수동 로또 구매 개수가 입력되었을 때") + @Test + public void testManualNumber(){ + LotteryMachine lotteryMachine = new LotteryMachine(); + lotteryMachine.pay(1000); + assertThatThrownBy(()-> lotteryMachine.setManualNumber(3)).isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("수동로또 티켓이 올바르게 당첨 되는 지 확인") + @Test + public void testManualTicket(){ + LotteryMachine lotteryMachine = new LotteryMachine(); + lotteryMachine.pay(3000); + lotteryMachine.setManualNumber(3); + lotteryMachine.buyManualTicket(Arrays.asList("1, 2, 3, 4, 5, 6", + "1, 3, 5, 7, 9, 11", "2, 4, 6, 8, 10, 12")); + WinningStat stat = lotteryMachine.getStat(new WinningNumbers("1, 2, 3, 4, 5, 6", 7)); + assertThat(stat.get(Prize.FIRST)).isEqualTo(1); + } +} diff --git a/src/test/java/org/sscc/study/lotto/domain/LotteryTicketTest.java b/src/test/java/org/sscc/study/lotto/domain/LotteryTicketTest.java new file mode 100644 index 0000000..9935e45 --- /dev/null +++ b/src/test/java/org/sscc/study/lotto/domain/LotteryTicketTest.java @@ -0,0 +1,25 @@ +package org.sscc.study.lotto.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.HashSet; +import java.util.Set; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; + +class LotteryTicketTest { + @Test + @DisplayName("로또 번호 갯수가 6개가 아닐 때 에러처리 확인") + public void testLottoNumber() { + Set numbers = new HashSet<>(); + numbers.add(new LottoNumber(1)); + numbers.add(new LottoNumber(3)); + numbers.add(new LottoNumber(4)); + numbers.add(new LottoNumber(11)); + numbers.add(new LottoNumber(13)); + assertThatThrownBy(() -> { + LotteryTicket lotteryTicket = new LotteryTicket(numbers); + }).isInstanceOf(IllegalArgumentException.class); + } +} \ No newline at end of file diff --git a/src/test/java/org/sscc/study/lotto/domain/LotteryTicketsTest.java b/src/test/java/org/sscc/study/lotto/domain/LotteryTicketsTest.java index 9f90f6f..69d0a37 100644 --- a/src/test/java/org/sscc/study/lotto/domain/LotteryTicketsTest.java +++ b/src/test/java/org/sscc/study/lotto/domain/LotteryTicketsTest.java @@ -12,7 +12,7 @@ class LotteryTicketsTest { @DisplayName("당첨번호 입력 시 번호가 일치하는 지 확인") public void testWinning() { LotteryTickets lotteryTickets = new LotteryTickets(); - lotteryTickets.addLotteryTicket(new StaticLottoNumberGenerator()); + lotteryTickets.addAutoTicket(new StaticLottoNumberGenerator()); List matchData = lotteryTickets. duplicateNumbers(new WinningNumbers("1, 2, 3, 4, 5, 6", 7)); assertThat(matchData.get(0)).isEqualTo(Prize.FIRST); @@ -22,7 +22,7 @@ public void testWinning() { @DisplayName("2등(5개-보너스볼)이 일치되는 지 확인") public void testSecond() { LotteryTickets lotteryTickets = new LotteryTickets(); - lotteryTickets.addLotteryTicket(new StaticLottoNumberGenerator()); + lotteryTickets.addAutoTicket(new StaticLottoNumberGenerator()); List matchData = lotteryTickets. duplicateNumbers(new WinningNumbers("1, 2, 3, 4, 5, 10", 6)); assertThat(matchData.get(0)).isEqualTo(Prize.SECOND); @@ -32,7 +32,7 @@ public void testSecond() { @DisplayName("로또 번호가 문자열 형식에 맞게 출력되는 지 확인") public void testNumberString() { LotteryTickets lotteryTickets = new LotteryTickets(); - lotteryTickets.addLotteryTicket(new StaticLottoNumberGenerator()); + lotteryTickets.addAutoTicket(new StaticLottoNumberGenerator()); String result = lotteryTickets.getString(); assertThat(result).isEqualTo("[1, 2, 3, 4, 5, 6]"); }