From 158a08f012b0ea0a5121666175fd9f3d6477e2da Mon Sep 17 00:00:00 2001 From: Kibret Tecle Date: Sat, 14 Apr 2018 15:01:23 -0400 Subject: [PATCH 1/2] Done --- pom.xml | 12 ++ src/main/java/io/zipcoder/Problem6.java | 115 ++++++++++++++++++++ src/test/java/io/zipcoder/Problem6Test.java | 91 ++++++++++++++++ 3 files changed, 218 insertions(+) diff --git a/pom.xml b/pom.xml index 5fd8efa..cfd1ef3 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ io.zipcoder InterviewProblem5 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.6 + 1.6 + + + + diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 4ee4e64..e49110e 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -1,4 +1,119 @@ package io.zipcoder; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + public class Problem6 { + + + Map myMap = new HashMap(); + String[] numberString = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", + "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Ninteen", "Twenty"}; + + public Map populateMap() { + for (int i = 0; i < numberString.length; i++) { + myMap.put(i, numberString[i]); + } + myMap.put(21, "Twenty One"); + myMap.put(22, "Twenty Two"); + myMap.put(23, "Twenty Three"); + myMap.put(30, "Thirty"); + myMap.put(40, "Forty"); + myMap.put(50, "Fifty"); + return myMap; + } + + public String militaryTimeConverter(String input) { + myMap = populateMap(); + String outPut = ""; + String inputTime=""; + if(input.contains("am")){ + inputTime += input.replace("am", ""); + }else{ + inputTime+=input.replace("pm",""); + } + + String[] inputTimeArray = inputTime.split(":"); + int[] convertedToInt = stringToIntConverter(inputTimeArray); + if(input.contains("am")){ + outPut+=amTimeFormat(convertedToInt[0],convertedToInt[1],myMap); + }else{ + outPut+=pmTimeFormat(convertedToInt[0],convertedToInt[1],myMap); + } + + return outPut; + } + + public int[] stringToIntConverter(String[] input) { + int[] intArray = new int[0]; + for (int i = 0; i < input.length; i++) { + intArray = Arrays.copyOf(intArray, intArray.length + 1); + intArray[i] = Integer.parseInt(String.valueOf(input[i])); + } + return intArray; + } + + public String pmTimeFormat(int inputHour,int inputMinutes, Map inputMap) { + String outPut = ""; + int divisionByTen = inputMinutes / 10; + int modulus = inputMinutes % 10; + if (inputHour == 12) { + outPut += inputMap.get(inputHour); + } else { + outPut += inputMap.get(12 + inputHour); + } + + if (inputMinutes < 24) { + outPut += " Hundred "; + if(inputMinutes<10){ + outPut+="Zero "; + } + outPut += myMap.get(inputMinutes); + outPut += " Hours"; + } else { + outPut += " Hundred and "; + outPut += myMap.get(10 * divisionByTen); + if (modulus != 0) { + outPut += " " + myMap.get(modulus); + } + outPut += " Hours"; + } + + return outPut; + } + + public String amTimeFormat(int inputHour,int inputMinute, Map inputMap) { + String outPut = ""; + int divisionByTen = inputMinute / 10; + int modulus = inputMinute% 10; + if (inputHour< 10) { + outPut += "Zero "; + outPut+= inputMap.get(inputHour); + }else if(inputHour==12){ + outPut+="Zero "; + outPut+=inputMap.get(0); + }else{ + outPut += inputMap.get(inputHour); + } + if (inputMinute< 24) { + outPut += " Hundred and "; + if(inputMinute<10){ + outPut+="Zero "; + } + outPut += myMap.get(inputMinute); + outPut += " Hours"; + } else { + outPut += " Hundred and "; + outPut += myMap.get(10 * (divisionByTen)); + outPut += " "; + if (modulus != 0) { + outPut += myMap.get(modulus); + } + outPut += " Hours"; + + } + return outPut; + } + } diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index d262e88..98150ca 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -1,4 +1,95 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + public class Problem6Test { + + @Test + public void testMilitaryTimeConverter1(){ + Problem6 test = new Problem6(); + String input = "1:30pm"; + String expected = "Thirteen Hundred and Thirty Hours"; + + String actual = test.militaryTimeConverter(input); + + System.out.println(actual); + Assert.assertEquals(expected,actual); + } + @Test + public void testMilitaryTimeConverter2(){ + Problem6 test = new Problem6(); + String input = "7:30pm"; + String expected = "Ninteen Hundred and Thirty Hours"; + + String actual = test.militaryTimeConverter(input); + + System.out.println(actual); + Assert.assertEquals(expected,actual); + } + @Test + public void testMilitaryTimeConverter3(){ + Problem6 test = new Problem6(); + String input = "2:11am"; + String expected = "Zero Two Hundred and Eleven Hours"; + + String actual = test.militaryTimeConverter(input); + + System.out.println(actual); + Assert.assertEquals(expected,actual); + } + @Test + public void testMilitaryTimeConverter4(){ + Problem6 test = new Problem6(); + String input = "11:55pm"; + String expected = "Twenty Three Hundred and Fifty Five Hours"; + + String actual = test.militaryTimeConverter(input); + + System.out.println(actual); + Assert.assertEquals(expected,actual); + } + + @Test + public void testMilitaryTimeConverter5(){ + Problem6 test = new Problem6(); + String input = "1:22am"; + String expected = "Zero One Hundred and Twenty Two Hours"; + + String actual = test.militaryTimeConverter(input); + + System.out.println(actual); + Assert.assertEquals(expected,actual); + } + + @Test + public void testMilitaryTimeConverter6(){ + Problem6 test = new Problem6(); + String input = "12:00am"; + String expected = "Zero Zero Hundred and Zero Zero Hours"; + + String actual = test.militaryTimeConverter(input); + + System.out.println(actual); + Assert.assertEquals(expected,actual); + } + + @Test + public void testMilitaryTimeConverter7(){ + Problem6 test = new Problem6(); + String input = "12:00pm"; + String expected = "Twelve Hundred Zero Zero Hours"; + + String actual = test.militaryTimeConverter(input); + + System.out.println(actual); + Assert.assertEquals(expected,actual); + } + + + + + + + } From 0cf84b5f26aae3a4cef92c44972cc85e1af9991d Mon Sep 17 00:00:00 2001 From: Kibret Tecle Date: Mon, 16 Apr 2018 21:35:01 -0400 Subject: [PATCH 2/2] added a minuteFormat method and refactored the code --- src/main/java/io/zipcoder/Problem6.java | 55 +++++++++------------ src/test/java/io/zipcoder/Problem6Test.java | 16 +----- 2 files changed, 23 insertions(+), 48 deletions(-) diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index e49110e..e3b5cdc 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -37,10 +37,11 @@ public String militaryTimeConverter(String input) { String[] inputTimeArray = inputTime.split(":"); int[] convertedToInt = stringToIntConverter(inputTimeArray); if(input.contains("am")){ - outPut+=amTimeFormat(convertedToInt[0],convertedToInt[1],myMap); + outPut+=amTimeFormat(convertedToInt[0],myMap); }else{ - outPut+=pmTimeFormat(convertedToInt[0],convertedToInt[1],myMap); + outPut+=pmTimeFormat(convertedToInt[0],myMap); } + outPut+=minuteFormat(convertedToInt[1]); return outPut; } @@ -54,48 +55,36 @@ public int[] stringToIntConverter(String[] input) { return intArray; } - public String pmTimeFormat(int inputHour,int inputMinutes, Map inputMap) { + public String pmTimeFormat(int inputHour,Map inputMap) { String outPut = ""; - int divisionByTen = inputMinutes / 10; - int modulus = inputMinutes % 10; if (inputHour == 12) { outPut += inputMap.get(inputHour); } else { outPut += inputMap.get(12 + inputHour); } - - if (inputMinutes < 24) { - outPut += " Hundred "; - if(inputMinutes<10){ - outPut+="Zero "; - } - outPut += myMap.get(inputMinutes); - outPut += " Hours"; - } else { - outPut += " Hundred and "; - outPut += myMap.get(10 * divisionByTen); - if (modulus != 0) { - outPut += " " + myMap.get(modulus); - } - outPut += " Hours"; - } - return outPut; } - public String amTimeFormat(int inputHour,int inputMinute, Map inputMap) { + public String amTimeFormat(int inputHour,Map inputMap) { String outPut = ""; - int divisionByTen = inputMinute / 10; - int modulus = inputMinute% 10; - if (inputHour< 10) { + if (inputHour < 10) { outPut += "Zero "; - outPut+= inputMap.get(inputHour); - }else if(inputHour==12){ - outPut+="Zero "; - outPut+=inputMap.get(0); - }else{ + outPut += inputMap.get(inputHour); + } else if (inputHour == 12) { + outPut += "Zero "; + outPut += inputMap.get(0); + } else { outPut += inputMap.get(inputHour); } + return outPut; + + } + + + public String minuteFormat(int inputMinute){ + String outPut=""; + int divisionByTen = inputMinute / 10; + int modulus = inputMinute % 10; if (inputMinute< 24) { outPut += " Hundred and "; if(inputMinute<10){ @@ -108,9 +97,9 @@ public String amTimeFormat(int inputHour,int inputMinute, Map i outPut += myMap.get(10 * (divisionByTen)); outPut += " "; if (modulus != 0) { - outPut += myMap.get(modulus); + outPut += myMap.get(modulus) +" "; } - outPut += " Hours"; + outPut += "Hours"; } return outPut; diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index 98150ca..14ad111 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -12,8 +12,6 @@ public void testMilitaryTimeConverter1(){ String expected = "Thirteen Hundred and Thirty Hours"; String actual = test.militaryTimeConverter(input); - - System.out.println(actual); Assert.assertEquals(expected,actual); } @Test @@ -23,8 +21,6 @@ public void testMilitaryTimeConverter2(){ String expected = "Ninteen Hundred and Thirty Hours"; String actual = test.militaryTimeConverter(input); - - System.out.println(actual); Assert.assertEquals(expected,actual); } @Test @@ -34,8 +30,6 @@ public void testMilitaryTimeConverter3(){ String expected = "Zero Two Hundred and Eleven Hours"; String actual = test.militaryTimeConverter(input); - - System.out.println(actual); Assert.assertEquals(expected,actual); } @Test @@ -45,8 +39,6 @@ public void testMilitaryTimeConverter4(){ String expected = "Twenty Three Hundred and Fifty Five Hours"; String actual = test.militaryTimeConverter(input); - - System.out.println(actual); Assert.assertEquals(expected,actual); } @@ -57,8 +49,6 @@ public void testMilitaryTimeConverter5(){ String expected = "Zero One Hundred and Twenty Two Hours"; String actual = test.militaryTimeConverter(input); - - System.out.println(actual); Assert.assertEquals(expected,actual); } @@ -69,8 +59,6 @@ public void testMilitaryTimeConverter6(){ String expected = "Zero Zero Hundred and Zero Zero Hours"; String actual = test.militaryTimeConverter(input); - - System.out.println(actual); Assert.assertEquals(expected,actual); } @@ -78,11 +66,9 @@ public void testMilitaryTimeConverter6(){ public void testMilitaryTimeConverter7(){ Problem6 test = new Problem6(); String input = "12:00pm"; - String expected = "Twelve Hundred Zero Zero Hours"; + String expected = "Twelve Hundred and Zero Zero Hours"; String actual = test.militaryTimeConverter(input); - - System.out.println(actual); Assert.assertEquals(expected,actual); }