Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

7회차 과제 PR #6

Open
wants to merge 1 commit into
base: chjih
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.sscc.study.lotto.view.InputView;
import org.sscc.study.lotto.view.ResultView;

import java.util.List;

public class Controller {
public static void main(String[] args) {
InputView inputView = new InputView();
Expand All @@ -15,9 +17,12 @@ public static void main(String[] args) {

InputMoney money = inputView.inputPrice();
lotteryMachine.pay(money.getMoney());
lotteryMachine.setManualNumber(inputView.inputManualNumber());
List<String> manualNumbers = inputView.inputLotteryNumbers(lotteryMachine.getManualNumber());
lotteryMachine.buyManualTicket(manualNumbers);
lotteryMachine.drawNumbers();

resultView.printNumber(lotteryMachine.getNumber());
resultView.printNumber(lotteryMachine.getNumber(), lotteryMachine.getManualNumber());
resultView.printLottoNumbers(lotteryMachine.getString());

WinningNumbers winningNumbers = inputView.inputWinningNumbers();
Expand Down
36 changes: 34 additions & 2 deletions src/main/java/org/sscc/study/lotto/domain/LotteryMachine.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package org.sscc.study.lotto.domain;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class LotteryMachine {
private static final int PRICE = 1000;
private int number;
private int manualNumber;
private LotteryTickets lotteryTickets;
private WinningStat winningStat = new WinningStat();
private final NumberStrategy numberStrategy = new RandomLottoNumberGenerator();
Expand All @@ -13,9 +19,31 @@ public void pay(int money) {
winningStat = new WinningStat();
}

public void setManualNumber(int number) {
this.manualNumber = number;
validate();
}

private void validate() {
if (number - manualNumber < 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

이것도 별도의 method 로 extract 할 수 있습니다.
그러면 비지니스 의미를 명확하게 할 수 있는 별도의 메소드 이름을 생성할 수 있죠 :)

throw new IllegalArgumentException("입급된 금액보다 수동으로 입력된 티켓이 더 많습니다.");
}
}

public void buyManualTicket(List<String> givenNumbers) {
givenNumbers.forEach(x -> lotteryTickets.addManualTicket(new LotteryTicket(getNumbers(x))));
}

public Set<LottoNumber> getNumbers(String givenNumber) {
return Arrays.stream(givenNumber.split(", "))
.map(x -> new LottoNumber(Integer.parseInt(x)))
.collect(Collectors.toSet());
}

// 자동
public void drawNumbers() {
for (int i = 0; i < number; i++) {
lotteryTickets.addLotteryTicket(numberStrategy);
for (int i = 0; i < number - manualNumber; i++) {
lotteryTickets.addAutoTicket(numberStrategy);
Comment on lines 44 to +46
Copy link
Collaborator

Choose a reason for hiding this comment

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

자동만 처리한다면 메소드 이름을 바꿔볼 수도 있을것 같아요

}
}

Expand All @@ -27,6 +55,10 @@ public int getNumber() {
return number;
}

public int getManualNumber() {
return manualNumber;
}

public WinningStat getStat(WinningNumbers winningNumbers) {
lotteryTickets.duplicateNumbers(winningNumbers).forEach(winningStat::add);
return winningStat;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/sscc/study/lotto/domain/LotteryTicket.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
import java.util.stream.Collectors;

public class LotteryTicket {
private static final int NUMBER = 6;
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

private final Set<LottoNumber> lotteryNumbers = new HashSet<>();

public LotteryTicket(Set<LottoNumber> numbers) {
validate(numbers);
lotteryNumbers.addAll(numbers);
}

private void validate(Set<LottoNumber> numbers) {
if (numbers.size() != NUMBER) {
throw new IllegalArgumentException("로또 번호는 6개여야 합니다.");
}
}

public String getString() {
return "[" + lotteryNumbers.stream()
.sorted()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ public LotteryTickets() {
lotteryTickets = new ArrayList<>();
}

public void addLotteryTicket(NumberStrategy numberStrategy) {
public void addManualTicket(LotteryTicket lotteryTicket) {
lotteryTickets.add(lotteryTicket);
}

public void addAutoTicket(NumberStrategy numberStrategy) {
lotteryTickets.add(new LotteryTicket(numberStrategy.getNumber()));
}

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/sscc/study/lotto/view/InputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.sscc.study.lotto.domain.InputMoney;
import org.sscc.study.lotto.domain.WinningNumbers;

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

public class InputView {
Expand All @@ -13,6 +15,20 @@ public InputMoney inputPrice() {
return new InputMoney(Integer.parseInt(sc.nextLine()));
}

public int inputManualNumber() {
System.out.println("수동으로 구매할 로또 수를 입력해 주세요.");
return Integer.parseInt(sc.nextLine());
}

public List<String> inputLotteryNumbers(int number) {
System.out.println("수동으로 구매할 번호를 입력해 주세요.");
List<String> get = new ArrayList<>();
for (int i = 0; i < number; i++) {
get.add(sc.nextLine());
}
return get;
}

public WinningNumbers inputWinningNumbers() {
System.out.println("지난 주 당첨 번호를 입력해 주세요.");
String s = sc.nextLine();
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/sscc/study/lotto/view/ResultView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import org.sscc.study.lotto.domain.WinningStat;

public class ResultView {
public void printNumber(int number) {
System.out.println(number + "개를 구매했습니다.");
}

public void printLottoNumbers(String result) {
System.out.println(result);
}

public void printNumber(int manual, int auto) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

🆗

System.out.println("수동으로 " + manual + "장, 자동으로 " + auto + "개를 구매했습니다.");
}

public void printStat(WinningStat winningStat) {
System.out.println("당첨 통계");
System.out.println("---------");
Expand Down
27 changes: 24 additions & 3 deletions src/test/java/org/sscc/study/lotto/domain/LotteryMachineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.Arrays;

import static org.assertj.core.api.Assertions.*;

class LotteryMachineTest {
@Test
Expand Down Expand Up @@ -41,4 +42,24 @@ public void testLottoNumber(String input) {
WinningNumbers winningNumbers = new WinningNumbers(input, 7);
}).isInstanceOf(IllegalArgumentException.class);
}
}

@DisplayName("금액보다 더 많은 개수의 수동 로또 구매 개수가 입력되었을 때")
@Test
public void testManualNumber(){
LotteryMachine lotteryMachine = new LotteryMachine();
lotteryMachine.pay(1000);
assertThatThrownBy(()-> lotteryMachine.setManualNumber(3)).isInstanceOf(IllegalArgumentException.class);
}

@DisplayName("수동로또 티켓이 올바르게 당첨 되는 지 확인")
@Test
public void testManualTicket(){
LotteryMachine lotteryMachine = new LotteryMachine();
lotteryMachine.pay(3000);
lotteryMachine.setManualNumber(3);
lotteryMachine.buyManualTicket(Arrays.asList("1, 2, 3, 4, 5, 6",
"1, 3, 5, 7, 9, 11", "2, 4, 6, 8, 10, 12"));
WinningStat stat = lotteryMachine.getStat(new WinningNumbers("1, 2, 3, 4, 5, 6", 7));
assertThat(stat.get(Prize.FIRST)).isEqualTo(1);
}
Comment on lines +46 to +64
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

}
25 changes: 25 additions & 0 deletions src/test/java/org/sscc/study/lotto/domain/LotteryTicketTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.sscc.study.lotto.domain;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.HashSet;
import java.util.Set;

import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

class LotteryTicketTest {
@Test
@DisplayName("로또 번호 갯수가 6개가 아닐 때 에러처리 확인")
public void testLottoNumber() {
Set<LottoNumber> numbers = new HashSet<>();
numbers.add(new LottoNumber(1));
numbers.add(new LottoNumber(3));
numbers.add(new LottoNumber(4));
numbers.add(new LottoNumber(11));
numbers.add(new LottoNumber(13));
assertThatThrownBy(() -> {
LotteryTicket lotteryTicket = new LotteryTicket(numbers);
}).isInstanceOf(IllegalArgumentException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class LotteryTicketsTest {
@DisplayName("당첨번호 입력 시 번호가 일치하는 지 확인")
public void testWinning() {
LotteryTickets lotteryTickets = new LotteryTickets();
lotteryTickets.addLotteryTicket(new StaticLottoNumberGenerator());
lotteryTickets.addAutoTicket(new StaticLottoNumberGenerator());
List<Prize> matchData = lotteryTickets.
duplicateNumbers(new WinningNumbers("1, 2, 3, 4, 5, 6", 7));
assertThat(matchData.get(0)).isEqualTo(Prize.FIRST);
Expand All @@ -22,7 +22,7 @@ public void testWinning() {
@DisplayName("2등(5개-보너스볼)이 일치되는 지 확인")
public void testSecond() {
LotteryTickets lotteryTickets = new LotteryTickets();
lotteryTickets.addLotteryTicket(new StaticLottoNumberGenerator());
lotteryTickets.addAutoTicket(new StaticLottoNumberGenerator());
List<Prize> matchData = lotteryTickets.
duplicateNumbers(new WinningNumbers("1, 2, 3, 4, 5, 10", 6));
assertThat(matchData.get(0)).isEqualTo(Prize.SECOND);
Expand All @@ -32,7 +32,7 @@ public void testSecond() {
@DisplayName("로또 번호가 문자열 형식에 맞게 출력되는 지 확인")
public void testNumberString() {
LotteryTickets lotteryTickets = new LotteryTickets();
lotteryTickets.addLotteryTicket(new StaticLottoNumberGenerator());
lotteryTickets.addAutoTicket(new StaticLottoNumberGenerator());
String result = lotteryTickets.getString();
assertThat(result).isEqualTo("[1, 2, 3, 4, 5, 6]");
}
Expand Down