-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Step4 #4156
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
base: youlalala
Are you sure you want to change the base?
Step4 #4156
Changes from all commits
9890191
1dfd6b3
d674e57
4a48f96
bf407d9
5af39cb
100e4a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,7 @@ | |
### 3단계 - 로또(2등) | ||
- [X] 보너스볼 입력 | ||
- [X] rank 2등 추가 | ||
|
||
### 4단계 - 로또(수동) | ||
- [X] 수동으로 구매할 로또 수 입력 | ||
- [X] 수동으로 구매할 로또 번호 입력 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,33 +3,39 @@ | |
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class Lotto { | ||
public static final int LOTTO_PRICE = 1000; | ||
public static final long LOTTO_NUM_COUNT = 6; | ||
|
||
private final List<Integer> lottoNumbers; | ||
private final Set<LottoNumber> lottoNumbers; | ||
|
||
public Lotto(List<Integer> lottoNumbers) { | ||
validateLottoNumbers(lottoNumbers); | ||
this.lottoNumbers = lottoNumbers; | ||
public Lotto(List<Integer> numbers) { | ||
this(new HashSet<>(numbers)); | ||
} | ||
|
||
public List<Integer> getLottoNumbers() { | ||
public Lotto(Set<Integer> numbers) { | ||
this.lottoNumbers = numbers.stream().map(LottoNumber::of).collect(Collectors.toSet()); | ||
validateLottoNumbers(); | ||
} | ||
|
||
public Set<LottoNumber> getLottoNumbers() { | ||
return lottoNumbers; | ||
} | ||
|
||
public LottoRank getLottoRank(List<Integer> winningNumbers, Integer bonusNumber) { | ||
return LottoRank.fromMatchCount((int) this.lottoNumbers.stream().filter(winningNumbers::contains).count(), lottoNumbers.contains(bonusNumber)); | ||
public LottoRank getLottoRank(Lotto winningNumbers, int bonusNumber) { | ||
int matchCount = (int) this.lottoNumbers.stream().filter(winningNumbers.getLottoNumbers()::contains).count(); | ||
boolean isBonusNumber = lottoNumbers.contains(LottoNumber.of(bonusNumber)); | ||
return LottoRank.fromMatchCount(matchCount, isBonusNumber); | ||
} | ||
|
||
private void validateLottoNumbers(List<Integer> numbers) { | ||
if (numbers.size() != LOTTO_NUM_COUNT) { | ||
private void validateLottoNumbers() { | ||
if (lottoNumbers.size() > LOTTO_NUM_COUNT) { | ||
throw new IllegalArgumentException("로또 번호는 6개여야 합니다."); | ||
} | ||
|
||
Set<Integer> uniqueNumbers = new HashSet<>(numbers); | ||
if (uniqueNumbers.size() != LOTTO_NUM_COUNT) { | ||
if (lottoNumbers.size() < LOTTO_NUM_COUNT) { | ||
throw new IllegalArgumentException("로또 번호는 중복될 수 없습니다."); | ||
Comment on lines
-32
to
39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서는 요 표현식이 가장 정확하겠네요 👍 |
||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package lotto; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.IntStream; | ||
|
||
public class LottoNumber { | ||
|
||
static final int LOTTO_MIN_NUM = 1; | ||
static final int LOTTO_MAX_NUM = 45; | ||
Comment on lines
+9
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 접근 제한자를 default(package private) 을 선택해주셨네요! |
||
|
||
public static final Map<Integer, LottoNumber> lottoNumberMap = IntStream.rangeClosed(LOTTO_MIN_NUM, LOTTO_MAX_NUM) | ||
.boxed() | ||
.collect(HashMap::new, (map, number) -> map.put(number, new LottoNumber(number)), HashMap::putAll); | ||
|
||
private final int number; | ||
|
||
private LottoNumber(int number) { | ||
this.number = number; | ||
} | ||
Comment on lines
+6
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
private static void validateLottoNumber(Integer number) { | ||
if (number == null) { | ||
throw new IllegalArgumentException("로또 번호는 null일 수 없습니다."); | ||
} | ||
if (number < LOTTO_MIN_NUM || number > LOTTO_MAX_NUM) { | ||
throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다."); | ||
} | ||
} | ||
|
||
public static LottoNumber of(Integer number) { | ||
validateLottoNumber(number); | ||
return lottoNumberMap.get(number); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.valueOf(number); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,57 @@ | ||
package view; | ||
|
||
import lotto.Lotto; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
|
||
private static final Scanner scanner = new Scanner(System.in); | ||
public static String showExpressionInput() { | ||
System.out.println("수식 입력 > "); | ||
Scanner scanner = new Scanner(System.in); | ||
return scanner.nextLine(); | ||
} | ||
|
||
public static Integer showLottoPurchaseAmountInput() { | ||
public static int showLottoPurchaseAmountInput() { | ||
System.out.println("구입금액을 입력해 주세요."); | ||
Scanner scanner = new Scanner(System.in); | ||
return scanner.nextInt(); | ||
return readInt(); | ||
} | ||
|
||
public static String showWinningLottoNumbersInput() { | ||
public static Lotto showWinningLottoNumbersInput() { | ||
System.out.println("\n지난 주 당첨 번호를 입력해 주세요."); | ||
Scanner scanner = new Scanner(System.in); | ||
return scanner.nextLine(); | ||
String input = scanner.nextLine(); | ||
return new Lotto(LottoNumbersParser.parse(input)); | ||
} | ||
|
||
public static Integer showLottoBonusNumberInput() { | ||
public static int showLottoBonusNumberInput() { | ||
System.out.println("보너스 볼을 입력해 주세요."); | ||
Scanner scanner = new Scanner(System.in); | ||
return scanner.nextInt(); | ||
return readInt(); | ||
} | ||
|
||
public static int showManualLottoCountInput() { | ||
System.out.println("\n수동으로 구매할 로또 수를 입력해 주세요."); | ||
return readInt(); | ||
} | ||
|
||
public static List<Lotto> showManualLottoNumbersInput(int manualLottoCount) { | ||
System.out.println("\n수동으로 구매할 번호를 입력해 주세요."); | ||
List<Lotto> manualLottoNumbers = new ArrayList<>(); | ||
for (int i = 0; i < manualLottoCount; i++) { | ||
String input = scanner.nextLine(); | ||
manualLottoNumbers.add(new Lotto(LottoNumbersParser.parse(input))); | ||
} | ||
return manualLottoNumbers; | ||
} | ||
|
||
private static int readInt() { | ||
String input = scanner.nextLine(); | ||
try { | ||
return Integer.parseInt(input); | ||
} catch (NumberFormatException e) { | ||
System.out.println("잘못된 입력입니다. 다시 시도하세요."); | ||
return readInt(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package lotto; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
public class LottoNumberTest { | ||
|
||
@Test | ||
void of_1에서_45사이_숫자면_같은_객체_반환한다() { | ||
assertThat(LottoNumber.of(2)).isEqualTo(LottoNumber.of(2)); | ||
} | ||
|
||
@Test | ||
void of_1미만일_경우_예외를_던진다() { | ||
assertThatThrownBy(() -> LottoNumber.of(0)) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
void of_45초과일_경우_예외를_던진다() { | ||
assertThatThrownBy(() -> LottoNumber.of(46)) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
Comment on lines
+9
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 경계값을 이용한 꼼꼼한 테스트 💯 |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
별건 아니지만! 이런 경우엔
lottoNumber.size() != LOTTO_NUM_COUNT
가 그대로 유지되어도 괜찮을거 같아요 😄