Skip to content

[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
merged 9 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/Application.java
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();
}
}
54 changes: 54 additions & 0 deletions src/main/java/controller/LottoController.java
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원 단위여야 합니다.");
}
}
}
38 changes: 38 additions & 0 deletions src/main/java/model/Lotto.java
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();
}
Comment on lines +20 to +22
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자바 스타일 가이드를 참고하면

  1. 중첩 클래스
  2. 필드(멤버 변수)
  3. 생성자
  4. 메서드

순서를 제시하고 있어요 생성자의 순서가 조금 어색하죠? 수정해봅시다ㅎㅎ

Copy link
Author

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() 메서드를 통해 초기화 로직을 분리하면, 생성자 내부가 간결해지고 코드의 역할이 명확해집니다.

불필요한 초기화를 제거하고 생성자에서 멤버 변수를 초기화하는 것이 자바 스타일 가이드에도 적합하며, 유지보수성과 가독성을 높일 수 있는 방법이라고 생각합니다.

좋은 피드백 감사합니다!


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;
}
}
30 changes: 30 additions & 0 deletions src/main/java/model/LottoTickets.java
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;
}
}
7 changes: 7 additions & 0 deletions src/main/java/view/ErrorView.java
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);
}
}
16 changes: 16 additions & 0 deletions src/main/java/view/InputView.java
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();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

입력이 끝난 시점에서 바로 닫아주는 거 좋아용👍


return purchaseAmount;
}
}
16 changes: 16 additions & 0 deletions src/main/java/view/ResultView.java
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);
}
}
}