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

#1

Open
wants to merge 1 commit into
base: main
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
63 changes: 63 additions & 0 deletions src/main/java/Controller/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package Controller;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

패키지명은 보통 다 소문자로 작성해요
자바 컨벤션을 읽어보는 걸 추천드려요~


import Enum.FixNum;
import Enum.Reward;
import Model.Lotto;
import Model.WinningNum;
import camp.nextstep.edu.missionutils.Randoms;

import java.util.*;


public class Controller {

// 로또 랜덤 넘버 뽑기
private List<Integer> random() {
List<Integer> lottoList = new ArrayList<>(Randoms.pickUniqueNumbersInRange(FixNum.MIN_NUM.get(), FixNum.MAX_NUM.get(), FixNum.LIST_SIZE.get()));
Collections.sort(lottoList);
return lottoList;
}

// 금액 만큼 로또 리스트 만들기
public List<Lotto> lottoList(int quntity) {
List<Lotto> list = new ArrayList<>();
for (int i = 0; i < quntity; i++) {
list.add(new Lotto(random()));
}
return list;
}

// 상금을 카운트해서 넣을 Map
private Map<Reward, Integer> resultList() {
Map<Reward, Integer> result = new LinkedHashMap<>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LinkedHashMap을 사용한 이유가 순서 유지 때문이라면 Map활용을 잘 하셨네요! 👍


for (Reward reward : Reward.values()) {
result.put(reward, 0);
}
return result;
}

// 상금에 해당하는 만큼 카운트 넣어주기
public Map<Reward, Integer> lottoResult(List<Lotto> lottoList, WinningNum winningLotto) {
Map<Reward, Integer> result = resultList();
Reward reward;

for (int i = 0; i < lottoList.size(); i++) {
reward = winningLotto.match(lottoList.get(i));
result.put(reward, result.get(reward) + 1);
}

return result;
}

// 수익률
public double rateOfReturn(Map<Reward, Integer> result, int quntity) {
double rateOfReturn = 0;
for (Reward reward : result.keySet()) {
rateOfReturn += ((double) (reward.getReward())
/ (quntity * FixNum.MIN_MONEY.get()) * (result.get(reward)) * (FixNum.PERCENTAGE.get()));

}
return rateOfReturn;
}
}
21 changes: 21 additions & 0 deletions src/main/java/Enum/FixNum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Enum;

public enum FixNum {
MIN_MONEY(1000),
LIST_SIZE(6),
MIN_NUM(1),
MAX_NUM(45),
PLUS_BONUS_NUM_SIZE(7),
WINNING_MIN_COUNT(3),
PERCENTAGE(100);

private int value;

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

public int get() {
return value;
}
}
20 changes: 20 additions & 0 deletions src/main/java/Enum/FixString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package Enum;

public enum FixString {
ERROR("[ERROR]"),
START("구입금액을 입력해 주세요."),
BUY_LOTTO("개를 구매했습니다."),
USER_LOTTO("당첨 번호를 입력해 주세요."),
BONUS_NUM("보너스 번호를 입력해 주세요."),
RESULT("당첨 통계");

private String message;

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

public String get() {
return message;
}
}
52 changes: 52 additions & 0 deletions src/main/java/Enum/Reward.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package Enum;

import View.Print;

public enum Reward {
FIRST(6, 2_000_000_000, "6개 일치 (2,000,000,000원) - "),
SECOND(5, 30_000_000, "5개 일치, 보너스 볼 일치 (30,000,000원) - "),
THIRD(5, 1_500_000, "5개 일치 (1,500,000원) - "),
FOURTH(4, 50_000, "4개 일치 (50,000원) - "),
FIVETH(3, 5_000, "3개 일치 (5,000원) - "),
MISS(0, 0, "");


private int count;
private int reward;
private String message;

Reward(int count, int reward, String message) {
this.count = count;
this.reward = reward;
this.message = message;
}

public int getReward() {
return reward;
}

public static Reward valueResult(int count, boolean bonusMatch) {
if (count < FixNum.WINNING_MIN_COUNT.get()) {
return MISS;
}

if (SECOND.count == count && bonusMatch) {
return SECOND;
}

for (Reward reward : values()) {
if (reward.count == count && reward != SECOND) {
return reward;
}
}

throw new IllegalArgumentException(FixString.ERROR.get());
}

public void printMessage(int count) {
if (this != MISS) {
Print.resultMessage(message, count);
}
}
}

64 changes: 64 additions & 0 deletions src/main/java/Model/Lotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package Model;

import Enum.FixNum;
import View.Print;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Lotto {
private final List<Integer> numbers;

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

private void validate(List<Integer> numbers) {
if (numbers.size() != 6) {
throw new IllegalArgumentException();
}
}

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

private void validateOver(List<Integer> numbers) {
Set<Integer> overCheck = new HashSet<>();
for (int i = 0; i < numbers.size(); i++) {
overCheck.add(numbers.get(i));
}

if (overCheck.size() != FixNum.LIST_SIZE.get()) {
Print.error();
throw new IllegalArgumentException();
}
}

public int countMatch(Lotto winningNum) {
return (int) numbers.stream().
filter(winningNum::containNumber).
count();
}

public boolean containNumber(int number) {
return numbers.contains(number);
}

private void rangeValue(List<Integer> numbers) {
for (int i = 0; i < numbers.size(); i++) {
if (!(FixNum.MIN_NUM.get() <= numbers.get(i) && numbers.get(i) <= FixNum.MAX_NUM.get())) {
Print.error();
throw new IllegalArgumentException();
}
}
}

// TODO: 추가 기능 구현
}
46 changes: 46 additions & 0 deletions src/main/java/Model/Money.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package Model;

import Enum.FixNum;
import View.Print;

public class Money {
private int money;

public Money(String money) {
int moneyMum = validateNm(money);
validateMoney(moneyMum);
this.money = moneyMum;
}

public int quantity() {
return money / FixNum.MIN_MONEY.get();
}

private void validateMoney(int money) {
isvalidateNum(money);
validateDivide(money);
}

private static int validateNm(String money) {
try {
return Integer.parseInt(money);
} catch (NumberFormatException e) {
Print.error();
throw new IllegalArgumentException();
}
}

private void isvalidateNum(int money) {
if (money <= 0) {
Print.error();
throw new IllegalArgumentException();
}
}

private void validateDivide(int money) {
if (money % FixNum.MIN_MONEY.get() != 0) {
Print.error();
throw new IllegalArgumentException();
}
}
}
56 changes: 56 additions & 0 deletions src/main/java/Model/WinningNum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package Model;

import Enum.FixNum;
import Enum.Reward;
import View.Print;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class WinningNum {
private Lotto winningNum;
private int bonusNum;

public WinningNum(String winningLotto, String inputBonusNum) {
this.winningNum = new Lotto(makeWinningNum(winningLotto));
this.bonusNum = validateDuplicate(winningNum, inputBonusNum);
}

public Reward match(Lotto randomNums) {
int count = randomNums.countMatch(winningNum);
boolean bonusCheck = randomNums.containNumber(bonusNum);
return Reward.valueResult(count, bonusCheck);
}

private List<Integer> makeWinningNum(String winningLotto) {
List<Integer> winLotto = new ArrayList<>();
try {
String[] temp = winningLotto.split(",");
for (String s : temp) {
winLotto.add(Integer.parseInt(s));
}
} catch (NumberFormatException e) {
Print.error();
throw new IllegalArgumentException();
}
return winLotto;
}

private int validateDuplicate(Lotto winningLotto, String inputBonusNum) {
Set<Integer> duplicateCheck = new HashSet<>(winningLotto.getNumbers());
try {
duplicateCheck.add(Integer.parseInt(inputBonusNum));

if (duplicateCheck.size() != FixNum.PLUS_BONUS_NUM_SIZE.get()) {
Print.error();
throw new IllegalArgumentException();
}
} catch (IllegalArgumentException e) {
Print.error();
throw new IllegalArgumentException();
}
return Integer.parseInt(inputBonusNum);
}
}
27 changes: 27 additions & 0 deletions src/main/java/View/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package View;

import camp.nextstep.edu.missionutils.Console;

public class Input {

public static String inputMoney() {
return Console.readLine();
}

public static String inputWinLottoNum() {
String winingNum = Console.readLine();
isvalidateInput(winingNum);
return winingNum;
}

public static String inputBonusNum() {
return Console.readLine();
}

private static void isvalidateInput(String winingNum) {
if (winingNum.contains("\"") || winingNum.contains(",,")) {
Print.error();
throw new IllegalArgumentException();
}
}
}
39 changes: 39 additions & 0 deletions src/main/java/View/Print.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package View;

import Enum.FixString;

public class Print {

public static void start() {
System.out.println(FixString.START.get());
}

public static void error() {
System.out.println(FixString.ERROR.get());
}

public static void buyLotto(int quntity) {
System.out.println(quntity + FixString.BUY_LOTTO.get());
}

public static void userLotto() {
System.out.println(FixString.USER_LOTTO.get());
}

public static void bonusNum() {
System.out.println(FixString.BONUS_NUM.get());
}

public static void result() {
System.out.println(FixString.RESULT.get());
System.out.println("---");
}

public static void resultMessage(String message, int count) {
System.out.println(message + count + "개");
}

public static void printRevenueRate(double EarningRate) {
System.out.println("총 수익률은 " + String.format("%.1f", EarningRate) + "%입니다.");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String.format("총 수익률은 %.1f%%입니다.", EarningRate);

하나로 format를 해줬으면 가독성이 더 좋았을 것 같아요

}
}
Loading