diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922..4e38ae5 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,12 @@ package lotto; +import lotto.controller.LottoController; + public class Application { public static void main(String[] args) { // TODO: 프로그램 구현 + + LottoController lottoController = new LottoController(); + lottoController.lottoStart(); } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java deleted file mode 100644 index 519793d..0000000 --- a/src/main/java/lotto/Lotto.java +++ /dev/null @@ -1,20 +0,0 @@ -package lotto; - -import java.util.List; - -public class Lotto { - private final List numbers; - - public Lotto(List numbers) { - validate(numbers); - this.numbers = numbers; - } - - private void validate(List numbers) { - if (numbers.size() != 6) { - throw new IllegalArgumentException(); - } - } - - // TODO: 추가 기능 구현 -} diff --git a/src/main/java/lotto/controller/LottoController.java b/src/main/java/lotto/controller/LottoController.java new file mode 100644 index 0000000..c1a5b9e --- /dev/null +++ b/src/main/java/lotto/controller/LottoController.java @@ -0,0 +1,46 @@ +package lotto.controller; + +import lotto.domain.LottoGenerator; +import lotto.view.OutputView; +import org.kokodak.Console; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; +import java.util.stream.Collectors; + +public class LottoController { + + public void lottoStart(){ + int buyMoney; + int bonusNumber; + List winLottoList; + List> lottoNumberList = new ArrayList<>(); + + Scanner sc = new Scanner(System.in); + + OutputView.printBuyMoney(); + buyMoney = sc.nextInt(); + + LottoGenerator lottoGenerator = new LottoGenerator(buyMoney); //로또 생성기 + + + OutputView.printBuyLotto(buyMoney); + for (int i=0; i numbers; + + public Lotto(List numbers) { + validate(numbers); + dupValidate(numbers); + this.numbers = numbers; + } + + private void validate(List numbers) { + if (numbers.size() != 6) { + throw new IllegalArgumentException(); + } + } + + // TODO: 추가 기능 구현 + private void dupValidate(List numbers){ + HashSet set = new HashSet<>(); + for(Integer e : numbers){ + set.add(String.valueOf(e)); + } + if(set.size()!=6){ + throw new IllegalArgumentException(); + } + } + + + + + +} diff --git a/src/main/java/lotto/domain/LottoGenerator.java b/src/main/java/lotto/domain/LottoGenerator.java new file mode 100644 index 0000000..eeb2f37 --- /dev/null +++ b/src/main/java/lotto/domain/LottoGenerator.java @@ -0,0 +1,38 @@ +package lotto.domain; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; + +public class LottoGenerator { + private final List lottos = new ArrayList<>(); + public final int lottoQuantity; + + public LottoGenerator(int money){ + validateMoney(money); + lottoQuantity = money / 1000; + } + + private void validateMoney(int money){ + if (isZeroOrNegativeNumber(money) || !isDividedByOneThousand(money)) { + throw new IllegalArgumentException(); + } + } + private boolean isZeroOrNegativeNumber(int money) { + return money <= 0; + } + + private boolean isDividedByOneThousand(int money) { + return money % 1000 == 0; + } + + public List generateLottos(){ + return new Random() + .ints(1, 45 + 1) + .distinct() + .limit(6) + .boxed() + .collect(Collectors.toList()); + } +} diff --git a/src/test/java/lotto/LottoGeneratorTest.java b/src/test/java/lotto/LottoGeneratorTest.java new file mode 100644 index 0000000..712e2e1 --- /dev/null +++ b/src/test/java/lotto/LottoGeneratorTest.java @@ -0,0 +1,52 @@ +package lotto; + +import lotto.domain.LottoGenerator; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class LottoGeneratorTest { + + @DisplayName("0원이면 예외가 발생한다.") + @Test + void createLottoNumberByZeroSize() { + assertThatThrownBy(() -> new LottoGenerator(0)) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("1000으로 안나눠지면 예외가 발생한다.") + @Test + void createLottoNumberByNotDivide() { + assertThatThrownBy(() -> new LottoGenerator(525)) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("잘 개수가 생성되는가.") + @Test + void lottoNumberSize() { + //give + final LottoGenerator lottoGenerator = new LottoGenerator(1000); + //when + final List lottoNumber = lottoGenerator.generateLottos(); + //then + assertThat(lottoNumber.size()).isEqualTo(6); + } + + @DisplayName("범위가 올바른지") + @Test + void lottoNumberRange(){ + //give + final LottoGenerator lottoGenerator = new LottoGenerator(1000); + //when + final List lottoNumber = lottoGenerator.generateLottos(); + //then + assertThat(lottoNumber.stream().allMatch(v -> v >= 1 && v <= 45)).isTrue(); + } + + + +} \ No newline at end of file diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 14ed50f..96f4dd0 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -1,8 +1,11 @@ package lotto; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.util.List; + +import lotto.domain.Lotto; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -23,4 +26,12 @@ void createLottoByDuplicatedNumber() { } // 아래에 추가 테스트 작성 가능 + + @DisplayName("랜덤으로 숫자 6개 생성") + @Test + void createRandomLottoNumber(){ + + + } + }