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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
15 changes: 15 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## 구현 기능 목록 ##

# View
- 로또 가격 입력
- 로또 개수 출력
- 로또 개수만큼 리스트 출력
- 당첨 번호 입력
- 보너스 번호 입력
- 당첨 통계 출력

# Controller
-

# Model

7 changes: 0 additions & 7 deletions src/main/java/lotto/Application.java

This file was deleted.

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

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

public class ErrorCheckingAndParsing {

static final int THOUSAND_PLACE = 1000;
static final int MINIMUM_NUMBER_RANGE = 1;
static final int MAXIMUM_NUMBER_RANGE = 45;
static final int LOTTO_NUM_COUNT = 6;

public static int isDividedThousand(int price){

if (price % THOUSAND_PLACE != 0){
throw new IllegalArgumentException("[Error] 남은 동전이 없어 잔돈을 드리지 못 합니다. 다시 입력해 주세요.");
}

return price / THOUSAND_PLACE;
}

public static int isNum(String lottoPrice){
try {
int price = Integer.parseInt(lottoPrice);

return price;
}catch (IllegalArgumentException e){
throw new IllegalArgumentException("숫자를 입력해 주세요.");
}

}

public static void confirmNumRange(int lottoNum){
if (lottoNum < MINIMUM_NUMBER_RANGE || lottoNum > MAXIMUM_NUMBER_RANGE){
throw new IllegalArgumentException("[Error] 1부터 45까지의 숫자만 입력해 주세요.");
}

}

public static void isOnlySixNums(List<Integer> winNums){
if (winNums.size() != LOTTO_NUM_COUNT){
throw new IllegalArgumentException("[Error] 숫자 6개를 입력해 주세요.");
}
}

public static List<Integer> isNumsAsString(String[] winNumbersAsStrings){
try {
List<Integer> winNums = Arrays.stream(winNumbersAsStrings)
.map(Integer::parseInt)
.collect(Collectors.toList());

return winNums;
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("[Error] 숫자를 입력해 주세요.");
}
}

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

import java.util.Scanner;
import lotto.error.ErrorCheckingAndParsing;

public class InputHandler {

int price = 0;
static Scanner scanner = new Scanner(System.in);

public void getPrice(){
System.out.println("구입 금액을 입력해 주세요.");

String lottoPrice = scanner.nextLine();
price = ErrorCheckingAndParsing.isNum(lottoPrice);
}

public int getInputPrice() {
return price;
}

public String getWinNums() {
System.out.println("\n당첨 번호를 입력해 주세요.");

String winNumbers = scanner.nextLine();

return winNumbers;
}

public int getBonusNum(){
System.out.println("\n보너스 번호를 입력해 주세요.");
int bonusNumber = scanner.nextInt();

ErrorCheckingAndParsing.confirmNumRange(bonusNumber);

return bonusNumber;
}

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

import java.util.List;
import java.util.Map;
import lotto.lotto.LottoMatchCounter;
import lotto.lotto.MyLottoMaker;
import lotto.lotto.ProfitCalculator;

public class OutputHandler {

final int FIRST_PLACE = 6;
final int SECOND_PLACE = 5;
final int THIRD_PLACE = 4;
final int FOURTH_PLACE = 3;
final int FIFTH_PLACE = 2;
MyLottoMaker myLottoMaker;
LottoMatchCounter lottoMatchCounter;
ProfitCalculator profitCalculator;

public OutputHandler(MyLottoMaker myLottoMaker){
this.myLottoMaker = myLottoMaker;
printLottos(myLottoMaker);
}

public OutputHandler(LottoMatchCounter lottoMatchCounter, ProfitCalculator profitCalculator){

this.lottoMatchCounter = lottoMatchCounter;
this.profitCalculator = profitCalculator;
printLottoResult(lottoMatchCounter, profitCalculator);
}

public void printLottos (MyLottoMaker myLottoMaker){
int count = myLottoMaker.getLottoCount();
List<List<Integer>> lottoStorage = myLottoMaker.getMyLottos();
System.out.println("\n" + count + "개를 입력했습니다.");
for (int i =0; i < lottoStorage.size(); i++){
System.out.println(lottoStorage.get(i));
}
}

public void printLottoResult(LottoMatchCounter lottoMatchCounter, ProfitCalculator profitCalculator){

Map<Integer, Integer> prizeRanking = lottoMatchCounter.getPrizeRanking();
double profitRate = profitCalculator.getProfitRate();

System.out.println("\n당첨 통계");
System.out.println("---------");
System.out.println("3개 일치 (5,000원)- " + prizeRanking.get(FIFTH_PLACE) + "개");
System.out.println("4개 일치 (50,000원)- " + prizeRanking.get(FOURTH_PLACE) + "개");
System.out.println("5개 일치 (1,500,000원)- "+ prizeRanking.get(THIRD_PLACE) + "개");
System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원)-"+ prizeRanking.get(SECOND_PLACE) + "개");
System.out.println("6개 일치 (2,000,000,000원)- "+ prizeRanking.get(FIRST_PLACE) + "개");
System.out.println("총 수익률은 " + String.format("%.1f", profitRate) + "%입니다.");
}

}


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

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

public class LottoComparison {

int matchCount;
LottoMatchCounter lottoMatchCounter;
WinLottoMaker winLottoMaker;
InputHandler inputHandler;
MyLottoMaker myLottoMaker;

public LottoComparison(LottoMatchCounter lottoMatchCounter, MyLottoMaker myLottoMaker, WinLottoMaker winLottoMaker, InputHandler inputHandler) {
this.lottoMatchCounter = lottoMatchCounter;
this.myLottoMaker = myLottoMaker;
this.winLottoMaker = winLottoMaker;
this.inputHandler = inputHandler;
compareNums(myLottoMaker, winLottoMaker, inputHandler);
}

public void compareNums(MyLottoMaker myLottoMaker, WinLottoMaker winLottoMaker, InputHandler inputHandler) {
List<List<Integer>> myLottos = myLottoMaker.getMyLottos();
List<Integer> winLotto = winLottoMaker.getWinLotto();
int bonusNumber = inputHandler.getBonusNum();
for (List<Integer> myLotto : myLottos) {
int matchCount = countMatchingNumbers(myLotto, winLotto);
boolean bonusMatch = myLotto.contains(bonusNumber);

lottoMatchCounter.countMatch(matchCount, bonusMatch);
}
}

private int countMatchingNumbers(List<Integer> myLotto, List<Integer> winLotto) {
matchCount = 0;
for (int num : myLotto) {
if (winLotto.contains(num)) {
matchCount++;
}
}
return matchCount;
}

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

import java.util.Map;

public class LottoMatchCounter {

int sixMatch = 0;
int fiveAndBonusMatch = 0;
int fiveMatch = 0;
int fourMatch = 0;
int threeMatch = 0;
final int FIRST_PLACE = 6;
final int SECOND_PLACE = 5;
final int THIRD_PLACE = 4;
final int FOURTH_PLACE = 3;
final int FIFTH_PLACE = 2;

public void countMatch(int matchCount, boolean bonusMatch) {
switch (matchCount) {
case 6:
sixMatch++;
break;
case 5:
if (bonusMatch) {
fiveAndBonusMatch++;
} else {
fiveMatch++;
}
break;
case 4:
fourMatch++;
break;
case 3:
threeMatch++;
break;
}
}

public Map<Integer, Integer> getPrizeRanking() {
Map<Integer, Integer> prizeRanking = Map.of(
FIRST_PLACE, sixMatch,
SECOND_PLACE, fiveAndBonusMatch,
THIRD_PLACE, fiveMatch,
FOURTH_PLACE, fourMatch,
FIFTH_PLACE, threeMatch
);

return prizeRanking;
}

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

import camp.nextstep.edu.missionutils.Randoms;
import java.util.ArrayList;
import java.util.List;
import lotto.error.ErrorCheckingAndParsing;
import lotto.io.InputHandler;

public class MyLottoMaker {

int LOTTO_COUNT = 0;
final int MINIMUM_NUMBER = 1;
final int MAXIMUM_NUMBER = 45;
final int LOTTO_NUM_COUNT = 6;
List<List<Integer>> lottoStorage = new ArrayList<>();
InputHandler inputHandler;

public MyLottoMaker(InputHandler inputHandler) {
this.inputHandler = inputHandler;
inputHandler.getPrice();
makeMyLottos(inputHandler.getInputPrice());
}

public void makeMyLottos(int price) {

LOTTO_COUNT = ErrorCheckingAndParsing.isDividedThousand(price);

for (int i = 0; i < LOTTO_COUNT; i++) {
List<Integer> myLotto = Randoms.pickUniqueNumbersInRange(MINIMUM_NUMBER, MAXIMUM_NUMBER, LOTTO_NUM_COUNT);
lottoStorage.add(myLotto);
}
}

public List<List<Integer>> getMyLottos() {
return lottoStorage;
}

public int getLottoCount(){
return LOTTO_COUNT;
}

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

import java.util.Map;
import lotto.io.InputHandler;

public class ProfitCalculator {

double profitRate = 0;
LottoMatchCounter lottoMatchCounter;
InputHandler inputHandler;

public ProfitCalculator(LottoMatchCounter lottoMatchCounter, InputHandler inputHandler) {
this.lottoMatchCounter = lottoMatchCounter;
this.inputHandler = inputHandler;
calcProfit(lottoMatchCounter, inputHandler);
}

public void calcProfit(LottoMatchCounter lottoMatchCounter, InputHandler inputHandler) {
Map<Integer, Integer> prizeRanking = lottoMatchCounter.getPrizeRanking();
int inputPrice = inputHandler.getInputPrice();
final int FIRST_PLACE = 6;
final int SECOND_PLACE = 5;
final int THIRD_PLACE = 4;
final int FOURTH_PLACE = 3;
final int FIFTH_PLACE = 2;

int totalPrize = prizeRanking.get(FIRST_PLACE) * 2000000000 + prizeRanking.get(SECOND_PLACE) * 30000000 + prizeRanking.get(THIRD_PLACE) * 1500000 + prizeRanking.get(FOURTH_PLACE) * 50000 + prizeRanking.get(FIFTH_PLACE) * 5000;

profitRate = (double) totalPrize/ inputPrice * 100;
}

public double getProfitRate() {
return profitRate;
}

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

import java.util.List;
import lotto.error.ErrorCheckingAndParsing;
import lotto.io.InputHandler;

public class WinLottoMaker {

String[] winNumbersAsStrings;
List<Integer> winNumbers;
InputHandler inputHandler;

public WinLottoMaker(InputHandler inputHandler) {
this.inputHandler = inputHandler;
makeWinLotto(inputHandler.getWinNums());
}

public void makeWinLotto(String winNumbersAsString) {
winNumbersAsStrings = winNumbersAsString.split(",");

winNumbers = ErrorCheckingAndParsing.isNumsAsString(winNumbersAsStrings);
ErrorCheckingAndParsing.isOnlySixNums(winNumbers);

for (int i = 0; i < winNumbers.size(); i++) {
ErrorCheckingAndParsing.confirmNumRange(winNumbers.get(i));
}
}

public List<Integer> getWinLotto() {
return winNumbers;
}

}
Loading