-
Notifications
You must be signed in to change notification settings - Fork 248
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
[디디] 로또 수동 입력 #168
[디디] 로또 수동 입력 #168
Changes from 3 commits
d0aae32
6f1132e
c4607af
65f73c8
080664c
d73a3c7
f6085be
43f275a
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,37 @@ | ||
package lotto.domain; | ||
|
||
import lotto.exception.ExceedMoneyException; | ||
import lotto.util.StringUtil; | ||
|
||
public class LottoCount { | ||
private final int manualLotto; | ||
private final int autoLotto; | ||
|
||
public LottoCount(String manualLotto, int totalLotto) { | ||
validate(manualLotto); | ||
validateMoney(manualLotto, totalLotto); | ||
this.manualLotto = Integer.parseInt(manualLotto); | ||
this.autoLotto = totalLotto - this.manualLotto; | ||
} | ||
|
||
private void validate(String input) { | ||
StringUtil.checkNull(input); | ||
StringUtil.checkBlank(input); | ||
StringUtil.checkNumberFormat(input); | ||
StringUtil.checkRange(input); | ||
} | ||
|
||
private void validateMoney(String input, int totalLotto) { | ||
if (totalLotto < Integer.parseInt(input)) { | ||
throw new ExceedMoneyException(totalLotto + "장 이하만 구매가 가능합니다."); | ||
} | ||
} | ||
|
||
public int getManualLotto() { | ||
return manualLotto; | ||
} | ||
|
||
public int getAutoLottoCount() { | ||
return autoLotto; | ||
} | ||
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. 해당 메서드는 자동/수동의 개수를 반환하는 getter인데 서로 네이밍 형식이 다르네요! |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,4 +70,15 @@ public static Lottos create(int count) { | |
} | ||
return new Lottos(lottos); | ||
} | ||
|
||
public static Lottos create(List<String> list, int autoCount){ | ||
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<Lotto> lottos = new ArrayList<>(); | ||
for (String input : list) { | ||
lottos.add(create(input)); | ||
} | ||
for(int i =0; i< autoCount;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. 띄어쓰기 컨벤션 위반입니다. |
||
lottos.add(create()); | ||
} | ||
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 new Lottos(lottos); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package lotto.exception; | ||
|
||
public class EmptyInputException extends RuntimeException { | ||
public EmptyInputException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package lotto.exception; | ||
|
||
public class ExceedMoneyException extends RuntimeException { | ||
public ExceedMoneyException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package lotto.exception; | ||
|
||
public class InvalidRangeException extends RuntimeException { | ||
public InvalidRangeException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
package lotto.util; | ||
|
||
import lotto.exception.EmptyInputException; | ||
import lotto.exception.InvalidRangeException; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class StringUtil { | ||
public static final String BLANK = " "; | ||
|
@@ -15,4 +19,29 @@ public static String removeBlank(String value) { | |
public static List<String> parseByComma(String value) { | ||
return Arrays.asList(value.split(DELIMITER)); | ||
} | ||
|
||
public static void checkBlank(String input) { | ||
if(input.trim().isEmpty()){ | ||
throw new EmptyInputException("공백은 사용할 수 없습니다."); | ||
} | ||
} | ||
|
||
public static void checkNull(Object o) { | ||
if(Objects.isNull(o)){ | ||
throw new NullPointerException("널문자는 사용할 수 없습니다."); | ||
} | ||
} | ||
|
||
public static void checkNumberFormat(String input) { | ||
try{ | ||
Integer.parseInt(input); | ||
} catch(NumberFormatException e){ | ||
throw new NumberFormatException("문자열은 사용할 수 없습니다."); | ||
} | ||
} | ||
|
||
public static void checkRange(String input) { | ||
if(Integer.parseInt(input) < 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. 여기만 괄호가 없는 이유가 무엇일까요? 일관된 포맷을 가져가면 좋을 것 같습니다~ |
||
throw new InvalidRangeException("음수는 입력할 수 없습니다."); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package lotto.view; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
|
@@ -19,4 +21,18 @@ public static String getBonusNumber() { | |
System.out.println("보너스 번호를 입력해 주세요."); | ||
return scanner.nextLine(); | ||
} | ||
|
||
public static String getCount() { | ||
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("수동으로 구매할 로또 수를 입력해 주세요."); | ||
return scanner.nextLine(); | ||
} | ||
|
||
public static List<String> getManualLottosNumber(int manualLottoCount) { | ||
List<String> input = new ArrayList<>(); | ||
System.out.println("수동으로 구매할 번호를 입력해 주세요."); | ||
for(int i=0;i<manualLottoCount;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. 컨벤션 위반입니다. |
||
input.add(scanner.nextLine()); | ||
} | ||
return input; | ||
} | ||
} |
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.
input
을 더 의미있는 이름으로 바꿔보면 좋을 듯 합니다.