-
Notifications
You must be signed in to change notification settings - Fork 92
[LBP] 현정빈 로또 미션 1단계 제출합니다. #83
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
Changes from 8 commits
5e2fb30
f9f5a87
1308961
d7bfc50
8188fc6
a63a201
643a77f
2be807c
4a44acc
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,9 @@ | ||
| import controller.LottoController; | ||
|
|
||
| public class Application { | ||
|
|
||
| public static void main(String[] args) { | ||
| LottoController lottoController = new LottoController(); | ||
| lottoController.run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package controller; | ||
|
|
||
| import model.Lotto; | ||
| import model.LottoTickets; | ||
| import view.InputView; | ||
| import view.ResultView; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class LottoController { | ||
|
|
||
| public void run() { | ||
| try { | ||
| int purchaseAmount = InputView.getPurchaseAmount(); | ||
|
|
||
| validatePurchaseAmount(purchaseAmount); | ||
|
|
||
| int ticketCount = Lotto.getTicketCount(purchaseAmount); | ||
|
|
||
| LottoTickets lottoTickets = new LottoTickets(ticketCount); | ||
|
|
||
| ResultView.printOrderTickets(ticketCount); | ||
| ResultView.printPurchasedLottoTickets(formatTickets(lottoTickets.getTickets())); | ||
| }catch (IllegalArgumentException e){ | ||
| ResultView.printErrorMessage(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private List<String> formatTickets(List<Lotto> tickets) { | ||
| return tickets.stream() | ||
| .map(this::convertLottoToString) | ||
| .toList(); | ||
| } | ||
|
|
||
| private String convertLottoToString(Lotto lotto) { | ||
| return lotto.getSortedNumbers().stream() | ||
| .map(String::valueOf) | ||
| .collect(Collectors.joining(",", "[", "]")); | ||
| } | ||
|
|
||
| private void validatePurchaseAmount(int purchaseAmount) { | ||
| if (purchaseAmount < Lotto.LOTTO_PRICE) { | ||
| throw new IllegalArgumentException("구매 금액은 1000원 이상이어야 합니다."); | ||
| } | ||
|
|
||
| if (purchaseAmount % Lotto.LOTTO_PRICE != 0) { | ||
| throw new IllegalArgumentException("구매 금액은 1000원 단위여야 합니다."); | ||
| } | ||
| } | ||
|
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. 입력값 검증에 대해서는 정말 의견이 분분해요
Author
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. 저는 검증은 Controller에서 이루어지는 것이 적절하다고 생각합니다.
따라서, Controller에서 검증을 수행하여 상황에 맞는 데이터를 Model에 전달하는 것이 가장 적절한 구조라고 판단됩니다! |
||
|
|
||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package model; | ||
|
|
||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class Lotto { | ||
|
|
||
| // 로또 번호 관련 상수 선언 | ||
| public static final int LOTTO_MIN_NUMBER = 1; | ||
| public static final int LOTTO_MAX_NUMBER = 45; | ||
| public static final int LOTTO_CREATE_SIZE = 6; | ||
| public static final int LOTTO_PRICE = 1000; | ||
| public static final List<Integer> LOTTO_NUMBER_POOL = | ||
| IntStream.rangeClosed(LOTTO_MIN_NUMBER,LOTTO_MAX_NUMBER) | ||
| .boxed() | ||
| .collect(Collectors.toList()); | ||
|
|
||
| private List<Integer> numbers = new ArrayList<>(); | ||
|
|
||
| public static int getTicketCount(int purchaseAmount){ | ||
| return purchaseAmount / LOTTO_PRICE; | ||
| } | ||
|
||
|
|
||
| public Lotto(){ | ||
| this.numbers = createLottoNumbers(); | ||
| } | ||
|
Comment on lines
+20
to
+22
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.
Author
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. 초기화는 생성자에서 이루어지는 것이 적절하다고 생각합니다. 멤버 변수(numbers)는 생성자에서 초기화됩니다. Lotto 클래스에서 numbers는 객체가 생성될 때마다 새로운 로또 번호를 가져야 하므로, 생성자 내부에서 this.numbers = createLottoNumbers(); 와 같이 초기화하는 것이 적절합니다. createLottoNumbers() 메서드를 통해 초기화 로직을 분리하면, 생성자 내부가 간결해지고 코드의 역할이 명확해집니다. 불필요한 초기화를 제거하고 생성자에서 멤버 변수를 초기화하는 것이 자바 스타일 가이드에도 적합하며, 유지보수성과 가독성을 높일 수 있는 방법이라고 생각합니다. 좋은 피드백 감사합니다! |
||
|
|
||
| private List<Integer> createLottoNumbers(){ | ||
| List<Integer> shuffledNumbers = new ArrayList<>(LOTTO_NUMBER_POOL); | ||
| Collections.shuffle(shuffledNumbers); | ||
| numbers = shuffledNumbers.subList(0, LOTTO_CREATE_SIZE); | ||
|
|
||
| return numbers; | ||
| } | ||
|
|
||
| public List<Integer> getSortedNumbers() { | ||
| List<Integer> sortedNumbers = new ArrayList<>(numbers); | ||
| Collections.sort(sortedNumbers); | ||
|
|
||
| return sortedNumbers; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package model; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class LottoTickets { | ||
|
|
||
| private final List<Lotto> tickets; | ||
|
|
||
| public LottoTickets(int ticketCount) { | ||
| this.tickets = generateLottoTickets(ticketCount); | ||
| } | ||
|
|
||
| private List<Lotto> generateLottoTickets(int ticketCount) { | ||
| List<Lotto> tickets = new ArrayList<>(); | ||
| for (int i = 0; i < ticketCount; i++) { | ||
| tickets.add(new Lotto()); | ||
| } | ||
| return tickets; | ||
| } | ||
|
|
||
| public List<Lotto> getTickets() { | ||
| return tickets; | ||
| } | ||
|
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. 불변성 유지의 중요성에 대해서 알아가봅시다
충분히 고민, 공부해보고 왜 수정될 가능성을 열어두면 안되는지 정빈이의 생각을 알려주세요
Author
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. 방어적 복사는 원본 데이터를 보호하고 불변성을 유지하기 위한 중요한 기법입니다. 현재 List의 참조를 직접 반환하면 외부에서 리스트를 수정할 가능성이 있습니다. public List<Lotto> getTickets() {
return new ArrayList<>(tickets);
}<방어적 복사의 장점>
좋은 피드백 감사합니다! |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package view; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
|
|
||
| private static final Scanner scanner = new Scanner(System.in); | ||
|
|
||
| public static int getPurchaseAmount(){ | ||
| System.out.println("구입금액을 입력해 주세요."); | ||
| int purchaseAmount = scanner.nextInt(); | ||
| scanner.close(); | ||
|
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 purchaseAmount; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package view; | ||
| import java.util.List; | ||
|
|
||
| public class ResultView { | ||
|
|
||
| public static void printOrderTickets(int ticketCount){ | ||
| System.out.println(); | ||
| System.out.println(ticketCount + "개를 구매했습니다."); | ||
| } | ||
|
|
||
| public static void printPurchasedLottoTickets(List<String> formattedTickets){ | ||
| for (String ticket : formattedTickets) { | ||
| System.out.println(ticket); | ||
| } | ||
| } | ||
|
|
||
| public static void printErrorMessage(String message) { | ||
| System.out.println(message); | ||
| } | ||
|
||
| } | ||
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.
공백이 필요해보여요
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.
catch 앞에 공백을 추가하고, 메서드 호출문 사이에 공백을 추가하여 가독성을 높였습니다. 좋은 피드백 감사합니다!