-
Notifications
You must be signed in to change notification settings - Fork 92
[그리디] 하수한 로또 미션 3, 4, 5단계 제출합니다. #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: chemistryx
Are you sure you want to change the base?
Changes from all commits
0be3108
e0ea09c
ade5b82
9a72bef
f65ce69
a3438d7
15bd177
381d084
f9bce69
3768376
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.suhan.lotto.model; | ||
|
||
import java.util.Arrays; | ||
|
||
public enum Rank { | ||
FIRST(6, false, 2000000000, "6개 일치"), | ||
SECOND(5, true, 30000000, "5개 일치, 보너스 볼 일치"), | ||
THIRD(5, false, 1500000, "5개 일치"), | ||
FOURTH(4, false, 50000, "4개 일치"), | ||
FIFTH(3, false, 5000, "3개 일치"), | ||
NONE(0, false, 0, ""); // fallback | ||
|
||
private final int matchedCount; | ||
private final boolean bonusRequired; | ||
private final int prize; | ||
private final String description; | ||
|
||
Rank(int matchedCount, boolean bonusRequired, int prize, String description) { | ||
this.matchedCount = matchedCount; | ||
this.bonusRequired = bonusRequired; | ||
this.prize = prize; | ||
this.description = description; | ||
} | ||
|
||
public static Rank of(int matchedCount, boolean bonusMatched) { | ||
return Arrays.stream(Rank.values()) | ||
.filter((rank) -> rank.getMatchedCount() == matchedCount) | ||
.filter((rank) -> rank.isBonusRequired() == bonusMatched) | ||
.findFirst() | ||
.orElse(Rank.NONE); | ||
} | ||
|
||
public int getMatchedCount() { | ||
return matchedCount; | ||
} | ||
|
||
public boolean isBonusRequired() { | ||
return bonusRequired; | ||
} | ||
|
||
public int getPrize() { | ||
return prize; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,6 @@ | ||||||||||||||||||
package io.suhan.lotto.model.lotto; | ||||||||||||||||||
|
||||||||||||||||||
import java.util.Collections; | ||||||||||||||||||
import java.util.Comparator; | ||||||||||||||||||
import java.util.HashSet; | ||||||||||||||||||
import java.util.Set; | ||||||||||||||||||
|
@@ -9,22 +10,36 @@ public class Lotto { | |||||||||||||||||
public static final int LOTTO_NUMBER_MIN = 1; | ||||||||||||||||||
public static final int LOTTO_NUMBER_MAX = 45; | ||||||||||||||||||
|
||||||||||||||||||
private final LottoType type; | ||||||||||||||||||
private final Set<LottoNumber> numbers; | ||||||||||||||||||
|
||||||||||||||||||
public Lotto(Set<LottoNumber> numbers) { | ||||||||||||||||||
private Lotto(LottoType type, Set<LottoNumber> numbers) { | ||||||||||||||||||
if (numbers.size() != LOTTO_SIZE) { | ||||||||||||||||||
throw new IllegalArgumentException("로또 번호는 " + LOTTO_SIZE + "개여야 합니다."); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
this.type = type; | ||||||||||||||||||
this.numbers = new HashSet<>(numbers); | ||||||||||||||||||
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. 중복 제거를 위해 Set을 사용하신 거 맞죠? 😊 로또의 자료구조를 고민하셨다는 게 잘 느껴져서 좋게 봤습니다. 다만 Set의 대표적인 구현체인 HashSet, LinkedHashSet, TreeSet 중에서 HashSet을 선택하신 이유가 조금 궁금합니다. 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. 네 맞습니다~! 로또 구조의 특성상 중복을 제거할 필요가 있다고 판단하여
|
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
public static Lotto of(Set<LottoNumber> numbers) { | ||||||||||||||||||
return new Lotto(LottoType.AUTOMATIC, numbers); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
public static Lotto of(LottoType type, Set<LottoNumber> numbers) { | ||||||||||||||||||
return new Lotto(type, numbers); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
@Override | ||||||||||||||||||
public String toString() { | ||||||||||||||||||
return numbers.stream().sorted(Comparator.comparingInt(LottoNumber::getValue)).toList().toString(); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
public LottoType getType() { | ||||||||||||||||||
return type; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
public Set<LottoNumber> getNumbers() { | ||||||||||||||||||
return numbers; | ||||||||||||||||||
return Collections.unmodifiableSet(numbers); | ||||||||||||||||||
} | ||||||||||||||||||
} |
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.
Controller에서 이미 로또 수 검증을 하고 있는데, 실행 단계에서도 같은 검증이 중복되고 있네요.
검증 로직은 하나만 두어도 괜찮을 거 같아요.
추가적으로 로또 수 검증 로직은 Controller에 두는 게 적절할 지, 아니면 Model에 두는 게 더 적절할지 한 번 고민해보시면 좋겠습니다. (정답은 없습니다 :))
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.
현재 Controller에서는
manualCount
가 음수인 경우를 1차적으로 필터링하고, 이후PurchaseExecutor
에서는 양수인 경우에만InputView.getManualNumbers()
를 호출하여 수동 번호를 입력받도록 하고 있습니다.말씀하신대로 어느정도 중복되는 느낌이 들긴 하나,
PurchaseExecutor
에 있는 검증 로직(if (manualCount > 0)
)을 없앨 경우InputView.getManualNumbers()
가 무조건 1회는 실행되게 되어 0을 입력받게 되는 경우에도수동으로 구매할 번호를 입력해 주세요.
메시지가 출력되는 문제가 있었습니다.따라서 저런 방식으로 두번 검증하여 0인 경우에는 메시지 자체도 출력되지 않도록 구현하였는데, 혹시 더 좋은 방법이 있을까요?
Uh oh!
There was an error while loading. Please reload this page.
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.
"수동으로 구매할 번호를 입력해 주세요." 메시지가 출력되는 문제가 있었다는 사실은 몰랐네요.
다른 방법이라고 한다면 inputView에서 0을 바로 처리하는 방법도 있습니다.
하지만 이렇게 되면 코드에서 ‘빈 리스트를 반환하는 이유’가 명확히 드러나지 않고,
로직의 책임 분리 측면에서도 적합하지 않은 것 같습니다.
따라서, 저 역시 수한님께서 하신 방법을 사용할 거 같네요. 자세한 설명 감사드립니다 :)