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

Seokwoo #2129

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open

Seokwoo #2129

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
4 changes: 3 additions & 1 deletion src/main/java/lotto/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package lotto;

import lotto.controller.MainController;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
MainController.start();
}
}
47 changes: 47 additions & 0 deletions src/main/java/lotto/controller/BuyerController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package lotto.controller;

import lotto.domain.Buyer;
import lotto.global.exception.LottoException;
import lotto.global.handler.ExceptionHandler;
import lotto.view.input.InputView;

import static java.lang.String.format;
import static lotto.global.config.LottoConfig.LOTTO_COST;
import static lotto.global.exception.ErrorMessage.COST_ERROR_MESSAGE;
import static lotto.global.exception.ErrorMessage.NON_DIGIT_COST_MESSAGE;
import static lotto.view.output.OutputMessage.ASK_PURCHASE_PRICE_MESSAGE;
import static lotto.view.output.OutputMessage.PURCHASE_INFORMATION_MESSAGE;
import static lotto.view.output.OutputView.*;

public class BuyerController {
public static Buyer requestCost() {
return ExceptionHandler.execute(BuyerController::purchase);
}

private static Buyer purchase() {
PrintStaticMessage(ASK_PURCHASE_PRICE_MESSAGE);

String input = InputView.Input();
int cost;

if(!isDigit(input)) throw new LottoException(NON_DIGIT_COST_MESSAGE);

cost = Integer.parseInt(input);

if (!isValidCost(cost)) throw new LottoException(COST_ERROR_MESSAGE);

PrintNewLine();
PrintDynamicMessage(format(PURCHASE_INFORMATION_MESSAGE.getMessage(), cost / LOTTO_COST.getValue()));

return Buyer.createBuyer(cost);
}

private static boolean isValidCost(int cost) {
return cost % LOTTO_COST.getValue() == 0;
}

private static boolean isDigit(String input) {
return input.matches("[0-9]*");
}

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

import lotto.domain.Lottos;
import lotto.service.LottoService;

import java.util.ArrayList;
import java.util.List;

import static lotto.view.output.OutputView.PrintLottoNumbers;
import static lotto.view.output.OutputView.PrintNewLine;

public class LottoController {
public static Lottos generateLottos(int purchaseCount) {
List<List<Integer>> numbersList = new ArrayList<>();

for (int i = 0; i < purchaseCount; i++) {
List<Integer> numbers = LottoService.generateRandomNumbers();
numbersList.add(numbers);
PrintLottoNumbers(numbers);
}

PrintNewLine();

return LottoService.convertToLottos(numbersList);
}


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

import lotto.domain.Buyer;
import lotto.domain.Lottos;
import lotto.domain.Prize;

public class MainController {
public static void start() {
Buyer buyer = BuyerController.requestCost();

Lottos lottos = LottoController.generateLottos(buyer.purchaseCount);

Prize prize = PrizeController.pickWinningNumber();

StatisticsController.getStatistics(lottos, prize, buyer);
}
}
42 changes: 42 additions & 0 deletions src/main/java/lotto/controller/PrizeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package lotto.controller;

import lotto.domain.Prize;
import lotto.global.handler.ExceptionHandler;
import lotto.service.PrizeService;
import lotto.view.input.InputView;

import java.util.List;

import static lotto.view.output.OutputMessage.ASK_BONUS_NUMBER_MESSAGE;
import static lotto.view.output.OutputMessage.ASK_WINNING_NUMBER_MESSAGE;
import static lotto.view.output.OutputView.PrintNewLine;
import static lotto.view.output.OutputView.PrintStaticMessage;

public class PrizeController {
public static Prize pickWinningNumber() {
return ExceptionHandler.execute(PrizeController::setWinningNumber);
}

private static Prize setWinningNumber() {
List<Integer> numbers;

PrintStaticMessage(ASK_WINNING_NUMBER_MESSAGE);
numbers = PrizeService.parsingNumbers(InputView.Input());

PrintNewLine();

return ExceptionHandler.execute(() -> setBonusNumber(numbers));
}

private static Prize setBonusNumber(List<Integer> numbers) {
String input;
int bonusNumber;

PrintStaticMessage(ASK_BONUS_NUMBER_MESSAGE);

input = InputView.Input();
bonusNumber = PrizeService.validateBonusNumber(numbers, input);

return Prize.createPrize(numbers, bonusNumber);
}
}
25 changes: 25 additions & 0 deletions src/main/java/lotto/controller/StatisticsController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package lotto.controller;

import lotto.domain.Buyer;
import lotto.domain.Lottos;
import lotto.domain.Prize;
import lotto.domain.Statistics;
import lotto.service.StatisticsService;

import java.util.List;

import static lotto.view.output.OutputMessage.WINNING_STATISTICS_MESSAGE;
import static lotto.view.output.OutputView.PrintStaticMessage;
import static lotto.view.output.OutputView.PrintStatisticsMessage;

public class StatisticsController {
public static void getStatistics(Lottos lottos, Prize prize, Buyer buyer) {
PrintStaticMessage(WINNING_STATISTICS_MESSAGE);

Statistics statistics = StatisticsService.computeStatistics(lottos, prize, buyer);

List<String> result = StatisticsService.createStatisticsString(statistics);

PrintStatisticsMessage(result);
}
}
26 changes: 26 additions & 0 deletions src/main/java/lotto/domain/Buyer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package lotto.domain;

import static lotto.global.config.LottoConfig.LOTTO_COST;

public class Buyer {
private int purchaseCost;
public int purchaseCount;

private Buyer(int purchaseCost) {
purchaseCount = getPurchaseCount(purchaseCost);

this.purchaseCost = purchaseCost;
}

public static Buyer createBuyer(int purchaseCost) {
return new Buyer(purchaseCost);
}

public int getPurchaseCost() {
return purchaseCost;
}

private int getPurchaseCount(int payment) {
return payment / LOTTO_COST.getValue();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lotto;
package lotto.domain;

import java.util.List;

Expand All @@ -16,5 +16,7 @@ private void validate(List<Integer> numbers) {
}
}

// TODO: 추가 기능 구현
public List<Integer> getNumbers() {
return numbers;
}
}
19 changes: 19 additions & 0 deletions src/main/java/lotto/domain/Lottos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package lotto.domain;

import java.util.List;

public class Lottos {
private final List<Lotto> lottos;

private Lottos(List<Lotto> lottos) {
this.lottos = lottos;
}

public static Lottos createLottos(List<Lotto> lottos) {
return new Lottos(lottos);
}

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

import java.util.List;

public class Prize {
private List<Integer> numbers;
private int bonusNumber;

private Prize(List<Integer> numbers, int bonusNumber) {
this.numbers = numbers;
this.bonusNumber = bonusNumber;
}

public static Prize createPrize(List<Integer> numbers, int bonusNumber) {
return new Prize(numbers, bonusNumber);
}

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

public int getBonusNumber() {
return bonusNumber;
}

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

import java.util.List;

public class Statistics {
private static List<Integer> rank;
private static float profitability;

private Statistics(List<Integer> rank, float profitability) {
this.rank = rank;
this.profitability = profitability;
}

public static Statistics createStatistics(List<Integer> rank, float profitability) {
return new Statistics(rank, profitability);
}

public static List<Integer> getRank() {
return rank;
}

public static float getProfitability() {
return profitability;
}
}
24 changes: 24 additions & 0 deletions src/main/java/lotto/global/config/LottoConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package lotto.global.config;

public enum LottoConfig {
LOTTO_COST(1000),

MIN_LOTTO_NUM(1),
MAX_LOTTO_NUM(45),

NUMBER_OF_PICKS(6),
NUMBER_OF_BONUS(1),

NUMBER_OF_WINNER(5);

public int getValue() {
return value;
}

private final int value;

LottoConfig(int value) {
this.value = value;
}

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

public enum PrizeConfig {
FIRST_WINNING_PRIZE(1, 6,2000000000),
SECOND_WINNING_PRIZE(2,5,30000000),
THIRD_WINNING_PRIZE(3,5,1500000),
FOURTH_WINNING_PRIZE(4,4,50000),
FIFTH_WINNING_PRIZE(5,3,5000),
NON_WINNING(-1,-1,0);

private final int price;
private final int rank;
private final int count;

public static int getPriceByRank(int rank) {
for (PrizeConfig prizeConfig : PrizeConfig.values()) {
if (prizeConfig.rank == rank) {
return prizeConfig.price;
}
}
return -1;
}

public static int getCountByRank(int rank) {
for (PrizeConfig prizeConfig : PrizeConfig.values()) {
if (prizeConfig.rank == rank) {
return prizeConfig.count;
}
}
return -1;
}

PrizeConfig(int rank, int count, int price) {
this.rank = rank;
this.count = count;
this.price = price;
}
}
29 changes: 29 additions & 0 deletions src/main/java/lotto/global/exception/ErrorMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package lotto.global.exception;

import static java.lang.String.format;
import static lotto.global.config.LottoConfig.*;

public enum ErrorMessage {
COST_ERROR_MESSAGE(format("구입 금액은 로또 1장 가격(%d)으로 나누어 떨어져야합니다.", LOTTO_COST.getValue())),
NON_DIGIT_COST_MESSAGE("구입 금액은 숫자만 입력할 수 있습니다."),

INVALID_WINNING_NUMBER_MESSAGE("로또 번호는 1부터 45 사이의 숫자여야 합니다."),
NON_DIGIT_ERROR_MESSAGE("로또 번호는 숫자만 입력할 수 있습니다."),

EMPTY_CONTAINS_ERROR_MESSAGE("공백 문자 없이 입력되어야 합니다."),
INVALID_DELIMITER_MESSAGE("당첨 번호는 ','로 구분되어야 합니다."),
INVALID_PICK_NUMBER_MESSAGE(format("총 %d개의 숫자가 입력되어야 합니다.", NUMBER_OF_PICKS.getValue())),
DUPLICATED_MESSAGE("중복된 당첨 번호는 입력할 수 없습니다."),

DUPLICATED_BONUS_NUMBER_MESSAGE("당첨 번호와 중복되는 보너스 번호는 입력할 수 없습니다.");

public String getMessage() {
return message;
}

private final String message;

ErrorMessage(String message) {
this.message = message;
}
}
9 changes: 9 additions & 0 deletions src/main/java/lotto/global/exception/LottoException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package lotto.global.exception;

public class LottoException extends IllegalArgumentException {
private static final String PREFIX = "[ERROR]";

public LottoException(ErrorMessage message) {
super(String.format("%s %s", PREFIX, message.getMessage()));
}
}
Loading