diff --git a/build.gradle b/build.gradle index 8172fb73f..c87b8af75 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ apply plugin: 'eclipse' group = 'camp.nextstep' version = '1.0.0' -sourceCompatibility = '1.8' +sourceCompatibility = "14" repositories { mavenCentral() @@ -17,3 +17,20 @@ dependencies { test { useJUnitPlatform() } + +//// annotation processing 추가 +//tasks.withType(JavaCompile) { +// options.annotationProcessorPath = configurations.apt +//} +// +//configurations { +// apt +//} +// +//dependencies { +// apt 'org.projectlombok:lombok:1.18.20' +// compileOnly 'org.projectlombok:lombok:1.18.20' +//} + +sourceCompatibility = 11 +targetCompatibility = 11 diff --git a/src/main/java/Calculator.class b/src/main/java/Calculator.class new file mode 100644 index 000000000..365efbdc8 Binary files /dev/null and b/src/main/java/Calculator.class differ diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 000000000..30c2fcb6a --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,64 @@ +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Calculator { + + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + String expression = sc.nextLine(); + Calculator cc = new Calculator(); + Calculator.create(expression); + System.out.println(cc.calculate(expression)); + } + public static Calculator create (String expression){ + + if (!validatingExpression(expression)) + throw new IllegalArgumentException("올바른 연산식이 아닙니다."); + + return new Calculator(); + } + + private static boolean validatingExpression (String expression){ + + Pattern pattern = Pattern.compile("^[+\\-]?\\d( ?[*\\-+/] ?\\d)*$"); + Matcher matcher = pattern.matcher(expression); + return matcher.find(); + } + + + public int calculate (String expression) { + + String[] parsingExpression = expression.split(" "); + + int calculationResult = Integer.parseInt(parsingExpression[0]); + + String operator = ""; + + int operand = 0; + + for (int i = 0; i < parsingExpression.length; i++) { + + if (i % 2 != 0) { // 홀수 인덱스에서 연산자 값 추출 + operator = parsingExpression[i]; + continue; + } + + operand = Integer.parseInt(parsingExpression[i]); + switch (operator) { + case "+" : calculationResult += operand; break; + case "-" : calculationResult -= operand; break; + case "*" : calculationResult *= operand; break; + case "/" : { + if (operand != 0) calculationResult /= operand; + else throw new ArithmeticException("0을 나눌 수 없습니다."); + break; + } + } + + } + return calculationResult; + } + +} diff --git a/src/main/java/NumberBaseballGame.class b/src/main/java/NumberBaseballGame.class new file mode 100644 index 000000000..1105cb47c Binary files /dev/null and b/src/main/java/NumberBaseballGame.class differ diff --git a/src/main/java/NumberBaseballGame.java b/src/main/java/NumberBaseballGame.java new file mode 100644 index 000000000..58ae7e833 --- /dev/null +++ b/src/main/java/NumberBaseballGame.java @@ -0,0 +1,50 @@ +import java.util.Scanner; + +public class NumberBaseballGame { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int min = 100; + int max = 999; + int com = (int) (Math.random() * (max - min + 1)) + min; + + while (com != -1) { + System.out.println(com); + int strike = 0; + int ball = 0; + System.out.print("숫자를 입력해 주세요 : "); + int userNum = sc.nextInt(); + com = NumberBaseballGame.numberCheck(com, userNum, sc); + } + + } + + public static int numberCheck(int com, int userNum, Scanner sc) { + int strike = 0, ball = 0; + int min = 100; + int max = 999; + + if (com / 100 == userNum / 100) strike++; + if (com / 10 % 10 == userNum / 10 % 10) strike++; + if (com % 10 == userNum % 10) strike++; + if (strike == 3) { + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + int select = sc.nextInt(); + if (select == 1) { + return (int) (Math.random() * (max - min + 1)) + min; + + } else return -1; + } + if (strike != 3) { + if (com / 100 != userNum / 100 && (com / 10 % 10 == userNum / 100 || com % 10 == userNum / 100)) ball++; + if (com / 10 % 10 != userNum / 10 % 10 && (com / 100 == userNum / 10 % 10 || com % 10 == userNum / 10 % 10)) + ball++; + if (com % 10 != userNum % 10 && (com / 100 == userNum % 10 || com / 10 % 10 == userNum % 10)) ball++; + if (strike != 0 && ball != 0) System.out.printf("%d스트라이크 %d볼 %n", strike, ball); + if (strike == 0 && ball != 0) System.out.printf("%d볼 %n", ball); + if (strike != 0 && ball == 0) System.out.printf("%d스트라이크 %n", strike); + if (strike == 0 && ball == 0) System.out.printf(""); + } + return com; + } +} diff --git a/src/main/java/baseball/Ball.java b/src/main/java/baseball/Ball.java new file mode 100644 index 000000000..9eaf60d09 --- /dev/null +++ b/src/main/java/baseball/Ball.java @@ -0,0 +1,33 @@ +package baseball; + +public class Ball { + private final int ballNum; + private final int position; + + public Ball(int position, int ballNum) { + this.position = position; + this.ballNum = ballNum; + } + + public BallStatus play(Ball ball) { + BallStatus ballStatus = null; + if(ball.mathBallNum(ballNum)){ + ballStatus = BallStatus.BALL; + if(ball.mathPosition(position)){ + ballStatus = BallStatus.STRIKE; + } + } + else ballStatus = BallStatus.NOTING; + + return ballStatus; + } + + private boolean mathPosition(int position) { + return this.position == position; + } + + private boolean mathBallNum(int ballNum) { + return this.ballNum == ballNum; + } + +} diff --git a/src/main/java/baseball/BallStatus.java b/src/main/java/baseball/BallStatus.java new file mode 100644 index 000000000..9d0d38b05 --- /dev/null +++ b/src/main/java/baseball/BallStatus.java @@ -0,0 +1,5 @@ +package baseball; + +public enum BallStatus { + BALL, STRIKE, NOTING +} diff --git a/src/main/java/baseball/ValidationUtils.java b/src/main/java/baseball/ValidationUtils.java new file mode 100644 index 000000000..4ed4f72a3 --- /dev/null +++ b/src/main/java/baseball/ValidationUtils.java @@ -0,0 +1,13 @@ +package baseball; + +public class ValidationUtils { + + public static final int MIN_NUM = -1; + public static final int MAX_NUM = 9; + + public static boolean validNumTest(int num) { +// if( -1 < num && num <= 9)return true; +// return false; + return MIN_NUM < num && num <= MAX_NUM; + } +} diff --git a/src/test/java/baseball/BallTest.java b/src/test/java/baseball/BallTest.java new file mode 100644 index 000000000..5c71c9451 --- /dev/null +++ b/src/test/java/baseball/BallTest.java @@ -0,0 +1,26 @@ +package baseball; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +public class BallTest { + private Ball com; + @BeforeEach + void sutup(){ + com = new Ball(1, 4); + } + @Test + void noting(){ + assertThat(com.play(new Ball(2,5))).isEqualTo(BallStatus.NOTING); + } + @Test + void ball(){ + assertThat(com.play(new Ball(2,4))).isEqualTo(BallStatus.BALL); + } + @Test + void strike(){ + assertThat(com.play(new Ball(1, 4))).isEqualTo(BallStatus.STRIKE); + } +} diff --git a/src/test/java/baseball/ValidationUtilsTest.java b/src/test/java/baseball/ValidationUtilsTest.java new file mode 100644 index 000000000..589f14988 --- /dev/null +++ b/src/test/java/baseball/ValidationUtilsTest.java @@ -0,0 +1,19 @@ +package baseball; + +import baseball.ValidationUtils; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +class ValidationUtilsTest { + @Test + @DisplayName("숫자야 0~9 사이 숫자 테스트") + void baseballNumberTest(){ + assertThat(ValidationUtils.validNumTest(9)).isTrue(); + assertThat(ValidationUtils.validNumTest(1)).isTrue(); + assertThat(ValidationUtils.validNumTest(0)).isTrue(); + assertThat(ValidationUtils.validNumTest(-1)).isFalse(); + assertThat(ValidationUtils.validNumTest(10)).isFalse(); + } +} \ No newline at end of file diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java index 43e47d90b..46819ad5b 100644 --- a/src/test/java/study/StringTest.java +++ b/src/test/java/study/StringTest.java @@ -1,8 +1,22 @@ package study; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Scanner; +import java.util.Set; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + public class StringTest { @Test @@ -10,4 +24,168 @@ void replace() { String actual = "abc".replace("b", "d"); assertThat(actual).isEqualTo("adc"); } -} + + @Test + void split() { + String[] number1 = "1,2".split(","); + assertThat(number1).contains("2", "1"); // 순서 상관없이 + assertThat(number1).containsExactly("1", "2"); // 순서까지 일치 + System.out.println("number1 배열 = " + Arrays.toString(number1)); + + String[] number2 = "1".split(","); + assertThat(number2).contains("1"); + assertThat(number2).containsExactly("1"); + System.out.println("number2 배열 = " + Arrays.toString(number2)); + } + + @Test + void substring() { + String str = "(1,2)".substring(1, 4); + assertThat(str).isEqualTo("1,2"); + } + + @Test + @DisplayName("특정 위치의 문자를 가져올 때 위치 값을 벗어나면 StringIndexOutOfBoundsException이 발생") + void charAt() { + String str = "abc"; + + assertThatThrownBy(() -> { + char charTest = str.charAt(3); + }).isInstanceOf(StringIndexOutOfBoundsException.class) + .hasMessageContaining("String index out of range: 3"); + + assertThatThrownBy(() -> { + char charTest = str.charAt(2); + }).isInstanceOf(StringIndexOutOfBoundsException.class) + .hasMessageContaining("String index out of range: 3"); + } + } + + + + class SetTest { + private Set numbers; + + @BeforeEach + void setUp() { + numbers = new HashSet<>(); + numbers.add(1); + numbers.add(1); + numbers.add(2); + numbers.add(3); + } + + @Test + void setSize() { + System.out.println("set size = " + numbers.size()); + } + + @Test + void contains() { + assertThat(numbers.contains(1)).isTrue(); + assertThat(numbers.contains(2)).isTrue(); + assertThat(numbers.contains(3)).isTrue(); + assertThat(numbers.contains(4)).isFalse(); + assertThat(numbers.contains(5)).isFalse(); + } + + @ParameterizedTest + @ValueSource(ints = {1, 2, 3}) + void setNumberTest(int number) { + assertTrue(numbers.contains(number)); + } + + @ParameterizedTest + @CsvSource({"1", "2", "3"}) + void setNumberTest(String input) { + int number = Integer.parseInt(input); + assertTrue(numbers.contains(number)); + } + } + + class calculatorTest { + @Test + void calculatorTest_Addition() { + String input = "2 + 3 * 4 / 2"; + String[] values = input.split(" "); + + int num1 = Integer.parseInt(values[0]); + int num2 = Integer.parseInt(values[2]); + int num3 = Integer.parseInt(values[4]); + int num4 = Integer.parseInt(values[6]); + int result1 = 0, result2 = 0, result3 = 0; + + switch (values[1]) { + case "+" : result1 = num1 + num2; break; + case "-" : result1 = num1 - num2; break; + case "*" : result1 = num1 * num2; break; + case "/" : result1 = num1 / num2; break; + default: + } + switch (values[3]) { + case "+" : result2 = result1 + num3; break; + case "-" : result2 = result1 - num3; break; + case "*" : result2 = result1 * num3; break; + case "/" : result2 = result1 / num3; break; + default: + } + switch (values[5]) { + case "+" : result3 = result2 + num4; + case "-" : result3 = result2 - num4; + case "*" : result3 = result2 * num4; + case "/" : result3 = result2 / num4; + default: + } + System.out.println(result3); + assertThat(result3).isEqualTo(10); + } + + +// @ParameterizedTest +// @ValueSource(strings = {"1 + 2 * 3 - 4 / 2", "5 - 3 * 2 + 4 / 2"}) +// void calculatorTest(String input) { +// String[] values = input.split(" "); +// assertThat(values).hasSize(9); // 입력된 값이 공백으로 구분된 3개의 값인지 검증 +// assertThat(values[0]).matches("\\d+"); // 첫 번째 값이 정수인지 검증 +// assertThat(values[1]).matches("[+\\-*/]"); // 두 번째 값이 연산자인지 검증 +// assertThat(values[2]).matches("\\d+"); // 세 번째 값이 정수인지 검증 +// assertThat(values[3]).matches("[+\\-*/]"); // 네 번째 값이 연산자인지 검증 +// assertThat(values[4]).matches("\\d+"); // 다섯 번째 값이 정수인지 검증 +// assertThat(values[5]).matches("[+\\-*/]"); // 여섯 번째 값이 연산자인지 검증 +// assertThat(values[6]).matches("\\d+"); // 일곱 번째 값이 정수인지 검증 +// +// int num1 = Integer.parseInt(values[0]); +// int num2 = Integer.parseInt(values[2]); +// int num3 = Integer.parseInt(values[4]); +// int num4 = Integer.parseInt(values[6]); +// int num5 = Integer.parseInt(values[8]); +// int result1 = 0, result2 = 0, result3 = 0; +// switch (values[1]) { +// case "+" : result1 = num1 + num2; break; +// case "-" : result1 = num1 - num2; break; +// case "*" : result1 = num1 * num2; break; +// case "/" : result1 = num1 / num2; break; +// } +// switch (values[3]) { +// case "+" : result2 = result1 + num3; +// case "-" : result2 = result1 - num3; // 기존 코드에서 수정 +// case "*" : result2 = result1 * num3; // 기존 코드에서 수정 +// case "/" : result2 = result1 / num3; // 기존 코드에서 수정 +// } +// switch (values[5]) { +// case "+" : result3 = result2 + num4; +// case "-" : result3 = result2 - num4; // 기존 코드에서 수정 +// case "*" : result3 = result2 * num4; // 기존 코드에서 수정 +// case "/" : result3 = result2 / num4; // 기존 코드에서 수정 +// } +// System.out.println(result3); +// +// assertThat(result3).isEqualTo(Integer.parseInt(values[6])); +// } + } + + + + + +