-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Step2: 로또(자동) #3964
base: maeng24
Are you sure you want to change the base?
Step2: 로또(자동) #3964
Changes from all commits
9da3676
e7afde1
c4447f7
5cdc8ef
00c92ea
7fce25f
48cf976
84fa7dd
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import lotto.domain.Lotto; | ||
import lotto.domain.LottoChecker; | ||
import lotto.view.LottoInputView; | ||
import lotto.view.LottoOutputView; | ||
|
||
import java.util.List; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
|
||
LottoInputView.initiateScanner(); | ||
List<Lotto> purchasedLottos = LottoInputView.createPurchasedLottos(LottoInputView.scanAmount()); | ||
|
||
LottoOutputView.printPurchaseInfo(purchasedLottos); | ||
|
||
Lotto winLotto = LottoInputView.createLottoWithScan(LottoInputView.scanWinNumbers()); | ||
|
||
LottoOutputView.printResultInfo(new LottoChecker(purchasedLottos, winLotto)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package lotto.domain; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class Lotto { | ||
private final List<Integer> numbers; | ||
|
||
public Lotto(List<Integer> numbers) { | ||
numberEmptyCheck(numbers); | ||
duplicateNumberCheck(numbers); | ||
|
||
Collections.sort(numbers); | ||
this.numbers = numbers; | ||
} | ||
|
||
public Integer getLottoSize() { | ||
return this.numbers.size(); | ||
} | ||
|
||
private void duplicateNumberCheck(List<Integer> numbers) { | ||
if (numbers.stream().distinct().count() < 6) | ||
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. 소소한 의견인데 |
||
throw new IllegalArgumentException("중복된 숫자가 있습니다."); | ||
} | ||
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 void numberEmptyCheck(List<Integer> numbers) { | ||
if (numbers == null || numbers.isEmpty()) | ||
throw new IllegalArgumentException("로또 번호가 없습니다."); | ||
} | ||
|
||
public Boolean containsNumbers(List<Integer> numbers) { | ||
return this.numbers.containsAll(numbers); | ||
} | ||
|
||
public Integer getNumberByIndex(Integer index) { | ||
return this.numbers.get(index); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package lotto.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class LottoChecker { | ||
|
||
private final int[] matchCounts; | ||
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 final List<Lotto> purchasedLottos; | ||
private final Lotto winNumberLotto; | ||
|
||
public LottoChecker(List<Lotto> purchasedLottos, Lotto winNumberLotto) { | ||
winnerLottoEmptyCheck(winNumberLotto); | ||
|
||
this.purchasedLottos = purchasedLottos != null ? purchasedLottos : new ArrayList<>(); | ||
this.winNumberLotto = winNumberLotto; | ||
int[] matchCounts = new int[7]; | ||
|
||
for (Lotto lotto : this.purchasedLottos) { | ||
matchCounts[countMatch(lotto)]++; | ||
} | ||
|
||
this.matchCounts = matchCounts; | ||
} | ||
|
||
private int countMatch(Lotto lotto) { | ||
int count = 0; | ||
for (int i = 0; i < 6; i++) { | ||
count += countMatchNumber(lotto.getNumberByIndex(i)); | ||
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.
// Lotto
public List<Integer> getNumbers() {
return Collections.unmodifiableList(this.numbers);
}
// AS IS
for (int i = 0; i < 6; i++) {
count += countMatchNumber(lotto.getNumberByIndex(i));
}
// TO BE
for (Integer number : lotto.getNumbers()) {
count += countMatchNumber(number);
} |
||
} | ||
return count; | ||
} | ||
|
||
private void winnerLottoEmptyCheck(Lotto winnerLotto) { | ||
if (winnerLotto == null) | ||
throw new IllegalArgumentException("우승 번호가 없습니다."); | ||
} | ||
|
||
public Boolean containsWinNumbers(List<Integer> numbers) { | ||
return winNumberLotto.containsNumbers(numbers); | ||
} | ||
|
||
public Integer countMatchNumber(Integer number) { | ||
if (containsWinNumbers(Arrays.asList(number))) | ||
return 1; | ||
return 0; | ||
} | ||
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. 이 부분 처음에 코드 읽기가 좀 어려운 부분이었습니다 🤔 |
||
|
||
public Integer getWinnerCount(int index) { | ||
return this.matchCounts[index]; | ||
} | ||
|
||
public Integer getPurchaseCount() { | ||
return this.purchasedLottos.size(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package lotto.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class LottoFactory { | ||
|
||
private static final List<Integer> numbers = | ||
IntStream.rangeClosed(1, 45) | ||
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.
|
||
.boxed() | ||
.collect(Collectors.toList()); | ||
|
||
private static final Random random = new Random(); | ||
|
||
public static Lotto createLotto(List<Integer> lottoNumbers) { | ||
return new Lotto(lottoNumbers); | ||
} | ||
|
||
public static Lotto createLottoWithRandomNumbers() { | ||
List<Integer> copyOfNumbers = new ArrayList<>(numbers); | ||
Collections.shuffle(copyOfNumbers, random); | ||
return createLotto(copyOfNumbers.subList(0, 6)); | ||
} | ||
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,23 @@ | ||
package lotto.domain; | ||
|
||
public class LottoPrizeCalculator { | ||
|
||
private static final Integer LOTTO_PRICE = 1000; | ||
private static final int[] prizeMoney = {0, 0, 0, 5000, 50000, 1500000, 2000000000}; | ||
|
||
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.
|
||
public static Double calculateRateOfReturn(LottoChecker lottoChecker) { | ||
int prize = 0; | ||
for (int i = 3; i < 7; i++) { | ||
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. 위에서도 의견 드렸고, 1단계에서도 |
||
prize += calculatePrizeByNumberAndCount(i, lottoChecker.getWinnerCount(i)); | ||
} | ||
return (double) prize / (lottoChecker.getPurchaseCount() * LOTTO_PRICE); | ||
} | ||
|
||
public static Integer calculatePrizeByNumberAndCount(int numberOfMatch, int count) { | ||
return count * prizeMoney[numberOfMatch]; | ||
} | ||
|
||
public static Integer getPrizeByIndex(int index) { | ||
return prizeMoney[index]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package lotto.view; | ||
|
||
import lotto.domain.Lotto; | ||
import lotto.domain.LottoFactory; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
import java.util.stream.Collectors; | ||
|
||
public class LottoInputView { | ||
|
||
private static Scanner scanner; | ||
private static final Integer LOTTO_PRICE = 1000; | ||
|
||
public static void initiateScanner() { | ||
scanner = new Scanner(System.in); | ||
} | ||
|
||
public static Integer scanAmount() { | ||
System.out.println("구입금액을 입력해 주세요."); | ||
Integer amount = scanner.nextInt(); | ||
scanner.nextLine(); | ||
|
||
return amount; | ||
} | ||
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. ![]() 아마 별도로 질문 주신 부분일 것 같습니다.
보통 이럴 경우 2가지 해법이 있을 수 있을 것 같습니다. 다른 방법으로는 금액에 대한 원시객체를 만드는 방법입니다. 저는 개인적으로 상황에 따라서 1, 2번을 선택적, 복합적으로 사용합니다. 요 내용이 도움 되실 것 같습니다 🙇
|
||
|
||
public static String scanWinNumbers() { | ||
System.out.println("지난 주 당첨 번호를 입력해 주세요."); | ||
return scanner.nextLine(); | ||
} | ||
|
||
public static Lotto createLottoWithScan(String numberString) { | ||
String[] numbers = numberString.split(", "); | ||
|
||
return LottoFactory.createLotto( | ||
Arrays.stream(numbers) | ||
.map(Integer::parseInt) | ||
.collect(Collectors.toList())); | ||
} | ||
|
||
public static List<Lotto> createPurchasedLottos(Integer amount) { | ||
List<Lotto> purchasedLottos = new ArrayList<>(); | ||
int count = amount / LOTTO_PRICE; | ||
|
||
for (int i = 0; i < count; i++) { | ||
purchasedLottos.add( | ||
LottoFactory.createLottoWithRandomNumbers()); | ||
} | ||
|
||
return purchasedLottos; | ||
} | ||
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,39 @@ | ||
package lotto.view; | ||
|
||
import lotto.domain.Lotto; | ||
import lotto.domain.LottoChecker; | ||
import lotto.domain.LottoPrizeCalculator; | ||
|
||
import java.util.List; | ||
|
||
public class LottoOutputView { | ||
|
||
public static void printPurchaseInfo(List<Lotto> purchaseList) { | ||
System.out.println(purchaseList.size() + "개를 구매했습니다."); | ||
for (Lotto lotto : purchaseList) { | ||
System.out.println("[" + getPurchaseInfoToString(lotto) + "]"); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
private static String getPurchaseInfoToString(Lotto lotto) { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append(lotto.getNumberByIndex(0).toString()); | ||
|
||
for (int i = 1; i < 6; i++) { | ||
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.
|
||
sb.append(", " + lotto.getNumberByIndex(i).toString()); | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
|
||
public static void printResultInfo(LottoChecker lottoChecker) { | ||
System.out.println(); | ||
System.out.println("당첨 통계"); | ||
System.out.println("---------"); | ||
for (int i = 3; i < 7; i++) { | ||
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.
|
||
System.out.println(i + "개 일치 (" + LottoPrizeCalculator.getPrizeByIndex(i) + "원)-" + lottoChecker.getWinnerCount(i) + "개"); | ||
} | ||
System.out.println("총 수익률은 " + LottoPrizeCalculator.calculateRateOfReturn(lottoChecker) + "입니다."); | ||
} | ||
} |
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.
요구사항 정리도 잘 해주셨고, 이전단계 개선사항도 잘 정리 해주셨네요 💯