Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,24 @@
- indent(인덴트, λ“€μ—¬μ“°κΈ°) depthλ₯Ό 2λ₯Ό λ„˜μ§€ μ•Šλ„λ‘ κ΅¬ν˜„ν•œλ‹€. 1κΉŒμ§€λ§Œ ν—ˆμš©ν•œλ‹€.
- ν•¨μˆ˜(λ˜λŠ” λ©”μ†Œλ“œ)의 길이가 15라인을 λ„˜μ–΄κ°€μ§€ μ•Šλ„λ‘ κ΅¬ν˜„ν•œλ‹€.
- μžλ°” μ½”λ“œ μ»¨λ²€μ…˜μ„ μ§€ν‚€λ©΄μ„œ ν”„λ‘œκ·Έλž˜λ°ν•œλ‹€.
- else μ˜ˆμ•½μ–΄λ₯Ό μ“°μ§€ μ•ŠλŠ”λ‹€.
- else μ˜ˆμ•½μ–΄λ₯Ό μ“°μ§€ μ•ŠλŠ”λ‹€.

## Step4. 둜또(μˆ˜λ™)

### κΈ°λŠ₯ μš”κ΅¬μ‚¬ν•­

- [ ] ν˜„μž¬ 둜또 μƒμ„±κΈ°λŠ” μžλ™ 생성 κΈ°λŠ₯만 μ œκ³΅ν•œλ‹€. μ‚¬μš©μžκ°€ μˆ˜λ™μœΌλ‘œ 좔첨 번호λ₯Ό μž…λ ₯ν•  수 μžˆλ„λ‘ ν•΄μ•Ό ν•œλ‹€.
- [ ] μž…λ ₯ν•œ κΈˆμ•‘, μžλ™ 생성 숫자, μˆ˜λ™ 생성 번호λ₯Ό μž…λ ₯ν•˜λ„λ‘ ν•΄μ•Ό ν•œλ‹€.

### ν”„λ‘œκ·Έλž˜λ° μš”κ΅¬μ‚¬ν•­

- κ·œμΉ™ 3: λͺ¨λ“  μ›μ‹œκ°’κ³Ό λ¬Έμžμ—΄μ„ 포μž₯ν•œλ‹€.
- κ·œμΉ™ 5: 쀄여쓰지 μ•ŠλŠ”λ‹€(μΆ•μ•½ κΈˆμ§€).
- μ˜ˆμ™Έ 처리λ₯Ό 톡해 μ—λŸ¬κ°€ λ°œμƒν•˜μ§€ μ•Šλ„λ‘ ν•œλ‹€.
- λͺ¨λ“  κΈ°λŠ₯을 TDD둜 κ΅¬ν˜„ν•΄ λ‹¨μœ„ ν…ŒμŠ€νŠΈκ°€ μ‘΄μž¬ν•΄μ•Ό ν•œλ‹€. 단, UI(System.out, System.in) λ‘œμ§μ€ μ œμ™Έ
- java enum을 μ μš©ν•΄ ν”„λ‘œκ·Έλž˜λ°μ„ κ΅¬ν˜„ν•œλ‹€.
- κ·œμΉ™ 8: 일급 μ½œλ ‰μ…˜μ„ μ“΄λ‹€.
- indent(인덴트, λ“€μ—¬μ“°κΈ°) depthλ₯Ό 2λ₯Ό λ„˜μ§€ μ•Šλ„λ‘ κ΅¬ν˜„ν•œλ‹€. 1κΉŒμ§€λ§Œ ν—ˆμš©ν•œλ‹€.
- ν•¨μˆ˜(λ˜λŠ” λ©”μ†Œλ“œ)의 길이가 15라인을 λ„˜μ–΄κ°€μ§€ μ•Šλ„λ‘ κ΅¬ν˜„ν•œλ‹€.
- μžλ°” μ½”λ“œ μ»¨λ²€μ…˜μ„ μ§€ν‚€λ©΄μ„œ ν”„λ‘œκ·Έλž˜λ°ν•œλ‹€.
- else μ˜ˆμ•½μ–΄λ₯Ό μ“°μ§€ μ•ŠλŠ”λ‹€.
23 changes: 19 additions & 4 deletions src/main/java/lotto/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import lotto.domain.*;


import java.util.List;

import static lotto.domain.LottoResult.*;
import static lotto.view.InputView.*;
import static lotto.view.InputView.inputBuyPrice;
Expand All @@ -13,11 +15,24 @@ public class Main {

public static void main(String[] args) {
printStart();
LottoPrice lottoPrice = inputBuyPrice();
int price = inputBuyPrice();

printPassiveBuyLottoCount();
int passiveCount = inputPassiveBuyLottoCount();
LottoPrice lottoPrice = new LottoPrice(price, passiveCount);

PurchasedLottos purchasedLottos = new PurchasedLottos();
if (passiveCount > 0) {
printPassiveBuyLotto();
inputPassiveBuyLotto(purchasedLottos, passiveCount);
}

printLottoCount(lottoPrice);

BuyLotto buyLotto = LottoMachine.createLotto(lottoPrice);
printBuyLotto(buyLotto);
PurchasedLottos createLottos = LottoMachine.createLotto(lottoPrice);
purchasedLottos.addAll(createLottos);

printBuyLotto(purchasedLottos);

printResultLottoNumber();
Lotto resultLotto = inputResultLottoNumber();
Expand All @@ -26,7 +41,7 @@ public static void main(String[] args) {
WinningLotto winningLotto = new WinningLotto(resultLotto, bonusNumber);

printResultMessage();
LottoResult lottoResult = winningLotto.checkLottoNumber(buyLotto);
LottoResult lottoResult = winningLotto.checkLottoNumber(purchasedLottos);

printResult(lottoResult);
printProfit(profitPercent(lottoPrice));
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/lotto/domain/Lotto.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ public class Lotto {
private final Set<NumberElement> numbers;

public Lotto(Integer... numbers) {
this(Arrays.stream(numbers).map(NumberElement::create).collect(Collectors.toSet()));
this(Arrays.stream(numbers)
.map(NumberElement::create)
.collect(Collectors.toCollection(LinkedHashSet::new)));
}

public Lotto(Set<NumberElement> numbers) {
Expand Down
13 changes: 4 additions & 9 deletions src/main/java/lotto/domain/LottoMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

public class LottoMachine {

Expand All @@ -17,16 +16,12 @@ public class LottoMachine {
}
}

public static BuyLotto createLotto(LottoPrice lottoPrice) {
List<Lotto> lottoNumbers = new ArrayList<>();

public static PurchasedLottos createLotto(LottoPrice lottoPrice) {
List<Lotto> lottos = new ArrayList<>();
lottoPrice.create(() -> {
Collections.shuffle(LottoMachine.lottoNumbers);
lottoNumbers.add(new Lotto(new HashSet<>(
LottoMachine.lottoNumbers.subList(0, 6).stream().collect(Collectors.toSet())))
);
lottos.add(new Lotto(new HashSet<>(LottoMachine.lottoNumbers.subList(0, 6))));
});

return new BuyLotto(lottoNumbers);
return new PurchasedLottos(lottos);
}
}
16 changes: 7 additions & 9 deletions src/main/java/lotto/domain/LottoPrice.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ public class LottoPrice {
private final Price price;
private final TicketCount ticketCount;

public LottoPrice(int price) {
this(price, price / 1_000);
public LottoPrice(int price, int passiveTicketCount) {
this(price, new TicketCount(price, passiveTicketCount));
}

public LottoPrice(int price, int ticketCount) {
this(new Price(price), new TicketCount(ticketCount));
public LottoPrice(int price, TicketCount ticketCount) {
this(new Price(price), ticketCount);
}

public LottoPrice(Price price, TicketCount ticketCount) {
Expand All @@ -21,17 +21,15 @@ public LottoPrice(Price price, TicketCount ticketCount) {
}

public void create(Runnable runnable) {
for (int i = 0; i < this.ticketCount.getValue(); i++) {
runnable.run();
}
this.ticketCount.create(runnable);
}

public double profitPercent(int totalResultPrice) {
return price.profitPercent(totalResultPrice);
}

public int getTicketCount() {
return ticketCount.getValue();
public TicketCount getTicketCount() {
return ticketCount;
}

@Override
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/lotto/domain/NumberElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
public class NumberElement {

private static final Map<Integer, NumberElement> numberElementMap = new HashMap<>();
private static final int START_NUMBER = 1;
private static final int END_NUMBER = 45;

static {
for (int i = 1; i <= 45; i++) {
for (int i = START_NUMBER; i <= END_NUMBER; i++) {
numberElementMap.put(i, new NumberElement(i));
}
}

private final int value;

public NumberElement() {
private NumberElement() {
this(0);
}

public NumberElement(int value) {
private NumberElement(int value) {
validationRange(value);
this.value = value;
}
Expand All @@ -36,7 +38,7 @@ private static void validationRange(int value) {
}

private static boolean isRange(int value) {
return 1 > value || value > 45;
return START_NUMBER > value || value > END_NUMBER;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package lotto.domain;

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

public class BuyLotto {
public class PurchasedLottos {

private final List<Lotto> lottos;

public BuyLotto(Integer... numbers) {
public PurchasedLottos() {
lottos = new ArrayList<>();
}

public PurchasedLottos(Integer... numbers) {
this(List.of(new Lotto(numbers)));
}

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

Expand All @@ -29,17 +34,25 @@ public LottoResult match(WinningLotto winningLotto) {
return lottoResult;
}

public void add(Lotto lotto){
this.lottos.add(lotto);
}

public void addAll(PurchasedLottos lottos){
this.lottos.addAll(lottos.lottos);
}

@Override
public String toString() {
return "BuyLotto{" +
return "PurchasedLottos{" +
"lotto=" + lottos +
'}';
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
BuyLotto buyLotto = (BuyLotto) o;
PurchasedLottos buyLotto = (PurchasedLottos) o;
return Objects.equals(getLottos(), buyLotto.getLottos());
}

Expand Down
26 changes: 19 additions & 7 deletions src/main/java/lotto/domain/TicketCount.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,37 @@

public class TicketCount {

private final int value;
private final int autoTicketCount;
private final int passiveTicketCount;

public TicketCount(int value) {
this.value = value;
public TicketCount(int price, int passiveTicketCount) {
this.autoTicketCount = (price / 1_000) - passiveTicketCount;
Copy link
Contributor

Choose a reason for hiding this comment

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

'int price'λ₯Ό 포μž₯ν•œ Price 객체가 μžˆλŠ”λ° 이 객체가 price / 1_000 둜직 κ΅¬ν˜„μ„ 담당해도 λ˜μ§€ μ•Šμ„κΉŒ?
κ·Έλ ‡λ‹€λ©΄ (price / 1_000) - passiveTicketCount λ‘œμ§λ„ Price 객체가 λ‹΄λ‹Ήν•  μˆ˜λ„ μžˆμ„ 것 κ°™λ‹€.

this.passiveTicketCount = passiveTicketCount;
}

public int getValue() {
return this.value;
public void create(Runnable runnable) {
for (int i = 0; i < this.autoTicketCount; i++) {
runnable.run();
}
}

public int getAutoTicketCount() {
return this.autoTicketCount;
}

public int getPassiveTicketCount() {
return passiveTicketCount;
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
TicketCount that = (TicketCount) o;
return getValue() == that.getValue();
return getAutoTicketCount() == that.getAutoTicketCount();
}

@Override
public int hashCode() {
return Objects.hashCode(getValue());
return Objects.hashCode(getAutoTicketCount());
}
}
18 changes: 4 additions & 14 deletions src/main/java/lotto/domain/WinningLotto.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ public class WinningLotto {
private final Lotto numbers;
private final NumberElement bonusNumber;

public WinningLotto(Integer... numbers) {
this(integerToLottoNumbers(numbers), NumberElement.create(0));
}

public WinningLotto(List<Integer> numbers, int bonusNumber) {
this(integerToLottoNumbers(numbers), NumberElement.create(bonusNumber));
}
Expand All @@ -29,14 +25,8 @@ public WinningLotto(Lotto numbers, NumberElement bonusNumber) {
this.bonusNumber = bonusNumber;
}

public LottoResult checkLottoNumber(BuyLotto buyLottos) {
return buyLottos.match(this);
}

private static Lotto integerToLottoNumbers(Integer... numbers) {
return new Lotto(Arrays.stream(numbers)
.map(NumberElement::create)
.collect(Collectors.toSet()));
public LottoResult checkLottoNumber(PurchasedLottos purchasedLottos) {
return purchasedLottos.match(this);
}

private static Lotto integerToLottoNumbers(List<Integer> numbers) {
Expand All @@ -59,8 +49,8 @@ private static boolean isBonus(int count) {
return count == 5;
}

public LottoRank checkBonusNumber(Lotto buyLotto, NumberElement bonusNumber) {
if (buyLotto.checkBonusNumber(bonusNumber)) {
public LottoRank checkBonusNumber(Lotto purchasedLotto, NumberElement bonusNumber) {
if (purchasedLotto.checkBonusNumber(bonusNumber)) {
return LottoRank.SECOND;
}
return LottoRank.THIRD;
Expand Down
39 changes: 32 additions & 7 deletions src/main/java/lotto/view/InputView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lotto.view;

import lotto.domain.BuyLotto;
import lotto.domain.PurchasedLottos;
import lotto.domain.Lotto;
import lotto.domain.LottoPrice;

Expand All @@ -12,24 +12,49 @@ public class InputView {
private static final Scanner scanner = new Scanner(System.in);

private static final String START_MESSAGE = "κ΅¬μž…κΈˆμ•‘μ„ μž…λ ₯ν•΄ μ£Όμ„Έμš”.";
private static final String LOTTO_COUNT_MESSAGE = "%d개λ₯Ό κ΅¬λ§€ν–ˆμŠ΅λ‹ˆλ‹€.";
private static final String LOTTO_COUNT_MESSAGE = "μˆ˜λ™μœΌλ‘œ %dμž₯, μžλ™μœΌλ‘œ %d개λ₯Ό κ΅¬λ§€ν–ˆμŠ΅λ‹ˆλ‹€.";
private static final String RESULT_LOTTO_MESSAGE = "μ§€λ‚œ μ£Ό 당첨 번호λ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš”.";
private static final String BONUS_NUMBER_MESSAGE = "λ³΄λ„ˆμŠ€ 볼을 μž…λ ₯ν•΄μ£Όμ„Έμš”.";
private static final String PASSIVE_BUY_COUNT_MESSAGE = "μˆ˜λ™μœΌλ‘œ ꡬ맀할 둜또 수λ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš”.";
private static final String PASSIVE_BUY_MESSAGE = "μˆ˜λ™μœΌλ‘œ ꡬ맀할 번호λ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš”.";

public static void printStart() {
System.out.println(START_MESSAGE);
}

public static LottoPrice inputBuyPrice() {
return new LottoPrice(scanner.nextInt());
public static int inputBuyPrice() {
return scanner.nextInt();
}

public static void printLottoCount(LottoPrice lottoPrice) {
System.out.println(String.format(LOTTO_COUNT_MESSAGE, lottoPrice.getTicketCount()));
System.out.println(String.format(
LOTTO_COUNT_MESSAGE,
lottoPrice.getTicketCount().getPassiveTicketCount(),
lottoPrice.getTicketCount().getAutoTicketCount())
);
}

public static void printBuyLotto(PurchasedLottos purchasedLottos) {
purchasedLottos.getLottos().stream().forEach(System.out::println);
}

public static void printBuyLotto(BuyLotto buyLotto) {
buyLotto.getLottos().stream().forEach(System.out::println);
public static void printPassiveBuyLottoCount() {
System.out.println(PASSIVE_BUY_COUNT_MESSAGE);
}

public static void printPassiveBuyLotto() {
System.out.println(PASSIVE_BUY_MESSAGE);
}

public static void inputPassiveBuyLotto(PurchasedLottos purchasedLottos, int count) {
scanner.nextLine();
for (int i = 0; i < count; i++) {
purchasedLottos.add(new Lotto(StringToIntegerArray(scanner.next())));
}
}

public static int inputPassiveBuyLottoCount() {
return scanner.nextInt();
}

public static void printResultLottoNumber() {
Expand Down
Loading