Skip to content

Commit 77c3a4a

Browse files
authored
Step4 - 로또(수동) (#4231)
* refactor: 피드백 반영 * feat: 수동과 자동의 개수를 가지는 객체 생성 * feat: 로또 수동,자동 개수와 구매하는 돈을 묶는 객체 생성 * refactor: 로또 생성시 중복, 6개 예외 처리 * refactor: 컨벤션 오류 수정 * refactor: LottoGenerator 인터페이스, 자동으로만 로또 만드는 구현체(AutoLottoGenerator), (자동,수동)으로 만드는 구현체(MixedLottoGenerator) 생성 * refactor: LottoGenerator 인터페이스, 자동으로만 로또 만드는 구현체(AutoLottoGenerator), (자동,수동)으로 만드는 구현체(MixedLottoGenerator) 생성
1 parent fd11412 commit 77c3a4a

22 files changed

+286
-43
lines changed

src/main/java/lotto/LottoMain.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@
44
import lotto.view.InputView;
55
import lotto.view.ResultView;
66

7+
import java.util.List;
8+
79
public class LottoMain {
810
public static void main(String[] args) {
911
Money money = new Money(InputView.getInputMoney());
1012

11-
LottoGroup lottoGroup = new LottoGroup(money);
13+
int inputManualLottoCount = InputView.getInputManualLottoCount();
14+
15+
LottoCount lottoCount = new LottoCount(money, inputManualLottoCount);
16+
17+
LottoPurChase lottoPurChase = new LottoPurChase(money, lottoCount);
18+
19+
List<String> manualLottos = InputView.getInputManualLottos(inputManualLottoCount);
20+
21+
LottoGroup lottoGroup = new LottoGroup(new LottosBundleGenerator(lottoPurChase, manualLottos));
1222

13-
ResultView.showBuyLottos(lottoGroup);
23+
ResultView.showBuyLottos(lottoGroup, lottoPurChase);
1424

1525
Lotto winLotto = new Lotto(InputView.getInputWinNumber());
1626

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package lotto.domain;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class AutoLottoGenerator implements LottoGenerator {
7+
private final Money money;
8+
9+
public AutoLottoGenerator(int money) {
10+
this(new Money(money));
11+
}
12+
13+
public AutoLottoGenerator(Money money) {
14+
this.money = money;
15+
}
16+
17+
@Override
18+
public List<Lotto> generate() {
19+
List<Lotto> lottoArray = new ArrayList<>();
20+
21+
for (int i = 0; i < money.getBuyableCount(); i++) {
22+
lottoArray.add(new Lotto(LottoMachine.createLottoNumbers()));
23+
}
24+
25+
return lottoArray;
26+
}
27+
}

src/main/java/lotto/domain/Lotto.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package lotto.domain;
22

3-
import java.util.Arrays;
4-
import java.util.List;
5-
import java.util.Objects;
3+
import java.util.*;
4+
import java.util.stream.Collectors;
65

76
public class Lotto {
8-
private final List<LottoNumber> numbers;
7+
private final Set<LottoNumber> numbers;
98

109
public Lotto(int... numbers) {
1110
this(intToList(numbers));
@@ -15,20 +14,42 @@ public Lotto(String... numbers) {
1514
this(stringToList(numbers));
1615
}
1716

18-
public Lotto(List<LottoNumber> numbers) {
17+
public Lotto(String numbers) {
18+
this(stringToList(parse(numbers)));
19+
}
20+
21+
private static String[] parse(String s) {
22+
String[] strs = s.split(",");
23+
24+
for (int i = 0; i < strs.length; i++) {
25+
strs[i] = strs[i].trim();
26+
}
27+
28+
return strs;
29+
}
30+
31+
public Lotto(Set<LottoNumber> numbers) {
32+
System.out.println(numbers);
33+
validation(numbers);
1934
this.numbers = numbers;
2035
}
2136

22-
private static List<LottoNumber> stringToList(String... numbers) {
37+
private void validation(Set<LottoNumber> numbers) {
38+
if (numbers.size() != 6) {
39+
throw new IllegalArgumentException("로또 번호는 6개여야 합니다.");
40+
}
41+
}
42+
43+
private static Set<LottoNumber> stringToList(String... numbers) {
2344
return Arrays.stream(numbers)
2445
.map(LottoNumber::valueOf)
25-
.toList();
46+
.collect(Collectors.toSet());
2647
}
2748

28-
private static List<LottoNumber> intToList(int... numbers) {
49+
private static Set<LottoNumber> intToList(int... numbers) {
2950
return Arrays.stream(numbers)
3051
.mapToObj(LottoNumber::valueOf)
31-
.toList();
52+
.collect(Collectors.toSet());
3253
}
3354

3455
public int countMatchedNumbers(Lotto winningLotto) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package lotto.domain;
2+
3+
public class LottoCount {
4+
private final int manualCount;
5+
private final int autoCount;
6+
7+
public LottoCount(Money money, int manualCount) {
8+
this(money.getBuyableCount() - manualCount, manualCount);
9+
}
10+
11+
public LottoCount(int autoCount, int manualCount) {
12+
this.autoCount = autoCount;
13+
this.manualCount = manualCount;
14+
}
15+
16+
public int getManualCount() {
17+
return this.manualCount;
18+
}
19+
20+
public int getAutoCount() {
21+
return this.autoCount;
22+
}
23+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package lotto.domain;
2+
3+
import java.util.List;
4+
5+
public interface LottoGenerator {
6+
List<Lotto> generate();
7+
}

src/main/java/lotto/domain/LottoGroup.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,13 @@
66
public class LottoGroup {
77
private final List<Lotto> lottos;
88

9-
public LottoGroup(Money money) {
10-
this(buyLotto(money));
9+
public LottoGroup(LottoGenerator lottoGenerator) {
10+
this(lottoGenerator.generate());
1111
}
1212

1313
public LottoGroup(List<Lotto> lottos) {
1414
this.lottos = lottos;
1515
}
16-
private static List<Lotto> buyLotto(Money money) {
17-
int cnt = money.getBuyableCount();
18-
19-
List<Lotto> lottoArray = new ArrayList<>();
20-
21-
for(int i = 0; i < cnt; i++) {
22-
lottoArray.add(new Lotto(LottoMachine.createLottoNumbers()));
23-
}
24-
25-
return lottoArray;
26-
}
2716

2817
public List<Lotto> getLottoNumbers() {
2918
return lottos;

src/main/java/lotto/domain/LottoMachine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class LottoMachine {
1111

1212
public static int[] createLottoNumbers() {
1313
List<Integer> nums = new ArrayList<>();
14-
for(int i = MIN_NUMBER; i <= MAX_NUMBER; i++){
14+
for (int i = MIN_NUMBER; i <= MAX_NUMBER; i++) {
1515
nums.add(i);
1616
}
1717

@@ -22,7 +22,7 @@ public static int[] createLottoNumbers() {
2222

2323
private static int[] selectLottoNumbers(List<Integer> lottoNumbers) {
2424
int[] nums = new int[COUNT];
25-
for(int i = 0; i < COUNT; i++){
25+
for (int i = 0; i < COUNT; i++) {
2626
nums[i] = lottoNumbers.get(i);
2727
}
2828
return nums;

src/main/java/lotto/domain/LottoNumber.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public class LottoNumber {
99
private static final int MAX = 45;
1010
private static final Map<Integer, LottoNumber> CACHE_NUM = new HashMap<>();
1111

12-
static{
13-
for (int i = MIN; i <= MAX; i++){
12+
static {
13+
for (int i = MIN; i <= MAX; i++) {
1414
CACHE_NUM.put(i, new LottoNumber(i));
1515
}
1616
}
@@ -22,12 +22,12 @@ public LottoNumber(int number) {
2222
this.number = number;
2323
}
2424

25-
public static LottoNumber valueOf(String number){
25+
public static LottoNumber valueOf(String number) {
2626
validation(Integer.parseInt(number));
27-
return CACHE_NUM.get(Integer.parseInt(number));
27+
return valueOf(Integer.parseInt(number));
2828
}
2929

30-
public static LottoNumber valueOf(int number){
30+
public static LottoNumber valueOf(int number) {
3131
validation(number);
3232
return CACHE_NUM.get(number);
3333
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package lotto.domain;
2+
3+
public class LottoPurChase {
4+
private final Money money;
5+
private final LottoCount lottoCount;
6+
7+
8+
public LottoPurChase(int money, int autoCount, int manualCount) {
9+
this(new Money(money), new LottoCount(autoCount, manualCount));
10+
}
11+
12+
public LottoPurChase(Money money, LottoCount lottoCount) {
13+
this.money = money;
14+
this.lottoCount = lottoCount;
15+
}
16+
public Money deductManualLottoCost() {
17+
return this.money.deductManualLottoCost(lottoCount.getManualCount());
18+
}
19+
20+
public Money getMoney() {
21+
return this.money;
22+
}
23+
24+
public int getAutoCount() {
25+
return this.lottoCount.getAutoCount();
26+
}
27+
28+
public int getManualCount() {
29+
return this.lottoCount.getManualCount();
30+
}
31+
}

src/main/java/lotto/domain/LottoResult.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public class LottoResult {
99
public LottoResult() {
1010
this(initResult());
1111
}
12-
public LottoResult(Map<LottoRank, Integer> result) {
12+
13+
public LottoResult(Map<LottoRank, Integer> result) {
1314
this.result = result;
1415
}
1516

@@ -36,7 +37,7 @@ private static Map<LottoRank, Integer> initResult() {
3637
}
3738

3839
public void rank(LottoRank rank) {
39-
if(rank.isAddAble()) {
40+
if (rank.isAddAble()) {
4041
result.put(rank, result.get(rank) + 1);
4142
}
4243
}

0 commit comments

Comments
 (0)