Skip to content
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

[로또 미션] 오지현 미션 제출합니다. #20

Open
wants to merge 7 commits into
base: zhy2on
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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: 0 additions & 9 deletions src/main/java/LottoApplication.java

This file was deleted.

31 changes: 13 additions & 18 deletions src/main/java/controller/LottoController.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
package controller;

import domain.LottoTicket;
import domain.Lotto;
import domain.LottoNumber;
import domain.LottoStatistics;
import view.InputView;
import view.OutputView;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class LottoController {
public void run() {
int amountToBuy = InputView.getAmountToBuy();
List<LottoTicket> tickets = buyLottoTickets(amountToBuy);
OutputView.displayTicketCount(tickets.size());
OutputView.displayLottoTickets(tickets);
public static void main(String[] args) {
LottoController controller = new LottoController();
controller.run();
}
Copy link

Choose a reason for hiding this comment

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

1단계에서 구현을 하신 것처럼 Controller 안에서 말고 Application 안에서 컨트롤러 객체 생성 후 controller.run() 사용하시면 좋을 것 같습니다.


private List<LottoTicket> buyLottoTickets(final int amountToBuy) {
int ticketCount = amountToBuy / LottoTicket.getLottoPrice();
return generateLottoTickets(ticketCount);
}
private void run() {
int purchaseAmount = InputView.getPurchaseAmount();
List<Lotto> lottos = Lotto.purchaseLottos(purchaseAmount);
OutputView.printLottos(lottos);

private List<LottoTicket> generateLottoTickets(final int ticketCount) {
return IntStream.range(0, ticketCount)
.mapToObj(i -> new LottoTicket())
.collect(Collectors.toList());
List<LottoNumber> winningNumbers = InputView.getWinningNumbers();
LottoStatistics statistics = Lotto.getStatistics(lottos, winningNumbers);
OutputView.printStatistics(statistics);
}

}
53 changes: 53 additions & 0 deletions src/main/java/domain/Lotto.java
Copy link

Choose a reason for hiding this comment

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

원시 값 포장을 잘 사용하셨네요! 저는 그냥 1000으로 나눠버렸는데 하나 배워갑니다 :-)

Copy link
Author

Choose a reason for hiding this comment

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

아하 감사합니다! 그런데 저 부분은 매직넘버를 상수로 선언해서 제거한 부분이고 이번 과제에서 말한 원시값 포장이랑은 다른 개념인 것 같아요..!!

저도 잘은 모르지만
int money; 로 바로 사용하는 게 아니라
Money 클래스를 따로 구현하고 Money money = new Money(int amount);
이런식으로 원시 값을 객체로 포장해서 사용하는 게 원시값 포장인 것 같습니다..!!

돈에 대해선 제가 원시값 포장을 못 했어요🥲 이 부분은 추가로 더 고민해보고 수정하겠습니다!

Copy link

Choose a reason for hiding this comment

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

죄송해요 저도 어제 스터디에서 설명해주시는 거 듣고 제가 틀렸단 걸 깨달았습니다..전 단순히 포장(감싼다)인 줄 알았어요ㅠㅠ저도 아직 배워야할 게 많은 것 같습니다..ㅠㅠ

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public record Lotto(List<LottoNumber> numbers) {
public static final int LOTTO_NUMBER_COUNT = 6;
public static final int LOTTO_PRICE = 1000;

public static List<Lotto> purchaseLottos(int purchaseAmount) {
int lottoCount = purchaseAmount / LOTTO_PRICE;
return IntStream.range(0, lottoCount)
.mapToObj(i -> generateLotto())
.collect(Collectors.toList());
}

private static Lotto generateLotto() {
List<LottoNumber> numbers = new ArrayList<>();
while (numbers.size() < LOTTO_NUMBER_COUNT) {
LottoNumber number = LottoNumber.generate();
if (!numbers.contains(number)) {
numbers.add(number);
}
}
Copy link

Choose a reason for hiding this comment

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

예외 처리도 잘 하신 것 같아요.

Collections.sort(numbers);
return new Lotto(numbers);
}

public static LottoStatistics getStatistics(List<Lotto> lottos, List<LottoNumber> winningNumbers) {
LottoStatistics statistics = new LottoStatistics();
for (Lotto lotto : lottos) {
int matchCount = getMatchCount(lotto, winningNumbers);
statistics.addCount(matchCount);
}
return statistics;
}

private static int getMatchCount(Lotto lotto, List<LottoNumber> winningNumbers) {
return (int) lotto.numbers.stream()
.filter(winningNumbers::contains)
.count();
}

@Override
public String toString() {
return numbers.stream()
.map(LottoNumber::toString)
.collect(Collectors.joining(", ", "[", "]"));
}
}
33 changes: 33 additions & 0 deletions src/main/java/domain/LottoNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package domain;

import java.util.Random;

public record LottoNumber(int value) implements Comparable<LottoNumber> {
private static final int MIN_VALUE = 1;
private static final int MAX_VALUE = 45;

public LottoNumber {
validateValue(value);
}

private void validateValue(int value) {
if (value < MIN_VALUE || value > MAX_VALUE) {
throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 값이어야 합니다.");
}
}

public static LottoNumber generate() {
Random random = new Random();
return new LottoNumber(random.nextInt(MAX_VALUE - MIN_VALUE + 1) + MIN_VALUE);
}

@Override
public String toString() {
return String.valueOf(value);
}

@Override
public int compareTo(LottoNumber other) {
return Integer.compare(this.value, other.value);
}
}
26 changes: 26 additions & 0 deletions src/main/java/domain/LottoStatistics.java
Copy link

Choose a reason for hiding this comment

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

잘 하신 것 같습니다. PRIZES를 배열로 선언하지 않고 따로 클래스를 생성해서 관리하고(Map을 사용한다던지) 로또 수익 계산을 해도 괜찮을 것 같습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package domain;

public class LottoStatistics {
private static final int[] PRIZES = {0, 0, 15000000, 5000, 50000, 1500000, 2000000000};
private final int[] counts = new int[7];

public void addCount(int matchCount) {
counts[matchCount]++;
}

public int getCountFor(int matchCount) {
return counts[matchCount];
}

public int getPrizeFor(int matchCount) {
return PRIZES[matchCount];
}

public double getProfitRatio() {
int totalPrize = 0;
for (int i = 3; i <= 6; i++) {
totalPrize += getPrizeFor(i) * getCountFor(i);
}
return (double) totalPrize / (Lotto.purchaseLottos(14000).size() * Lotto.LOTTO_PRICE);
}
}
36 changes: 0 additions & 36 deletions src/main/java/domain/LottoTicket.java
Copy link

Choose a reason for hiding this comment

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

원시 값 포장을 잘 하신 것 같습니다.

This file was deleted.

19 changes: 16 additions & 3 deletions src/main/java/view/InputView.java
Copy link

Choose a reason for hiding this comment

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

문자열도 포장을 하시면 좋을 것 같아요. 위에서 원시 값을 포장하신 것처럼요!

Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
package view;

import domain.LottoNumber;

import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class InputView {
private static final Scanner scanner = new Scanner(System.in);

public static int getAmountToBuy() {
System.out.println("구입금액을 입력해 주세요.");
return scanner.nextInt();
public static int getPurchaseAmount() {
System.out.print("구입금액을 입력해 주세요.\n");
return Integer.parseInt(scanner.nextLine());
}

public static List<LottoNumber> getWinningNumbers() {
System.out.print("지난 주 당첨 번호를 입력해 주세요.\n");
return Stream.of(scanner.nextLine().split(","))
.map(Integer::parseInt)
.map(LottoNumber::new)
.collect(Collectors.toList());
}
}
23 changes: 16 additions & 7 deletions src/main/java/view/OutputView.java
Copy link

Choose a reason for hiding this comment

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

문자열을 포장하시고 반복문으로 출력하시면 코드가 훨씬 더 간결해질 것 같습니다.

Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package view;

import domain.LottoTicket;
import domain.Lotto;
import domain.LottoStatistics;

import java.util.List;

public class OutputView {
public static void displayTicketCount(final int ticketCount) {
System.out.println(ticketCount + "개를 구매했습니다.");
public static void printLottos(List<Lotto> lottos) {
System.out.printf("%d개를 구매했습니다.\n", lottos.size());
for (Lotto lotto : lottos) {
System.out.println(lotto);
}
}

public static void displayLottoTickets(final List<LottoTicket> tickets) {
for (LottoTicket ticket : tickets) {
System.out.println(ticket.getNumbers());
}
public static void printStatistics(LottoStatistics statistics) {
System.out.println("\n당첨 통계\n---------");
System.out.printf("3개 일치 (%d원)- %d개\n", statistics.getPrizeFor(3), statistics.getCountFor(3));
System.out.printf("4개 일치 (%d원)- %d개\n", statistics.getPrizeFor(4), statistics.getCountFor(4));
System.out.printf("5개 일치 (%d원)- %d개\n", statistics.getPrizeFor(5), statistics.getCountFor(5));
System.out.printf("6개 일치 (%d원)- %d개\n", statistics.getPrizeFor(6), statistics.getCountFor(6));
System.out.printf("총 수익률은 %.2f입니다.(기준이 1이기 때문에 결과적으로 %s라는 의미임)\n",
statistics.getProfitRatio(),
statistics.getProfitRatio() < 1 ? "손해" : "이익");
}
}