Skip to content

[4,5단계 - 로또 수동 구매] 최지수 미션 제출합니다 #125

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

Open
wants to merge 4 commits into
base: jisoo78
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ plugins {
id 'java'
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

group = 'cholog'
version = '1.0-SNAPSHOT'

Expand Down
82 changes: 28 additions & 54 deletions src/main/java/lotto/Application.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
package lotto;

import lotto.domain.*;
import lotto.input.PassivityLottoInput;
import lotto.input.PassivityNumberInput;
import lotto.input.WinningNumberInput;
import lotto.parser.WinningNumberParser;

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


public class Application {
private static final int MINIMUM_AMOUNT = 1000;

public static void main(String[] args) {
new Application().run();
}

// 테스트 확인
private void run() {
int ticketCount = buyLotto();
List<InputLottoNumber> tickets = createTickets(ticketCount);
WinningNumberInput input = new WinningNumberInput();
WinningNumber winning = input.inputWinningNumber();
int ticketCount = buyLotto();
List<InputLottoNumber> tickets = createTickets(ticketCount);
WinningNumber winning = WinningNumber.inputWinningNumbers();
LottoStatus status = checkResult(tickets, winning);
printResults(status, ticketCount);
List<InputLottoNumber> ticket = prepareTicket(ticketCount);
WinningNumber winningNumber = new WinningNumberInput().inputWinningNumber();
LottoStatus lottoStatus = checkResult(ticket, winningNumber);
lottoStatus.print(ticketCount, MINIMUM_AMOUNT);
}

private List<InputLottoNumber> prepareTicket(int ticketCount) {
int passivityCount = new PassivityNumberInput(ticketCount).passivityLottoCount();
List<InputLottoNumber> passivityTicket = new PassivityLottoInput(
new WinningNumberParser()).inputPassivityTicket(passivityCount);
List<InputLottoNumber> auto = LottoCounter.generateAuto(ticketCount, passivityCount);
List<InputLottoNumber> totalTicket = new ArrayList<>();
totalTicket.addAll(passivityTicket);
totalTicket.addAll(auto);
System.out.printf("수동 %d장, 자동 %d장 구매 완료%n", passivityCount, ticketCount - passivityCount);
return totalTicket;
}

private static int buyLotto() {
Money money = new Money(inputMoney());
int ticketCount = money.countTickets(MINIMUM_AMOUNT);
Expand All @@ -38,40 +54,7 @@ private static List<InputLottoNumber> createTickets(int count) {
List<Integer> picked = LottoManage.pickupLottoNumbers(lottoParts);
List<LottoNumber> wrapped = picked.stream()
.map(LottoNumber::new)
.collect(Collectors.toList());

tickets.add(InputLottoNumber.of(wrapped));
}
return tickets;
}

private static LottoStatus checkResult(
List<InputLottoNumber> tickets,
WinningNumber winning) {
LottoStatus status = new LottoStatus();
tickets.forEach(ticket -> {
System.out.println(ticket);
status.record(MatchResult.of(ticket.countMatching(winning)));
});
return status;
}

private static void printResults(LottoStatus status, int ticketCount) {
System.out.println("\n당첨 통계\n---------");
status.print();
Money totalCost = new Money(ticketCount * MINIMUM_AMOUNT);
Money totalPayout = status.getTotalPrizeAmount();
double returnMoney = (double) totalPayout.getAmount() / totalCost.getAmount();
System.out.printf("총 수익률: %.2f 입니다.%n", returnMoney);
int money = inputMoney();
int count = money / MINIMUM_AMOUNT;
for (int i = 0; i < count; i++) {
List<Integer> lottoParts = LottoManage.pullOutNumbers();
LottoManage.shuffleNumbers(lottoParts);
List<Integer> picked = LottoManage.pickupLottoNumbers(lottoParts);
List<LottoNumber> wrapped = picked.stream()
.map(LottoNumber::new)
.toList();
.toList();

tickets.add(InputLottoNumber.of(wrapped));
}
Expand All @@ -89,15 +72,6 @@ private static LottoStatus checkResult(
return status;
}

private static void printResults(LottoStatus status, int ticketCount) {
System.out.println("\n당첨 통계\n---------");
status.print();
Money totalCost = new Money(ticketCount * MINIMUM_AMOUNT);
Money totalPayout = status.getTotalPrizeAmount();
double returnMoney = (double) totalPayout.getAmount() / totalCost.getAmount();
System.out.printf("총 수익률: %.2f 입니다.%n", returnMoney);
}

private static int inputMoney() {
Scanner scanner = new Scanner(System.in);
System.out.print("구입 금액 입력(금액은 1000원 이상 입력해주세요): ");
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/lotto/LottoCounter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package lotto;

import lotto.domain.InputLottoNumber;
import lotto.domain.LottoNumber;

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

public class LottoCounter {

public static List<InputLottoNumber> generateAuto(int totalTicket, int passivityCount) {
int autoLottoCount = totalTicket - passivityCount;
return IntStream.range(0, autoLottoCount)
.mapToObj(i -> {
List<Integer> pool = LottoManage.pullOutNumbers();
LottoManage.shuffleNumbers(pool);
List<LottoNumber> picked = LottoManage.pickupLottoNumbers(pool)
.stream().map(LottoNumber::new).toList();
return InputLottoNumber.of(picked);
})
.toList();
}
}
Comment on lines +9 to +23

Choose a reason for hiding this comment

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

LottoCounter 객체의 역할은 무엇인가요?

Copy link
Author

Choose a reason for hiding this comment

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

전체 구매 로또 티켓에서 수동 장수를 제외한 만큼 6개 번호의 티켓을 InputLottoNumber에 반환합니다

Choose a reason for hiding this comment

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

static method를 사용하면 객체지향적인 코드로부터 멀어진다고 생각하는데요!
다음 자료를 참고해보시고 제 의견에 대한 지수님의 생각을 말해주세요 🤔

https://tecoble.techcourse.co.kr/post/2020-07-16-static-method/

Copy link
Author

Choose a reason for hiding this comment

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

정적 메소드를 계속 사용하면 다형성 특징을 해치고 이후 상속과 같은 개념을 사용할 때 오버라이딩이 불가능한 단점도 생기겠어요
정적 메소드를 사용하는 경우와 사용하지 않는 경우를 정확히 구분하고 객체지향적인 코드를 바라보는 것이 좋은 방향인 거 같아요

44 changes: 0 additions & 44 deletions src/main/java/lotto/LottoNumber.java

This file was deleted.

39 changes: 0 additions & 39 deletions src/main/java/lotto/LottoStatus.java

This file was deleted.

97 changes: 0 additions & 97 deletions src/main/java/lotto/WinningNumber.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lotto;
package lotto.domain;

public class BonusNumber {
private final LottoNumber bonus;
Comment on lines +1 to 4

Choose a reason for hiding this comment

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

해당 객체는 사용되지 않는것 같은데요! 이 객체의 역할과 책임은 무엇이였을까요

Copy link
Author

Choose a reason for hiding this comment

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

이전 미션에서 Enum을 사용하는 과정에서 누락된 객체입니다
로또의 숫자를 맞춘 개수와 상금을 지닌 역할을 가지고 있어요

Choose a reason for hiding this comment

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

그렇다면 지금은 이 역할이 누구에게 향했고 왜 그렇게 변했는지 이야기해주실 수 있을까요?

Copy link
Author

Choose a reason for hiding this comment

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

다시 확인해 보니 당첨번호에 보너스 번호를 추가하고 보너스 번호가 로또 번호에 들어가 있는지 검사하는 기능을 수행했네요
BounsNumber에서 담당하던 해당 기능이 WinningNumber로 넘어가 보너스 번호에 대해서 클래스를 유지할 필요가 없어졌어요
단일 책임 원칙을 지키기 위해서 해당 클래스는 지우는 게 맞다고 생각합니다

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lotto;
package lotto.domain;

import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -29,9 +29,5 @@ public String toString() {
return numbers.stream()
.map(n -> String.valueOf(n.getValue()))
.collect(Collectors.joining(", ", "[", "]"));

@Override
public String toString() {
return numbers.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package lotto;
package lotto.domain;

import java.util.Comparator;
import java.util.EnumSet;

public enum LottoBonus {
FIRST_PLACE(6, false, 2000000000),
Expand Down Expand Up @@ -31,16 +34,9 @@ public Money getWinningMoney() {
}

public static LottoBonus valueOf (int matchCount, boolean needBonusMatch) {
Copy link

Choose a reason for hiding this comment

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

for-if로 2 depth로 들어가는 구조인데, 조건 분기를 메서드로 분리해보면 가독성이 더 좋아질 것 같아요! 🙌

Copy link
Author

Choose a reason for hiding this comment

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

오 확인 감사해요 수정해서 올렸습니다!

for (LottoBonus bonus : values()) {
if (bonus.matchCount == matchCount && bonus.needBonusMatch == needBonusMatch) {
return bonus;
}
}
for (LottoBonus bonus : values()) {
if (bonus.matchCount == matchCount && !bonus.needBonusMatch) {
return bonus;
}
}
return LOSING_PLACE;
return EnumSet.allOf(LottoBonus.class).stream()
.filter(b -> b.matchCount == matchCount)
.min(Comparator.comparing(b -> b.needBonusMatch != needBonusMatch))
.orElse(LOSING_PLACE);
}
}
Loading