-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Step2 auto #4036
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
Step2 auto #4036
Changes from all commits
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,25 @@ | ||
package step2.lotto; | ||
|
||
import step2.lotto.domain.Lottos; | ||
import step2.lotto.domain.Statistic; | ||
import step2.lotto.game.Game; | ||
import step2.lotto.view.InputView; | ||
import step2.lotto.view.ResultView; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
int paidMoney = InputView.getPaidMoney(); | ||
|
||
Game game = new Game(); | ||
Lottos lottos = game.createLottos(paidMoney); | ||
ResultView.showLottos(lottos); | ||
|
||
String winningLotto = InputView.lastWeekLottoNumbers(); | ||
Statistic stat = game.play(winningLotto); | ||
|
||
ResultView.showStatistics(stat, paidMoney); | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package step2.lotto.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
public class Lotto { | ||
|
||
private final List<Integer> lottoNumbers; | ||
bourbonkk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public Lotto(List<Integer> lottoNumbers) { | ||
lottoNumbers.forEach(this::validateNumber); | ||
checkDuplicate(lottoNumbers); | ||
checkSize(lottoNumbers); | ||
this.lottoNumbers = lottoNumbers; | ||
} | ||
|
||
private void validateNumber(int lottoNumber) { | ||
if (lottoNumber < 1 || lottoNumber > 45) { | ||
throw new IllegalArgumentException("로또 번호는 1부터 45까지의 숫자만 가능합니다."); | ||
} | ||
} | ||
|
||
private void checkSize(List<Integer> lottoNumbers) { | ||
if (lottoNumbers.size() != 6) { | ||
throw new IllegalArgumentException("로또 번호는 6개여야 합니다."); | ||
} | ||
} | ||
|
||
private void checkDuplicate(List<Integer> lottoNumbers) { | ||
if (lottoNumbers.stream().distinct().count() != lottoNumbers.size()) { | ||
throw new IllegalArgumentException("로또 번호는 중복될 수 없습니다."); | ||
} | ||
} | ||
|
||
public List<Integer> lottoNumbers() { | ||
return lottoNumbers; | ||
} | ||
|
||
public int matchCount(Lotto lastWeekLotto) { | ||
return (int) this.lottoNumbers().stream() | ||
.filter(lastWeekLotto.lottoNumbers()::contains) | ||
.count(); | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package step2.lotto.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class LottoNumberGenerator { | ||
|
||
private final List<Integer> numbers; | ||
|
||
public LottoNumberGenerator() { | ||
this.numbers = new ArrayList<>(); | ||
for (int i = 1; i <= 45; i++) { | ||
numbers.add(i); | ||
} | ||
} | ||
|
||
public List<Integer> generate() { | ||
Collections.shuffle(numbers); | ||
List<Integer> generatedNumbers = new ArrayList<>(numbers.subList(0, 6)); | ||
Collections.sort(generatedNumbers); | ||
return generatedNumbers; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package step2.lotto.domain; | ||
|
||
import java.util.List; | ||
|
||
public class Lottos { | ||
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 List<Lotto> lottos; | ||
|
||
public Lottos(List<Lotto> lottos) { | ||
this.lottos = lottos; | ||
} | ||
|
||
public int size() { | ||
return lottos.size(); | ||
} | ||
|
||
public List<Lotto> getLottos() { | ||
return lottos; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package step2.lotto.domain; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Statistic { | ||
|
||
public static final Map<Integer, Integer> PRIZE_MAP = Map.of( | ||
3, 5000, | ||
4, 50000, | ||
5, 1500000, | ||
6, 2000000000 | ||
); | ||
private final Map<Integer, Integer> matchCountMap = new HashMap<>(); | ||
private int totalPrize = 0; | ||
|
||
public void calculate(Lottos lottos, Lotto lastWeekLotto) { | ||
for (Lotto lotto : lottos.getLottos()) { | ||
int match = lotto.matchCount(lastWeekLotto); | ||
statistic(match); | ||
} | ||
} | ||
|
||
public Map<Integer, Integer> getMatchCountMap() { | ||
return matchCountMap; | ||
} | ||
|
||
public int getTotalPrize() { | ||
return totalPrize; | ||
} | ||
|
||
private void statistic(int match) { | ||
if (match >= 3) { | ||
matchCountMap.put(match, matchCountMap.getOrDefault(match, 0) + 1); | ||
totalPrize += PRIZE_MAP.get(match); | ||
} | ||
} | ||
|
||
public double getProfitRate(int paidMoney) { | ||
// lottoCount * 1000 = 총 투자금 | ||
int lottoCount = paidMoney / 1000; | ||
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. 로또 금액 1000원이 이곳저곳에서 사용되므로 |
||
if (lottoCount <= 0) { | ||
return 0.0; | ||
} | ||
return (double) totalPrize / paidMoney; | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,55 @@ | ||||||
package step2.lotto.game; | ||||||
|
||||||
import java.util.ArrayList; | ||||||
import java.util.List; | ||||||
import step2.lotto.domain.LottoNumberGenerator; | ||||||
import step2.lotto.domain.Lotto; | ||||||
import step2.lotto.domain.Lottos; | ||||||
import step2.lotto.domain.Statistic; | ||||||
|
||||||
public class Game { | ||||||
|
||||||
private final int LOTTO_PRICE = 1000; | ||||||
private int gameCount; | ||||||
private final LottoNumberGenerator generator; | ||||||
private Lottos lottos; | ||||||
|
||||||
public Game() { | ||||||
this.generator = new LottoNumberGenerator(); | ||||||
} | ||||||
|
||||||
public Lottos createLottos(int paidMoney) { | ||||||
calculateLottoCount(paidMoney); | ||||||
rollingLotto(); | ||||||
return lottos; | ||||||
} | ||||||
|
||||||
public void calculateLottoCount(int paidMoney) { | ||||||
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으로 노출을 최소화시키는 게 좋겠네요 |
||||||
gameCount = paidMoney / LOTTO_PRICE; | ||||||
} | ||||||
|
||||||
public void rollingLotto() { | ||||||
List<Lotto> lotto = new ArrayList<>(); | ||||||
for (int i = 0; i < gameCount; i++) { | ||||||
lotto.add(new Lotto(generator.generate())); | ||||||
} | ||||||
this.lottos = new Lottos(lotto); | ||||||
} | ||||||
|
||||||
public Statistic play(String winningLotto) { | ||||||
Lotto lastWeekLotto = new Lotto(convertStringToList(winningLotto)); | ||||||
Statistic stat = new Statistic(); | ||||||
stat.calculate(lottos, lastWeekLotto); | ||||||
return stat; | ||||||
} | ||||||
|
||||||
private List<Integer> convertStringToList(String lastWeekLottoResult) { | ||||||
String[] split = lastWeekLottoResult.trim().split(","); | ||||||
List<Integer> integerList = new ArrayList<>(); | ||||||
for (String s : split) { | ||||||
integerList.add(Integer.parseInt(s.trim())); | ||||||
} | ||||||
return integerList; | ||||||
} | ||||||
Comment on lines
+46
to
+53
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. Lotto 애플리케이션 중심으로 봤을 때 InputView는 단순 콘솔 입력만을 처리하는 것이 아닌 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. 좋은것같습니다. 의견감사합니다. |
||||||
|
||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package step2.lotto.view; | ||
|
||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
|
||
private static final Scanner scanner = new Scanner(System.in); | ||
|
||
public static int getPaidMoney() { | ||
System.out.println("구입금액을 입력해 주세요."); | ||
int money = scanner.nextInt(); | ||
scanner.nextLine(); | ||
return money; | ||
} | ||
|
||
public static String lastWeekLottoNumbers() { | ||
System.out.println("지난 주 당첨 번호를 입력해 주세요."); | ||
return scanner.nextLine(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package step2.lotto.view; | ||
|
||
import step2.lotto.domain.Lotto; | ||
import step2.lotto.domain.Lottos; | ||
import step2.lotto.domain.Statistic; | ||
|
||
public class ResultView { | ||
|
||
public static void showLottos(Lottos lottos) { | ||
System.out.println(lottos.size() + "개를 구매했습니다."); | ||
for (Lotto lotto : lottos.getLottos()) { | ||
System.out.println(lotto.lottoNumbers()); | ||
} | ||
} | ||
|
||
public static void showStatistics(Statistic stat, int paidMoney) { | ||
System.out.println("\n당첨 통계\n---------"); | ||
for (int i = 3; i <= 6; i++) { | ||
int count = stat.getMatchCountMap().getOrDefault(i, 0); | ||
int prize = Statistic.PRIZE_MAP.get(i); | ||
System.out.printf("%d개 일치 (%d원)- %d개\n", i, prize, count); | ||
} | ||
|
||
double rate = stat.getProfitRate(paidMoney); | ||
|
||
System.out.printf("총 수익률은 %.2f입니다.(기준이 1이기 때문에 결과적으로 %s)\n", | ||
rate, rate < 1 ? "손해라는 의미임" : "이득이라는 의미임"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package step2.domain; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import step2.lotto.game.Game; | ||
|
||
public class GameTest { | ||
|
||
@Test | ||
void 지난주_로또_번호는_쉼표를_구분지어_입력한다() { | ||
assertThatThrownBy(() -> { | ||
Game game = new Game(); | ||
game.play("1, 2, 3, 4, 5, 6, 10"); | ||
}).hasMessage("로또 번호는 6개여야 합니다."); | ||
|
||
assertThatThrownBy(() -> { | ||
Game game = new Game(); | ||
game.play("1. 2. 3. 4. 5"); | ||
}).hasMessage("For input string: \"1. 2. 3. 4. 5\""); | ||
|
||
assertThatThrownBy(() -> { | ||
Game game = new Game(); | ||
game.play("1, 2, 3, 4, 5, 46"); | ||
}).hasMessage("로또 번호는 1부터 45까지의 숫자만 가능합니다."); | ||
|
||
assertThatCode(() -> { | ||
Game game = new Game(); | ||
game.calculateLottoCount(1000); | ||
game.rollingLotto(); | ||
game.play("1, 2, 3, 4, 5, 6"); | ||
}).doesNotThrowAnyException(); | ||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.