-
Notifications
You must be signed in to change notification settings - Fork 0
3주차미션 #1
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: main
Are you sure you want to change the base?
3주차미션 #1
Changes from all commits
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 |
|---|---|---|
| @@ -1 +1,7 @@ | ||
| # java-lotto-precourse | ||
| 1. 로또 구입 금앱 입력받기 및 예외처리 | ||
| 2. 로또 갯수 구하기 | ||
| 3. 로또 발행하기 및 오름차순으로 정렬 | ||
| 4. 당첨 번호 입력 받기 | ||
| 5. 보너스 번호 입력 받기 | ||
| 6. 당첨 내역 출력 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| package lotto; | ||
|
|
||
| import lotto.controller.LottoController; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| new LottoController().run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package lotto.controller; | ||
|
|
||
| import lotto.service.LottoService; | ||
| import lotto.view.InputView; | ||
|
|
||
| public class LottoController { | ||
| public void run() { | ||
| int money = InputView.readMoney(); | ||
| int count = money / 1000; | ||
|
|
||
| LottoService lottoService = new LottoService(); | ||
| lottoService.buyLottos(count); | ||
| lottoService.inputWinningNumbers(); | ||
| lottoService.checkResults(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package lotto.model; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class Lotto { | ||
| private final List<Integer> numbers; | ||
|
|
||
| public Lotto() { | ||
| this.numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); | ||
| Collections.sort(numbers); // 오름차순 정렬 | ||
| } | ||
|
|
||
| public List<Integer> getNumbers() { | ||
| return numbers; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package lotto.service; | ||
|
|
||
| import lotto.model.Lotto; | ||
| import lotto.view.InputView; | ||
| import lotto.view.OutputView; | ||
| import java.util.*; | ||
|
|
||
| public class LottoService { | ||
| private List<Lotto> userLottos; // 사용자가 구매한 로또 리스트 | ||
| private List<Integer> winningNumbers; // 당첨 번호 | ||
| private int bonusNumber; // 보너스 번호 | ||
|
|
||
| public void buyLottos(int count) { | ||
| userLottos = new ArrayList<>(); | ||
| for (int i = 0; i < count; i++) { | ||
| userLottos.add(new Lotto()); | ||
| } | ||
| OutputView.printLottos(userLottos); | ||
| } | ||
|
|
||
| public void inputWinningNumbers() { | ||
| winningNumbers = InputView.getWinningNumbers(); | ||
| bonusNumber = InputView.getBonusNumber(winningNumbers); | ||
| } | ||
|
|
||
| public void checkResults() { | ||
| Map<Integer, Integer> results = new HashMap<>(); | ||
| results.put(3, 0); | ||
| results.put(4, 0); | ||
| results.put(5, 0); | ||
| results.put(6, 0); | ||
| results.put(7, 0); // 5개 + 보너스 | ||
|
|
||
| for (Lotto lotto : userLottos) { | ||
| int matchCount = countMatches(lotto.getNumbers()); | ||
| boolean hasBonus = lotto.getNumbers().contains(bonusNumber); | ||
|
|
||
| if (matchCount == 5 && hasBonus) results.put(7, results.get(7) + 1); | ||
| else if (results.containsKey(matchCount)) results.put(matchCount, results.get(matchCount) + 1); | ||
| } | ||
|
|
||
| OutputView.printResults(results); | ||
| } | ||
|
|
||
| private int countMatches(List<Integer> userNumbers) { | ||
| int count = 0; | ||
| for (int num : userNumbers) { | ||
| if (winningNumbers.contains(num)) { | ||
| count++; | ||
| } | ||
| } | ||
|
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. for문 말고 .stream()을 활용해보셔도 좋을 것 같아요! 더 간결하게 나올수도 있슴다 |
||
| return count; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package lotto.view; | ||
|
|
||
| import static camp.nextstep.edu.missionutils.Console.readLine; | ||
| import java.util.*; | ||
|
|
||
| public class InputView { | ||
| public static int readMoney() { | ||
| System.out.println("구입금액을 입력해 주세요."); | ||
|
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. 이런 시스템 메세지들을 enum으로 따로 빼서 관리하셔도 좋을 것 같아용!! |
||
| return validateMoney(readLine()); | ||
| } | ||
|
|
||
| public static List<Integer> getWinningNumbers() { | ||
| System.out.println("당첨 번호를 입력해 주세요."); | ||
| return validateNumbers(readLine().split(",")); | ||
| } | ||
|
|
||
| public static int getBonusNumber(List<Integer> winningNumbers) { | ||
| System.out.println("보너스 번호를 입력해 주세요."); | ||
| while (true) { | ||
| try { | ||
| int bonus = Integer.parseInt(readLine()); | ||
| if (winningNumbers.contains(bonus)) throw new IllegalArgumentException("보너스 번호는 당첨 번호와 중복될 수 없습니다."); | ||
| return bonus; | ||
| } catch (Exception e) { | ||
| System.out.println("[ERROR] " + e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static int validateMoney(String input) { | ||
| if (!input.matches("\\d+") || Integer.parseInt(input) % 1000 != 0) { | ||
| throw new IllegalArgumentException("구입 금액은 1,000원 단위여야 합니다."); | ||
| } | ||
| return Integer.parseInt(input); | ||
| } | ||
|
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. 이런 validate 함수들은 뷰에 넣으면 조금 부적합할수도 있을 것 같아요, 따로 Validator파일을 만들어서 Validator 클래스 아래로 여러 검증 메서드를 가지게 해도 될 것같아요! |
||
|
|
||
| private static List<Integer> validateNumbers(String[] input) { | ||
| if (input.length != 6) throw new IllegalArgumentException("당첨 번호는 6개여야 합니다."); | ||
|
|
||
| List<Integer> numbers = new ArrayList<>(); | ||
| for (String num : input) { | ||
| int number = Integer.parseInt(num.trim()); | ||
| if (number < 1 || number > 45 || numbers.contains(number)) { | ||
| throw new IllegalArgumentException("유효하지 않은 로또 번호입니다."); | ||
| } | ||
| numbers.add(number); | ||
| } | ||
| return numbers; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package lotto.view; | ||
|
|
||
| import lotto.model.Lotto; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class OutputView { | ||
| public static void printLottos(List<Lotto> lottos) { | ||
| System.out.println(lottos.size() + "개를 구매했습니다."); | ||
| for (Lotto lotto : lottos) { | ||
| System.out.println(lotto.getNumbers()); | ||
| } | ||
| } | ||
|
|
||
| public static void printResults(Map<Integer, Integer> results) { | ||
| System.out.println("\n당첨 통계"); | ||
| System.out.println("---"); | ||
| System.out.println("3개 일치 (5,000원) - " + results.get(3) + "개"); | ||
| System.out.println("4개 일치 (50,000원) - " + results.get(4) + "개"); | ||
| System.out.println("5개 일치 (1,500,000원) - " + results.get(5) + "개"); | ||
| System.out.println("5개 + 보너스 볼 일치 (30,000,000원) - " + results.get(7) + "개"); | ||
| System.out.println("6개 일치 (2,000,000,000원) - " + results.get(6) + "개"); | ||
| } | ||
| } | ||
|
|
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.
빠른 조회 및 입력을 신경써서 map을 한것 굉장히 좋은 시도라고 생각합니다!! 그런데 map이 은근 무거운편이라 길이 5짜리 배열을 쓰셔도 될것 같아용 이건 제 개인적인 코드 취향이라 무시하셔도 됨~~