-
Notifications
You must be signed in to change notification settings - Fork 6
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
로또 2차 수동 미션 제출입니다. #8
base: main
Are you sure you want to change the base?
Changes from all commits
3fa5d55
c55fa2b
37f86af
41e90a8
6236937
dd1d112
0392693
7ea6d89
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea/ | ||
.gradle/ | ||
build/ | ||
out |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import domain.LottoNumber; | ||
import domain.Lotto; | ||
import domain.LottoCount; | ||
import domain.Lottos; | ||
import domain.Money; | ||
import domain.ProfitResult; | ||
import domain.WinningLotto; | ||
import ui.Receiver; | ||
import ui.Printer; | ||
|
||
public class LottoApplication { | ||
|
||
Printer printer = new Printer(); | ||
Receiver receiver = new Receiver(); | ||
|
||
public static void main(String[] args) { | ||
LottoApplication app = new LottoApplication(); | ||
app.start(); | ||
} | ||
|
||
private void start() { | ||
LottoCount count = getCountByMoney(); | ||
List<String> manualLottoNumbers = getManualLottoNumbers(count); | ||
printer.printNumberOfLottoTickets(count); | ||
Lottos lottos = Lottos.createLottos(manualLottoNumbers, count); | ||
printer.printLottos(lottos); | ||
WinningLotto winningLotto = getWinningLotto(); | ||
ProfitResult result = winningLotto.getResult(lottos); | ||
printer.printLottoProfit(result); | ||
printer.printIsLottoProfit(result.isProfit()); | ||
} | ||
|
||
private List<String> getManualLottoNumbers(LottoCount count) { | ||
List<String> manualLottoNumbers = new ArrayList<>(); | ||
if (count.hasManualLottoCount()) { | ||
manualLottoNumbers.addAll(receiver.receiveManualLottoNumbers(count)); | ||
} | ||
return manualLottoNumbers; | ||
} | ||
|
||
private LottoCount getCountByMoney() { | ||
int inputMoney = receiver.receiveMoney(); | ||
Money money = new Money(inputMoney); | ||
return money.getLottoCount(receiver.receiveManualNumberOfLottoTickets()); | ||
} | ||
|
||
private WinningLotto getWinningLotto() { | ||
Lotto winningLotto = Lotto.of(receiver.receiveWinningLottoNumbers()); | ||
LottoNumber bonusBall = LottoNumber.of(receiver.receiveBonusBall()); | ||
return new WinningLotto(winningLotto, bonusBall); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package domain; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class Lotto { | ||
|
||
private static final String LOTTO_NUMBER_COUNT_EXCEPTION_MESSAGE = "6개를 입력해주세요"; | ||
private static final String DELIMITER = ", "; | ||
private static final int NUMBER_COUNT_ZERO = 0; | ||
private static final int NUMBER_COUNT = 6; | ||
|
||
private final Set<LottoNumber> balls; | ||
|
||
private Lotto(Set<LottoNumber> balls) { | ||
validateBallCount(balls); | ||
this.balls = Collections.unmodifiableSet(new HashSet<>(balls)); | ||
} | ||
|
||
private void validateBallCount(Set<LottoNumber> balls) { | ||
if (balls.size() != NUMBER_COUNT) { | ||
throw new IllegalArgumentException(LOTTO_NUMBER_COUNT_EXCEPTION_MESSAGE); | ||
} | ||
} | ||
|
||
public static Lotto of(String rawLotto) { | ||
String[] numbers = rawLotto.split(DELIMITER); | ||
Set<LottoNumber> balls = new HashSet<>(); | ||
for (String number : numbers) { | ||
balls.add(LottoNumber.of(Integer.parseInt(number))); | ||
} | ||
return new Lotto(balls); | ||
} | ||
|
||
public static Lotto newAutoLotto() { | ||
List<LottoNumber> balls = LottoNumber.getBalls(); | ||
Collections.shuffle(balls); | ||
Set<LottoNumber> randomBalls = new HashSet<>(balls.subList(NUMBER_COUNT_ZERO, NUMBER_COUNT)); | ||
return new Lotto(randomBalls); | ||
} | ||
|
||
public static boolean moreThanBallCount(int matchCount) { | ||
return matchCount > NUMBER_COUNT; | ||
} | ||
|
||
public boolean contains(LottoNumber ball) { | ||
return balls.contains(ball); | ||
} | ||
|
||
public int countCommonBalls(Lotto lotto) { | ||
Set<LottoNumber> sameBalls = new HashSet<>(balls); | ||
sameBalls.retainAll(lotto.balls); | ||
return sameBalls.size(); | ||
} | ||
|
||
public List<String> getLotto() { | ||
List<String> ballsData = balls.stream() | ||
.map(LottoNumber::toString) | ||
.collect(Collectors.toList()); | ||
return Collections.unmodifiableList(ballsData); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,52 @@ | ||||||||||||
package domain; | ||||||||||||
|
||||||||||||
import java.util.Objects; | ||||||||||||
|
||||||||||||
public class LottoCount { | ||||||||||||
private static final int MINIMUM_MANUAL_LOTTO_COUNT = 0; | ||||||||||||
private final int manualLottoCount; | ||||||||||||
Comment on lines
+6
to
+7
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.
Suggested change
🙂 |
||||||||||||
private final int lottoCount; | ||||||||||||
|
||||||||||||
public LottoCount(int lottoCount) { | ||||||||||||
this(lottoCount, MINIMUM_MANUAL_LOTTO_COUNT); | ||||||||||||
} | ||||||||||||
|
||||||||||||
public LottoCount(int lottoCount, int manualLottoCount) { | ||||||||||||
this.lottoCount = lottoCount; | ||||||||||||
this.manualLottoCount = manualLottoCount; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public boolean hasManualLottoCount() { | ||||||||||||
return manualLottoCount != 0; | ||||||||||||
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. 0을 NO_COUNT 정도로 표현해줄수 있지 않을까요? |
||||||||||||
} | ||||||||||||
|
||||||||||||
public Money getBuyMoney(int lottoPrice) { | ||||||||||||
return new Money(lottoCount * lottoPrice); | ||||||||||||
} | ||||||||||||
|
||||||||||||
public int getAutoLottoCount() { | ||||||||||||
return lottoCount - manualLottoCount; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public int getManualLottoCount() { | ||||||||||||
return manualLottoCount; | ||||||||||||
} | ||||||||||||
|
||||||||||||
@Override | ||||||||||||
public boolean equals(Object o) { | ||||||||||||
if (this == o) { | ||||||||||||
return true; | ||||||||||||
} | ||||||||||||
if (o == null || getClass() != o.getClass()) { | ||||||||||||
return false; | ||||||||||||
} | ||||||||||||
LottoCount that = (LottoCount) o; | ||||||||||||
return manualLottoCount == that.manualLottoCount && | ||||||||||||
lottoCount == that.lottoCount; | ||||||||||||
} | ||||||||||||
|
||||||||||||
@Override | ||||||||||||
public int hashCode() { | ||||||||||||
return Objects.hash(manualLottoCount, lottoCount); | ||||||||||||
} | ||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class LottoNumber implements Comparable<LottoNumber> { | ||
|
||
private static final int MINIMUM_LOTTO_NUMBER = 1; | ||
private static final int MAXIMUM_LOTTO_NUMBER = 45; | ||
private static final String PRINT_IF_NUMBER_IS_INVALID_NUMBER = "유효한 로또 번호가 아닙니다."; | ||
private static final Map<Integer, LottoNumber> MAPPING_BALL = new HashMap<>(); | ||
|
||
private final int number; | ||
|
||
static { | ||
DisposeBall(); | ||
} | ||
|
||
private static void DisposeBall() { | ||
for (int ballNumber = MINIMUM_LOTTO_NUMBER; ballNumber <= MAXIMUM_LOTTO_NUMBER; ballNumber++) { | ||
MAPPING_BALL.put(ballNumber, new LottoNumber(ballNumber)); | ||
} | ||
} | ||
Comment on lines
+22
to
+26
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. 로또 넘버들을 초기화하는데 Map을 사용하셨군요, 좋은 아이디어 같습니다! 오늘 세션에서처럼 이같은 경우 Map을 사용하면 중복을 제거하는 로직을 넣지 않아도 된다는 이점이 있을 것 같네요. |
||
|
||
private LottoNumber(int number) { | ||
this.number = number; | ||
} | ||
|
||
public static LottoNumber of(int number) { | ||
if (MAPPING_BALL.containsKey(number)) { | ||
return MAPPING_BALL.get(number); | ||
} | ||
throw new IllegalArgumentException(PRINT_IF_NUMBER_IS_INVALID_NUMBER); | ||
} | ||
|
||
public static List<LottoNumber> getBalls() { | ||
return new ArrayList<>(MAPPING_BALL.values()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Integer.toString(number); | ||
} | ||
|
||
@Override | ||
public int compareTo(LottoNumber o) { | ||
return Integer.compare(number, o.number); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
LottoNumber ball = (LottoNumber) o; | ||
return number == ball.number; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(number); | ||
} | ||
} |
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.
main이니 큰 문제는 없겠지만, 그래도 오용을 막기위해선 private final을 붙여주는게 안전할것 같네요 🙂