Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1e74285
docs: 기본 기능 구현 목록 작성
qwer920414-ctrl Nov 25, 2025
1c52a14
feat: LottoGenerator 구현 -> Lotto 클래스 분리 구현
qwer920414-ctrl Nov 26, 2025
72e1ab2
feat: Lotto 클래스 shuffle 추가
qwer920414-ctrl Nov 26, 2025
be061d3
test: Lotto 클래스 테스트 케이스 추가
qwer920414-ctrl Nov 26, 2025
3bb2bd6
refactor: Lotto 생성자 로직 수정
qwer920414-ctrl Nov 26, 2025
98d7b01
test: Lotto 테스트 케이스 추가
qwer920414-ctrl Nov 26, 2025
e67feb0
feat: LottoChecker 클래스 구현(개선 전)
qwer920414-ctrl Nov 26, 2025
220311b
feat: InputView 클래스 구현
qwer920414-ctrl Nov 26, 2025
825337e
feat: view, main 클래스 구현
qwer920414-ctrl Nov 26, 2025
5997498
코드 초기화(재시도)
qwer920414-ctrl Nov 30, 2025
b97a1a8
feat: Lotto 클래스 구현
qwer920414-ctrl Nov 30, 2025
32139a4
Lottos 클래스 생성 - 생성역할은 Factory로 나누기
qwer920414-ctrl Nov 30, 2025
3f27e24
Lotto 클래스 테스트 코드 추가
qwer920414-ctrl Nov 30, 2025
70c380c
Money 클래스 구현 - LottoGame 클래스 구현을 위한
qwer920414-ctrl Nov 30, 2025
ea81f46
Money 클래스 추가 구현
qwer920414-ctrl Nov 30, 2025
888683b
LottoGame 클래스 구현, 중간 확인을 위해 view 클래스 일부 구현
qwer920414-ctrl Nov 30, 2025
fb651e6
LottoGame - check 메소드 구현 -> Lottos - findResult -> Lotto - match 메소드 …
qwer920414-ctrl Nov 30, 2025
7365bbe
LottoResult 구현 - Game 테스트 코드 생성
qwer920414-ctrl Nov 30, 2025
f0da2f1
Rank 도입 -> 변동사항 개선
qwer920414-ctrl Nov 30, 2025
b6564e0
피드백 개선
qwer920414-ctrl Dec 1, 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: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,17 @@
* 나눗셈
* Executor
* execute
* valiteInput
* valiteInput

# 로또 2단계(자동)
## 기능 요구사항
* 로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다.
* 로또 1장의 가격은 1000원이다.

## 기능 구현 목록
* domain
* LottoGenerator
* LottoChecker
* view
* InputView
* ResultView
20 changes: 20 additions & 0 deletions src/main/java/lotto/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package lotto;

import lotto.domain.Lotto;
import lotto.domain.LottoGame;
import lotto.domain.LottoResult;
import lotto.view.InputView;
import lotto.view.ResultView;

public class Main {
public static void main(String[] args) {
long price = InputView.initLottoPrice();
LottoGame lottoGame = new LottoGame(price);
ResultView.printLottos(lottoGame.lottos());

Lotto winningLotto = new Lotto(InputView.initWinningLotto());
LottoResult result = lottoGame.check(winningLotto);
ResultView.printResult(result, lottoGame);

}
}
75 changes: 75 additions & 0 deletions src/main/java/lotto/domain/Lotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package lotto.domain;

import java.util.*;

public class Lotto {
Copy link
Contributor

Choose a reason for hiding this comment

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

여러 개의 생성자 추가 👍

public static final int LOTTO_NUMBER_SIZE = 6;

private final Set<Integer> numbers;

public Lotto() {
this(new TreeSet<>(LottoFactory.generateLotto()));
}

public Lotto(String value) {
this(splitAndParseInt(value));
}

private static Set<Integer> splitAndParseInt(String value) {
String[] split = getSplit(value);
Set<Integer> numbers = strToIntSet(split);
return numbers;
}

private static Set<Integer> strToIntSet(String[] split) {
Set<Integer> numbers = new TreeSet<>();
for (String s : split) {
numbers.add(Integer.parseInt(s));
}
return numbers;
}

private static String[] getSplit(String value) {
return value.split(",");
}

public Lotto(Integer... numbers) {
this(new TreeSet<>(Arrays.asList(numbers)));
}

public Lotto(Set<Integer> numbers) {
validate(numbers);
this.numbers = Collections.unmodifiableSet(numbers);
}

private void validate(Set<Integer> numbers) {
if (numbers.size() != LOTTO_NUMBER_SIZE) {
throw new IllegalArgumentException();
}

boolean invalidNumber = numbers.stream()
.anyMatch(n -> n < 1 || n > 45);
if (invalidNumber) {
throw new IllegalArgumentException();
}
}

public int match(Lotto winningLotto) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public int match(Lotto winningLotto) {
public Rank match(Lotto winningLotto) {

int보다 Rank로 반환하면 어떨까?

int count = 0;
for (Integer number : numbers) {
if (winningLotto.numbers().contains(number)) {
count++;
}
}
return count;
}

private Set<Integer> numbers() {
return numbers;
}

@Override
public String toString() {
return numbers.toString();
}
}
32 changes: 32 additions & 0 deletions src/main/java/lotto/domain/LottoFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package lotto.domain;

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

public class LottoFactory {

private static final List<Integer> DEFAULT_NUMBERS = IntStream
Copy link
Contributor

Choose a reason for hiding this comment

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

데이터 재사용 👍

.rangeClosed(1, 45)
.boxed()
.collect(Collectors.toList());

private static final int LOTTO_SIZE = 6;

public static List<Integer> generateLotto() {
Collections.shuffle(DEFAULT_NUMBERS);
List<Integer> lottoNumbers = DEFAULT_NUMBERS.subList(0, LOTTO_SIZE);
Collections.sort(lottoNumbers);
return new ArrayList<>(lottoNumbers);
}

public static List<Lotto> generateLottos(int count) {
List<Lotto> list = new ArrayList<>();
for (int i = 0; i < count; i++) {
list.add(new Lotto());
}
return list;
}
}
33 changes: 33 additions & 0 deletions src/main/java/lotto/domain/LottoGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package lotto.domain;

import java.util.List;

public class LottoGame {
private final Lottos lottos;
private final Money money;

public LottoGame(long amount) {
this(new Money(amount));
}

public LottoGame(Money money) {
this(new Lottos(money.buyCount()), money);
}

public LottoGame(Lottos lottos, Money money) {
this.lottos = lottos;
this.money = money;
}

public List<Lotto> lottos() {
return lottos.values();
}

public LottoResult check(Lotto winningLotto) {
return lottos.findResult(winningLotto);
}

public double rateOfReturn(LottoResult result) {
return money.rateOfReturn(result.getTotal());
}
}
29 changes: 29 additions & 0 deletions src/main/java/lotto/domain/LottoResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package lotto.domain;

import java.util.HashMap;
import java.util.Map;

public class LottoResult {
private final Map<Rank, Integer> lottoResult = new HashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

일급 콜렉션 👍


public LottoResult() {
for (Rank rank : Rank.values()) {
lottoResult.put(rank, 0);
}
}

public void addMatch(int match) {
Rank rank = Rank.from(match);
lottoResult.put(rank, lottoResult.get(rank) + 1);
}

public int getCount(Rank rank) {
return lottoResult.get(rank);
}

public long getTotal() {
return lottoResult.entrySet().stream()
.mapToLong(entry -> entry.getKey().prize() * entry.getValue())
.sum();
}
}
28 changes: 28 additions & 0 deletions src/main/java/lotto/domain/Lottos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package lotto.domain;

import java.util.List;

public class Lottos {
private final List<Lotto> lottos;

public Lottos(int count) {
this(LottoFactory.generateLottos(count));
}

public Lottos(List<Lotto> lottos) {
this.lottos = lottos;
}

public List<Lotto> values() {
return lottos;
}

public LottoResult findResult(Lotto winningLotto) {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

LottoResult result = new LottoResult();
for (Lotto lotto : lottos) {
int match = lotto.match(winningLotto);
result.addMatch(match);
}
return result;
}
}
5 changes: 5 additions & 0 deletions src/main/java/lotto/domain/Matchable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package lotto.domain;

public interface Matchable {
boolean isMatch(int matchCount);
}
26 changes: 26 additions & 0 deletions src/main/java/lotto/domain/Money.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package lotto.domain;

public class Money {
private static final long LOTTO_PRICE = 1_000;

private final long money;

public Money(long money) {
validate(money);
this.money = money;
}

private void validate(long money) {
if (money < LOTTO_PRICE) {
throw new IllegalArgumentException();
}
}

public int buyCount() {
return (int) (money / LOTTO_PRICE);
}

public double rateOfReturn(long getTotal) {
return Math.round((double) (getTotal / money) * 100) / 100.0;
}
}
38 changes: 38 additions & 0 deletions src/main/java/lotto/domain/Rank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package lotto.domain;

import java.util.Arrays;

public enum Rank {
FIRST(6, 2_000_000_000),
THIRD(5, 1_500_000),
FOURTH(4, 50_000),
FIFTH(3, 5_000),
NONE(0, 0);

private final int match;
private final int prize;

Rank(int match, int prize) {
this.match = match;
this.prize = prize;
}

public int prize() {
return prize;
}

public int match() {
return match;
}

public boolean isMatch(int match) {
return this.match == match;
}

public static Rank from(int matchCount) {
return Arrays.stream(values())
.filter(rank -> rank.isMatch(matchCount))
.findFirst()
.orElse(NONE);
}
}
17 changes: 17 additions & 0 deletions src/main/java/lotto/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package lotto.view;

import java.util.Scanner;

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

public static long initLottoPrice() {
System.out.println("구매금액을 입력해 주세요.");
return Long.parseLong(SCANNER.nextLine());
}

public static String initWinningLotto() {
System.out.println("지난 주 당첨 번호를 입력해 주세요.");
return SCANNER.nextLine();
}
}
38 changes: 38 additions & 0 deletions src/main/java/lotto/view/ResultView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package lotto.view;

import lotto.domain.Lotto;
import lotto.domain.LottoGame;
import lotto.domain.LottoResult;
import lotto.domain.Rank;

import java.util.List;

public class ResultView {
public static void printLottos(List<Lotto> lottos) {
System.out.println(lottos.size() + "개를 구매했습니다.");
for (Lotto lotto : lottos) {
System.out.println(lotto);
}
}

public static void printResult(LottoResult result, LottoGame lottoGame) {
System.out.println("당첨 통계");
System.out.println("---------");
for (Rank rank : Rank.values()) {
System.out.println(rank.match() + "개 일치 (" + rank.prize() + "원) - " + result.getCount(rank) + "개");
}
printRateOfReturn(result, lottoGame);

}

private static void printRateOfReturn(LottoResult result, LottoGame lottoGame) {
double rate = lottoGame.rateOfReturn(result);
String message = String.format("총 수익률은 %.2f입니다.", rate);
if (rate < 1.0) {
message += "(기준이 1이기 때문에 결과적으로 손해라는 의미임)";
}
System.out.println(message);
}


}
Loading