Skip to content

Commit 97bf6f1

Browse files
committed
2단계 미션 완료
1 parent efe0fa1 commit 97bf6f1

12 files changed

Lines changed: 166 additions & 25 deletions

File tree

src/main/java/com/javabom/lotto/LottoApplication.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,26 @@
66
import com.javabom.lotto.view.InputView;
77
import com.javabom.lotto.view.OutputView;
88

9+
import java.util.List;
10+
911
public class LottoApplication {
1012
public static void main(String[] args) {
1113
Money inputMoney = new Money(InputView.getMoneyToBuyTicket());
1214

15+
int manualLottoAmount = InputView.getManualLottoAmount();
16+
List<ManualLottoNumbers> manualLottoNumbers = InputView.getManualLottoNumbers(manualLottoAmount);
17+
1318
LottoTicketDispenser lottoTicketDispenser = new LottoTicketDispenser(new RandomLottoNumberGenerator());
14-
LottoTickets lottoTickets = lottoTicketDispenser.getAutoTickets(inputMoney);
15-
OutputView.printLottoTickets(lottoTickets);
19+
LottoTickets manualLottoTickets = lottoTicketDispenser.getManualTickets(manualLottoNumbers);
20+
Money restMoney = inputMoney.spend(LottoTicketDispenser.LOTTO_TICKET_PRICE.multiply(manualLottoAmount));
21+
LottoTickets autoLottoTickets = lottoTicketDispenser.getAutoTickets(restMoney);
22+
OutputView.printLottoTickets(manualLottoTickets, autoLottoTickets);
1623

1724
LottoTicket basicLuckyTicket = new LottoTicket(LottoNumberConverter.convert(InputView.getLottoBasicLuckyNumbers()));
1825
LottoNumber bonusNumber = new LottoNumber(InputView.getBonusNumber());
1926
LottoLuckyNumbers LuckyNumbers = new LottoLuckyNumbers(basicLuckyTicket, bonusNumber);
2027

21-
LottoResults lottoResults = LuckyNumbers.getLottoResults(lottoTickets);
28+
LottoResults lottoResults = LuckyNumbers.getLottoResults(autoLottoTickets.add(manualLottoTickets));
2229
OutputView.printLottoResults(lottoResults);
2330
OutputView.printEarningRate(inputMoney, lottoResults.getTotalPrizeMoney());
2431
}

src/main/java/com/javabom/lotto/domain/ticket/LottoTicketDispenser.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
import java.util.stream.Collectors;
56

67
public class LottoTicketDispenser {
78

8-
private static final int LOTTO_TICKET_PRICE = 1_000;
9+
public static final Money LOTTO_TICKET_PRICE = new Money(1_000);
910

1011
private final LottoNumberGenerator lottoNumberGenerator;
1112

@@ -21,4 +22,10 @@ public LottoTickets getAutoTickets(Money inputMoney) {
2122
}
2223
return new LottoTickets(tickets);
2324
}
25+
26+
public LottoTickets getManualTickets(List<ManualLottoNumbers> manualNumbers) {
27+
return new LottoTickets(manualNumbers.stream()
28+
.map(ManualLottoNumbers::toLottoTicket)
29+
.collect(Collectors.toList()));
30+
}
2431
}

src/main/java/com/javabom/lotto/domain/ticket/LottoTickets.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.javabom.lotto.domain.ticket;
22

3+
import java.util.ArrayList;
34
import java.util.Collections;
45
import java.util.List;
56
import java.util.Objects;
@@ -16,6 +17,13 @@ public List<LottoTicket> get() {
1617
return Collections.unmodifiableList(tickets);
1718
}
1819

20+
public LottoTickets add(LottoTickets lottoTickets) {
21+
List<LottoTicket> newTickets = new ArrayList<>();
22+
newTickets.addAll(this.tickets);
23+
newTickets.addAll(lottoTickets.tickets);
24+
return new LottoTickets(newTickets);
25+
}
26+
1927
@Override
2028
public boolean equals(Object o) {
2129
if (this == o) return true;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.javabom.lotto.domain.ticket;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
6+
public class ManualLottoNumbers {
7+
8+
private final List<Integer> numbers;
9+
10+
public ManualLottoNumbers(List<Integer> numbers) {
11+
this.numbers = numbers;
12+
}
13+
14+
public LottoTicket toLottoTicket() {
15+
return new LottoTicket(numbers.stream()
16+
.map(LottoNumber::new)
17+
.collect(Collectors.toList())
18+
);
19+
}
20+
}

src/main/java/com/javabom/lotto/domain/ticket/Money.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,52 @@
44

55
public class Money {
66

7-
private final long money;
7+
private final long value;
88

99
public Money(long money) {
1010
validateMoney(money);
11-
this.money = money;
11+
value = money;
1212
}
1313

1414
private void validateMoney(long money) {
1515
if (money < 0) {
16-
throw new IllegalArgumentException("돈은 음수일 수 없습니다.");
16+
throw new IllegalArgumentException("돈은 음수일 수 없습니다. 입력값 : " + money);
1717
}
1818
}
1919

20-
public int calculateQuantityPer(int price) {
21-
return (int) money / price;
20+
public Money multiply(int value) {
21+
return new Money(this.value * value);
22+
}
23+
24+
public Money spend(Money money) {
25+
validateCanSpendMoney(money);
26+
return new Money(this.value - money.value);
27+
}
28+
29+
private void validateCanSpendMoney(Money money) {
30+
if (money.value > this.value) {
31+
throw new IllegalArgumentException("현재 값 (" + value + ") 보다 사용하려는 금액 (" + money.value + ") 이 더 많습니다.");
32+
}
33+
}
34+
35+
public int calculateQuantityPer(Money price) {
36+
return (int) (value / price.value);
2237
}
2338

2439
public double calculateRatioOf(Money input) {
25-
return (double) input.money / this.money * 100;
40+
return (double) input.value / this.value * 100;
2641
}
2742

2843
@Override
2944
public boolean equals(Object o) {
3045
if (this == o) return true;
3146
if (o == null || getClass() != o.getClass()) return false;
3247
Money money1 = (Money) o;
33-
return money == money1.money;
48+
return value == money1.value;
3449
}
3550

3651
@Override
3752
public int hashCode() {
38-
return Objects.hash(money);
53+
return Objects.hash(value);
3954
}
4055
}

src/main/java/com/javabom/lotto/view/InputView.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package com.javabom.lotto.view;
22

3+
import com.javabom.lotto.domain.ticket.ManualLottoNumbers;
4+
5+
import java.util.ArrayList;
36
import java.util.Arrays;
47
import java.util.List;
58
import java.util.Scanner;
69
import java.util.stream.Collectors;
710

811
public class InputView {
912

13+
private static final String DELIMITER_COMMA = ",";
1014
private static final String NOTICE_INPUT_MONEY = "구입 금액을 입력해 주세요. 잔돈은 없습니다 ^^";
1115
private static final String NOTICE_INPUT_BASIC_LUCKY_NUMBER = "지난 주 당첨 번호를 입력해 주세요. (,로 나눠서 입력)";
1216
private static final String NOTICE_INPUT_BONUS_NUMBER = "보너스 볼을 입력해 주세요.";
17+
private static final String NOTICE_INPUT_MANUAL_LOTTO_AMOUNT = "수동으로 구매할 로또 수를 입력해 주세요.";
18+
private static final String NOTICE_INPUT_MANUAL_LOTTO_NUMBER = "수동으로 구매할 번호를 입력해 주세요.";
1319

1420
private static final Scanner scanner = new Scanner(System.in);
1521

@@ -20,7 +26,7 @@ public static int getMoneyToBuyTicket() {
2026

2127
public static List<Integer> getLottoBasicLuckyNumbers() {
2228
printLineOf(NOTICE_INPUT_BASIC_LUCKY_NUMBER);
23-
return Arrays.stream(scanner.nextLine().split(","))
29+
return Arrays.stream(scanner.nextLine().split(DELIMITER_COMMA))
2430
.map(Integer::parseInt)
2531
.collect(Collectors.toList());
2632
}
@@ -30,6 +36,26 @@ public static int getBonusNumber() {
3036
return scanner.nextInt();
3137
}
3238

39+
public static int getManualLottoAmount() {
40+
printLineOf(NOTICE_INPUT_MANUAL_LOTTO_AMOUNT);
41+
return Integer.parseInt(scanner.nextLine());
42+
}
43+
44+
public static List<ManualLottoNumbers> getManualLottoNumbers(int amount) {
45+
printLineOf(NOTICE_INPUT_MANUAL_LOTTO_NUMBER);
46+
List<ManualLottoNumbers> manualLottoNumbers = new ArrayList<>();
47+
for (int i = 0; i < amount; i++) {
48+
manualLottoNumbers.add(new ManualLottoNumbers(getManualLottoNumber()));
49+
}
50+
return manualLottoNumbers;
51+
}
52+
53+
public static List<Integer> getManualLottoNumber() {
54+
return Arrays.stream(scanner.nextLine().split(DELIMITER_COMMA))
55+
.map(Integer::parseInt)
56+
.collect(Collectors.toList());
57+
}
58+
3359
private static void printLineOf(String string) {
3460
System.out.println(string);
3561
}

src/main/java/com/javabom/lotto/view/OutputView.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66
import com.javabom.lotto.domain.ticket.LottoTickets;
77
import com.javabom.lotto.domain.ticket.Money;
88

9-
import java.util.stream.Collectors;
10-
119
public class OutputView {
1210

13-
private static final String NOTICE_BUY_COMPLETE = "개를 구매했습니다.";
11+
private static final String NOTICE_BUY_COMPLETE = "수동으로 %d장, 자동으로 %d개를 구매했습니다.";
1412
private static final String NOTICE_WINNING_STATISTICS = "당첨 통계";
1513
private static final String NOTICE_MATCH_QUANTITY = "%d개 일치, ";
1614
private static final String NOTICE_PRIZE_MONEY = "(%d원)";
1715
private static final String NOTICE_MATCH_RESULT_QUANTITY = " - %d개";
1816
private static final String NOTICE_MATCH_BONUS = "보너스 볼 일치";
1917
private static final String NOTICE_EARNING_RATE = "총 수익률은 %.2f%% 입니다.";
2018

21-
public static void printLottoTickets(LottoTickets lottoTickets) {
22-
printLineOf(lottoTickets.get().size() + NOTICE_BUY_COMPLETE);
23-
for (LottoTicket lottoTicket : lottoTickets.get()) {
19+
public static void printLottoTickets(LottoTickets manualLottoTickets, LottoTickets autoLottoTickets) {
20+
printLineOf(String.format(NOTICE_BUY_COMPLETE, manualLottoTickets.get().size(), autoLottoTickets.get().size()));
21+
for (LottoTicket lottoTicket : manualLottoTickets.get()) {
22+
printLineOf(lottoTicket.toString());
23+
}
24+
for (LottoTicket lottoTicket : autoLottoTickets.get()) {
2425
printLineOf(lottoTicket.toString());
2526
}
2627
}

src/test/java/com/javabom/lotto/domain/results/LottoResultsTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import java.util.Arrays;
88
import java.util.List;
9-
import java.util.stream.Collectors;
109

1110
import static com.javabom.lotto.domain.results.LottoResult.FIFTH_PRIZE;
1211
import static com.javabom.lotto.domain.results.LottoResult.FOURTH_PRIZE;

src/test/java/com/javabom/lotto/domain/ticket/MoneyTest.java renamed to src/test/java/com/javabom/lotto/domain/ticket/LottoMoneyTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66
import static org.assertj.core.api.Assertions.assertThat;
77
import static org.assertj.core.api.Assertions.assertThatThrownBy;
88

9-
class MoneyTest {
9+
class LottoMoneyTest {
1010

1111
@DisplayName("돈에 음수가 들어가면 Exception")
1212
@Test
1313
void validateMoney() {
1414
assertThatThrownBy(() -> new Money(-1))
1515
.isInstanceOf(IllegalArgumentException.class)
16-
.hasMessage("돈은 음수일 수 없습니다.");
16+
.hasMessage("돈은 음수일 수 없습니다. 입력값 : -1");
1717
}
1818

1919
@DisplayName("인풋값에 대한 수량 가져오기")
2020
@Test
2121
void calculateQuantityPer() {
2222
// given
2323
Money money = new Money(10_000);
24-
int price = 1_000;
24+
Money price = new Money(1_000);
2525
int expectedQuantity = 10;
2626

2727
// then
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.javabom.lotto.domain.ticket;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
class LottoNumberConverterTest {
12+
13+
@DisplayName("정수 리스트를 로또 넘버 리스트로 제대로 변환하는 지 검사")
14+
@Test
15+
void convert() {
16+
// given
17+
List<Integer> stubNumbers = Arrays.asList(1, 2, 3, 4, 5, 6);
18+
List<LottoNumber> expectedLottoNumbers = Arrays.asList(
19+
new LottoNumber(1),
20+
new LottoNumber(2),
21+
new LottoNumber(3),
22+
new LottoNumber(4),
23+
new LottoNumber(5),
24+
new LottoNumber(6)
25+
);
26+
27+
// then
28+
assertThat(LottoNumberConverter.convert(stubNumbers))
29+
.isEqualTo(expectedLottoNumbers);
30+
}
31+
}

0 commit comments

Comments
 (0)