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

Project finished #16

Open
wants to merge 3 commits into
base: master
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
103 changes: 102 additions & 1 deletion src/RomanNumerals.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,109 @@
import java.util.*;

public class RomanNumerals {

private TreeMap<String, Integer> numbers;

public RomanNumerals(){
numbers = new TreeMap<String, Integer>();
numbers.put("I", 1);
numbers.put("V", 5);
numbers.put("X", 10);
numbers.put("L", 50);
numbers.put("C", 100);
numbers.put("D", 500);
numbers.put("M", 1000);
}

public int convertToInteger(String romanNum) {
// To be Implemented

return convertMultiple(romanNum);

}
private int convertMultiple(String romanNum){
String[] n = romanNum.split("");
if(checkThreeTimes(n) && substractOkay(n)){
return count(n);
}
return 0;

}
//count for checking the total
private int count(String[] n){
int c = numbers.get(n[n.length - 1]);
String prev = n[n.length - 1];
for(int i = n.length -2; i >= 0; i--){
if(numbers.get(prev) > numbers.get(n[i])){
c -= numbers.get(n[i]);
}
else{
c += numbers.get(n[i]);
}
prev = n[i];
}
return c;
}
private boolean substractOkay(String[] n){
String prev = n[n.length - 1];
int times = 0;
for(int i = n.length - 2; i >= 0; i--){
if(prev.equals("M") || prev.equals("D")){
if(!n[i].equals("C")){
return false;
}
else{
times++;
}
}
if(prev.equals("C") || prev.equals("L")){
if(!n[i].equals("X")){
return false;
}
else{
times++;
}
}
if(prev.equals("X") || prev.equals("V")){
if(!n[i].equals("I")){
return false;
}
else{
times++;
}
}
if(n[i].equals("V") || n[i].equals("L") || n[i].equals("D")){
if(numbers.get(n[i]) < numbers.get(prev)){
return false;
}
}
if(!n[i].equals("I") || !n[i].equals("X") || !n[i].equals("C")){
times = 0;
}
if(times > 1){
return false;
}
prev = n[i];
}
return true;
}
private boolean checkThreeTimes(String[] n){

String prev = n[n.length - 1];
int times = 0;
for(int i = n.length - 2; i >= 0; i--){
if(prev.equals(n[i])){
times++;
}
if(times > 2){
return false;
}
if(!prev.equals(n[i])){
times = 0;
}
prev = n[i];
}
return true;

}

}
64 changes: 62 additions & 2 deletions tests/TestRomanNumerals.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,69 @@

public class TestRomanNumerals {

RomanNumerals num = new RomanNumerals();
@Test
public void test() {
fail("Not yet implemented");
public void test_ConvertToInteger() {

int arabic = num.convertToInteger("I");
assertEquals("Number is not one", 1, arabic);
}
@Test
public void test_ConvertToIntegerThree(){

int arabic = num.convertToInteger("III");
assertEquals("Number is not three", 3, arabic);
}
@Test
public void test_ConvertToIntegerFive(){

int arabic = num.convertToInteger("V");
assertEquals("Number is not five", 5, arabic);
}
@Test
public void test_ConvertToIntegerTen(){

int arabic = num.convertToInteger("X");
assertEquals("Number is not ten", 10, arabic);
}
@Test
public void test_ConvertToIntegerFifty(){

int arabic = num.convertToInteger("L");
assertEquals("Number is not fifty", 50, arabic);
}
@Test
public void test_ConvertToIntegerHundred(){

int arabic = num.convertToInteger("C");
assertEquals("Number is not hundred", 100, arabic);
}
@Test
public void test_ConvertToIntegerFour(){

int arabic = num.convertToInteger("IV");
assertEquals("Number is not four", 4, arabic);
}
@Test
public void test_ConvertToIntegerSeve(){
int arabic = num.convertToInteger("VII");
assertEquals("Number is not seven", 7, arabic);
}
@Test
public void test_ConvertToIntegerNine(){
int arabic = num.convertToInteger("IX");
assertEquals("Number is not nine", 9, arabic);
}
@Test
public void test_NotRepeated(){
int arabic = num.convertToInteger("VV");
assertEquals("Number cannot be repeated", 0, arabic);
}
@Test
public void test_Substraction(){
int arabic = num.convertToInteger("XXC");
assertEquals("Number is substracted even though not possible", 0, arabic);
}


}