Skip to content
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
23 changes: 22 additions & 1 deletion src/main/java/lotto/Application.java
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<>();

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로 변경해야한다면 변수 타입과 생성 객체를 모두 변경해야하는 상황이 발생하는 경우가 있습니다.

Choose a reason for hiding this comment

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

추가로, numberU라는 변수명이 무엇을 의미하는지 알기 힘듭니다.
List 객체의 경우 s를 붙여 복수형으로 끝내는 것이 일반적입니다.

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

Choose a reason for hiding this comment

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

해당 당첨복권을 객체로 관리하는 방법은 어떨까요?

Output.luckyResult(win, lotto.Lotto.LottoReturnRate(win, count));


}
}


61 changes: 61 additions & 0 deletions src/main/java/lotto/Input.java
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);

Choose a reason for hiding this comment

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

해당 클래스는 static 메소드만을 사용하는데 Scanner또한 static 멤버로 사용하는게 좋지 않을까요?

int money = sc1.nextInt();

if(money % 1000 != 0) {
UserHelp.errorUserBuy();
}

return money / 1000;
Comment on lines +16 to +20

Choose a reason for hiding this comment

The 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));

Choose a reason for hiding this comment

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

List와 같이 자료형을 나타내는 것은 변수 네이밍으로 좋지 않은 방법입니다. 즉, 변수명이 자료형에 종속적이면 안됩니다.
만약 추후에 Set과 같은 자료형으로 변경되었을 때 해당 변수도 모두 변경해야하는 문제점이 생기기때문입니다.


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;
}


}
55 changes: 53 additions & 2 deletions src/main/java/lotto/Lotto.java
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 검증용

Choose a reason for hiding this comment

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

string 검증용이라고 작성하셨는데, 실제 파라미터는 숫자로 이루어진 리스트입니다.
주석도 명백한 코드의 일부분이니 고려 후 작성해주시면 좋을 것 같습니다

this.numbers = numbers;
}

Expand All @@ -16,5 +21,51 @@ private void validate(List<Integer> numbers) {
}
}

// TODO: 추가 기능 구현
public static ArrayList<List<Integer>> LottoNumber(int count) {

Choose a reason for hiding this comment

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

번호를 생성하신다면 Lotto가 아닌 LottoGenerator 혹은 LottoFactory처럼 Lotto에 관한 객체 생성에 도움을 주는 클래스를 따로 작성하시는 것이 좋을 것 같습니다


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());

Choose a reason for hiding this comment

The 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};

Choose a reason for hiding this comment

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

List가 아닌 배열을 사용하신 이유가 있으실까요?

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

Choose a reason for hiding this comment

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

평균값 계산을 나눠서 하지 않고, 한 줄로 끝내거나 메소드를 분리해서 사용하는 것이 더 좋아보입니다

}
}
58 changes: 58 additions & 0 deletions src/main/java/lotto/Output.java
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

Choose a reason for hiding this comment

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

만약 당첨금액이 바뀐다면 해당부분뿐만 아니라 위에서 당첨금액 계산하는 부분까지도 전부 찾아 변경해야합니다
당첨 금액, 당첨 갯수, 보너스볼 일치 여부, 등수를 ENUM으로 관리하는 것은 어떨까요?

}


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

public class UserHelp {

Choose a reason for hiding this comment

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

해당 클래스는 잘못된 입력에 대한 예외를 처리하는 것으로 보이는데, UserHelp라는 네이밍과 어울리지 않습니다

또한, 외부에서 특정 상황일 때 해당 메소드를 부르는 것 같은데, 예외는 해당 부분에서 던지는 것이여야지 다른 메소드가 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();
}

}