-
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 4 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,34 @@ | ||
| package controller; | ||
|
|
||
| import model.Lotto; | ||
| import view.InputView; | ||
| import view.ResultView; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class LottoController { | ||
|
|
||
| public void run() { | ||
| int purchaseAmount = InputView.getPurchaseAmount(); | ||
|
|
||
| int ticketCount = Lotto.getTicketCount(purchaseAmount); | ||
|
|
||
| List<Lotto> tickets = Lotto.generateLottoTickets(ticketCount); | ||
|
|
||
| ResultView.printOrderTickets(ticketCount); | ||
| ResultView.printTickets(ticketCount, formatTickets(tickets)); | ||
| } | ||
|
|
||
| public List<String> formatTickets(List<Lotto> tickets) { | ||
| List<String> formattedTickets = tickets.stream() | ||
| .map(lotto -> lotto.getNumbers() | ||
| .stream() | ||
| .sorted() | ||
| .map(String::valueOf) | ||
| .collect(Collectors.joining(",","[","]"))) | ||
| .toList(); | ||
|
|
||
| return formattedTickets; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| 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()); | ||
|
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. 개행을 일관성 있게 정리하여 수정하였습니다. 기존에는 IntStream을 개별 줄에 나누어 작성했지만, 가독성을 높이기 위해 한 줄에서 시작하도록 변경했습니다. 피드백 감사드립니다! 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 static 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<Integer> getNumbers() { | ||
| List<Integer> sortedNumbers = new ArrayList<>(numbers); | ||
| Collections.sort(sortedNumbers); | ||
|
|
||
| return sortedNumbers; | ||
| } | ||
|
||
| } | ||
| 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,16 @@ | ||
| 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 printTickets(int ticketCount ,List<String> formattedTickets){ | ||
|
||
| for (String ticket : formattedTickets) { | ||
| System.out.println(ticket); | ||
| } | ||
| } | ||
|
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. mvc 패턴에 대한 이해도가 높이진게 티가 나네요👍 |
||
| } | ||
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.
접근 제어자에 대한 부분을 자주 놓치고 있는 것 같아요
접근 제어자를 지정하는 것은
private -> protected -> public순서로 고민해보며 지정하면 좋은 습관 만들어 갈 수 있을 것 같아요
또한 stream이 중첩되어 있는 구조인데 SRP를 준수하기에는 조금 미흡하지 않나 생각이 들어요
메서드 분리를 통해서 개선해봅시다!
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.
formatTickets()는 외부에서 직접 호출될 필요가 없는 내부 메서드이므로, public에서 private으로 변경했습니다.
접근 제어자는 최소한의 공개 범위를 설정하는 것이 중요하다는 점을 다시 한번 확인했습니다.
앞으로는 private → protected → public 순서로 접근 제어자를 고민하는 습관을 들이겠습니다!
로또 번호를 문자열로 변환하는 역할을 별도의 메서드(convertLottoToString())로 분리하여 SRP를 준수하도록 개선했습니다.
소중한 시간 내어 피드백 해주셔서 항상 감사드립니다!