Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
01a15d2
1,2단계 초안(테스트코드 전)
ke-62 Sep 23, 2025
b74016a
README수정
ke-62 Sep 23, 2025
c56ec71
LottoTest테스트코드완성
ke-62 Sep 24, 2025
e7fdac1
원시값포장
ke-62 Sep 24, 2025
7c0f366
원시값, 일급컬렉션 수정
ke-62 Sep 24, 2025
8d25d65
테스트 코드 오류 수정
ke-62 Sep 24, 2025
aa93828
피드백 1차 수정사항
ke-62 Sep 27, 2025
712355d
Match오류부분 LottoNumber에 로직추가로 수정
ke-62 Sep 27, 2025
5483100
[TEST]하드코딩 되어있는 부분들 수정, 문법적 오류 수정
ke-62 Sep 29, 2025
a950c6d
[TEST]ParameterizedTest사용 및 와일드카드 사용부 제거
ke-62 Sep 29, 2025
32e4236
[TEST]오타수정
ke-62 Sep 29, 2025
db12651
[TEST]오타수정2
ke-62 Sep 29, 2025
3ee3b9e
리스트->hastset,treeset변경
ke-62 Sep 30, 2025
a7f1d52
상수화, 네이밍 변경
ke-62 Sep 30, 2025
c1a8a28
오탈자 수정
ke-62 Sep 30, 2025
684a177
[feat]보너스볼 로직 추가
ke-62 Sep 30, 2025
bb099d0
정팩메추가
ke-62 Sep 30, 2025
c0a617d
예외처리 로직 추가
ke-62 Oct 1, 2025
0a3d25d
[TEST]보너스볼테스트코드작성
ke-62 Oct 1, 2025
a195cdd
[feat]4단계 README 및 오류 수정
ke-62 Oct 1, 2025
31cf72e
[feat]수동입력 로직 추가
ke-62 Oct 1, 2025
448f371
[refector]보너스볼 테스트코드 수정
ke-62 Oct 1, 2025
461131a
[feat]오류 수정
ke-62 Oct 1, 2025
58cf0ec
[refector]숫자들 매직넘버화
ke-62 Oct 2, 2025
fff7970
[TEST]수동입력 테스트코드
ke-62 Oct 2, 2025
7995e01
Merge branch 'ke-62' into goeun
ke-62 Oct 4, 2025
ebf2453
[refector]getTicketCount부분 수정
ke-62 Oct 4, 2025
f157862
[refector]getTicketCount수정
ke-62 Oct 4, 2025
050a96b
[refector]MathCountTest 간결하게 수정
ke-62 Oct 5, 2025
b2ca2bc
[refector]코드 가독성을 위한 줄바꿈 적용
ke-62 Oct 5, 2025
43244b9
[refector]로또 금액이 1000 미만일 때, 수동 구매 로또가 총 로또 수보다 클 때 다시 입력받고록 수정
ke-62 Oct 5, 2025
0e25574
[refector]LottoTickets -> Lottos 클래스 네이밍 더 연관성이 보이도록 수정, Lotto 유효성 검사…
ke-62 Oct 6, 2025
9326c01
[refector]보너스볼, 당첨로또 오류 입력 시 다시 입력받도록 수정
ke-62 Oct 6, 2025
cea3d80
[refector]줄바꿈 수정
ke-62 Oct 6, 2025
0f760a5
[refector]ResultView에서 네이밍 가독성 좋게 수정
ke-62 Oct 9, 2025
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
15 changes: 11 additions & 4 deletions README.md
Copy link

Choose a reason for hiding this comment

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

5단계 요구사항이 없어 보이는데 확인 부탁드려요!

Copy link
Author

Choose a reason for hiding this comment

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

5단계는 리팩토링이라 크게 적은게 없긴 한데, 추가해 두겠습니다!!

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 로또 미션 1-2단계
## 로또 미션 1-5단계

### 기능 요구사항

Expand All @@ -23,6 +23,13 @@
- 소수점 둘째 자리까지 표시한다.
- 1미만시 손해이고 1이상이면 이득이다.

### 추가된 기능 요구사항
1. 모든 원시 값과 문자열을 포장한다.
2. 일급 컬렉션을 쓴다
#### 3단계
- 보너스볼이 들어온다
- 5개 일치 + 보너스볼 일치: 30000000원
- 5개 일치 + 보너스볼 일치하지 않음 : 1500000원

#### 4단계
- 사용자는 수동으로 구매할 로또 수를 입력할 수 있다.

#### 5단계
- 리팩토링하기
1 change: 0 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

public class Main {
public static void main(String[] args) {

LottoController lottoController = new LottoController();
lottoController.run();
}
Expand Down
111 changes: 96 additions & 15 deletions src/main/java/controller/LottoController.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,137 @@
package controller;

import domain.*;
import domain.Lotto;
import domain.LottoNumber;
import domain.LottoService;
import domain.LottoTicketCount;
import domain.Lottos;
import domain.LottoTotalPrice;
import domain.Money;
import domain.MatchCount;
import domain.ProfitRate;
import view.InputView;
import view.OutputView;
import view.ResultView;

import java.util.ArrayList;
import java.util.List;


public class LottoController {

OutputView outputView = new OutputView();
InputView inputView = new InputView();
ResultView resultView = new ResultView();

public void run() {
outputView.printWonMessage();
Money money = new Money(inputView.inputMoney());
Money money;
while (true) {
try {
money = new Money(inputView.inputMoney());
break;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
System.out.println("다시 입력해주세요.");
}
}

LottoTickets lottoTickets = buyLotto(money);
Lottos lottoTickets = buyLotto(money);
checkLotto(lottoTickets, money);
}


public LottoTickets buyLotto(Money money) {

LottoTicketCount ticketNumber = Money.getTicketCount(money);
public Lottos buyLotto(Money money) {
LottoTicketCount ticketNumber = money.getTicketCount();
resultView.printTicketNumbers(ticketNumber.getCount());

outputView.printManualCount();
int manualCount;

while (true) {
try {
manualCount = inputView.inputManualCount();
ticketNumber.validateManualCount(manualCount);
break;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
System.out.println("다시 입력해주세요.");
}
}

List<Lotto> manualLottos = new ArrayList<>();
outputView.printManualNumbers();

LottoService lottoService = new LottoService();
for (int i = 0; i < manualCount; i++) {
while (true) {
try {
String manualNumbers = inputView.inputManualNumbers();
Lotto lotto = lottoService.parseLottoAnswer(manualNumbers);
manualLottos.add(lotto);
break;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
System.out.println("다시 입력해주세요.");
}
}
}

int autoCount = ticketNumber.getCount() - manualCount;

resultView.printManualAuto(manualCount, autoCount);
outputView.lottoResult();
Lottos lottoTickets = Lottos.createMixedTickets(manualLottos, autoCount);

LottoTickets lottoTickets = new LottoTickets(ticketNumber);
for (Lotto lotto : lottoTickets.getTickets()) {
System.out.println(lotto);
}

return lottoTickets;
}

public void checkLotto(LottoTickets lottoTickets, Money money) {

public void checkLotto(Lottos ticketAutoCount, Money money) {
outputView.printLottoAnswer();

LottoService lottoService = new LottoService();
String lottoAnswer = inputView.inputLottoAnswer();

Lotto lottoAnswerObj = null;
while (lottoAnswerObj == null) {
try {
String lottoAnswer = inputView.inputLottoAnswer();
lottoAnswerObj = lottoService.parseLottoAnswer(lottoAnswer);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
System.out.println("다시 입력해주세요.");
}
}

outputView.printBonusMessage();
int bonusBallNumber = inputView.inputBonusNumber();
LottoNumber bonuseBall = new LottoNumber(bonusBallNumber);

Lotto lottoAnswerObj = lottoService.parseLottoAnswer(lottoAnswer);
MatchCount matchCount = lottoService.calculateMatchCount(lottoTickets.getTickets(), lottoAnswerObj, bonuseBall);
LottoNumber bonusBall;
while (true) {
try {
int bonusBallNumber = inputView.inputBonusNumber();
bonusBall = new LottoNumber(bonusBallNumber);
lottoService.validateBonusBall(lottoAnswerObj, bonusBall);
break;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
System.out.println("다시 입력해주세요.");
}
}

MatchCount matchCount = lottoService.calculateMatchCount(
ticketAutoCount.getTickets(),
lottoAnswerObj,
bonusBall);

int totalSum = LottoProfit.LottoSum(matchCount);
// ProfitRate profitRate = new ProfitRate(money, new LottoTotalPrice(totalSum));
ProfitRate profitRate = new ProfitRate(money, new LottoTotalPrice(matchCount));

outputView.lottoResult();
resultView.printLottoMatch(matchCount);
resultView.printLottoProfit(profitRate.getProfitRate());
}

}
11 changes: 8 additions & 3 deletions src/main/java/domain/Lotto.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
import java.util.TreeSet;

public class Lotto {

private final SortedSet<LottoNumber> numbers;

public Lotto(SortedSet<LottoNumber> numbers) {
if (numbers.size() != 6) {
validateSize(numbers);
this.numbers = new TreeSet<>(numbers);
}

private void validateSize(SortedSet<LottoNumber> numbers) {
if (numbers.size() != createList.LOTTO_NUMBER_COUNT) {
throw new IllegalArgumentException("로또 숫자는 6개여야 하며, 중복될 수 없습니다.");
}
this.numbers = new TreeSet<>(numbers);
}

public int contains(LottoNumber number) {
if(numbers.contains(number)) return 1;
if (numbers.contains(number)) return 1;
return 0;
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/domain/LottoNumber.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package domain;

public class LottoNumber implements Comparable<LottoNumber> {

private final int number;

public LottoNumber(int number) {
if (number < createList.LOTTO_MIN_NUMBER|| number > createList.LOTTO_MAX_NUMBER) {
if (number < createList.LOTTO_MIN_NUMBER || number > createList.LOTTO_MAX_NUMBER) {
throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다.");
}
this.number = number;
Expand Down Expand Up @@ -35,4 +36,5 @@ public int hashCode() {
public String toString() {
return String.valueOf(number);
}

}
1 change: 1 addition & 0 deletions src/main/java/domain/LottoPrice.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public enum LottoPrice {
public int getPrice() {
return price;
}

}
21 changes: 0 additions & 21 deletions src/main/java/domain/LottoProfit.java

This file was deleted.

15 changes: 9 additions & 6 deletions src/main/java/domain/LottoService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package domain;

import java.util.Arrays;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class LottoService {

public Lotto parseLottoAnswer(String lottoAnswer) {
SortedSet<LottoNumber> numbers = new TreeSet<>();
for (String num : lottoAnswer.split(",")) {
Expand All @@ -15,11 +14,15 @@ public Lotto parseLottoAnswer(String lottoAnswer) {
return new Lotto(numbers);
}

public void validateBonusBall(Lotto answer, LottoNumber bonusBall) {
if (answer.getNumbers().contains(bonusBall)) {
throw new IllegalArgumentException("보너스 볼은 당첨 번호와 중복될 수 없습니다.");
}
}

public MatchCount calculateMatchCount(List<Lotto> tickets, Lotto answer, LottoNumber bonusBall) {
MatchCount matchCount = MatchCount.countAllMatches(tickets, answer);
MatchCount bonusCount = MatchCount.countBonusBallMatches(tickets, answer, bonusBall);
matchCount.merge(bonusCount);
return matchCount;
return MatchCount.calculateStatistics(tickets, answer, bonusBall);
}

}

11 changes: 11 additions & 0 deletions src/main/java/domain/LottoTicketCount.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
package domain;

public class LottoTicketCount {

private final int count;

public LottoTicketCount(int count) {
if(count < 0) {
throw new IllegalArgumentException("로또 티켓 수는 0장 이상이어야 합니다.");
}
this.count = count;
}

public int getCount() {
return count;
}

public void validateManualCount(int manualCount) {
if (manualCount < 0 || manualCount > count) {
throw new IllegalArgumentException("수동으로 구매할 로또 수는 0 이상이여야 하며, 구매 가능한 티켓 수를 초과할 수 없습니다.");
}
}
Comment on lines +18 to +22
Copy link

Choose a reason for hiding this comment

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

수동으로 구매할 로또 수를 검증하는 로직이 LottoTicketCount 객체에 존재하는 이유가 있을까요??

Copy link
Author

Choose a reason for hiding this comment

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

처음에 검증 로직 넣으면서, 수동 구매 수는 0이상에 총 로또 수 보다 작아야 해서 총 로또 수는 변수로 가지고 있는 클래스들을 살펴보다가 클래스 이름도 TicketCount로 포괄적인 것 같아 여기에 넣는게 자연스러울 것 같아서 LottoTicketCount에 넣어두었습니다!


}
23 changes: 0 additions & 23 deletions src/main/java/domain/LottoTickets.java

This file was deleted.

17 changes: 8 additions & 9 deletions src/main/java/domain/LottoTotalPrice.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package domain;

import java.util.Arrays;

public class LottoTotalPrice {

private final long totalSum;

public LottoTotalPrice(MatchCount matchCount) {
this.totalSum = calculateTotalSum(matchCount);
}

private long calculateTotalSum(MatchCount matchCount) {
return Arrays.stream(LottoPrice.values())
.mapToLong(price -> (long) matchCount.getCount(price) * price.getPrice())
.sum();
this.totalSum =
((long) matchCount.getCount(LottoPrice.MATCH_3) * LottoPrice.MATCH_3.getPrice()) +
((long) matchCount.getCount(LottoPrice.MATCH_4) * LottoPrice.MATCH_4.getPrice()) +
((long) matchCount.getCount(LottoPrice.MATCH_5) * LottoPrice.MATCH_5.getPrice()) +
((long) matchCount.getCount(LottoPrice.MATCH_5_BONUS) * LottoPrice.MATCH_5_BONUS.getPrice()) +
((long) matchCount.getCount(LottoPrice.MATCH_6) * LottoPrice.MATCH_6.getPrice());
}

public long getTotalSum() {
return totalSum;
}

}
Loading