Skip to content
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

Create calculator following "step2: calculator" #8

Open
wants to merge 2 commits into
base: gyim1345
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions src/main/java/Step1_학습테스트/CharMethods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package Step1_학습테스트;

public class CharMethods {
public char charAt(String string, int index) {
return string.charAt(index);
}
}
10 changes: 10 additions & 0 deletions src/main/java/Step1_학습테스트/SetMethods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package Step1_학습테스트;

import java.util.Set;

public class SetMethods {
public int getSize(Set<Integer> set) {
return set.size();
}

}
11 changes: 11 additions & 0 deletions src/main/java/Step1_학습테스트/StringMethods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package Step1_학습테스트;

public class StringMethods {
public String[] splitStrings(String string) {
return string.split("");
}

public static String subString(String string, int a, int b) {
return string.substring(a,b);
}
}
41 changes: 41 additions & 0 deletions src/main/java/Step2_Calculator/ArithmeticOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package Step2_Calculator;

public class ArithmeticOperation {

public static int arithmeticOperation(int acc, int curr, String arithmeticOperator) {

if(arithmeticOperator.equals("+")){
return sum(acc, curr);
}

if(arithmeticOperator.equals("-")){
return subtract(acc, curr);
}

if(arithmeticOperator.equals("/")){
return divide(acc, curr);
}

if(arithmeticOperator.equals("*")){
return multiply(acc, curr);
}

return acc;
}

private static int sum(int acc, int curr) {
return acc + curr;
}

private static int divide(int acc, int curr) {
return acc / curr;
}

private static int multiply(int acc, int curr) {
return acc * curr;
}

private static int subtract(int acc, int curr) {
return acc - curr;
}
}
17 changes: 17 additions & 0 deletions src/main/java/Step2_Calculator/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package Step2_Calculator;

public class Calculator {
private int accumulator;

public int calculate(String s) {
InputErrorHandler.checkCalculatorIllegalArgument(s);
String[] arithmetics = InputExtactor.extractCalculatorInputs(s);

accumulator = Integer.parseInt(arithmetics[0]);

for(int i = 1; i< arithmetics.length; i += 2) {
accumulator = ArithmeticOperation.arithmeticOperation(accumulator, Integer.parseInt(arithmetics[i+1]), arithmetics[i]);
}
return accumulator;
}
}
11 changes: 11 additions & 0 deletions src/main/java/Step2_Calculator/CalculatorApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package Step2_Calculator;

public class CalculatorApp {
public static void main(String[] args) {
InputView.startCalculator();
String input = InputView.inputArithmeticOperations();
Calculator calculator = new Calculator();
int result = calculator.calculate(input);
OutputView.showOutput(result);
}
}
17 changes: 17 additions & 0 deletions src/main/java/Step2_Calculator/InputErrorHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package Step2_Calculator;

public class InputErrorHandler {
public static void checkCalculatorIllegalArgument(String s){
String numbersToSingleLength = s.replaceAll("-?\\d+", "1");
String noSpace = numbersToSingleLength.replaceAll(" ", "");
if(numbersToSingleLength.length() != noSpace.length()*2-1) {
throw new IllegalArgumentException("Wrong input Format! Must be (number) (arithmetic Operator) (number) ... so on with no null inputs");
}

if(!s.matches("[\\d+*/\\-]+")){
System.out.println("asdasdasd");
throw new IllegalArgumentException("Wrong inputs!!!");
}

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

public class InputExtactor {
public static String[] extractCalculatorInputs(String s) {
return s.split(" ");
}
}
16 changes: 16 additions & 0 deletions src/main/java/Step2_Calculator/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package Step2_Calculator;

import java.util.Scanner;

public class InputView {
private static Scanner scanner;

public static void startCalculator(){
System.out.print("Input Arithmetic Operations : ");
}
public static String inputArithmeticOperations() {
scanner = new Scanner(System.in);
return scanner.nextLine();
}

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

public class OutputView {

public static void showOutput(int result) {
System.out.println("Result :" + result);
}

}
1 change: 1 addition & 0 deletions src/main/java/empty.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ㄷㄱㅀㄴㄷㄹ
51 changes: 51 additions & 0 deletions src/test/java/Step1_학습테스트/CharMethodsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package Step1_학습테스트;

import Step1_학습테스트.CharMethods;
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 static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

@DisplayName("Char Methods 테스트")
public class CharMethodsTest {
private CharMethods charMethods;
@BeforeEach
void setUp() {
charMethods = new CharMethods();
}

@DisplayName("charAt 테스트")
@ParameterizedTest
@CsvSource(value = {"abc:0:a", "abc:1:b", "abc:2:c"}, delimiter = ':')
public void charIOTest(String string,int index, char expected) {
assertThat(charMethods.charAt(string, index)).isEqualTo(expected);
}

@DisplayName("charAt Error Index out of bound 테스트")
@Test
public void charErrorTest() {

assertThatThrownBy(() -> {
charMethods.charAt("abc", 3);
}).isInstanceOf(IndexOutOfBoundsException.class)
.hasMessageContaining("String index out of range: 3");

// Throwable thrown = catchThrowable(() -> { throw new Exception("String index out of range: 3"); });
// assertThat(thrown).isInstanceOf(Exception.class) .hasMessageContaining("String index out of range: 3");

assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> charMethods.charAt("abc", 3))
.withMessage("String index out of range: 3")
.withMessageContaining("String")
.withNoCause();

// assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> { throw new IndexOutOfBoundsException("NO!"); })
// .withMessage("NO!")
// .withMessageContaining("NO")
// .withNoCause();

}
}
61 changes: 61 additions & 0 deletions src/test/java/Step1_학습테스트/SetMethodsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package Step1_학습테스트;

import Step1_학습테스트.SetMethods;
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.junit.jupiter.api.Assertions.assertTrue;


import java.util.HashSet;
import java.util.Set;

import static org.assertj.core.api.Assertions.*;

@DisplayName("set Method Test")
public class SetMethodsTest {
private Set<Integer> numbers;
private SetMethods setMethods;
@BeforeEach
void setUp() {
setMethods = new SetMethods();
numbers = new HashSet<>();
numbers.add(1);
numbers.add(1);
numbers.add(2);
numbers.add(3);
}

@DisplayName("set 사이즈 테스트")
@Test
public void setSizeTest() {
int setSize = setMethods.getSize(numbers);
assertThat(setSize).isEqualTo(3);
}

@DisplayName("set 내용물 확인 테스트")
@ParameterizedTest
@ValueSource(ints = {1,1,2,3})
public void setContainsTest(int number) {
assertThat(numbers.contains(number)).isTrue();
}

// @Test
// public void setThrowErrorTest() {
// assertThat(numbers.contains(true)).isEqualTo(false);
// assertThat(numbers.contains(4)).isEqualTo(false);
// assertThat(numbers.contains(5)).isEqualTo(false);
// }

@DisplayName("set 입력 출력 테스트")
@ParameterizedTest
@CsvSource(value = {"1:true", "2:true", "3:true", "4:false", "5:false" }, delimiter = ':')
public void setIOTest(int input, boolean expected) {
assertThat(numbers.contains(input)).isEqualTo(expected);

}

}
44 changes: 44 additions & 0 deletions src/test/java/Step1_학습테스트/StringMethodsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package Step1_학습테스트;

import Step1_학습테스트.StringMethods;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.*;

@DisplayName("String Method 테스트")
public class StringMethodsTest {
private StringMethods stringMethods;

@BeforeEach
void setUp() {
stringMethods = new StringMethods();
}

@DisplayName("String split 테스트")
@Test
public void splitStringTest() {
String[] splittedString = stringMethods.splitStrings("12");
assertThat(splittedString).contains("1","2");
assertThat(splittedString).containsExactly("1","2");
assertThat(splittedString).isEqualTo(new String[]{"1","2"});

String[] splittedString1 = stringMethods.splitStrings("1");
assertThat(splittedString1).isEqualTo(new String[]{"1"});
assertThat(splittedString1).contains("1");
assertThat(splittedString1).containsExactly("1");

}

@DisplayName("String subString 테스트")
@Test
public void subStringTest() {
String modifiedString = stringMethods.subString("(1,2)", 1,4);
assertThat(modifiedString).isEqualTo("1,2");
assertThat(modifiedString).doesNotContain("(");
assertThat(modifiedString).doesNotContain(")");
}
}


67 changes: 67 additions & 0 deletions src/test/java/Step2_Calculator/CalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package Step2_Calculator;

import Step2_Calculator.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 static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

import static org.assertj.core.api.Assertions.*;

public class CalculatorTest {
private Calculator calculator;

@BeforeEach
void setUp() {
calculator = new Calculator();
}

@DisplayName("test calculator")
@ParameterizedTest
@CsvSource(value = {"2 + 3 * 4 / 2:10", "1:1", "0 * 10 * 11 + 3: 3"}, delimiter = ':')
public void calculatorTestArithmeticOperations(String arithmetic, int result) {
assertThat(calculator.calculate(arithmetic)).isEqualTo(result);
}

@DisplayName("null input")
@Test
public void calculatorTestWithNull() {
assertThatIllegalArgumentException().isThrownBy(() -> {
calculator.calculate("null");
});
}

@DisplayName("wrong input")
@Test
public void calculatorTestWithWrongInput() {
assertThatIllegalArgumentException().isThrownBy(() -> {
calculator.calculate("!");
});
}

@DisplayName("wrong format")
@Test
public void calculatorTestWithWrongFormat() {
assertThatIllegalArgumentException().isThrownBy(() -> {
calculator.calculate("4 + a");
});
}

@DisplayName("space inputs")
@Test
public void calculatorTestWithSpaceInputs() {
assertThatIllegalArgumentException().isThrownBy(() -> {
calculator.calculate("4 + 1 * 2");
});
}

@DisplayName("negative inputs")
@Test
public void calculatorTestWithNegativeInputs() {
assertThat(calculator.calculate("-4 - 1 * 2")).isEqualTo(-10);
}


}
1 change: 1 addition & 0 deletions test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test pull request