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

[๊น€์˜์„œ] ๐Ÿš€ 2๋‹จ๊ณ„ - ๋กœ๋˜(์ˆ˜๋™) #3796

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
b2121f7
docs: ๊ธฐ๋Šฅ ๋ชฉ๋ก ์ž‘์„ฑ
dudtjakdl Jun 16, 2024
3007395
feat: LottoTicket ๋กœ๋˜ ํ‹ฐ์ผ“์ด ์ˆซ์ž๋ฅผ 6๊ฐœ ๊ฐ€์ง€๋Š” ๊ธฐ๋Šฅ ์ถ”๊ฐ€
dudtjakdl Jun 16, 2024
6c233f1
feat: LottoNumber ๋กœ๋˜ ํ‹ฐ์ผ“์˜ ์ˆซ์ž ๋ฒ”์œ„ ์ œํ•œ ๊ธฐ๋Šฅ ์ถ”๊ฐ€
dudtjakdl Jun 16, 2024
e64934d
refactor: LottoTicket ์˜ List<LottoNumber> -> Set<LottoNumber> ๋กœ ๋ณ€ํ™˜
dudtjakdl Jun 16, 2024
198e4be
feat: ํ‹ฐ์ผ“ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ž…๋ ฅํ•œ ์ˆ˜๋งŒํผ ์ž๋™ ๋กœ๋„ ํ‹ฐ์ผ“์„ ์ƒ์„ฑํ•˜๋Š” ๊ธฐ๋Šฅ ์ถ”๊ฐ€
dudtjakdl Jun 16, 2024
2241c44
fix: ๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅํ•˜๋ฉด ์ž๋™ ๋กœ๋˜ ํ‹ฐ์ผ“์„ ์ƒ์„ฑํ•˜๋„๋ก ๊ธฐ๋Šฅ ์ˆ˜์ •
dudtjakdl Jun 16, 2024
39b6586
fear: View, Controller, LottoTicketBundle ๊ตฌํ˜„
dudtjakdl Jun 16, 2024
eec46a8
feat: ๋‹น์ฒจ๋ฒˆํ˜ธ, ๋ณด๋„ˆ์Šค๋ณผ ์ž…๋ ฅ inputView ์ถ”๊ฐ€
dudtjakdl Jun 23, 2024
702abda
feat: LottRank enum ํด๋ž˜์Šค ์ถ”๊ฐ€, match ๊ธฐ๋Šฅ ์ถ”๊ฐ€
dudtjakdl Jun 23, 2024
a49cbc3
feat: LottoResult (๋‹น์ฒจ ํ†ต๊ณ„) ์ถ”๊ฐ€
dudtjakdl Jun 23, 2024
98111fb
feat: ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ˆซ์ž + ๋ณด๋„ˆ์Šค๋ณผ ์ˆซ์ž ์ผ์น˜ ๊ฐœ์ˆ˜ ๋ฐ˜ํ™˜ ๊ธฐ๋Šฅ ์ถ”๊ฐ€
dudtjakdl Jun 23, 2024
7f81e10
feat: ์ˆ˜์ต๋ฅ  ๊ณ„์‚ฐ ๊ธฐ๋Šฅ ์ถ”๊ฐ€
dudtjakdl Jun 23, 2024
5cb34b9
feat: ๋กœ๋˜ ํ†ต๊ณ„ ์ถœ๋ ฅ ๊ธฐ๋Šฅ ์ถ”๊ฐ€
dudtjakdl Jun 23, 2024
597f537
feat: ๋ณด๋„ˆ์Šค๋ณผ ์ฒดํฌ ๋กœ์ง ์ถ”๊ฐ€
dudtjakdl Jun 23, 2024
5b1663d
refactor: ํด๋ž˜์Šค ์ด๋ฆ„ ๋ณ€๊ฒฝ
dudtjakdl Jun 23, 2024
3379e7b
refactor: inputView ๋ฆฌํŒฉํ† ๋ง
dudtjakdl Jun 23, 2024
a8dc6ae
fix: ๋กœ๋˜ ๋“ฑ์ˆ˜ ๊ณ„์‚ฐ ๋กœ์ง ์ˆ˜์ •, ์ˆ˜์ต๋ฅ  ๊ณ„์‚ฐ ๋กœ์ง ์ˆ˜์ •
dudtjakdl Jun 23, 2024
238069e
fix: ๋‚™์ ๋œ ๊ฒฝ์šฐ๋Š” ๋‹น์ฒจ ํ†ต๊ณ„์—์„œ ์ถœ๋ ฅํ•˜์ง€ ์•Š๋„๋ก ์ˆ˜์ •
dudtjakdl Jun 23, 2024
f60c7ff
refactor: LottoController, LottoService ๋ฆฌํŒฉํ† ๋ง
dudtjakdl Jun 23, 2024
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
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,14 @@
* ๋ชจ๋“  ํ”ผ๋“œ๋ฐฑ์„ ์™„๋ฃŒํ•˜๋ฉด ๋‹ค์Œ ๋‹จ๊ณ„๋ฅผ ๋„์ „ํ•˜๊ณ  ์•ž์˜ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

## ์˜จ๋ผ์ธ ์ฝ”๋“œ ๋ฆฌ๋ทฐ ๊ณผ์ •
* [ํ…์ŠคํŠธ์™€ ์ด๋ฏธ์ง€๋กœ ์‚ดํŽด๋ณด๋Š” ์˜จ๋ผ์ธ ์ฝ”๋“œ ๋ฆฌ๋ทฐ ๊ณผ์ •](https://github.com/next-step/nextstep-docs/tree/master/codereview)
* [ํ…์ŠคํŠธ์™€ ์ด๋ฏธ์ง€๋กœ ์‚ดํŽด๋ณด๋Š” ์˜จ๋ผ์ธ ์ฝ”๋“œ ๋ฆฌ๋ทฐ ๊ณผ์ •](https://github.com/next-step/nextstep-docs/tree/master/codereview)

## ๊ธฐ๋Šฅ ๋ชฉ๋ก
- [x] ๋กœ๋˜ ํ‹ฐ์ผ“์€ ์ˆซ์ž๋ฅผ 6๊ฐœ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
- [x] ๋กœ๋˜ ํ‹ฐ์ผ“์— ๋“ค์–ด๊ฐ€๋Š” ๋กœ๋˜ ์ˆซ์ž๋Š” 1 ์ด์ƒ 45 ์ดํ•˜์˜ ์ˆซ์ž๋‹ค.
- [x] ๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•˜๋ฉด ๊ตฌ์ž…ํ•  ์ˆ˜ ์žˆ์„ ๋งŒํผ ์ž๋™ ๋กœ๋˜ ํ‹ฐ์ผ“์ด ๊ตฌ๋งค๋œ๋‹ค.
- [x] ๊ตฌ์ž…๊ธˆ์•ก๊ณผ ์ˆ˜๋™์œผ๋กœ ๊ตฌ๋งคํ•  ํ‹ฐ์ผ“ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋‚จ์€ ๊ธˆ์•ก๋งŒํผ ์ž๋™ ๊ตฌ๋งค ํ‹ฐ์ผ“ ์ˆ˜๊ฐ€ ๊ฒฐ์ •๋œ๋‹ค.
- [x] ๋‹น์ฒจ ๋ฒˆํ˜ธ 6๊ฐœ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋กœ๋˜ ํ‹ฐ์ผ“์— ์ˆซ์ž๊ฐ€ ๋ช‡๊ฐœ ์ผ์น˜ํ•˜๋Š”์ง€ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
- [x] ๋‹น์ฒจ ๋ฒˆํ˜ธ 6๊ฐœ์™€ ๋ณด๋„ˆ์Šค ๋ณผ์„ ์ž…๋ ฅํ•˜๋ฉด ์ˆซ์ž๊ฐ€ ๋ช‡๊ฐœ ์ผ์น˜ํ•˜๋Š”์ง€์™€ ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
- [x] ๋กœ๋˜ ํ‹ฐ์ผ“๊ณผ ๋‹น์ฒจ ๋ฒˆํ˜ธ 6๊ฐœ, ๋ณด๋„ˆ์Šค ๋ณผ์„ ์ž…๋ ฅํ•˜๋ฉด ๋ช‡๋“ฑ ์ƒ์ธ์ง€ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
- [x] ์ด ์ˆ˜์ต๋ฅ ์„ ๊ณ„์‚ฐํ•œ๋‹ค.
10 changes: 10 additions & 0 deletions src/main/java/lotto/LottoApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package lotto;

import lotto.controller.LottoController;

public class LottoApplication {
public static void main(String[] args) {
new LottoController().run();
}

}
38 changes: 38 additions & 0 deletions src/main/java/lotto/controller/LottoController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package lotto.controller;

import lotto.model.*;
import lotto.service.LottoService;
import lotto.view.InputView;
import lotto.view.ResultView;

public class LottoController {
private final InputView inputView = new InputView();
private final ResultView resultView = new ResultView();
private final LottoService lottoService = new LottoService();

public void run() {
/** ๊ตฌ์ž…๊ธˆ์•ก ์ž…๋ ฅ */
int payAmount = inputView.inputPayAmount();

/** ์ˆ˜๋™ ๊ตฌ๋งค ๋กœ๋˜ ์ˆ˜ ์ž…๋ ฅ ์ž…๋ ฅ */
int manualLottoTicketCount = inputView.inputManualLottoTicketCount();

/** ์ˆ˜๋™ ๊ตฌ๋งคํ•  ๋กœ๋˜ ๋ฒˆํ˜ธ ์ž…๋ ฅ */
inputView.printManualLottoNumbers();
LottoTicketBundle manualLottoTickets = inputView.inputManualLottoNumbers(manualLottoTicketCount);

/** ์ž๋™ ๊ตฌ๋งค ๋กœ๋˜ ์ƒ์„ฑ */
LottoTicketBundle autoLottoTickets = lottoService.createAutoLottoTickets(payAmount, manualLottoTicketCount);
LottoTicketBundle userLottoTickets = new LottoTicketBundle(manualLottoTickets, autoLottoTickets);

/** ๊ตฌ๋งคํ•œ ์ˆ˜๋™, ์ž๋™ ๋กœ๋˜ ํ‹ฐ์ผ“ ์ถœ๋ ฅ */
resultView.printLottoTicketCounts(manualLottoTicketCount, autoLottoTickets.getLottoTickets().size());
resultView.printUserLottoTickets(userLottoTickets);

/** ๋‹น์ฒจ ๋ฒˆํ˜ธ, ๋ณด๋„ˆ์Šค ๋ณผ ์ž…๋ ฅ */
WinningLotto winningLotto = new WinningLotto(new LottoTicket(inputView.inputWinningNumbers()), new LottoNumber(inputView.inputBonusNumber()));

/** ๋‹น์ฒจ ํ†ต๊ณ„ ์ถœ๋ ฅ */
resultView.printLottoResult(lottoService.calculateResult(userLottoTickets, winningLotto), payAmount);
}
}
38 changes: 38 additions & 0 deletions src/main/java/lotto/model/LottoNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package lotto.model;

import java.util.Objects;

public class LottoNumber {
public static final int LOTTO_NUBER_MIN = 1;
public static final int LOTTO_NUBER_MAX = 45;
private final int number;

public LottoNumber(int number) {
if (number < LOTTO_NUBER_MIN || number > LOTTO_NUBER_MAX) {
throw new IllegalArgumentException("๋กœ๋˜ ์ˆซ์ž๋Š” 1 ์ด์ƒ 45 ์ดํ•˜์˜ ์ˆซ์ž์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
}
this.number = number;
}

public static LottoNumber of(int number) {
return new LottoNumber(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.hashCode(number);
}

@Override
public String toString() {
return String.valueOf(number);
}
}
38 changes: 38 additions & 0 deletions src/main/java/lotto/model/LottoRank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package lotto.model;

public enum LottoRank {
BAD(0, 0),
THREE(3, 5000),
FOUR(4, 50000),
FIVE(5, 1500000),
FIVE_BONUS(5, 30000000),
SIX(6, 2000000000);

private final int matchCount;
private final int winningPrize;

LottoRank(int matchCount, int winningPrize) {
this.matchCount = matchCount;
this.winningPrize = winningPrize;
}

public int getMatchCount() {
return matchCount;
}

public int getWinningPrize() {
return winningPrize;
}

public static LottoRank valueOf(int matchCount, boolean hasBonus) {
if (matchCount == 5 && hasBonus) {
return FIVE_BONUS;
}
for (LottoRank rank : values()) {
if (rank.matchCount == matchCount) {
return rank;
}
}
return BAD;
}
}
31 changes: 31 additions & 0 deletions src/main/java/lotto/model/LottoResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lotto.model;

import java.util.EnumMap;
import java.util.Map;

public class LottoResult {
private final Map<LottoRank, Integer> resultMap;

public LottoResult() {
resultMap = new EnumMap<>(LottoRank.class);
for (LottoRank rank : LottoRank.values()) {
resultMap.put(rank, 0);
}
}

public void addRank(LottoRank rank) {
resultMap.put(rank, resultMap.get(rank) + 1);
}

public int getCount(LottoRank rank) {
return resultMap.get(rank);
}

public double getProfitRate() {
int totalWinningPrize = resultMap.entrySet().stream()
.mapToInt(entry -> entry.getKey().getWinningPrize() * entry.getValue())
.sum();
int totalTicketCost = resultMap.values().stream().mapToInt(count -> count * LottoTicket.LOTTO_TICKET_PRICE).sum();
return Math.floor((double) totalWinningPrize / totalTicketCost * 100) / 100;
}
}
39 changes: 39 additions & 0 deletions src/main/java/lotto/model/LottoTicket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lotto.model;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class LottoTicket {
public static final int LOTTO_TICKET_PRICE = 1000;
public static final int LOTTO_NUBERS_SIZE = 6;
private final Set<LottoNumber> lottoNumbers;

public LottoTicket(List<Integer> lottoNumbers) {
this(lottoNumbers.stream()
.map(number -> LottoNumber.of(number))
.collect(Collectors.toSet()));
}

protected LottoTicket(Set<LottoNumber> lottoNumbers) {
if (lottoNumbers.size() != LOTTO_NUBERS_SIZE) {
throw new IllegalArgumentException("๋กœ๋˜ ํ‹ฐ์ผ“์˜ ๋กœ๋˜ ์ˆซ์ž๋Š” 6๊ฐœ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
}
this.lottoNumbers = lottoNumbers;
}

public Set<LottoNumber> getLottoNumbers() {
return lottoNumbers;
}

public int countMatchingNumbers(LottoTicket otherNumbers) {
return (int) lottoNumbers.stream()
.filter(otherNumbers::contains)
.count();
}

public boolean contains(LottoNumber number) {
return lottoNumbers.contains(number);
}

}
45 changes: 45 additions & 0 deletions src/main/java/lotto/model/LottoTicketBundle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package lotto.model;

import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class LottoTicketBundle {
private final List<LottoTicket> lottoTickets;

public LottoTicketBundle(List<LottoTicket> lottoTickets) {
this.lottoTickets = lottoTickets;
}

public LottoTicketBundle(LottoTicketBundle manualLottoTickets, LottoTicketBundle autoLottoTickets) {
this(Stream.concat(manualLottoTickets.getLottoTickets().stream(), autoLottoTickets.getLottoTickets().stream())
.collect(Collectors.toList()));
}

public static LottoTicketBundle create(int payAmount) {
int ticketCount = payAmount / LottoTicket.LOTTO_TICKET_PRICE;
return new LottoTicketBundle(
IntStream.range(0, ticketCount)
.mapToObj(i -> new LottoTicket(generateRandomNumbers()))
.collect(Collectors.toList())
);
}

private static List<Integer> generateRandomNumbers() {
return IntStream.rangeClosed(LottoNumber.LOTTO_NUBER_MIN, LottoNumber.LOTTO_NUBER_MAX)
.boxed()
.collect(Collectors.collectingAndThen(
Collectors.toList(),
list -> {
Collections.shuffle(list, new Random());
return list.stream().limit(LottoTicket.LOTTO_NUBERS_SIZE).collect(Collectors.toList());
}));
}

public List<LottoTicket> getLottoTickets() {
return lottoTickets;
}
}
26 changes: 26 additions & 0 deletions src/main/java/lotto/model/WinningLotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package lotto.model;

public class WinningLotto {
private final LottoTicket winningLotto;
private final LottoNumber bonusNumber;

public WinningLotto(LottoTicket winningLotto, LottoNumber bonusNumber) {
this.winningLotto = winningLotto;
this.bonusNumber = bonusNumber;
}

public int match(LottoTicket target) {
int matchCount = winningLotto.countMatchingNumbers(target);
boolean hasBonus = target.getLottoNumbers().contains(bonusNumber);

if (matchCount == 5 && hasBonus) {
return 5;
}

return matchCount;
}

public LottoNumber getBonusNumber() {
return bonusNumber;
}
}
20 changes: 20 additions & 0 deletions src/main/java/lotto/service/LottoService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package lotto.service;

import lotto.model.*;

public class LottoService {
public LottoResult calculateResult(LottoTicketBundle userLottoTikcets, WinningLotto winningLotto) {
LottoResult lottoResult = new LottoResult();
for (LottoTicket lotto : userLottoTikcets.getLottoTickets()) {
int matchCount = winningLotto.match(lotto);
lottoResult.addRank(LottoRank.valueOf(matchCount, lotto.contains(winningLotto.getBonusNumber())));
}
return lottoResult;
}

public LottoTicketBundle createAutoLottoTickets(int payAmount, int manualLottoTicketCount) {
int autoLottoTicketCount
= (payAmount - (LottoTicket.LOTTO_TICKET_PRICE * manualLottoTicketCount)) / LottoTicket.LOTTO_TICKET_PRICE;
return LottoTicketBundle.create(autoLottoTicketCount * LottoTicket.LOTTO_TICKET_PRICE);
}
}
58 changes: 58 additions & 0 deletions src/main/java/lotto/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package lotto.view;

import lotto.model.LottoTicket;
import lotto.model.LottoTicketBundle;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class InputView {
private static final Scanner scanner = new Scanner(System.in);

public int inputPayAmount() {
System.out.println("๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");
return nextIntFromScanner();
}

public int inputManualLottoTicketCount() {
System.out.println("์ˆ˜๋™์œผ๋กœ ๊ตฌ๋งคํ•  ๋กœ๋˜ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");
return nextIntFromScanner();
}

public void printManualLottoNumbers() {
scanner.nextLine();
System.out.println("์ˆ˜๋™์œผ๋กœ ๊ตฌ๋งคํ•  ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");
}

public List<Integer> inputWinningNumbers() {
System.out.println("\n์ง€๋‚œ ์ฃผ ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");
return readIntegerListFromInput();
}

public int inputBonusNumber() {
System.out.println("๋ณด๋„ˆ์Šค ๋ณผ์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");
return nextIntFromScanner();
}

public LottoTicketBundle inputManualLottoNumbers(int manualLottoTicketCount) {
return new LottoTicketBundle(
IntStream.range(0, manualLottoTicketCount)
.mapToObj(i -> new LottoTicket(readIntegerListFromInput()))
.collect(Collectors.toList())
);
}

private int nextIntFromScanner() {
return scanner.nextInt();
}

private List<Integer> readIntegerListFromInput() {
return Arrays.stream(scanner.nextLine().split(","))
.map(String::trim)
.map(Integer::parseInt)
.collect(Collectors.toList());
}
}
Loading