From e34378e577c532baa98ea77673e28a996b58bf19 Mon Sep 17 00:00:00 2001 From: ihietani Date: Fri, 30 Sep 2016 17:08:59 +0300 Subject: [PATCH 1/3] Project finished --- src/RomanNumerals.java | 44 +++++++++++++++++++++++-- tests/TestRomanNumerals.java | 64 ++++++++++++++++++++++++++++++++++-- 2 files changed, 104 insertions(+), 4 deletions(-) diff --git a/src/RomanNumerals.java b/src/RomanNumerals.java index 20904f0..c5b78e2 100644 --- a/src/RomanNumerals.java +++ b/src/RomanNumerals.java @@ -1,8 +1,48 @@ +import java.util.*; public class RomanNumerals { + + private TreeMap numbers; + + public RomanNumerals(){ + numbers = new TreeMap(); + 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 0; + return convertMultiple(romanNum); + + } + private int convertMultiple(String romanNum){ + int num = 0; + String[] n = romanNum.split(""); + String prev = ""; + for(int i = 0; i < romanNum.length(); i++){ + if(prev.equals(n[i]) && (n[i].equals("V") || n[i].equals("L") || n[i].equals("D"))){ + num = 0; + break; + } + if(prev.equals("I") && (n[i].equals("V") || n[i].equals("X"))){ + num -= 2; + } + else if(prev.equals("X") && n[i].equals("C") || n[i].equals("D")){ + num -= 20; + } + else if(prev.equals("C") && n[i].equals("D") || n[i].equals("M")){ + num -= 200; + } + num += numbers.get(n[i]); + prev = n[i]; + + } + return num; } + } diff --git a/tests/TestRomanNumerals.java b/tests/TestRomanNumerals.java index 5d1de75..ca16f2e 100644 --- a/tests/TestRomanNumerals.java +++ b/tests/TestRomanNumerals.java @@ -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); + } + } From 6f03e207bc9690ddfedc4602f879fe4756b03673 Mon Sep 17 00:00:00 2001 From: Ilkka Date: Sat, 1 Oct 2016 11:12:58 +0300 Subject: [PATCH 2/3] Tried out a bit different kind of approach from the usual one --- src/RomanNumerals.java | 97 +++++++++++++++++++++++++++++++++--------- 1 file changed, 77 insertions(+), 20 deletions(-) diff --git a/src/RomanNumerals.java b/src/RomanNumerals.java index c5b78e2..b17cc25 100644 --- a/src/RomanNumerals.java +++ b/src/RomanNumerals.java @@ -2,17 +2,19 @@ public class RomanNumerals { + final String ONE = "I", FIVE = "V", TEN = "X", FIFTY = "L", HUNDRED = "C", FIVEHUNDRED= "D", THOUSAND = "M"; + //Trying out treeMap private TreeMap numbers; public RomanNumerals(){ numbers = new TreeMap(); - 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); + numbers.put(ONE, 1); + numbers.put(FIVE, 5); + numbers.put(TEN, 10); + numbers.put(FIFTY, 50); + numbers.put(HUNDRED, 100); + numbers.put(FIVEHUNDRED, 500); + numbers.put(THOUSAND, 1000); } public int convertToInteger(String romanNum) { @@ -20,27 +22,82 @@ public int convertToInteger(String romanNum) { return convertMultiple(romanNum); } + //If same number repeats + private boolean checkRepetition(String prev, String current){ + boolean repeat = false; + if(prev != null && prev.equals(current)){ + repeat = true; + } + else{ + repeat = false; + } + return repeat; + } + //If smaller + private boolean checkIfSmallerNumber(String prev, String current){ + boolean ok = false; + if(prev != null){ + if(current.equals(ONE)){ + if(prev.equals(FIVE) || prev.equals(TEN)){ + ok = true; + } + } + else if(current.equals(TEN)){ + if(prev.equals(FIFTY) || prev.equals(HUNDRED)){ + ok = true; + } + } + else if(current.equals(HUNDRED)){ + if(prev.equals(FIVEHUNDRED) || prev.equals(THOUSAND)){ + ok = true; + } + } + } + return ok; + } private int convertMultiple(String romanNum){ int num = 0; + String[] n = romanNum.split(""); - String prev = ""; - for(int i = 0; i < romanNum.length(); i++){ - if(prev.equals(n[i]) && (n[i].equals("V") || n[i].equals("L") || n[i].equals("D"))){ - num = 0; - break; + String prev = null; + int times = 0; + boolean substract = false; + + for(int i = n.length - 1; i >= 0; i--){ + + if(n[i].equals(FIVE) || n[i].equals(FIFTY) || n[i].equals(FIVEHUNDRED)){ + if(checkRepetition(prev, n[i])){ + num = 0; + break; + } } - if(prev.equals("I") && (n[i].equals("V") || n[i].equals("X"))){ - num -= 2; + else{ + if(checkRepetition(prev, n[i])){ + times++; + if(times == 3){ + break; + } + } + } + + + if(prev != null && numbers.get(prev) > numbers.get(n[i])){ + if(checkIfSmallerNumber(prev, n[i])){ + num -= numbers.get(n[i]); + + } + substract = true; + } - else if(prev.equals("X") && n[i].equals("C") || n[i].equals("D")){ - num -= 20; + else{ + num += numbers.get(n[i]); } - else if(prev.equals("C") && n[i].equals("D") || n[i].equals("M")){ - num -= 200; + if(substract && checkRepetition(prev, n[i])){ + num = 0; + break; } - num += numbers.get(n[i]); - prev = n[i]; + prev = n[i]; } return num; } From 2f69d163047f199815132622a70423d178ccfe39 Mon Sep 17 00:00:00 2001 From: ihietani Date: Fri, 7 Oct 2016 17:06:22 +0300 Subject: [PATCH 3/3] Update RomanNumerals.java --- src/RomanNumerals.java | 142 +++++++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 69 deletions(-) diff --git a/src/RomanNumerals.java b/src/RomanNumerals.java index b17cc25..da6d19b 100644 --- a/src/RomanNumerals.java +++ b/src/RomanNumerals.java @@ -2,19 +2,17 @@ public class RomanNumerals { - final String ONE = "I", FIVE = "V", TEN = "X", FIFTY = "L", HUNDRED = "C", FIVEHUNDRED= "D", THOUSAND = "M"; - //Trying out treeMap private TreeMap numbers; public RomanNumerals(){ numbers = new TreeMap(); - numbers.put(ONE, 1); - numbers.put(FIVE, 5); - numbers.put(TEN, 10); - numbers.put(FIFTY, 50); - numbers.put(HUNDRED, 100); - numbers.put(FIVEHUNDRED, 500); - numbers.put(THOUSAND, 1000); + 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) { @@ -22,84 +20,90 @@ public int convertToInteger(String romanNum) { return convertMultiple(romanNum); } - //If same number repeats - private boolean checkRepetition(String prev, String current){ - boolean repeat = false; - if(prev != null && prev.equals(current)){ - repeat = true; - } - else{ - repeat = false; + private int convertMultiple(String romanNum){ + String[] n = romanNum.split(""); + if(checkThreeTimes(n) && substractOkay(n)){ + return count(n); } - return repeat; + return 0; + } - //If smaller - private boolean checkIfSmallerNumber(String prev, String current){ - boolean ok = false; - if(prev != null){ - if(current.equals(ONE)){ - if(prev.equals(FIVE) || prev.equals(TEN)){ - ok = true; - } - } - else if(current.equals(TEN)){ - if(prev.equals(FIFTY) || prev.equals(HUNDRED)){ - ok = true; - } + //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 if(current.equals(HUNDRED)){ - if(prev.equals(FIVEHUNDRED) || prev.equals(THOUSAND)){ - ok = true; - } + else{ + c += numbers.get(n[i]); } + prev = n[i]; } - return ok; + return c; } - private int convertMultiple(String romanNum){ - int num = 0; - - String[] n = romanNum.split(""); - String prev = null; + private boolean substractOkay(String[] n){ + String prev = n[n.length - 1]; int times = 0; - boolean substract = false; - - for(int i = n.length - 1; i >= 0; i--){ - - if(n[i].equals(FIVE) || n[i].equals(FIFTY) || n[i].equals(FIVEHUNDRED)){ - if(checkRepetition(prev, n[i])){ - num = 0; - break; + 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++; } } - else{ - if(checkRepetition(prev, n[i])){ + 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(times == 3){ - break; - } } } - - - if(prev != null && numbers.get(prev) > numbers.get(n[i])){ - if(checkIfSmallerNumber(prev, n[i])){ - num -= numbers.get(n[i]); - + if(n[i].equals("V") || n[i].equals("L") || n[i].equals("D")){ + if(numbers.get(n[i]) < numbers.get(prev)){ + return false; } - substract = true; - } - else{ - num += numbers.get(n[i]); + if(!n[i].equals("I") || !n[i].equals("X") || !n[i].equals("C")){ + times = 0; + } + if(times > 1){ + return false; } - if(substract && checkRepetition(prev, n[i])){ - num = 0; - break; + 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 num; + return true; + } }