This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
7회차 과제 PR #6
Open
chjih
wants to merge
1
commit into
SoongSilComputingClub:chjih
Choose a base branch
from
chjih:master
base: chjih
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
7회차 과제 PR #6
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
@@ -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) { | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 자동만 처리한다면 메소드 이름을 바꿔볼 수도 있을것 같아요 |
||
} | ||
} | ||
|
||
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,20 @@ | |
import java.util.stream.Collectors; | ||
|
||
public class LotteryTicket { | ||
private static final int NUMBER = 6; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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("---------"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} |
25 changes: 25 additions & 0 deletions
25
src/test/java/org/sscc/study/lotto/domain/LotteryTicketTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이것도 별도의 method 로 extract 할 수 있습니다.
그러면 비지니스 의미를 명확하게 할 수 있는 별도의 메소드 이름을 생성할 수 있죠 :)