Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] 로또 만들기 #2121

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/main/java/lotto/BuyingLotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lotto;

import camp.nextstep.edu.missionutils.Console;

public class BuyingLotto {
static int lottoCount;

public BuyingLotto() {
buyLotto();
}

private void buyLotto() {
boolean validInput = false;

while (!validInput) {
try {
System.out.println("구입금액을 입력해 주세요.");
String input = Console.readLine();
int money = Integer.parseInt(input);
validateInput(money);
lottoCount = money / 1000;
System.out.println(lottoCount + "개를 구매했습니다.");
validInput = true;
} catch (IllegalArgumentException e) {
System.out.println("[ERROR] 금액은 1000원 단위여야 합니다. 다시 입력해 주세요.");
}
}
}

public int getLottoCount() {
return lottoCount;
}

private void validateInput(int money) {
if (money % 1000 == 0) return;
throw new IllegalArgumentException();
}

}
26 changes: 26 additions & 0 deletions src/main/java/lotto/LottoCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package lotto;

import camp.nextstep.edu.missionutils.Randoms;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class LottoCreator {
private List<Lotto> lottoList;

public LottoCreator(int count) {
createLotto(count);
}

private void createLotto(int count) {
lottoList = IntStream.range(0, count)
.mapToObj(i -> new Lotto(LottoNumberGenerator.generateLottoNumber()))
.collect(Collectors.toList());

}

public List<Lotto> getLottoList() {
return lottoList;
}
}
24 changes: 24 additions & 0 deletions src/main/java/lotto/LottoNumberGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package lotto;

import camp.nextstep.edu.missionutils.Randoms;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class LottoNumberGenerator {
private static final int MAX_NUM = 45;
private static final int MIN_NUM = 1;
private static final int COUNT = 6;

private LottoNumberGenerator() {
throw new UnsupportedOperationException("Utility class");
}

public static List<Integer> generateLottoNumber() {
List<Integer> numbers = Randoms.pickUniqueNumbersInRange(MIN_NUM, MAX_NUM, COUNT);
Collections.sort(numbers);
return numbers;
}
}