-
Notifications
You must be signed in to change notification settings - Fork 39
[스프링 월요일 B팀] (강지윤) 미션 제출합니다. #15
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: main
Are you sure you want to change the base?
Changes from all commits
1587436
469ce7a
537e677
8b619d0
0930d8a
92f9d18
d50df9b
6575e3a
04e352c
de5eab9
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 |
|---|---|---|
| @@ -1,7 +1,28 @@ | ||
| package lotto; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
|
|
||
| int count = Output.userBuyOut(); | ||
|
|
||
| ArrayList<List<Integer>> numberU = new ArrayList<>(); | ||
| numberU = lotto.Lotto.LottoNumber(count); | ||
|
|
||
| Output.usernumber(numberU, count); | ||
|
|
||
| List<Integer> luckyNumber = new ArrayList<>(); | ||
| luckyNumber = Output.luckyNumberOut(); | ||
|
|
||
| int plusNumber = Output.plusNumberOut(luckyNumber); | ||
|
|
||
| int[] win = lotto.Lotto.LottoWinning(numberU, luckyNumber, plusNumber, count); | ||
|
Comment on lines
+16
to
+21
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. 해당 당첨복권을 객체로 관리하는 방법은 어떨까요? |
||
| Output.luckyResult(win, lotto.Lotto.LottoReturnRate(win, count)); | ||
|
|
||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package lotto; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
|
|
||
|
|
||
| public class Input { | ||
|
|
||
| public static int userBuyIn() { | ||
| Scanner sc1 = new Scanner(System.in); | ||
|
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. 해당 클래스는 static 메소드만을 사용하는데 |
||
| int money = sc1.nextInt(); | ||
|
|
||
| if(money % 1000 != 0) { | ||
| UserHelp.errorUserBuy(); | ||
| } | ||
|
|
||
| return money / 1000; | ||
|
Comment on lines
+16
to
+20
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. 로또 단위인 1000원을 하드코딩(할당하지 않고 직접 기재)하지 말고, 상수로 사용하는 것이 좋아보입니다 만약 단위가격이 2천원으로 변경된다면 1000원인 부분을 모두 찾아 변경해야할 뿐더러, 오타의 위험성이 있습니다. |
||
| } | ||
|
|
||
| public static List<Integer> luckyNumberIn() { | ||
|
|
||
| Scanner sc = new Scanner(System.in); | ||
| String str = sc.next(); | ||
|
|
||
| String[] strArr = str.split(","); | ||
| List<String> intList = new ArrayList<>(Arrays.asList(strArr)); | ||
|
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> luckyNumber = new ArrayList<>(); | ||
| for (String s : intList) { | ||
| luckyNumber.add(Integer.parseInt(s)); | ||
| } | ||
|
|
||
| if(luckyNumber.size() != 6) { | ||
| UserHelp.errorLuckyNumber_size(); | ||
| } | ||
| for (int i = 0; i < 6; ++i) { | ||
| if(luckyNumber.get(i) > 45 || luckyNumber.get(i) < 0) { | ||
| UserHelp.errorLuckyNumber_number(); | ||
| } | ||
| } | ||
|
|
||
| return luckyNumber; | ||
|
|
||
| } | ||
|
|
||
| public static int plusNumberIn(List<Integer> luckyNumber) { | ||
| Scanner sc = new Scanner(System.in); | ||
| int plusNumber = sc.nextInt(); | ||
| boolean plus = luckyNumber.contains(plusNumber); | ||
| if(plus) { | ||
| UserHelp.errorPlusNumber(); | ||
| } | ||
|
|
||
| return plusNumber; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,17 @@ | ||
| package lotto; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
|
|
||
|
|
||
| public class Lotto { | ||
| private final List<Integer> numbers; | ||
|
|
||
| public Lotto(List<Integer> numbers) { | ||
| validate(numbers); | ||
| validate(numbers); //String 검증용 | ||
|
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 검증용이라고 작성하셨는데, 실제 파라미터는 숫자로 이루어진 리스트입니다. |
||
| this.numbers = numbers; | ||
| } | ||
|
|
||
|
|
@@ -16,5 +21,51 @@ private void validate(List<Integer> numbers) { | |
| } | ||
| } | ||
|
|
||
| // TODO: 추가 기능 구현 | ||
| public static ArrayList<List<Integer>> LottoNumber(int count) { | ||
|
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 i; | ||
| ArrayList<List<Integer>> numberU = new ArrayList<>(); | ||
|
|
||
| for (i = 0;i < count; ++i) { | ||
| List<Integer> numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); | ||
| numbers.sort(Comparator.naturalOrder()); | ||
|
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. sort를 하신 이유가 있으실까요? |
||
| numberU.add(i, numbers); | ||
| } | ||
|
|
||
| return numberU; | ||
| } | ||
|
|
||
| public static int[] LottoWinning(ArrayList<List<Integer>> numberU, List<Integer> luckyNumber, int plusNumber, int count) { | ||
| int[] win = {0, 0, 0, 0, 0}; | ||
|
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 i; | ||
|
|
||
| for(i = 0; i < count; ++i) { | ||
| int same = 0, p = 0; | ||
| boolean plus = numberU.get(i).contains(plusNumber); | ||
| if(plus) p = 1; | ||
|
|
||
| (numberU.get(i)).retainAll(luckyNumber); | ||
| same = (numberU.get(i)).size(); | ||
|
|
||
| if(same == 3) win[0] += 1; | ||
| if(same == 4) win[1] += 1; | ||
| if(same == 5) win[2] += 1; | ||
| if(same == 5 && p == 1) win[3] += 1; | ||
| if(same == 6) win[4] += 1; | ||
| } | ||
|
|
||
| return win; | ||
| } | ||
|
|
||
| public static double LottoReturnRate (int[] win, int count) { | ||
| int sum; | ||
| double avg; | ||
|
|
||
| sum = win[0] * 5000 + win[1] * 50000 + win[2] * 1500000 + win[3] * 30000000 + win[4] * 2000000000; | ||
| avg = sum / ((double)(count * 1000)) * 100; | ||
|
|
||
| avg = Math.round(avg * 10); | ||
|
|
||
| return avg / 10; | ||
|
Comment on lines
+65
to
+69
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. 평균값 계산을 나눠서 하지 않고, 한 줄로 끝내거나 메소드를 분리해서 사용하는 것이 더 좋아보입니다 |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package lotto; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Output { | ||
|
|
||
| public static int userBuyOut() { | ||
| System.out.println("구입금액을 입력해 주세요."); | ||
|
|
||
| int count = Input.userBuyIn(); | ||
| if(count != -1) { | ||
| System.out.println(" "); | ||
| System.out.println(count +"개를 구매했습니다."); | ||
| } | ||
|
|
||
| return count; | ||
| } | ||
|
|
||
| public static void usernumber(ArrayList<List<Integer>> numbers, int count) { | ||
| int i; | ||
| for(i = 0; i < count; ++i) { | ||
| System.out.println(numbers.get(i)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public static List<Integer> luckyNumberOut() { | ||
| System.out.println(" "); | ||
| System.out.println("당첨 번호를 입력해 주세요."); | ||
|
|
||
| List<Integer> luckyNumber = new ArrayList<>(); | ||
| luckyNumber = Input.luckyNumberIn(); | ||
|
|
||
| return luckyNumber; | ||
| } | ||
|
|
||
| public static int plusNumberOut(List<Integer> luckyNumber) { | ||
| System.out.println(" "); | ||
| System.out.println("보너스 번호를 입력해 주세요."); | ||
|
|
||
| return Input.plusNumberIn(luckyNumber); | ||
| } | ||
|
|
||
| public static void luckyResult(int[] win, double avg) { | ||
| System.out.println(" "); | ||
| System.out.println("당첨 통계"); | ||
| System.out.println("---"); | ||
| System.out.println("3개 일치 (5,000원) - "+ win[0] +"개"); | ||
| System.out.println("4개 일치 (50,000원) - "+ win[1] +"개"); | ||
| System.out.println("5개 일치 (1,500,000원) - "+ win[2] +"개"); | ||
| System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - "+ win[3] +"개"); | ||
| System.out.println("6개 일치 (2,000,000,000원) - "+ win[4] +"개"); | ||
| System.out.println("총 수익률은 " + avg + "%입니다."); | ||
|
Comment on lines
+49
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. 만약 당첨금액이 바뀐다면 해당부분뿐만 아니라 위에서 당첨금액 계산하는 부분까지도 전부 찾아 변경해야합니다 |
||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package lotto; | ||
|
|
||
| public class UserHelp { | ||
|
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. 해당 클래스는 잘못된 입력에 대한 예외를 처리하는 것으로 보이는데, 또한, 외부에서 특정 상황일 때 해당 메소드를 부르는 것 같은데, 예외는 해당 부분에서 던지는 것이여야지 다른 메소드가 throw만을 던지는 것은 좋지 않아보입니다. |
||
|
|
||
| public static void errorUserBuy() { | ||
| System.out.println("[ERROR] 지불한 금액이 많거나 적습니다."); | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| public static void errorLuckyNumber_size() { | ||
| System.out.println("[ERROR] 로또 번호의 개수는 6개입니다."); | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| public static void errorLuckyNumber_number() { | ||
| System.out.println("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."); | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| public static void errorPlusNumber() { | ||
| System.out.println("[ERROR] 보너스 번호는 로또 당첨 번호에 없는 숫자여야 합니다."); | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| } | ||
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.
ArrayList<List<Integer>>보단List<List<Integer>>가 더 좋아보입니다다형성을 위해 서브타입으로 지정하기보다 부모타입으로 지정하는 것이 좋습니다.
예를 들어, 객체를 ArrayList가 아닌 LinkedList로 변경해야한다면 변수 타입과 생성 객체를 모두 변경해야하는 상황이 발생하는 경우가 있습니다.
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.
추가로,
numberU라는 변수명이 무엇을 의미하는지 알기 힘듭니다.List 객체의 경우 s를 붙여 복수형으로 끝내는 것이 일반적입니다.