This commit is contained in:
DerTyp7
2023-08-25 11:48:11 +02:00
commit c5f1c15394
4 changed files with 151 additions and 0 deletions

38
.gitignore vendored Normal file
View File

@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

25
pom.xml Normal file
View File

@@ -0,0 +1,25 @@
<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>org.example</groupId>
<artifactId>RomanCharConverter</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>RomanCharConverter</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,37 @@
package com.tf.romanCharConverter;
import java.util.HashMap;
public class RomanCharConverter {
private final HashMap<String, Integer> romanToArabic = new HashMap<String, Integer>();
public RomanCharConverter() {
this.romanToArabic.put("I", 1);
this.romanToArabic.put("V", 5);
this.romanToArabic.put("X", 10);
this.romanToArabic.put("L", 50);
this.romanToArabic.put("C", 100);
this.romanToArabic.put("D", 500);
this.romanToArabic.put("M", 1000);
this.romanToArabic.put("A", 5000);
}
public int convert(String romanString) {
int result = 0;
for (int i = 0; i < romanString.length(); i++) {
String romanChar = String.valueOf(romanString.charAt(i));
if(i < romanString.length() - 1) {
String nextRomanChar = String.valueOf(romanString.charAt(i + 1));
if(this.romanToArabic.get(romanChar) < this.romanToArabic.get(nextRomanChar)) {
result -= this.romanToArabic.get(romanChar);
continue;
}
}
result += this.romanToArabic.get(romanChar);
}
return result;
}
}

View File

@@ -0,0 +1,51 @@
package com.tf.romanCharConverter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class RomanCharConverterTest {
private RomanCharConverter converter;
@BeforeEach
public void setUp() {
converter = new RomanCharConverter();
}
@Test
public void givenSingleRomanChar_whenConvert_thenArabic(){
assertEquals(10, converter.convert("X"));
assertEquals(50, converter.convert("L"));
assertEquals(100, converter.convert("C"));
assertEquals(500, converter.convert("D"));
assertEquals(1000, converter.convert("M"));
assertEquals(5000, converter.convert("A"));
}
@Test
public void givenMultipleRomanChar_whenConvert_thenArabic(){
assertEquals(2, converter.convert("II"));
assertEquals(20, converter.convert("XX"));
assertEquals(200, converter.convert("CC"));
assertEquals(2000, converter.convert("MM"));
assertEquals(10000, converter.convert("AA"));
assertEquals(3, converter.convert("III"));
assertEquals(30, converter.convert("XXX"));
assertEquals(300, converter.convert("CCC"));
assertEquals(3000, converter.convert("MMM"));
assertEquals(1500, converter.convert("MD"));
}
@Test
public void givenMultipleRomanSubstractChar_whenConvert_thenArabic(){
assertEquals(4, converter.convert("IV"));
assertEquals(9, converter.convert("IX"));
assertEquals(40, converter.convert("XL"));
assertEquals(90, converter.convert("XC"));
assertEquals(400, converter.convert("CD"));
assertEquals(900, converter.convert("CM"));
assertEquals(14, converter.convert("XIV"));
}
}