-
Notifications
You must be signed in to change notification settings - Fork 92
[로또 미션] 김진주 미션 제출합니다. #12
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
base: nova0128
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package controller; | ||
|
||
import random.ConvertToString; | ||
import random.ReturnAutoLotto; | ||
import util.CheckWinningResult; | ||
import util.ConvertToIntegerList; | ||
import util.Input; | ||
import util.TotalLotto; | ||
import view.OutputView; | ||
|
||
import java.math.BigInteger; | ||
import java.text.DecimalFormat; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Controller { | ||
public static void main(String[] args) { | ||
OutputView.inputMessage(); | ||
int inputMoney = Input.inputMoney(); | ||
|
||
OutputView.inputManualMessage(); | ||
int manualCount = Input.inputManualCount(); | ||
|
||
OutputView.printInputinfo(); | ||
|
||
List<List<Integer>> resultArray = new ArrayList<>(); | ||
resultArray = TotalLotto.createResultArray(); | ||
|
||
|
||
for (int i = 0; i < manualCount; i++) { | ||
String manualNumbers = Input.inputManualNumbers(); | ||
|
||
List<Integer> manulLottoList = new ArrayList<>(); | ||
manulLottoList = ConvertToIntegerList.removeCommasAndSpaces(manualNumbers); | ||
TotalLotto.createAddLotto(manulLottoList, resultArray); | ||
|
||
} | ||
|
||
OutputView.printLottoCountInfo(manualCount, Input.inputAutoCount(inputMoney / 1000, manualCount)); | ||
|
||
|
||
for (int i = 0; i < Input.inputAutoCount(inputMoney / 1000, manualCount); i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 매직넘버를 제거하면 더 좋을 것 같습니다! |
||
List<Integer> autoLottoList = new ArrayList<>(); | ||
autoLottoList = ReturnAutoLotto.randomLotto(); | ||
TotalLotto.createAddLotto(autoLottoList, resultArray); | ||
//OutputView.printAutoNumbers(ConvertToString.convertToString(autoLottoList)); | ||
} | ||
|
||
OutputView.printTotalNumbers(resultArray); | ||
|
||
OutputView.inputAnswerMessage(); | ||
List<Integer> answerNumbers = new ArrayList<>(); | ||
answerNumbers = ConvertToIntegerList.removeCommasAndSpaces(Input.answerNumber()); | ||
|
||
OutputView.inputBonusMessage(); | ||
int bonusNumber = Input.bonusNumber(); | ||
|
||
List<Integer> resultList = CheckWinningResult.resultCount(resultArray, answerNumbers, bonusNumber); | ||
|
||
|
||
OutputView.resultsMessage(); | ||
List<Integer> winnerCount = List.of(0, 6, 5, 5, 4, 3); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. enum 클래스를 사용하시면 당첨금 관련 로직이 훨씬 좋아질 것 같습니다! |
||
List<Integer> winnerPrice = List.of(0, 2000000000, 30000000, 1500000, 50000, 5000); | ||
long sum = 0; | ||
for (int i = 5; i >= 1; i--) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for문을 사용하여 한 번에 출력을 잘 해주신 것 같습니다! |
||
sum += ((long) winnerPrice.get(i) * resultList.get(i)); | ||
|
||
if (i == 2) { | ||
System.out.println(winnerCount.get(i) + "개 일치, 보너스 볼 일치(" + winnerPrice.get(i) + "원)- " + resultList.get(i) + "개"); | ||
continue; | ||
} | ||
System.out.println(winnerCount.get(i) + "개 일치 (" + winnerPrice.get(i) + "원)- " + resultList.get(i) + "개"); | ||
} | ||
|
||
DecimalFormat df = new DecimalFormat("#.##"); | ||
System.out.println("총 수익률은 " + df.format((double) sum / (double) inputMoney) + "입니다."); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CheckWinningResult { | ||
public static List<Integer> initResultList() { | ||
List<Integer> newResultList = new ArrayList<>(); | ||
for(int i=0; i<6; i++){ | ||
newResultList.add(0); | ||
} | ||
return newResultList; | ||
} | ||
|
||
//불러와서 하면 더 좋을듯 | ||
public static List<Integer> resultCount(List<List<Integer>> resultArray, List<Integer> answerNumbers, int bonusNumber) { | ||
List<Integer> resultWinningList = initResultList(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. answerCount에 따른 인덱스를 미리 정의하면 |
||
|
||
for (List<Integer> innerList : resultArray) { | ||
boolean flag = false; | ||
int answerCount; | ||
if (innerList.contains(bonusNumber)) flag = true; | ||
// System.out.println("innerList = " + innerList); | ||
|
||
innerList.retainAll(answerNumbers); | ||
answerCount = innerList.size(); | ||
|
||
if (answerCount == 3) { | ||
int currentValue = resultWinningList.get(5); // 5번째 값 | ||
resultWinningList.set(5, currentValue + 1); // 1 증가된 값으로 설정 | ||
} | ||
|
||
if (answerCount == 4) { | ||
int currentValue = resultWinningList.get(4); | ||
resultWinningList.set(4, currentValue + 1); | ||
} | ||
|
||
if (answerCount == 5 && !flag) { | ||
int currentValue = resultWinningList.get(3); | ||
resultWinningList.set(3, currentValue + 1); | ||
} | ||
|
||
if (answerCount == 5 && flag) { | ||
int currentValue = resultWinningList.get(2); | ||
resultWinningList.set(2, currentValue + 1); | ||
} | ||
|
||
if (answerCount == 6) { | ||
int currentValue = resultWinningList.get(1); | ||
resultWinningList.set(1, currentValue + 1); | ||
} | ||
|
||
} | ||
return resultWinningList; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class TotalLotto { | ||
public static List<List<Integer>> createResultArray () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로또를 Integer, List 객체로 관리하셨는데, LottoNumber, Lotto 클래스를 구현하셔서 사용하셨어도 좋았을 것 같습니다! |
||
List<List<Integer>> resultArray = new ArrayList<>(); | ||
return resultArray; | ||
} | ||
|
||
public static List<List<Integer>> createAddLotto (List<Integer> singleLotto, List<List<Integer>> resultArray) { | ||
resultArray.add(singleLotto); // ? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로또에 대한 예외 처리를 해주면 더 좋을 것 같습니다! |
||
return resultArray; //void? | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package random; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ConvertToString { //출력을 위해..? | ||
|
||
public static String convertToString(List<Integer> integerList) { | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
for (Integer num : integerList) { | ||
sb.append(num).append(", "); | ||
} | ||
|
||
// 마지막 쉼표와 공백 제거 | ||
if (sb.length() > 0) { | ||
sb.setLength(sb.length() - 2); | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package random; | ||
import java.util.Collections; | ||
import java.util.Random; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ReturnAutoLotto { //랜덤로또리스트를 반환 | ||
public static List<Integer> randomLotto(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분은 stream 사용법을 찾아보시고 이용하시면 가독성이 더 좋아질 것 같습니다! |
||
List<Integer> list = new ArrayList<>(); | ||
Random random = new Random(); | ||
|
||
while (list.size() < 6) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로또 개수와 로또번호의 범위를 지정하는 값들은 상수로 대체될 수 있는 값들인 것 같습니다! 스터디에서 배운대로 매직넘버를 제거하는 것도 고려해주시면 좋을 것 같습니다 |
||
int randomNumber = random.nextInt(45) + 1; | ||
|
||
if (list.contains(randomNumber)) { | ||
continue; | ||
} | ||
list.add(randomNumber); | ||
} | ||
Collections.sort(list); | ||
return list; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ConvertToIntegerList { | ||
public static List<Integer> removeCommasAndSpaces(String inputStringLotto) { | ||
// 쉼표와 공백을 기준으로 문자열을 분할하여 문자열 배열을 생성 | ||
String[] tokens = inputStringLotto.split("[,\\s]+"); | ||
|
||
List<Integer> numbers = new ArrayList<>(); | ||
|
||
// 분할된 각 문자열을 정수로 변환하여 리스트에 추가 | ||
for (String token : tokens) { | ||
try { | ||
int number = Integer.parseInt(token); | ||
numbers.add(number); | ||
} catch (NumberFormatException e) { | ||
// 정수로 변환할 수 없는 문자열은 무시 | ||
} | ||
} | ||
|
||
return numbers; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
public class Input { | ||
public static int inputMoney() { //입력금액 | ||
return Integer.parseInt(userInput()); | ||
} | ||
|
||
// public static int lottoTotalCount(int inputMoney){ | ||
// return inputMoney/1000; | ||
// } | ||
public static int inputManualCount() { | ||
return Integer.parseInt(userInput()); | ||
} | ||
|
||
public static int inputAutoCount(int totalCount, int manualCount){ | ||
return totalCount - manualCount; | ||
}// | ||
|
||
public static String inputManualNumbers() { | ||
Scanner sc = new Scanner(System.in); | ||
return sc.nextLine(); | ||
}// | ||
|
||
public static String answerNumber() { | ||
return userInput(); | ||
} | ||
|
||
public static int bonusNumber() { | ||
return Integer.parseInt(userInput()); | ||
} | ||
|
||
private static String userInput() {//입력도 따로 빼야하나? | ||
Scanner scanner = new Scanner(System.in); | ||
return (scanner.nextLine()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package view; | ||
|
||
import java.util.List; | ||
|
||
public class OutputView { | ||
|
||
public static void inputMessage() { | ||
System.out.println("구입금액을 입력해 주세요."); | ||
} | ||
|
||
|
||
public static void inputManualMessage() { | ||
System.out.println(); | ||
System.out.println("수동으로 구매할 로또 수를 입력해 주세요."); | ||
} | ||
|
||
|
||
public static void printInputinfo() { | ||
System.out.println(); | ||
System.out.println("수동으로 구매할 번호를 입력해 주세요."); | ||
} | ||
|
||
public static void printLottoCountInfo(int manualCount, int autoCount) { | ||
System.out.println(); | ||
System.out.println("수동으로" + manualCount + "장, 자동으로" + autoCount + "개를 구매했습니다."); | ||
} | ||
|
||
|
||
public static void printTotalNumbers(List<List<Integer>> resultArrays) { | ||
for (List<Integer> resultArray : resultArrays) { | ||
System.out.println(resultArray); | ||
} | ||
} | ||
|
||
public static void inputAnswerMessage() { | ||
System.out.println(); | ||
System.out.println("지난 주 당첨 번호를 입력해 주세요."); | ||
} | ||
|
||
|
||
public static void inputBonusMessage() { | ||
System.out.println(); | ||
System.out.println("보너스 볼을 입력해 주세요."); | ||
} | ||
|
||
public static void resultsMessage() { | ||
System.out.println(); | ||
System.out.println("당첨 통계\n" + | ||
"---------"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분을 int intputMoney = InputView.inputMoney(); 이런식으로 사용하는 건 어떨까요?
input에 담당하는 메서드를 InputView 클래스에서 따로 구현하는 것도 좋을 것 같습니다!