-
Notifications
You must be signed in to change notification settings - Fork 48
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
로또 미션 제출합니다 :) #62
base: jermany17
Are you sure you want to change the base?
로또 미션 제출합니다 :) #62
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,49 @@ | ||
package controller; | ||
|
||
import java.util.*; | ||
import view.Input; | ||
import view.Output; | ||
|
||
public class App { | ||
public static void main(String[] args) { | ||
|
||
// 모든 로또들을 넣을 리스트 | ||
List<List<String>> LottoList = new ArrayList<>(); | ||
|
||
// 구입 금액 입력한다 | ||
int total = Input.moneyInput(); | ||
|
||
// 수동으로 구매할 로또 수를 입력한다 | ||
int manual = Input.manualNumInput(); | ||
|
||
// 자동으로 구매할 로또 수를 계산한다. | ||
int auto = total - manual; | ||
|
||
// 수동 로또 번호를 입력한다 | ||
for(int i = 0; i < manual; i++) { | ||
List<String> manualLotto = Input.manualLottoInput(i); | ||
LottoList.add(manualLotto); | ||
} | ||
|
||
// 수동과 자동 구매 개수를 출력한다 | ||
Output.ManualAndAutoNumOutput(manual, auto); | ||
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 (int i = 0; i < auto; i++) { | ||
List<String> autoLotto = Input.AutoLottoInput(); | ||
Collections.sort(autoLotto); | ||
LottoList.add(autoLotto); | ||
Output.LottoNumOutput(autoLotto); | ||
} | ||
Comment on lines
+32
to
+37
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. 요 정렬까지 컨트롤러가 하게 되면 약간 컨트롤러의 책임이 많아지는 것 같은데 어떻게 생각하시나요? |
||
|
||
// 지난 주 당첨 번호를 입력한다 | ||
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<String> LastWeekLotto = Input.LastWeekLottoNum(); | ||
|
||
// 보너스 볼을 입력한다 | ||
String BonusBall = Input.BonusBall(); | ||
|
||
// 당첨 통계를 출력한다 | ||
Output.ResultOfLotto(LottoList, LastWeekLotto, BonusBall, total*1000); | ||
Input.closeScanner(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package domain; | ||
|
||
import java.util.List; | ||
|
||
public class Lotto { | ||
|
||
private List<String> lotto; | ||
|
||
public Lotto(List<String> lottoNum){ | ||
this.lotto = lottoNum; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package domain; | ||
|
||
import java.util.List; | ||
|
||
public class LottoGame { | ||
|
||
public static int matchNum(List<String> Lotto, List<String> LastWeekLotto){ | ||
int matchNum = 0; | ||
for (String n : Lotto) { | ||
if (LastWeekLotto.contains(n)) { | ||
matchNum++; | ||
} | ||
} | ||
return matchNum; | ||
Comment on lines
+8
to
+14
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. indent 1규칙을 지키려고 시도해보셔도 좋을 것 같아요! |
||
} | ||
|
||
public static int matchBonus(List<String> Lotto, String BonusBall){ | ||
for (String n : Lotto) { | ||
if (n.equals(BonusBall)) { | ||
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. 요기에 *.equals 는 항상 null 이 들어올 수 있는지 정말 많이 주의해줘야 합니다 |
||
return 1; | ||
} | ||
} | ||
return 0; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package view; | ||
|
||
import java.util.*; | ||
|
||
public class Input { | ||
|
||
private static Scanner scanner = new Scanner(System.in); | ||
|
||
public static int moneyInput(){ | ||
System.out.println("구입금액을 입력해 주세요."); | ||
int money = scanner.nextInt(); | ||
System.out.println(); | ||
return money/1000; | ||
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. 요 부분은 비즈니스 로직이 아닐까요? money /1000 부분이요! |
||
} | ||
|
||
public static int manualNumInput(){ | ||
System.out.println("수동으로 구매할 로또 수를 입력해 주세요."); | ||
int manual = scanner.nextInt(); | ||
System.out.println(); | ||
scanner.nextLine(); | ||
return manual; | ||
} | ||
|
||
public static List<String> manualLottoInput(int n) { | ||
if (n == 0) | ||
System.out.println("수동으로 구매할 번호를 입력해 주세요."); | ||
List<String> manualLotto = Arrays.asList(scanner.nextLine().split(",")); | ||
for(int i = 0; i < manualLotto.size(); i++){ | ||
manualLotto.set(i, manualLotto.get(i).trim()); | ||
} | ||
return manualLotto; | ||
} | ||
|
||
public static List<String> AutoLottoInput() { | ||
List<String> lottoRange = new ArrayList<>(); | ||
for (int i = 1; i <= 45; i++) { | ||
lottoRange.add(String.valueOf(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. 로또 숫자가 String 이 된 이유가 있을까요? 로또 숫자는 숫자다보니 정수 타입이 적절할 것 같아서요 |
||
} | ||
Collections.shuffle(lottoRange); | ||
return lottoRange.subList(0, 6); | ||
} | ||
|
||
public static List<String> LastWeekLottoNum(){ | ||
System.out.println("\n지난 주 당첨 번호를 입력해 주세요."); | ||
List<String> LastWeekLotto = Arrays.asList(scanner.nextLine().split(",")); | ||
for(int i = 0; i < LastWeekLotto.size(); i++){ | ||
LastWeekLotto.set(i, LastWeekLotto.get(i).trim()); | ||
} | ||
return LastWeekLotto; | ||
} | ||
|
||
public static String BonusBall(){ | ||
System.out.println("\n보너스 볼을 입력해 주세요."); | ||
return scanner.nextLine(); | ||
} | ||
|
||
public static void closeScanner() { | ||
if (scanner != null) { | ||
scanner.close(); | ||
Comment on lines
+57
to
+59
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. 여기서 쓰라는 것은 아니지만, Autocloseable 도 한번 보시면 좋을 것 같아요! |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package view; | ||
|
||
import java.util.*; | ||
import domain.LottoGame; | ||
|
||
public class Output { | ||
|
||
public static void ManualAndAutoNumOutput(int manual, int auto){ | ||
System.out.println(); | ||
System.out.println("수동으로 " + manual + "장, 자동으로 " + auto + "개를 구매했습니다."); | ||
} | ||
|
||
public static void LottoNumOutput(List<String> Lotto){ | ||
System.out.print("["); | ||
for(int i = 0; i < Lotto.size(); i++){ | ||
System.out.print(Lotto.get(i)); | ||
if(i != Lotto.size() - 1){ | ||
System.out.print(", "); | ||
} | ||
} | ||
System.out.println("]"); | ||
} | ||
|
||
public static void ResultOfLotto(List<List<String>> LottoList, List<String> LastWeekLotto, String BonusBall, int moneyInput){ | ||
int result3 = 0, result4 = 0, result5 = 0, result6 = 0, result5Bonus = 0; | ||
int matchNum = 0, matchBonus = 0; | ||
int resultMoney = 0; | ||
double rate = 0; | ||
|
||
for (List<String> Lotto : LottoList) { | ||
matchNum = LottoGame.matchNum(Lotto, LastWeekLotto); | ||
matchBonus = LottoGame.matchBonus(Lotto, BonusBall); | ||
if (matchNum == 3) { | ||
result3++; | ||
resultMoney += 5000; | ||
} | ||
if (matchNum == 4) { | ||
result4++; | ||
resultMoney += 50000; | ||
} | ||
if (matchNum == 5 && matchBonus == 0) { | ||
result5++; | ||
resultMoney += 1500000; | ||
} | ||
if (matchNum == 5 && matchBonus == 1) { | ||
result5Bonus++; | ||
resultMoney += 30000000; | ||
} | ||
if (matchNum == 6) { | ||
result6++; | ||
resultMoney += 2000000000; | ||
} | ||
} | ||
Comment on lines
+24
to
+53
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. 요 부분은 비즈니스가 아닐까요? |
||
|
||
System.out.println("\n당첨 통계"); | ||
System.out.println("---------"); | ||
System.out.println("3개 일치 (5000원)- " + result3 + "개"); | ||
System.out.println("4개 일치 (50000원)- " + result4 + "개"); | ||
System.out.println("5개 일치 (1500000원)- " + result5 + "개"); | ||
System.out.println("5개 일치, 보너스 볼 일치(30000000)원 - " + result5Bonus + "개"); | ||
System.out.println("6개 일치 (2000000000원)-" + result6 + "개"); | ||
rate = (double) resultMoney / moneyInput; | ||
System.out.println("총 수익률은 " + String.format("%.2f", rate) + "입니다."); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import domain.LottoGame; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class LottoTest { | ||
|
||
@Test | ||
@DisplayName("로또번호_6개_일치여부를_확인한다") | ||
public void 로또번호_6개_일치여부를_확인한다() { | ||
List<String> lotto = Arrays.asList("1", "2", "3", "4", "5", "6"); | ||
List<String> lastWeekLotto = Arrays.asList("6", "5", "4", "3", "2", "1"); | ||
int result = LottoGame.matchNum(lotto, lastWeekLotto); | ||
assertEquals(6, result); | ||
} | ||
|
||
@Test | ||
@DisplayName("로또번호_3개_일치여부를_확인한다") | ||
public void 로또번호_3개_일치여부를_확인한다() { | ||
List<String> lotto = Arrays.asList("1", "2", "3", "4", "5", "6"); | ||
List<String> lastWeekLotto = Arrays.asList("10", "9", "8", "3", "2", "1"); | ||
int result = LottoGame.matchNum(lotto, lastWeekLotto); | ||
assertEquals(3, result); | ||
} | ||
|
||
@Test | ||
@DisplayName("로또번호_0개_일치여부를_확인한다") | ||
public void 로또번호_0개_일치여부를_확인한다() { | ||
List<String> lotto = Arrays.asList("1", "2", "3", "4", "5", "6"); | ||
List<String> lastWeekLotto = Arrays.asList("7", "8", "9", "10", "11", "12"); | ||
int result = LottoGame.matchNum(lotto, lastWeekLotto); | ||
assertEquals(0, result); | ||
} | ||
|
||
@Test | ||
@DisplayName("보너스번호_일치여부를_확인한다") | ||
public void 보너스번호_일치여부를_확인한다() { | ||
List<String> lotto = Arrays.asList("1", "2", "3", "4", "5", "7"); | ||
String bonusBall = "7"; | ||
int result = LottoGame.matchBonus(lotto, bonusBall); | ||
assertEquals(1, result); // 보너스볼이 일치함 | ||
} | ||
|
||
@Test | ||
@DisplayName("보너스번호_불일치여부를_확인한다") | ||
public void 보너스번호_불일치여부를_확인한다() { | ||
List<String> lotto = Arrays.asList("1", "2", "3", "4", "5", "7"); | ||
String bonusBall = "8"; | ||
int result = LottoGame.matchBonus(lotto, bonusBall); | ||
assertEquals(0, result); // 보너스볼이 일치하지 않음 | ||
} | ||
} | ||
Comment on lines
+10
to
+54
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. 테스트 너무 잘 해주셨어요 |
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.
요 메인 함수가 전체적으로 긴 느낌이 드는데요 이 부분을 적절한 기준에 따라서 함수를 쪼개보면 어떨까요?