Skip to content
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

[안재홍] 로또 미션 제출합니다 #3794

Open
wants to merge 1 commit into
base: master
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
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,20 @@
* 모든 피드백을 완료하면 다음 단계를 도전하고 앞의 과정을 반복한다.

## 온라인 코드 리뷰 과정
* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/next-step/nextstep-docs/tree/master/codereview)
* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/next-step/nextstep-docs/tree/master/codereview)

## 기능 목록

1. 로또는 6개의 로또 번호를 가진다
2. 당첨 로또는 6개의 로또 번호와 1개의 보너스 로또 번호를 가진다
3. 로또가 가지는 로또 번호는 중복되지 않는다
4. 당첨 로또가 가지는 로또 번호는 보너스 로또 번호와 중복되지 않는다
5. 로또 번호는 1부터 45까지의 정수이다
6. 6개의 로또 번호 일치시 2000000000 원을 제공한다
7. 5개의 로또 번호와 보너스 번호 일치시 30000000 원을 제공한다
8. 5개의 로또 번호 일치시 1500000 원을 제공한다
9. 4개의 로또 번호 일치시 50000 원을 제공한다
10. 3개의 로또 번호 일치시 5000 원을 제공한다
11. 2개 이하의 로또 번호 일치시 0원을 제공한다
12. 사용자가 입력한 로또 가격으로 구매할 수 있는 최대 갯수의 로또를 구매한다
13. 수익률은 (당첨 금액/구입 금액) 으로 계산하며 소수점 둘째 자리까지 표기한다(내림으로 계산한다)
10 changes: 10 additions & 0 deletions src/main/java/LottoMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import view.LottoView;

public class LottoMain {

public static void main(String[] args) {
new LottoView().manual();
// new LottoView().auto();
}

}
32 changes: 32 additions & 0 deletions src/main/java/domain/GeneralLottoGamePolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package domain;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class GeneralLottoGamePolicy implements LottoGamePolicy {
public static final long FIRST_PRIZE_MATCH_COUNT = 6;
public static final long SECOND_PRIZE_MATCH_COUNT = 5;
public static final long THIRD_PRIZE_MATCH_COUNT = 5;
public static final long FOURTH_PRIZE_MATCH_COUNT = 4;
public static final long FIFTH_PRIZE_MATCH_COUNT = 3;

private Map<Long, LottoGamePrize> lottoGamePrizes = new HashMap<>();
private Map<LottoMatchResult, LottoGamePrize> lottoGamePrizesWithBonus = new HashMap<>();

public GeneralLottoGamePolicy() {
lottoGamePrizes.put(FIRST_PRIZE_MATCH_COUNT, LottoGamePrize.FIRST_PRIZE);
lottoGamePrizesWithBonus.put(LottoMatchResult.of(SECOND_PRIZE_MATCH_COUNT, true), LottoGamePrize.SECOND_PRIZE);
lottoGamePrizesWithBonus.put(LottoMatchResult.of(THIRD_PRIZE_MATCH_COUNT, false), LottoGamePrize.THIRD_PRIZE);
lottoGamePrizes.put(FOURTH_PRIZE_MATCH_COUNT, LottoGamePrize.FOURTH_PRIZE);
lottoGamePrizes.put(FIFTH_PRIZE_MATCH_COUNT, LottoGamePrize.FIFTH_PRIZE);
}

@Override
public LottoGamePrize rank(long matchCount, boolean isMatchBonus) {
if (matchCount == 5) {
return lottoGamePrizesWithBonus.get(LottoMatchResult.of(matchCount, isMatchBonus));
}
return Optional.ofNullable(lottoGamePrizes.get(matchCount)).orElse(LottoGamePrize.NONE);
}
}
37 changes: 37 additions & 0 deletions src/main/java/domain/Lotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package domain;

import java.util.*;
import java.util.stream.Collectors;

public class Lotto {
public static final int PRICE = 1000;
private static final int LOTTO_NUMBER_COUNT = 6;

private Set<LottoNumber> numbers;

protected Lotto(Set<LottoNumber> numbers) {
this.numbers = numbers;
}

public Set<LottoNumber> getNumbers() {
return numbers;
}

public static Lotto of(int... numbers) {
Lotto lotto = new Lotto(Arrays.stream(numbers)
.mapToObj(LottoNumber::from)
.collect(Collectors.toSet()));

if (lotto.getNumbers().size() != LOTTO_NUMBER_COUNT) {
throw new IllegalArgumentException("로또 번호는 " + LOTTO_NUMBER_COUNT + "개의 숫자를 가질 수 있습니다");
}

return lotto;
}

public static Lotto generate() {
return Lotto.of(new Random()
.ints(LottoNumber.MIN_LOTTO_NUMBER, LottoNumber.MAX_LOTTO_NUMBER + 1)
.distinct().limit(LOTTO_NUMBER_COUNT).toArray());
}
}
41 changes: 41 additions & 0 deletions src/main/java/domain/LottoGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package domain;

import java.util.ArrayList;
import java.util.HashMap;

public class LottoGame {

private final LottoGamePolicy lottoGamePolicy;

protected LottoGame(LottoGamePolicy lottoGamePolicy) {
this.lottoGamePolicy = lottoGamePolicy;
}

public static LottoGame policyFrom(LottoGamePolicy lottoGamePolicy) {
return new LottoGame(lottoGamePolicy);
}

public LottoGamePrize match(Lotto lotto, WinningLotto winningLotto) {
return lottoGamePolicy.rank(
lotto.getNumbers().stream().filter(winningLotto.getNumbers()::contains).count(),
lotto.getNumbers().stream().anyMatch(winningLotto.getBonusNumber()::equals));
}

public float calculateRoi(HashMap<LottoGamePrize, Integer> result, int ticketCount) {
int totalPrize = 0;

for (LottoGamePrize prize : result.keySet()) {
totalPrize += prize.getValue() * result.get(prize);
}

return Float.valueOf(totalPrize) / Float.valueOf(Lotto.PRICE * ticketCount);
}

public ArrayList<Lotto> generateLotto(int ticketCount) {
ArrayList<Lotto> lottos = new ArrayList<>();
for (int i = 0; i < ticketCount; i++) {
lottos.add(Lotto.generate());
}
return lottos;
}
}
5 changes: 5 additions & 0 deletions src/main/java/domain/LottoGamePolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package domain;

public interface LottoGamePolicy {
LottoGamePrize rank(long matchCount, boolean isMatchBonus);
}
20 changes: 20 additions & 0 deletions src/main/java/domain/LottoGamePrize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package domain;

public enum LottoGamePrize {
FIRST_PRIZE(2000000000),
SECOND_PRIZE(30000000),
THIRD_PRIZE(1500000),
FOURTH_PRIZE(50000),
FIFTH_PRIZE(5000),
NONE(0);

private final long value;

LottoGamePrize(long value) {
this.value = value;
}

public long getValue() {
return value;
}
}
29 changes: 29 additions & 0 deletions src/main/java/domain/LottoMatchResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package domain;

import java.util.Objects;

public class LottoMatchResult {
private final long matchCount;
private boolean isMatchBonus;
protected LottoMatchResult(long matchCount, boolean isMatchBonus) {
this.matchCount = matchCount;
this.isMatchBonus = isMatchBonus;
}

public static LottoMatchResult of(long matchCount, boolean isMatchBonus) {
return new LottoMatchResult(matchCount, isMatchBonus);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LottoMatchResult that = (LottoMatchResult) o;
return matchCount == that.matchCount && isMatchBonus == that.isMatchBonus;
}

@Override
public int hashCode() {
return Objects.hash(matchCount, isMatchBonus);
}
}
44 changes: 44 additions & 0 deletions src/main/java/domain/LottoNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package domain;

import java.util.Objects;

public class LottoNumber implements Comparable{

public static final int MAX_LOTTO_NUMBER = 45;
public static final int MIN_LOTTO_NUMBER = 1;

private int number;

public LottoNumber(int number) {
this.number = number;
}

public static LottoNumber from(int number) {
if (number < 1 || number > 45) {
throw new IllegalArgumentException("로또 번호는 1부터 45까지의 정수입니다");
}
return new LottoNumber(number);
}

public int getNumber() {
return number;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LottoNumber that = (LottoNumber) o;
return number == that.number;
}

@Override
public int hashCode() {
return Objects.hash(number);
}

@Override
public int compareTo(Object o) {
return this.getNumber() - ((LottoNumber) o).getNumber();
}
}
31 changes: 31 additions & 0 deletions src/main/java/domain/WinningLotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package domain;

import java.util.Set;

public class WinningLotto {

private Lotto lotto;
private LottoNumber bonusNumber;

protected WinningLotto(Lotto lotto, LottoNumber bonusNumber) {
this.lotto = lotto;
this.bonusNumber = bonusNumber;
}

public static WinningLotto of(Lotto lotto, LottoNumber bonusNumber) {
if (lotto.getNumbers().contains(bonusNumber)) {
throw new IllegalArgumentException("보너스 번호는 당첨 번호와 중복될 수 없습니다");
}
return new WinningLotto(lotto, bonusNumber);
}

public Set<LottoNumber> getNumbers() {
return lotto.getNumbers();

}

public LottoNumber getBonusNumber() {
return bonusNumber;
}

}
Loading