From 5906d8b3046be6edb75d32b0ac623def045384bc Mon Sep 17 00:00:00 2001 From: Eric Barnaba Date: Fri, 13 Apr 2018 09:38:20 -0400 Subject: [PATCH 1/3] end unit tests written --- src/main/java/io/zipcoder/Problem6.java | 5 ++ src/test/java/io/zipcoder/Problem6Test.java | 76 +++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 4ee4e64..ff75a49 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -1,4 +1,9 @@ package io.zipcoder; public class Problem6 { + + public String timeConverter(String time){ + return null; + } + } diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index d262e88..e6ad515 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -1,4 +1,80 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + public class Problem6Test { + + + @Test + public void timeTestOne(){ + //Given + Problem6 tester = new Problem6(); + String input = "1:30pm"; + + //When + String expected = "Thirteen Hundred and Thirty Hours"; + String actual = tester.timeConverter(input); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void timeTestTwo(){ + //Given + Problem6 tester = new Problem6(); + String input = "1:30am"; + + //When + String expected = "Zero One Hundred and Thirty Hours"; + String actual = tester.timeConverter(input); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void timeTestThree(){ + //Given + Problem6 tester = new Problem6(); + String input = "2:22pm"; + + //When + String expected = "Fourteen Hundred and Twenty Two Hours"; + String actual = tester.timeConverter(input); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void timeTestFour(){ + //Given + Problem6 tester = new Problem6(); + String input = "2:11am"; + + //When + String expected = "Zero Two Hundred and Eleven Hours"; + String actual = tester.timeConverter(input); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void timeTestFive(){ + //Given + Problem6 tester = new Problem6(); + String input = "10:02am"; + + //When + String expected = "Ten Hundred Zero Two Hours"; + String actual = tester.timeConverter(input); + + //Then + Assert.assertEquals(expected,actual); + } + + } From 325127bda6e1f9d9c3976ced7a3e79bc329703cc Mon Sep 17 00:00:00 2001 From: Eric Barnaba Date: Fri, 13 Apr 2018 09:56:29 -0400 Subject: [PATCH 2/3] TimeConverter unit tests written --- src/main/java/io/zipcoder/TimeConverter.java | 13 ++++ .../java/io/zipcoder/TimeConverterTest.java | 60 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/main/java/io/zipcoder/TimeConverter.java create mode 100644 src/test/java/io/zipcoder/TimeConverterTest.java diff --git a/src/main/java/io/zipcoder/TimeConverter.java b/src/main/java/io/zipcoder/TimeConverter.java new file mode 100644 index 0000000..acde20c --- /dev/null +++ b/src/main/java/io/zipcoder/TimeConverter.java @@ -0,0 +1,13 @@ +package io.zipcoder; + +public class TimeConverter { + + public static String[] splitTime(String time){ + return null; + } + + public static String[] convertTime(String [] input){ + return null; + } + +} diff --git a/src/test/java/io/zipcoder/TimeConverterTest.java b/src/test/java/io/zipcoder/TimeConverterTest.java new file mode 100644 index 0000000..43e35ee --- /dev/null +++ b/src/test/java/io/zipcoder/TimeConverterTest.java @@ -0,0 +1,60 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Test; + +public class TimeConverterTest { + + @Test + public void timeSplitTestOne(){ + //Given + String input = "1:30pm"; + + //When + String [] expected = {"01","30","pm"}; + String [] actual = TimeConverter.splitTime(input); + + //Then + Assert.assertArrayEquals(expected,actual); + } + + @Test + public void timeSplitTestTwo(){ + //Given + String input = "12:30pm"; + + //When + String [] expected = {"12","30","pm"}; + String [] actual = TimeConverter.splitTime(input); + + //Then + Assert.assertArrayEquals(expected,actual); + } + + @Test + public void timeConvertTestOne() { + //Given + String [] input = {"01","30","pm"}; + + //When + String [] expected = {"13","30"}; + String [] actual = TimeConverter.convertTime(input); + + //Then + Assert.assertArrayEquals(expected,actual); + } + + @Test + public void timeConvertTestTwo() { + //Given + String [] input = {"01","30","am"}; + + //When + String [] expected = {"01","30"}; + String [] actual = TimeConverter.convertTime(input); + + //Then + Assert.assertArrayEquals(expected,actual); + } + +} From 1a5920239e13ba40a0fcbfe30651de02acb2afe4 Mon Sep 17 00:00:00 2001 From: Eric Barnaba Date: Fri, 13 Apr 2018 10:47:18 -0400 Subject: [PATCH 3/3] finished --- pom.xml | 12 +++ src/main/java/io/zipcoder/Militarizer.java | 74 +++++++++++++++++++ src/main/java/io/zipcoder/Problem6.java | 7 +- src/main/java/io/zipcoder/TimeConverter.java | 23 +++++- .../java/io/zipcoder/MilitarizerTest.java | 56 ++++++++++++++ src/test/java/io/zipcoder/Problem6Test.java | 2 +- 6 files changed, 170 insertions(+), 4 deletions(-) create mode 100644 src/main/java/io/zipcoder/Militarizer.java create mode 100644 src/test/java/io/zipcoder/MilitarizerTest.java diff --git a/pom.xml b/pom.xml index 5fd8efa..7584aab 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.7 + 1.7 + + + + diff --git a/src/main/java/io/zipcoder/Militarizer.java b/src/main/java/io/zipcoder/Militarizer.java new file mode 100644 index 0000000..79e459b --- /dev/null +++ b/src/main/java/io/zipcoder/Militarizer.java @@ -0,0 +1,74 @@ +package io.zipcoder; + +import java.util.HashMap; + +public class Militarizer { + private HashMap teens = new HashMap<>(); + private HashMap firstDigits = new HashMap<>(); + private HashMap secondDigits = new HashMap<>(); + + public Militarizer(){ + buildTeensMap(); + buildFirstDigitsMap(); + buildSecondDigitsMap(); + } + + + public String speaker(String input){ + if(teens.containsKey(input)){ + return teens.get(input); + } + else if (input.charAt(1) == '0'){ + return firstDigits.get(input.substring(0,1)); + } + else{ + return firstDigits.get(input.substring(0,1)) + " " + secondDigits.get(input.substring(1)); + } + } + + + + private void buildTeensMap(){ + teens.put("10", "Ten"); + teens.put("11", "Eleven"); + teens.put("12", "Twelve"); + teens.put("13", "Thirteen"); + teens.put("14", "Fourteen"); + teens.put("15", "Fifteen"); + teens.put("16", "Sixteen"); + teens.put("17", "Seventeen"); + teens.put("18", "Eighteen"); + teens.put("19", "Nineteen"); + } + + private void buildFirstDigitsMap(){ + firstDigits.put("0", "Zero"); + firstDigits.put("2","Twenty"); + firstDigits.put("3","Thirty"); + firstDigits.put("4","Forty"); + firstDigits.put("5","Fifty"); + } + + private void buildSecondDigitsMap(){ + secondDigits.put("0", ""); + secondDigits.put("1", "One"); + secondDigits.put("2", "Two"); + secondDigits.put("3", "Three"); + secondDigits.put("4", "Four"); + secondDigits.put("5", "Five"); + secondDigits.put("6", "Six"); + secondDigits.put("7", "Seven"); + secondDigits.put("8", "Eight"); + secondDigits.put("9", "Nine"); + + + + + + + + + + } + +} diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index ff75a49..7f84345 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -3,7 +3,12 @@ public class Problem6 { public String timeConverter(String time){ - return null; + Militarizer converter = new Militarizer(); + + String [] stepOne = TimeConverter.splitTime(time); + String [] stepTwo = TimeConverter.convertTime(stepOne); + + return converter.speaker(stepTwo[0]) + " Hundred and " + converter.speaker(stepTwo[1]) + " Hours"; } } diff --git a/src/main/java/io/zipcoder/TimeConverter.java b/src/main/java/io/zipcoder/TimeConverter.java index acde20c..fe67604 100644 --- a/src/main/java/io/zipcoder/TimeConverter.java +++ b/src/main/java/io/zipcoder/TimeConverter.java @@ -1,13 +1,32 @@ package io.zipcoder; +import static java.lang.Integer.parseInt; + public class TimeConverter { public static String[] splitTime(String time){ - return null; + String[] stepOne = time.split(":"); + String[] stepTwo = {stepOne[1].substring(0,2), stepOne[1].substring(2)}; + String[] output = {stepOne[0],stepTwo[0],stepTwo[1]}; + if(output[0].length()==1){ + output[0] = "0" + output[0]; + } + + return output; } public static String[] convertTime(String [] input){ - return null; + if(input[2].equals("pm")){ + Integer workingInt = parseInt(input[0]); + workingInt +=12; + String[] output = {workingInt.toString(),input[1]}; + return output; + } + + else{ + String[] output = {input[0],input[1]}; + return output; + } } } diff --git a/src/test/java/io/zipcoder/MilitarizerTest.java b/src/test/java/io/zipcoder/MilitarizerTest.java new file mode 100644 index 0000000..cea9d90 --- /dev/null +++ b/src/test/java/io/zipcoder/MilitarizerTest.java @@ -0,0 +1,56 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class MilitarizerTest { + + Militarizer test; + + @Before + public void setup(){ + test = new Militarizer(); + } + + + @Test + public void SpeakerTest(){ + //Given + String input = "11"; + + //When + String expected = "Eleven"; + String actual = test.speaker(input); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void SpeakerTestTwo(){ + //Given + String input = "05"; + + //When + String expected = "Zero Five"; + String actual = test.speaker(input); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void SpeakerTestThree(){ + //Given + String input = "30"; + + //When + String expected = "Thirty"; + String actual = test.speaker(input); + + //Then + Assert.assertEquals(expected,actual); + } + +} diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index e6ad515..6808781 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -69,7 +69,7 @@ public void timeTestFive(){ String input = "10:02am"; //When - String expected = "Ten Hundred Zero Two Hours"; + String expected = "Ten Hundred and Zero Two Hours"; String actual = tester.timeConverter(input); //Then