-
Notifications
You must be signed in to change notification settings - Fork 86
[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
Merged
boorownie
merged 9 commits into
next-step:jeongbeanhyun
from
JeongBeanHyun:jeongbeanhyun
Mar 12, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5e2fb30
feat: Lotto 추가
JeongBeanHyun f9f5a87
feat: controller 추가
JeongBeanHyun 1308961
feat: view 추가
JeongBeanHyun d7bfc50
feat: application 추가
JeongBeanHyun 8188fc6
refactor: controller 예외 추가 및 수정
JeongBeanHyun a63a201
refactor: Lotto 책임분리및 개행 수정
JeongBeanHyun 643a77f
refactor: 네이밍 수정
JeongBeanHyun 2be807c
refactor: LottoTickets 추가
JeongBeanHyun 4a44acc
refactor: 피드백 수정
JeongBeanHyun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package controller; | ||
|
||
import model.Lotto; | ||
import model.LottoTickets; | ||
import view.ErrorView; | ||
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 = LottoTickets.getTicketCount(purchaseAmount); | ||
|
||
LottoTickets lottoTickets = new LottoTickets(ticketCount); | ||
|
||
ResultView.printOrderTickets(ticketCount); | ||
|
||
ResultView.printPurchasedLottoTickets(formatTickets(lottoTickets.getTickets())); | ||
|
||
} catch (IllegalArgumentException e) { | ||
ErrorView.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 < LottoTickets.LOTTO_PRICE) { | ||
throw new IllegalArgumentException("구매 금액은 1000원 이상이어야 합니다."); | ||
} | ||
|
||
if (purchaseAmount % LottoTickets.LOTTO_PRICE != 0) { | ||
throw new IllegalArgumentException("구매 금액은 1000원 단위여야 합니다."); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
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 List<Integer> LOTTO_NUMBER_POOL = | ||
IntStream.rangeClosed(LOTTO_MIN_NUMBER,LOTTO_MAX_NUMBER) | ||
.boxed() | ||
.collect(Collectors.toList()); | ||
|
||
private List<Integer> numbers; | ||
|
||
public Lotto(){ | ||
this.numbers = 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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LottoTickets { | ||
|
||
private final List<Lotto> tickets; | ||
public static final int LOTTO_PRICE = 1000; | ||
|
||
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 new ArrayList<>(tickets); | ||
} | ||
|
||
public static int getTicketCount(int purchaseAmount){ | ||
return purchaseAmount / LOTTO_PRICE; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package view; | ||
|
||
public class ErrorView { | ||
public static void printErrorMessage(String message) { | ||
System.out.println(message); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 printPurchasedLottoTickets(List<String> formattedTickets){ | ||
for (String ticket : formattedTickets) { | ||
System.out.println(ticket); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
초기화는 생성자에서 이루어지는 것이 적절하다고 생각합니다.
멤버 변수(numbers)는 생성자에서 초기화됩니다.
Lotto 클래스에서 numbers는 객체가 생성될 때마다 새로운 로또 번호를 가져야 하므로, 생성자 내부에서 this.numbers = createLottoNumbers(); 와 같이 초기화하는 것이 적절합니다.
초기화 로직은 별도의 메서드에서 처리하는 것이 가독성과 유지보수 측면에서 유리합니다.
createLottoNumbers() 메서드를 통해 초기화 로직을 분리하면, 생성자 내부가 간결해지고 코드의 역할이 명확해집니다.
불필요한 초기화를 제거하고 생성자에서 멤버 변수를 초기화하는 것이 자바 스타일 가이드에도 적합하며, 유지보수성과 가독성을 높일 수 있는 방법이라고 생각합니다.
좋은 피드백 감사합니다!