-
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
π 3λ¨κ³ - λ‘λ(2λ±) #3559
base: h3yon
Are you sure you want to change the base?
Changes from all commits
840dc19
842d683
aec102d
80c1d4b
b9175de
87a2d5b
2160905
a82ee90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package auto; | ||
|
||
import auto.application.AutoLottoService; | ||
import auto.application.MatchLottoService; | ||
import auto.application.MatchedAmount; | ||
import auto.domain.Lotto; | ||
import auto.domain.Lottos; | ||
import auto.view.InputView; | ||
import auto.view.ResultView; | ||
|
||
import java.util.Map; | ||
|
||
public class AutoLottoApplication { | ||
private static final AutoLottoService autoService = new AutoLottoService(); | ||
private static final MatchLottoService matchService = new MatchLottoService(); | ||
|
||
public static void main(String[] args) { | ||
int purchaseAmount = InputView.inputPurchaseAmount(); | ||
int lottoCount = autoService.getLottoCount(purchaseAmount); | ||
ResultView.printLottoCounts(lottoCount); | ||
|
||
Lottos totalLottoNumbers = Lottos.createLottoNumbersList(lottoCount); | ||
ResultView.printTotalLottoNumbers(totalLottoNumbers); | ||
|
||
Lotto winningNumbersLastWeek = InputView.inputWinningNumbersLastWeek(); | ||
int bonusBallNumber = InputView.inputBonusBallNumber(winningNumbersLastWeek); | ||
|
||
Map<MatchedAmount, Integer> matchedCountMap = matchService.getMatchedCountMap(totalLottoNumbers, | ||
winningNumbersLastWeek, | ||
bonusBallNumber); | ||
ResultView.printLottoStats(matchedCountMap, purchaseAmount); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package auto.application; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public class AutoLottoService { | ||
private static final int LOTTO_PRICE = 1000; | ||
|
||
public int getLottoCount(int purchaseAmount) { | ||
return purchaseAmount / LOTTO_PRICE; | ||
} | ||
|
||
public static BigDecimal getReturnRate(int totalAmount, int amount) { | ||
return BigDecimal.valueOf(totalAmount) | ||
.divide(BigDecimal.valueOf(amount)); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package auto.application; | ||
|
||
import auto.domain.Lotto; | ||
import auto.domain.Lottos; | ||
|
||
import java.util.ArrayList; | ||
import java.util.EnumSet; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static auto.application.MatchedAmount.NONE; | ||
import static auto.application.MatchedAmount.findByCount; | ||
|
||
public class MatchLottoService { | ||
private static final Map<MatchedAmount, Integer> matchedCountMap = new HashMap<>();// = MatchedAmount.getMatchedCountMap(); | ||
|
||
private static void setUpWinningStatisticsMap() { | ||
EnumSet.allOf(MatchedAmount.class) | ||
.stream() | ||
.filter(matchedAmount -> matchedAmount != NONE) | ||
.sorted() | ||
.forEach(matchedAmount -> matchedCountMap.put(matchedAmount, 0)); | ||
} | ||
|
||
static boolean isMatchedBonusNumber(List<Integer> lottoNumbers, | ||
List<Integer> winningNumbersLastWeek, | ||
int bonusBallNumber) { | ||
List<Integer> copyLottoNumbers = new ArrayList<>(lottoNumbers); | ||
copyLottoNumbers.removeAll(winningNumbersLastWeek); | ||
return copyLottoNumbers.contains(bonusBallNumber); | ||
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. bonusNumberκ° μ²μλΆν° λΉμ²¨ λ²νΈμ μ€λ³΅λμ§ μλ λ²νΈλΌλ©΄, removeAllμ μννλ©° λ§€λ² λ²νΈλ₯Ό μ κ±°ν νμκ° μμ κ² κ°λ€μ |
||
} | ||
|
||
public Map<MatchedAmount, Integer> getMatchedCountMap(Lottos lottoNumbersList, | ||
Lotto winningNumbersLastWeek, | ||
int bonusBallNumber) { | ||
setUpWinningStatisticsMap(); | ||
for (Lotto lotto : lottoNumbersList.getLottoList()) { | ||
setMatchedCountMap(lotto.getNumbers(), | ||
winningNumbersLastWeek.getNumbers(), | ||
isMatchedBonusNumber(lotto.getNumbers(), | ||
winningNumbersLastWeek.getNumbers(), | ||
bonusBallNumber)); | ||
} | ||
return matchedCountMap; | ||
} | ||
|
||
private void setMatchedCountMap(List<Integer> lottoNumbers, | ||
List<Integer> winningNumbersLastWeek, | ||
boolean isMatchedBonusNumber) { | ||
int matchedCountSum = (int) lottoNumbers.stream() | ||
.filter(winningNumbersLastWeek::contains) | ||
.count(); | ||
MatchedAmount matchedAmount = findByCount(matchedCountSum, isMatchedBonusNumber); | ||
if (matchedAmount.getCount() > 0) { | ||
matchedCountMap.put(matchedAmount, matchedCountMap.get(matchedAmount) + 1); | ||
} | ||
} | ||
Comment on lines
+51
to
+58
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. Stream APIμ groupByλ₯Ό νμ©νλ€λ©΄ μ’ λ κ°λ¨νκ²λ κ°λ₯νλ°, κ³ λ―Όν΄λ³΄λ©΄ μ’μ κ² κ°μ΅λλ€. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ public enum MatchedAmount { | |
THREE(3, 5_000), | ||
FOUR(4, 50_000), | ||
FIVE(5, 1_500_000), | ||
FIVE_AND_BONUS(5, 30_000_000), | ||
SIX(6, 2_000_000_000), | ||
NONE(0, 0); | ||
|
||
|
@@ -28,7 +29,10 @@ public enum MatchedAmount { | |
this.amount = amount; | ||
} | ||
|
||
public static MatchedAmount findByCount(int count) { | ||
public static MatchedAmount findByCount(int count, boolean isMatchedBonus) { // findByNumberWithMatchedBonus | ||
if (count == FIVE.getCount()) { | ||
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. μ§κΈμ 보λμ€ λ²νΈ λ§€μΉ μ¬λΆλ§ κ°μ§κ³ λ±κΈ νλ¨μ΄λμμ§λ§, μ΄λ° 쑰건λ€μ΄ κ³μν΄μ μΆκ°λλ€λ©΄ ν΄λΉ λ‘μ§μ κ³μν΄μ νλ¨ λΆκΈ°λ¬Έμ΄ μΆκ°λμΌν κΉμ? λλ€ννμμ νμ©ν΄μ μ΄κ±°νμ μ κ° μΈμ€ν΄μ€κ° μμ μ΄ λ§λμ§ νμ ν μ μλλ‘ ν μ μμκΉμ? |
||
return isMatchedBonus ? FIVE_AND_BONUS : FIVE; | ||
} | ||
if (!MATCHED_AMOUNT_MAP.containsKey(count)) { | ||
return NONE; | ||
} | ||
|
@@ -42,6 +46,10 @@ public static ConcurrentMap<Integer, Integer> getMatchedCountMap() { | |
.collect(Collectors.toConcurrentMap(MatchedAmount::getCount, u -> 0, (u, v) -> u)); | ||
} | ||
|
||
public static boolean isMatchedNumberFiveAndBonus(MatchedAmount matchedAmount) { | ||
return FIVE_AND_BONUS == matchedAmount; | ||
} | ||
|
||
public int getCount() { | ||
return count; | ||
} | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package auto.domain; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class Lotto { | ||
public final static int LOTTO_MIN_NUMBER = 1; | ||
public final static int LOTTO_MAX_NUMBER = 45; | ||
private static final String LOTTO_NUMBER_SIZE_ERROR_TEXT = "λ‘λ λ²νΈλ 6κ°μ μ«μλ‘ μ΄λ£¨μ΄μ ΈμΌ ν©λλ€."; | ||
private static final int MAXIMUM_LOTTO_NUMBER_SIZE = 6; | ||
private final List<Integer> numbers; | ||
Comment on lines
+12
to
+13
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 Lotto(List<Integer> numbers) { | ||
if (numbers == null || numbers.size() != MAXIMUM_LOTTO_NUMBER_SIZE) { | ||
throw new IllegalArgumentException(LOTTO_NUMBER_SIZE_ERROR_TEXT); | ||
} | ||
this.numbers = numbers; | ||
} | ||
|
||
public List<Integer> getNumbers() { | ||
return Collections.unmodifiableList(numbers); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "" + numbers; | ||
} | ||
|
||
public static Lotto createLottoNumbers() { | ||
List<Integer> numbers = IntStream.range(LOTTO_MIN_NUMBER, LOTTO_MAX_NUMBER + 1) | ||
.boxed() | ||
.collect(Collectors.toList()); | ||
Comment on lines
+32
to
+34
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.
|
||
Collections.shuffle(numbers); | ||
return new Lotto(numbers.subList(0, 6)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package auto.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class Lottos { | ||
private final List<Lotto> lottoList; | ||
|
||
public Lottos(List<Lotto> lottoList) { | ||
this.lottoList = lottoList; | ||
} | ||
|
||
public static Lottos createLottoNumbersList(int lottoCount) { | ||
return new Lottos(IntStream.range(0, lottoCount) | ||
.mapToObj(i -> Lotto.createLottoNumbers()) | ||
.collect(Collectors.toCollection(ArrayList::new))); | ||
} | ||
|
||
public List<Lotto> getLottoList() { | ||
return lottoList; | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,45 @@ | ||
package auto.view; | ||
|
||
import auto.domain.Lotto; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
import java.util.stream.Collectors; | ||
|
||
import static auto.domain.Lotto.LOTTO_MAX_NUMBER; | ||
import static auto.domain.Lotto.LOTTO_MIN_NUMBER; | ||
|
||
public class InputView { | ||
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 final Scanner scanner = new Scanner(System.in); | ||
private static final String EMPTY_BONUS_BALL_MESSAGE = "보λμ€ λ³Όμ΄ λΉμ΄μμ΅λλ€."; | ||
private static final String IN_LOTTO_BONUS_BALL_MESSAGE = "λ‘λ λ²νΈμ ν¬ν¨λ 보λμ€ λ³Όμ λλ€."; | ||
private static final String NOT_RANGE_LOTTO_NUMBER_MESSAGE = "λ²νΈλ 1 - 45κΉμ§ μ λ ₯ν μ μμ΅λλ€."; | ||
|
||
public static int inputPurchaseAmount() { | ||
System.out.println("ꡬμ κΈμ‘μ μ λ ₯ν΄ μ£ΌμΈμ."); | ||
return Integer.parseInt(scanner.nextLine()); | ||
} | ||
|
||
public static List<Integer> inputWinningNumbersLastWeek() { | ||
public static Lotto inputWinningNumbersLastWeek() { | ||
System.out.println("μ§λ μ£Ό λΉμ²¨ λ²νΈλ₯Ό μ λ ₯ν΄ μ£ΌμΈμ."); | ||
String winningNumbersLastWeek = scanner.nextLine(); | ||
return Arrays.stream(winningNumbersLastWeek.split(", ")) | ||
.map(Integer::parseInt) | ||
.collect(Collectors.toList()); | ||
return new Lotto(Arrays.stream(winningNumbersLastWeek.split(", ")) | ||
.map(Integer::parseInt) | ||
.collect(Collectors.toList())); | ||
} | ||
|
||
public static int inputBonusBallNumber(Lotto lastWeekWinningNumber) { | ||
System.out.println(EMPTY_BONUS_BALL_MESSAGE); | ||
int bonusNumber = Integer.parseInt(scanner.nextLine()); | ||
if (lastWeekWinningNumber.getNumbers() | ||
.contains(bonusNumber)) | ||
throw new IllegalArgumentException(IN_LOTTO_BONUS_BALL_MESSAGE); | ||
if (isNotInLottoNumberRange(bonusNumber)) | ||
throw new IllegalArgumentException(NOT_RANGE_LOTTO_NUMBER_MESSAGE); | ||
return bonusNumber; | ||
} | ||
|
||
private static boolean isNotInLottoNumberRange(int bonusNumber) { | ||
return !(bonusNumber >= LOTTO_MIN_NUMBER && bonusNumber <= LOTTO_MAX_NUMBER); | ||
} | ||
} |
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.