diff --git a/src/main/java/SetCollection.java b/src/main/java/SetCollection.java new file mode 100644 index 0000000..9cd1027 --- /dev/null +++ b/src/main/java/SetCollection.java @@ -0,0 +1,8 @@ +import java.util.Set; + +public class SetCollection { + public Integer getNumbersSize(Set numbers) { + return numbers.size(); + } + +} diff --git a/src/main/java/StringPs.java b/src/main/java/StringPs.java new file mode 100644 index 0000000..53b1e89 --- /dev/null +++ b/src/main/java/StringPs.java @@ -0,0 +1,15 @@ +public class StringPs { + + public String[] stringSplit(String s) { + String[] arr = s.split(","); + return arr; + } + + public String stringSub(String s) { + return s.substring(1,4); + } + + public String stringCharAt(int i, String abc) { + return Character.toString(abc.charAt(i)); + } +} diff --git a/src/main/java/calculator/Calculator.java b/src/main/java/calculator/Calculator.java new file mode 100644 index 0000000..5cb1f3b --- /dev/null +++ b/src/main/java/calculator/Calculator.java @@ -0,0 +1,89 @@ +package calculator; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Arrays; + +import static java.lang.Integer.parseInt; + +public class Calculator { + + public int getNumber(String s) { + String[] arr = s.split(" "); + ArrayList arrayList = new ArrayList<>(Arrays.asList(arr)); + if(arrayList.size() == 1) { + return Integer.parseInt(arrayList.get(0)); + } + while (arrayList.size() > 0) { + + if(arrayList.get(1).equals("+")) { + int number = addNumber(arrayList.get(0), arrayList.get(2)); + + for(int i =0; i<3; i++ ) { + arrayList.remove(0); + } + arrayList.add(0,Integer.toString(number)); + } + + if(arrayList.size()==1){ + return Integer.parseInt(arrayList.get(0)); + } + + if(arrayList.get(1).equals("-")) { + int number = subTractionNumber(arrayList.get(0), arrayList.get(2)); + + for(int i =0; i<3; i++ ) { + arrayList.remove(0); + } + arrayList.add(0,Integer.toString(number)); + } + + if(arrayList.size()==1){ + return Integer.parseInt(arrayList.get(0)); + } + + if(arrayList.get(1).equals("*")) { + int number = multiplicationNumber(arrayList.get(0), arrayList.get(2)); + + for(int i =0; i<3; i++ ) { + arrayList.remove(0); + } + arrayList.add(0,Integer.toString(number)); + } + + if(arrayList.size()==1){ + return Integer.parseInt(arrayList.get(0)); + } + + if(arrayList.get(1).equals("/")) { + int number = divideNumber(arrayList.get(0), arrayList.get(2)); + + for(int i =0; i<3; i++ ) { + arrayList.remove(0); + } + arrayList.add(0,Integer.toString(number)); + } + + if(arrayList.size()==1){ + return Integer.parseInt(arrayList.get(0)); + } + } + return Integer.parseInt(arrayList.get(0)); + } + + public int addNumber(String s, String b) { + return Integer.parseInt(s) + Integer.parseInt(b); + } + + public int subTractionNumber(String s, String b) { + return Integer.parseInt(s) - Integer.parseInt(b); + } + + public int divideNumber(String s, String b) { + return Integer.parseInt(s) / Integer.parseInt(b); + } + + public int multiplicationNumber(String s, String b) { + return Integer.parseInt(s) * Integer.parseInt(b); + } +} \ No newline at end of file diff --git a/src/main/java/calculator/Computation.java b/src/main/java/calculator/Computation.java new file mode 100644 index 0000000..34691f2 --- /dev/null +++ b/src/main/java/calculator/Computation.java @@ -0,0 +1,20 @@ +package calculator; + +public class Computation { + + public int addNumber(String s, String b) { + return Integer.parseInt(s) + Integer.parseInt(b); + } + + public int subTractionNumber(String s, String b) { + return Integer.parseInt(s) - Integer.parseInt(b); + } + + public int divideNumber(String s, String b) { + return Integer.parseInt(s) / Integer.parseInt(b); + } + + public int multiplicationNumber(String s, String b) { + return Integer.parseInt(s) * Integer.parseInt(b); + } +} diff --git a/src/test/java/SetCollectionTest.java b/src/test/java/SetCollectionTest.java new file mode 100644 index 0000000..1c5aef9 --- /dev/null +++ b/src/test/java/SetCollectionTest.java @@ -0,0 +1,57 @@ +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.HashSet; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class SetCollectionTest { + private Set numbers; + + @BeforeEach + void setUp() { + numbers = new HashSet<>(); + numbers.add(1); + numbers.add(1); + numbers.add(2); + numbers.add(3); + } + + @DisplayName("Hash 사이즈 검사 테스트") + @Test + public void testCheckSizeTest() { + SetCollection checkSize = new SetCollection(); + Integer result = checkSize.getNumbersSize(this.numbers); + assertEquals(3, result); + } + + @DisplayName("Hash안에 포함되어있는지 검사 테스트") + @Test + public void contains() { + assertThat(numbers.contains(1)).isTrue(); + assertThat(numbers.contains(2)).isTrue(); + assertThat(numbers.contains(3)).isTrue(); + } + + @DisplayName("중복된 메소드 제거 테스") + @ParameterizedTest + @ValueSource(ints= {1, 2, 3}) + public void testContaionsTrueTes트t(int number) { + assertTrue(numbers.contains(number)); + } + + @DisplayName("포함된 값이 없으면 false 반환하는 테스트") + @ParameterizedTest + @CsvSource(value= {"1:true", "2:true", "3:true", "4:false", "5:false"}, delimiter = ':') + public void testContaionsTest(int input,boolean expected) { + boolean result = numbers.contains(input); + assertEquals(expected, result); + } + } \ No newline at end of file diff --git a/src/test/java/StringTest.java b/src/test/java/StringTest.java new file mode 100644 index 0000000..45907c5 --- /dev/null +++ b/src/test/java/StringTest.java @@ -0,0 +1,44 @@ +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + + +import static org.assertj.core.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class StringTest { + + @DisplayName("배열의 숫자값들을트 ,을 기준으로 분리하는 테스트") + @Test + public void testSplitStringTest() { + String[] arr = {"1","2"}; + StringPs splitString = new StringPs(); + String[] result1 = splitString.stringSplit("1,2"); + assertArrayEquals(arr,result1); + String[] arr2 = {"1"}; + String[] result2 = splitString.stringSplit("1"); + assertArrayEquals(arr2,result2); + } + + @DisplayName("()를 제거하는 테스트") + @Test + public void testSubStringTest() { + String str = "1,2"; + StringPs removeString = new StringPs(); + String result = removeString.stringSub("(1,2)"); + assertEquals(str, result); + } + + @DisplayName("문자를 가져올때 위치값을 벗어나면 메세지가 발생하는 부분에 대한 테스트") + @Test + public void testCharAtStringTest() { + String str = "b"; + StringPs charAtString = new StringPs(); + String result = charAtString.stringCharAt(1,"abc"); + assertEquals(str,result); + assertThatThrownBy(() -> { + charAtString.stringCharAt(50,"abc"); + }).isInstanceOf(IndexOutOfBoundsException.class).hasMessageContaining("String index out of range:"); + } + +} diff --git a/src/test/java/calculator/CalculatorTest.java b/src/test/java/calculator/CalculatorTest.java new file mode 100644 index 0000000..0bf2ccf --- /dev/null +++ b/src/test/java/calculator/CalculatorTest.java @@ -0,0 +1,76 @@ +package calculator; + +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 static org.assertj.core.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CalculatorTest { + private Calculator calculator; + + @BeforeEach + void setUp() { + calculator = new Calculator(); + } + @DisplayName("계산기") + @ParameterizedTest + @CsvSource(value = {"2 + 3 * 4 / 2 :10","1:1", "0 * 10 * 11 + 3 :3"}, delimiter = ':') + public void testCalculator(String input, int result) { + assertThat(calculator.getNumber(input)).isEqualTo(result); + } + + @DisplayName("계산기의 합을 테스트한다.") + @Test + public void testAddCalculator() { + Calculator addTest = new Calculator(); + int result = addTest.addNumber("2","3"); + assertEquals(5,result); + } + + @DisplayName("계산기의 뺄셈을 테스트한다.") + @Test + public void testSubtractionCalculator() { + Calculator subTractionTest = new Calculator(); + int result = subTractionTest.subTractionNumber("4","3"); + assertEquals(1,result); + } + + @DisplayName("계산기의 나눗셈을 테스트한다.") + @Test + public void testDivideCalculator() { + Calculator divideTest = new Calculator(); + int result = divideTest.divideNumber("4","2"); + assertEquals(2,result); + } + + @DisplayName("계산기의 셈을 테스트한다.") + @Test + public void testMultiplicationCalculator() { + Calculator multiplication = new Calculator(); + int result = multiplication.multiplicationNumber("4","3"); + assertEquals(12,result); + } + + @DisplayName("not Null") + @Test + public void testNotnull() { + assertThatIllegalArgumentException().isThrownBy(() -> { + calculator.getNumber("null"); + }); + } + + @DisplayName("not Null") + @Test + public void testNotContribution() { + assertThatIllegalArgumentException().isThrownBy(() -> { + calculator.getNumber("!"); + }); + } + +}