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주차] 객체지향 코드연습 (Suehyun666) #31

Open
wants to merge 48 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
e6ecb6a
lotto 클래스
Suehyun666 Sep 30, 2024
e287377
로또 진행 클래스
Suehyun666 Sep 30, 2024
41845b7
로또 생성 클래스
Suehyun666 Sep 30, 2024
69aba52
result
Suehyun666 Oct 5, 2024
928bcc2
Create OutputHandler.java
Suehyun666 Oct 5, 2024
87adba7
lotto generate
Suehyun666 Oct 5, 2024
26c32dd
lotto
Suehyun666 Oct 5, 2024
355ebf0
inputhandler
Suehyun666 Oct 5, 2024
619aae4
Update LottoGame.java
Suehyun666 Oct 5, 2024
7aaa590
inputhandler
Suehyun666 Oct 5, 2024
77bae68
1.application
Suehyun666 Oct 5, 2024
6049a3f
2. inputhandler
Suehyun666 Oct 5, 2024
a14bc17
3. 로또 클래스
Suehyun666 Oct 5, 2024
bff4f55
4. 결과계산
Suehyun666 Oct 5, 2024
0d78f68
5. 로또 생성기
Suehyun666 Oct 5, 2024
20eb9f3
6. 로또 게임
Suehyun666 Oct 5, 2024
97b2cd3
7. 출력 핸들러
Suehyun666 Oct 5, 2024
1a2ebfe
로또 클래스 다이어그램입니다.
Suehyun666 Oct 6, 2024
1858311
1.1 application
Suehyun666 Oct 9, 2024
fde267e
2.1 inputhandler
Suehyun666 Oct 9, 2024
f208822
2.2 inputconstants
Suehyun666 Oct 9, 2024
55be10e
2.3 inputvalidator
Suehyun666 Oct 9, 2024
a6c7390
3.1 로또 클래스
Suehyun666 Oct 9, 2024
a5440df
3.2 lottoconstants
Suehyun666 Oct 9, 2024
bcb6e36
3.3 로또 구매 정보
Suehyun666 Oct 9, 2024
e48a13c
4.1 결과계산
Suehyun666 Oct 9, 2024
3867749
4.2 결과계산 constants
Suehyun666 Oct 9, 2024
6de8f99
4.3 매치카운트 정보
Suehyun666 Oct 9, 2024
771e99b
7.1 출력 핸들러
Suehyun666 Oct 9, 2024
8382b15
6.1 로또게임
Suehyun666 Oct 9, 2024
64269f2
5.1 당첨번호 저장
Suehyun666 Oct 9, 2024
f018d51
Merge branch 'main' of https://github.com/Suehyun666/practice-oop-lotto
Suehyun666 Oct 9, 2024
89a61c1
삭제할 내용
Suehyun666 Oct 10, 2024
f7c2bbf
1.1.1application
Suehyun666 Oct 10, 2024
3430382
2.1 inputhandler
Suehyun666 Oct 10, 2024
743cfc9
2.2.1 InputConstants
Suehyun666 Oct 10, 2024
732acb0
2.3.1 InputValidator
Suehyun666 Oct 10, 2024
ec97241
3.1.1 lotto
Suehyun666 Oct 10, 2024
2aa1c6f
3.2.1 lottoconstants
Suehyun666 Oct 10, 2024
01e7fe1
3.3.1 purchaseinfo
Suehyun666 Oct 10, 2024
d2112f1
3.4.1 WinningInfo
Suehyun666 Oct 10, 2024
c1af815
4.1.1 resultcalculator
Suehyun666 Oct 10, 2024
96bbaf3
4.2.1 resultconstants
Suehyun666 Oct 10, 2024
4a63945
5.0.1 lottogenerator
Suehyun666 Oct 10, 2024
e764b62
7.1.1OutputHandler
Suehyun666 Oct 10, 2024
dfb20a1
7.2.1 outputconstants
Suehyun666 Oct 10, 2024
bea7598
6.1.1 lottogame
Suehyun666 Oct 10, 2024
958f0a5
4.3.1MatchResult
Suehyun666 Oct 10, 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
7 changes: 0 additions & 7 deletions src/main/java/lotto/Application.java

This file was deleted.

20 changes: 0 additions & 20 deletions src/main/java/lotto/Lotto.java

This file was deleted.

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

import camp.nextstep.edu.missionutils.Randoms;
import lotto.domain.Lotto;
import lotto.utils.LottoConstants;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class LottoGenerator {

public List<Lotto> buy(int money) {
int lottoCount = money / LottoConstants.LOTTO_PRICE;
return generate(lottoCount);
}

private List<Lotto> generate(int count) {
return IntStream.range(0, count)
.mapToObj(i -> new Lotto(
Randoms.pickUniqueNumbersInRange(
LottoConstants.LOTTO_NUMBER_MIN,
LottoConstants.LOTTO_NUMBER_MAX,
LottoConstants.LOTTO_NUMBER_COUNT)
))
.collect(Collectors.toList());
}
}
44 changes: 44 additions & 0 deletions src/main/java/lotto/controller/ResultCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package lotto.controller;

import lotto.domain.Lotto;
import lotto.domain.MatchResult;
import lotto.utils.ResultConstants;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static lotto.utils.ResultConstants.*;

public class ResultCalculator {

public Map<ResultConstants, Integer> calculateResults(List<Lotto> boughtLottos, List<Integer> winningNumbers, int bonusNumber) {
Map<ResultConstants, Integer> resultMap = new HashMap<>();
for (Lotto lotto : boughtLottos) {
MatchResult result = getMatchResult(lotto.getNumbers(), winningNumbers, bonusNumber);
ResultConstants rank = ResultConstants.valueOf(result.getMatchCount(), result.isBonusMatch());
resultMap.put(rank, resultMap.getOrDefault(rank, ZERO) + ONE);
}
return resultMap;
}

public double calculateProfitRate(Map<ResultConstants, Integer> resultMap, int totalCost) {
int totalPrize = resultMap.entrySet().stream()
.mapToInt(entry -> entry.getKey().getPrize() * entry.getValue())
.sum();
return (double) totalPrize / totalCost * PERCENTAGE_CONVERTER;
}

private MatchResult getMatchResult(List<Integer> numbers, List<Integer> winningNumbers, int bonusNumber) {
int matchCount = getMatchCount(numbers, winningNumbers);
boolean bonusMatch = isBonusMatch(numbers, bonusNumber);
return new MatchResult(matchCount, bonusMatch);
}

private int getMatchCount(List<Integer> numbers, List<Integer> winningNumbers) {
return (int) numbers.stream().filter(winningNumbers::contains).count();
}

private boolean isBonusMatch(List<Integer> numbers, int bonusNumber) {
return numbers.contains(bonusNumber);
}
}
25 changes: 25 additions & 0 deletions src/main/java/lotto/domain/Lotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package lotto.domain;

import lotto.utils.LottoConstants;
import java.util.HashSet;
import java.util.List;

public class Lotto {

private final List<Integer> numbers;

public Lotto(List<Integer> numbers) {
validate(numbers);
this.numbers = numbers;
}

public List<Integer> getNumbers() {
return numbers;
}

private void validate(List<Integer> numbers) {
if (numbers.size() != LottoConstants.LOTTO_NUMBER_COUNT || new HashSet<>(numbers).size() != numbers.size()) {
throw new IllegalArgumentException(LottoConstants.LOTTO_ERROR);
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/lotto/domain/MatchResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package lotto.domain;

public class MatchResult {

private final int matchCount;
private final boolean bonusMatch;

public MatchResult(int matchCount, boolean bonusMatch) {
this.matchCount = matchCount;
this.bonusMatch = bonusMatch;
}

public int getMatchCount() {
return matchCount;
}

public boolean isBonusMatch() {
return bonusMatch;
}
}
28 changes: 28 additions & 0 deletions src/main/java/lotto/domain/PurchaseInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package lotto.domain;

import lotto.controller.LottoGenerator;
import java.util.List;

public class PurchaseInfo {

private final int money;
private final List<Lotto> boughtLottos;

public PurchaseInfo(int money, List<Lotto> boughtLottos) {
this.money = money;
this.boughtLottos = boughtLottos;
}

public static PurchaseInfo create(int money, LottoGenerator lottoGenerator) {
List<Lotto> boughtLottos = lottoGenerator.buy(money);
return new PurchaseInfo(money, boughtLottos);
}

public int getMoney() {
return money;
}

public List<Lotto> getBoughtLottos() {
return boughtLottos;
}
}
30 changes: 30 additions & 0 deletions src/main/java/lotto/domain/WinningInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package lotto.domain;

import lotto.service.InputHandler;
import java.util.List;

public class WinningInfo {

private final List<Integer> winningNumbers;
private final int bonusNumber;

public WinningInfo(List<Integer> winningNumbers, int bonusNumber) {
this.winningNumbers = winningNumbers;
this.bonusNumber = bonusNumber;
}

public static WinningInfo fromInput(InputHandler inputHandler) {
List<Integer> winningNumbers = inputHandler.getWinningNumbers();
int bonusNumber = inputHandler.getBonusNumber(winningNumbers);
return new WinningInfo(winningNumbers, bonusNumber);
}

public List<Integer> getWinningNumbers() {
return winningNumbers;
}

public int getBonusNumber() {
return bonusNumber;
}
}

18 changes: 18 additions & 0 deletions src/main/java/lotto/service/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package lotto.service;

import lotto.controller.LottoGenerator;
import lotto.controller.ResultCalculator;
import lotto.utils.InputValidator;

public class Application {

public static void main(String[] args) {
OutputHandler outputHandler = new OutputHandler();
InputValidator inputValidator = new InputValidator();
InputHandler inputHandler = new InputHandler(outputHandler, inputValidator);
ResultCalculator resultCalculator = new ResultCalculator();
LottoGenerator lottoGenerator = new LottoGenerator();
LottoGame lottoGame = new LottoGame(inputHandler, resultCalculator, outputHandler,lottoGenerator);
lottoGame.start();
}
}
58 changes: 58 additions & 0 deletions src/main/java/lotto/service/InputHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package lotto.service;

import camp.nextstep.edu.missionutils.Console;
import lotto.utils.InputConstants;
import lotto.utils.InputValidator;
import java.util.ArrayList;
import java.util.List;

public class InputHandler {

private final OutputHandler outputHandler;
private final InputValidator inputValidator;

public InputHandler(OutputHandler outputHandler, InputValidator inputValidator) {
this.outputHandler = outputHandler;
this.inputValidator = inputValidator;
}

public int getMoney() {
outputHandler.print(InputConstants.GET_MONEY_PROMPT.getMessage());
String input = Console.readLine();
inputValidator.validateMoney(input);
return Integer.parseInt(input);
}

public List<Integer> getWinningNumbers() {
outputHandler.print(InputConstants.GET_WINNING_PROMPT.getMessage());
String input = Console.readLine();
return parseNumbers(input);
}

public int getBonusNumber(List<Integer> winningNumbers) {
outputHandler.print(InputConstants.GET_BONUS_PROMPT.getMessage());
String input = Console.readLine();
return parseBonusNumber(input, winningNumbers);
}

private List<Integer> parseNumbers(String input) {
String[] tokens = input.split(InputConstants.PARSING_CRITERIA.getMessage());
inputValidator.validateWinningCount(tokens);

List<Integer> numbers = new ArrayList<>();
for (String token : tokens) {
int number = Integer.parseInt(token.trim());
inputValidator.validateRange(number);
numbers.add(number);
}

inputValidator.validateNoDupes(numbers);
return numbers;
}

private int parseBonusNumber(String input, List<Integer> winningNumbers) {
int bonusNumber = Integer.parseInt(input.trim());
inputValidator.validateBonus(bonusNumber, winningNumbers);
return bonusNumber;
}
}
44 changes: 44 additions & 0 deletions src/main/java/lotto/service/LottoGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package lotto.service;

import lotto.controller.LottoGenerator;
import lotto.controller.ResultCalculator;
import lotto.domain.PurchaseInfo;
import lotto.utils.ResultConstants;
import lotto.domain.WinningInfo;
import java.util.Map;

public class LottoGame {

private final InputHandler inputHandler;
private final ResultCalculator resultCalculator;
private final OutputHandler outputHandler;
private final LottoGenerator lottoGenerator;

public LottoGame(InputHandler inputHandler, ResultCalculator resultCalculator, OutputHandler outputHandler, LottoGenerator lottoGenerator) {
this.inputHandler = inputHandler;
this.resultCalculator = resultCalculator;
this.outputHandler = outputHandler;
this.lottoGenerator = lottoGenerator;
}

public void start() {
PurchaseInfo purchaseInfo = PurchaseProcess();
WinningInfo winningInfo = WinningInfo.fromInput(inputHandler);
Map<ResultConstants, Integer> result = resultCalculator.calculateResults(
purchaseInfo.getBoughtLottos(), winningInfo.getWinningNumbers(), winningInfo.getBonusNumber()
);
printresult(result, purchaseInfo);
}

private void printresult(Map<ResultConstants, Integer> result, PurchaseInfo purchaseInfo) {
outputHandler.printStatistics(result);
outputHandler.printProfitRate(resultCalculator.calculateProfitRate(result, purchaseInfo.getMoney()));
}

private PurchaseInfo PurchaseProcess() {
PurchaseInfo purchaseInfo = PurchaseInfo.create(inputHandler.getMoney(), lottoGenerator);
outputHandler.printLottos(purchaseInfo.getBoughtLottos());
return purchaseInfo;
}
}

34 changes: 34 additions & 0 deletions src/main/java/lotto/service/OutputHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package lotto.service;

import lotto.domain.Lotto;
import lotto.utils.ResultConstants;
import java.util.List;
import java.util.Map;
import static lotto.utils.OutputConstants.*;

public class OutputHandler {

public void printLottos(List<Lotto> lottos) {
System.out.println(lottos.size() + BoughtCount_PROMPT);
lottos.forEach(lotto -> System.out.println(lotto.getNumbers()));
}

public void print(Object message) {
System.out.println(message);
}

public void printStatistics(Map<ResultConstants, Integer> resultMap) {
System.out.println(STATISTIC_PROMPT);
System.out.println(PARSING);

resultMap.forEach((rank, count) -> {
if (count > ZERO) {
System.out.printf(RESULTCOUNT, rank.getRank(), rank.getPrize(), count);
}
});
}

public void printProfitRate(double profitRate) {
System.out.printf(PROFITRATE, profitRate);
}
}
24 changes: 24 additions & 0 deletions src/main/java/lotto/utils/InputConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package lotto.utils;

public enum InputConstants {

GET_MONEY_PROMPT("구입 금액을 입력해 주세요."),
GET_WINNING_PROMPT("당첨 번호를 입력해 주세요. (예: 1,2,3,4,5,6)"),
GET_BONUS_PROMPT("보너스 번호를 입력해 주세요."),
PARSING_CRITERIA(","),
ERROR_INVALID_MONEY("[ERROR] 유효한 금액을 입력해 주세요. 금액은 1000원 이상이어야 합니다."),
ERROR_INVALID_WINNING_COUNT("[ERROR] 당첨 번호는 6개여야 합니다."),
ERROR_INVALID_RANGE("[ERROR] 번호는 1부터 45 사이여야 합니다."),
ERROR_DUPLICATE_NUMBER("[ERROR] 중복된 번호가 있습니다."),
ERROR_INVALID_BONUS("[ERROR] 보너스 번호는 1부터 45 사이의 당첨 번호와 중복되지 않는 번호여야 합니다.");

private final String message;

InputConstants(String message) {
this.message = message;
}

public String getMessage() {
return message;
}
}
Loading