Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b4287d6
Merge pull request #5 from babysean/미션_3-로또게임_2등
babysean Jun 27, 2024
d6529c2
chore: 수동 로또 번호 구매 로직 추가
babysean Jun 27, 2024
02dde32
test: 테스트 코드 추가
babysean Jun 27, 2024
e25d0e0
test: 테스트 코드 오타 수정
babysean Jun 27, 2024
9dc4cce
refactor: 코드 컨벤션에 맞추어 format 변경
babysean Jun 28, 2024
f0e2d5c
chore: 로또 티켓 합치기
babysean Jul 1, 2024
2338e36
test: 로또 티켓 개수 확인 테스트 코드 수정
babysean Jul 1, 2024
5dbd0e7
refactor: 클래스 이름 변경
babysean Jul 1, 2024
c6429fb
refactor: 메서드 이름 변경
babysean Jul 1, 2024
6576481
refactor: 클래스 생성 구문 위치 변경
babysean Jul 1, 2024
e94b114
refactor: 로또 번호 생성 주체 변경
babysean Jul 1, 2024
2a6e1b6
refactor: 주석 및 줄바꿈 제거
babysean Jul 1, 2024
e3c822c
refactor: 수동 로또 티켓 구매 프로세스 수정
babysean Jul 1, 2024
ab04122
test: LottoService 테스트 코드 추가
babysean Jul 1, 2024
0471dfb
test: LottoCalculator 테스트 코드 수정
babysean Jul 11, 2024
442c41c
chore: 수동 로또 개수 final 선언
babysean Jul 11, 2024
d3d0aba
chore: 수동 로또 개수 final 키워드 제거
babysean Jul 15, 2024
167e34a
refactor: LottoGenerator 파일명 변경
babysean Jul 15, 2024
37aa7fb
chore: LottoTicketGenerator 의존성 해결
babysean Jul 15, 2024
51a569e
refactor: 상수 관리 대상 변경
babysean Jul 15, 2024
eb6d329
refactor: 전략 패턴 적용
babysean Jul 15, 2024
a913957
test: 테스트 코드 작성
babysean Jul 15, 2024
6d38f29
test: 테스트 코드 수정
babysean Jul 15, 2024
032c611
refactor: javadoc 수정
babysean Jul 15, 2024
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
35 changes: 29 additions & 6 deletions src/main/java/lotto/LottoApplication.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package lotto;

import java.util.List;
import lotto.domain.LottoConsumer;
import lotto.domain.LottoPrize;
import lotto.domain.LottoTicket;
import lotto.domain.purchaseStrategy.AutoStrategy;
import lotto.domain.purchaseStrategy.ManualStrategy;
import lotto.dto.LottoPurchaseDto;
import lotto.factory.LottoApplicationFactory;
import lotto.service.LottoService;
import lotto.view.InputView;
import lotto.view.OutputView;

import java.util.List;

public class LottoApplication {

private final InputView inputView;

private final OutputView outputView;
Expand All @@ -26,12 +29,32 @@ public LottoApplication(InputView inputView, OutputView outputView, LottoService
public void run() {
LottoConsumer consumer = new LottoConsumer();

// 로또 구매 금액 입력 및 구매
// 로또 구매 금액 입력
int money = inputView.insertMoney();
lottoService.buyLotto(consumer, money);

// 수동 구매 로또 수 입력
int manuallyPurchasedLottoTicketCount = inputView.inputManuallyPurchasedLottoTicketCount();

// 수동 로또 번호 입력
List<String[]> manuallyPurchasedLottoNumbers = inputView.inputManuallyPurchasedLottoTicketNumbers(manuallyPurchasedLottoTicketCount);

// 구매 정보 DTO
LottoPurchaseDto purchaseDto = new LottoPurchaseDto(money, manuallyPurchasedLottoNumbers);

// 수동 구매로 전략 변경
lottoService.setPurchaseStrategy(new ManualStrategy());

// 수동 로또 티켓 구매
lottoService.buyLottoTicket(consumer, purchaseDto);

// 자동 구매로 전략 변경
lottoService.setPurchaseStrategy(new AutoStrategy());

// 자동 로또 티켓 구매
lottoService.buyLottoTicket(consumer, purchaseDto);

// 로또 구매 정보 출력
outputView.printLottoTicketsInformation(consumer.getLottoTickets());
outputView.printLottoTicketsInformation(consumer);

// 지난 주 당첨번호 입력
String[] LastWeekWinningNumbers = inputView.inputLastWeekWinningLottoNumbers();
Expand All @@ -40,7 +63,7 @@ public void run() {
int bonusNumber = inputView.inputBonusNumber();

// 지난 주 당첨 티켓 생성
LottoTicket winningTicket = lottoService.winningNumberToTicket(LastWeekWinningNumbers);
LottoTicket winningTicket = lottoService.lottoNumberToTicket(LastWeekWinningNumbers);

// 맞춘 번호 개수와 맞춘 보너스 번호 결과
List<LottoPrize> winningResult = lottoService.calculate(consumer.getLottoTickets(), winningTicket, bonusNumber);
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/lotto/domain/LottoCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
import java.util.List;

public class LottoCalculator {

/**
* 전달받은 수 만큼 맞은 로또의 개수를 반환합니다.
*
* @param prizes 로또 티켓의 결과 목록
* @param prizes 로또 티켓의 결과 목록
* @param checkedPrize 확인할 로또 티켓
*
* @return int
* */
*/
public int getCountOfWin(List<LottoPrize> prizes, LottoPrize checkedPrize) {
return (int) prizes
.stream()
return (int) prizes.stream()
.filter(prize -> LottoPrize.isMatched(prize, checkedPrize.getMatches(), checkedPrize.getIsWonBonusNumber()))
.count();
}
Expand All @@ -21,8 +22,9 @@ public int getCountOfWin(List<LottoPrize> prizes, LottoPrize checkedPrize) {
* 당첨금을 계산하여 반환 합니다.
*
* @param prizes 로또 결과 객체
*
* @return int
* */
*/
public int getPrizeMoney(List<LottoPrize> prizes) {
int prizeMoney = 0;

Expand All @@ -37,9 +39,10 @@ public int getPrizeMoney(List<LottoPrize> prizes) {
* 로또 수익률을 계산하여 반환 합니다.
*
* @param prizeMoney 당첨금
* @param money 로또 구매 금액
* @param money 로또 구매 금액
*
* @return double
* */
*/
public double getProfit(int prizeMoney, int money) {
double rateOfReturn = (double) prizeMoney / (double) money;

Expand Down
65 changes: 49 additions & 16 deletions src/main/java/lotto/domain/LottoConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,65 @@
import java.util.ArrayList;
import java.util.List;

import static lotto.domain.LottoTicket.LOTTO_PRICE;

public class LottoConsumer {
/** 구매한 로또 티켓 목록 */
private final List<LottoTicket> lottoTickets = new ArrayList<>();

/** 자동 로또 티켓 목록 */
private final List<LottoTicket> lottoTicketsByAuto;

/** 수동 로또 티켓 목록 */
private final List<LottoTicket> lottoTicketsByManual;

public LottoConsumer() {
this.lottoTicketsByAuto = new ArrayList<>();
this.lottoTicketsByManual = new ArrayList<>();
}

/**
* 자동 로또 티켓을 추가합니다.
*
* @param lottoTickets 로또 티켓 목록
*/
public void addAutoLottoTickets(List<LottoTicket> lottoTickets) {
this.lottoTicketsByAuto.addAll(lottoTickets);
}

/**
* 로또 번호 생성하고 반환 합니다.
* 수동 로또 티켓을 추가합니다.
*
* @param money 구매할 금액
* */
public void buyLotto(int money) {
int purchasedCount = money / LOTTO_PRICE;

for (int i = 0; i < purchasedCount; i++) {
LottoGenerator generator = new LottoGenerator();
lottoTickets.add(new LottoTicket(generator.generate()));
}
* @param lottoTickets 로또 티켓 목록
*/
public void addManualLottoTickets(List<LottoTicket> lottoTickets) {
this.lottoTicketsByManual.addAll(lottoTickets);
}

/**
* 구매한 로또 티켓 목록을 반환 합니다.
* 전체 로또 티켓 목록을 반환 합니다.
*
* @return List<LottoTicket>
* */
*/
public List<LottoTicket> getLottoTickets() {
List<LottoTicket> lottoTickets = new ArrayList<>();
lottoTickets.addAll(lottoTicketsByAuto);
lottoTickets.addAll(lottoTicketsByManual);

return lottoTickets;
}

/**
* 자동 로또 티켓 개수를 반환합니다.
*
* @return int
*/
public int getAutoTicketCount() {
return lottoTicketsByAuto.size();
}

/**
* 수동 로또 티켓 개수를 반환합니다.
*
* @return int
*/
public int getManualTicketCount() {
return lottoTicketsByManual.size();
}
}
34 changes: 0 additions & 34 deletions src/main/java/lotto/domain/LottoGenerator.java

This file was deleted.

19 changes: 11 additions & 8 deletions src/main/java/lotto/domain/LottoPrize.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ public boolean getIsWonBonusNumber() {
/**
* matches 와 bonus 에 맞는 값을 반환 합니다.
*
* @param matches 맞춘 개수
* @param matches 맞춘 개수
Copy link
Member

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.

아 이거 컨벤션 적용한건데 javadocs의 default가 저건 가봐요 ㅎㅎ
줄맞춤 안하는게 좋을까요 ? 맞춰주는게 좋아보이기도 해서요

Copy link
Member

Choose a reason for hiding this comment

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

아 별뜻은 없었습니다~!

갑자기 줄을 맞추셔서 리액션..

* @param isWonBonusNumber 보너스 번호 맞춘 여부
*
* @return LottoPrize
* */
*/
public static LottoPrize findByMatchesAndBonus(int matches, boolean isWonBonusNumber) {
return Arrays.stream(values())
.filter(prize -> isMatched(prize, matches, isWonBonusNumber))
Expand All @@ -49,11 +50,12 @@ public static LottoPrize findByMatchesAndBonus(int matches, boolean isWonBonusNu
/**
* 로또 티켓을 확인 합니다.
*
* @param prize 로또 티켓의 결과
* @param checkedCount 확인할 맞춘 개수
* @param prize 로또 티켓의 결과
* @param checkedCount 확인할 맞춘 개수
* @param isWonBonusNumber 보너스 번호 맞춘 여부
*
* @return boolean
* */
*/
public static boolean isMatched(LottoPrize prize, int checkedCount, boolean isWonBonusNumber) {
if (checkedCount == SECOND_MATCHED_COUNT) {
return checkSecond(prize, checkedCount, isWonBonusNumber);
Expand All @@ -65,11 +67,12 @@ public static boolean isMatched(LottoPrize prize, int checkedCount, boolean isWo
/**
* 로또 티켓의 2등을 확인합니다.
*
* @param prize 로또 티켓의 결과
* @param checkedCount 확인할 맞춘 개수
* @param prize 로또 티켓의 결과
* @param checkedCount 확인할 맞춘 개수
* @param isMatchedBonusNumber 보너스 번호 맞춘 여부
*
* @return boolean
* */
*/
private static boolean checkSecond(LottoPrize prize, int checkedCount, boolean isMatchedBonusNumber) {
if (isMatchedBonusNumber) {
return prize.getIsWonBonusNumber() && prize.getMatches() == checkedCount;
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/lotto/domain/LottoTicket.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@

/**
* 로또 티켓을 관리하는 클래스 입니다.
* */
*/
public class LottoTicket {

/** 로또 가격 */
public static final int LOTTO_PRICE = 1000;

/** 로또 숫자의 최솟값 */
public static final int MIN_LOTTO_NUMBER = 1;

/** 로또 숫자의 최댓값 */
public static final int MAX_LOTTO_NUMBER = 45;

/** 로또 번호 */
private final List<Integer> numbers;

Expand All @@ -22,7 +29,7 @@ public LottoTicket(List<Integer> numbers) {
* 로또 티켓을 반환 합니다.
*
* @return List<Integer>
* */
*/
public List<Integer> get() {
return numbers;
}
Expand All @@ -31,9 +38,10 @@ public List<Integer> get() {
* 맞춘 결과를 반환 합니다.
*
* @param winningLottoTicket 당첨된 로또 티켓
* @param bonusNumber 보너스 번호
* @param bonusNumber 보너스 번호
*
* @return LottoPrize
* */
*/
public LottoPrize win(LottoTicket winningLottoTicket, int bonusNumber) {
Set<Integer> intersection = new HashSet<>(numbers);
intersection.retainAll(winningLottoTicket.get());
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/lotto/domain/LottoTicketGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package lotto.domain;

import static lotto.domain.LottoTicket.MAX_LOTTO_NUMBER;
import static lotto.domain.LottoTicket.MIN_LOTTO_NUMBER;

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

public class LottoTicketGenerator {

/**
* 자동 로또 번호를 생성하고 반환합니다.
*
* @return List<Integer> 생성된 로또 티켓 목록
*/
public LottoTicket autoGenerate() {
List<Integer> numbers = new ArrayList<>();

for (int i = MIN_LOTTO_NUMBER ; i <= MAX_LOTTO_NUMBER ; i++) {
numbers.add(i);
}

Collections.shuffle(numbers);

List<Integer> shuffledNumber = numbers.subList(0, 6);

Collections.sort(shuffledNumber);

return new LottoTicket(shuffledNumber);
}

/**
* 수동 로또 번호를 전달 받아 LottoTicket 객체로 반환 합니다.
*
* @param numbers 로또 번호
*
* @return LottoTicket
*/
public LottoTicket manualGenerate(String[] numbers) {
List<Integer> lottoNumbers = Arrays.stream(numbers)
.map(Integer::parseInt)
.collect(Collectors.toList());

// 유효성 체크
LottoValidator validator = new LottoValidator();
validator.winningNumbersValidation(lottoNumbers);

return new LottoTicket(lottoNumbers);
}
}
Loading