Skip to content

Commit

Permalink
Initial revision
Browse files Browse the repository at this point in the history
  • Loading branch information
fandigunawan committed Oct 3, 2023
0 parents commit baed6f3
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Maven
target/

# Ignore Gradle GUI config
gradle-app.setting

# Eclipse
/.classpath
/.settings/
/.project
/bin/

# IntelliJ
.idea
*.iml
*.ipr
*.iws

# Misc
*.log
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Java JUnit Example

## Kebutuhan
- Java JDK
- Maven 3+

## Cara menjalankan
### Build
- Jalankan `mvn package`
- Jalankan jar `java -jar target/polmed-calculator-1.0-SNAPSHOT.jar add 1 2`

### Pengujian
- Jalankan `mvn test`
61 changes: 61 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>id.polmed.calculator</groupId>
<artifactId>polmed-calculator</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.10.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>
id.polmed.calculator.Main
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

</project>
17 changes: 17 additions & 0 deletions src/main/java/id/polmed/calculator/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package id.polmed.calculator;

public class Calculator {

public int add(int a, int b) {
return a + b;
}
public int substract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public float divide(int a, int b) {
return (float) a/b;
}
}
26 changes: 26 additions & 0 deletions src/main/java/id/polmed/calculator/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package id.polmed.calculator;

public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
if(args.length != 3) {
System.out.println("Usage: polmed-calculator operation number1 number2");
return;
}
switch(args[0]) {
case "add":
System.out.println(calculator.add(Integer.parseInt(args[1]), Integer.parseInt(args[2])));
break;
case "substract":
System.out.println(calculator.substract(Integer.parseInt(args[1]), Integer.parseInt(args[2])));
break;
case "multiply":
System.out.println(calculator.multiply(Integer.parseInt(args[1]), Integer.parseInt(args[2])));
break;
case "divide":
System.out.println(calculator.divide(Integer.parseInt(args[1]), Integer.parseInt(args[2])));
break;
}

}
}
93 changes: 93 additions & 0 deletions src/test/java/id/polmed/calculator/CalculatorTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package id.polmed.calculator;

import static org.junit.jupiter.api.Assertions.assertEquals;

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;

class CalculatorTests {

@Test
@DisplayName("1 + 1 = 2")
void addsTwoNumbers() {
Calculator calculator = new Calculator();
assertEquals(2, calculator.add(1, 1), "1 + 1 should equal 2");
}

@ParameterizedTest(name = "{0} + {1} = {2}")
@CsvSource({
"0, 1, 1",
"1, 2, 3",
"49, 51, 100",
"1, 100, 101"
})
void add(int first, int second, int expectedResult) {
Calculator calculator = new Calculator();
assertEquals(expectedResult, calculator.add(first, second),
() -> first + " + " + second + " should equal " + expectedResult);
}


@Test
@DisplayName("2 - 1 = 1")
void substractTwoNumbers() {
Calculator calculator = new Calculator();
assertEquals(1, calculator.substract(2, 1), "2 - 1 should equal 1");
}

@ParameterizedTest(name = "{0} - {1} = {2}")
@CsvSource({
"0, 1, -1",
"5, 2, 3",
"100, 51, 49",
"101, 100, 1"
})
void substract(int first, int second, int expectedResult) {
Calculator calculator = new Calculator();
assertEquals(expectedResult, calculator.substract(first, second),
() -> first + " - " + second + " should equal " + expectedResult);
}


@Test
@DisplayName("2 * 1 = 2")
void multiplyTwoNumbers() {
Calculator calculator = new Calculator();
assertEquals(2, calculator.multiply(2, 1), "2 * 1 should equal 2");
}

@ParameterizedTest(name = "{0} * {1} = {2}")
@CsvSource({
"0, 1, 0",
"5, -2, -10",
"100, 51, 5100",
"101, 100, 10100"
})
void multiply(int first, int second, int expectedResult) {
Calculator calculator = new Calculator();
assertEquals(expectedResult, calculator.multiply(first, second),
() -> first + " * " + second + " should equal " + expectedResult);
}

@Test
@DisplayName("1 / 2 = 0.5")
void divideTwoNumbers() {
Calculator calculator = new Calculator();
assertEquals(0.5, calculator.divide(1, 2), "1 / 2 should equal 0.5");
}

@ParameterizedTest(name = "{0} / {1} = {2}")
@CsvSource({
"0, 1, 0",
"5, -2, -2.5",
"100, 20, 5.0",
"3, 4, 0.75"
})
void divide(int first, int second, float expectedResult) {
Calculator calculator = new Calculator();
assertEquals(expectedResult, calculator.divide(first, second),
() -> first + " / " + second + " should equal " + expectedResult);
}
}

0 comments on commit baed6f3

Please sign in to comment.