Skip to content

Commit

Permalink
Merge pull request #1 from tjdxo1193/exception
Browse files Browse the repository at this point in the history
Exception 처리 및 메세지 처리 확인
  • Loading branch information
tjdxo1193 authored Nov 4, 2023
2 parents f86ddca + 52b8e37 commit 6eb2d7b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 20 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repositories {

dependencies {
implementation 'com.github.woowacourse-projects:mission-utils:1.1.0'
implementation 'org.projectlombok:lombok:1.18.22'
}

java {
Expand Down
36 changes: 24 additions & 12 deletions src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,69 @@
import baseball.enums.GameFlag;
import baseball.enums.Message;
import baseball.enums.OrNot;
import baseball.exception.WrongInputException;
import camp.nextstep.edu.missionutils.Randoms;

import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class Application {


public static void main(String[] args) {
public static void main(String[] args) throws WrongInputException {
// Game 시작 Setting
GameFlag flag = GameFlag.START;
PrintMessage.printlnMessage(Message.START);

while (flag == GameFlag.START) {
// 랜덤한 값 서로 다른 수 3자리를 생성한다. TODO 함수로 뺄 예정
Random random = new Random();
random.setSeed(System.currentTimeMillis());

// 컴퓨터 수 생성
Set<Integer> targetSet = new HashSet<>();
while (targetSet.size() < 3) {
int randomNum = random.nextInt(9)+1;
targetSet.add(randomNum);
int randomNumber = Randoms.pickNumberInRange(1, 9);
targetSet.add(randomNumber);
}

GameInputValue gameValue = new GameInputValue(targetSet.stream()
.map(String::valueOf)
.reduce("", String::concat));

flag = GameFlag.ING;

// 맞출 때 까지 반복
while (flag == GameFlag.ING){
while (flag == GameFlag.ING) {
PrintMessage.printMessage(Message.INPUT_NUMBER);
Scanner sc = new Scanner(System.in);
gameValue.setUserInputValue(sc.nextLine());

// 사용자 수와 컴퓨터 수 검증하기
// 입력값 검증
try {
gameValue.checkValidUserInput();
} catch (WrongInputException exception) {
PrintMessage.printlnMessage(exception.getMessage());
continue;
}

// 사용자 수와 컴퓨터 수 비교
if (!gameValue.isEqaulsUserAndComputerValue()) {
// TODO 예외 사항 1. 입력 수가 3자리 이상일때 2. 입력수가 유효하지 않을 때(모두 숫자가 아닐시)
Result result = new Result(gameValue.countSameNumber() - gameValue.countSamePositionAndNumber(), gameValue.countSamePositionAndNumber());
if (gameValue.isNothing()) {
PrintMessage.printlnMessage(Message.NOTHING);
}else{
} else {
PrintMessage.printlnMessage(Message.makeHintMessage(result));
}
continue;
}


// 리플레이 또는 종료
PrintMessage.printlnMessage(Message.PLAY_NEXT_GAME_OR_NOT);
Scanner sc2 = new Scanner(System.in);
String answer = sc2.nextLine();

if (answer.equals(OrNot.YES.getProcessCode())) {
flag = GameFlag.START;
break;
}
if (answer.equals(OrNot.NO.getProcessCode())) {
flag = GameFlag.END;
Expand Down
35 changes: 31 additions & 4 deletions src/main/java/baseball/GameInputValue.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package baseball;

import baseball.enums.ExceptionMessage;
import baseball.exception.WrongInputException;

import java.util.HashSet;

public class GameInputValue {
Expand Down Expand Up @@ -28,10 +31,6 @@ public boolean isEqaulsUserAndComputerValue() {
}

public int countSameNumber() {
if (isDuplicationNumberInUserInputValue()) {
// TODO 그에 맞는 예외처리 생각하기
throw new IllegalArgumentException();
}
int count = 0;
for (char c : this.userInputValue.toCharArray()) {
if (this.computerValue.contains(String.valueOf(c))) {
Expand Down Expand Up @@ -64,4 +63,32 @@ public boolean isDuplicationNumberInUserInputValue() {
public boolean isNothing() {
return countSameNumber() == 0;
}


public boolean checkValidUserInput() {

if (!isThreeDigits()) {
throw new WrongInputException(ExceptionMessage.WRONG_INPUT_NOT_THREE_DIGITS.getMessage());
}

if (!isNumber()){
throw new WrongInputException(ExceptionMessage.WRONG_INPUT_NOT_NUMBER.getMessage());
}

if(isDuplicationNumberInUserInputValue()){
throw new WrongInputException(ExceptionMessage.WRONG_INPUT_DUPLICATE_VALUE.getMessage());
}

return true;
}


private boolean isThreeDigits() {
return userInputValue.length() == 3;
}

private boolean isNumber() {
return userInputValue != null && userInputValue.matches("[-+]?\\d*\\.?\\d+");
}

}
17 changes: 17 additions & 0 deletions src/main/java/baseball/enums/ExceptionMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package baseball.enums;

public enum ExceptionMessage {
WRONG_INPUT_DEFAULT("잘못된 입력입니다."),
WRONG_INPUT_NOT_THREE_DIGITS("3자리만 입력 할 수 있습니다."),
WRONG_INPUT_NOT_NUMBER("숫자가 아닙니다."),
WRONG_INPUT_DUPLICATE_VALUE("숫자가 중복되어 있습니다.");
String message;

ExceptionMessage(String message) {
this.message = message;
}

public String getMessage() {
return message;
}
}
4 changes: 0 additions & 4 deletions src/main/java/baseball/exception/Exception.java

This file was deleted.

8 changes: 8 additions & 0 deletions src/main/java/baseball/exception/WrongInputException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package baseball.exception;

public class WrongInputException extends RuntimeException {

public WrongInputException(String message) {
super(message);
}
}

0 comments on commit 6eb2d7b

Please sign in to comment.