diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 4ee4e64..d3ab32e 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -1,4 +1,70 @@ package io.zipcoder; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class Problem6 { -} + + public String convertToMilitaryTime(String input) throws Exception { + SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm"); + SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mma"); + Date date = parseFormat.parse(input); + return convertToWordMilitaryTime(displayFormat.format(date)); + } + + public String convertToWordMilitaryTime(String format) { + StringBuilder sb = new StringBuilder(); + Map hours = new HashMap(); //need to populate my map more efficently.... + hours.put("01", "Zero One Hundred"); + hours.put("02", "Two"); + hours.put("03", "Three"); + hours.put("04", "Four"); + hours.put("05", "Five"); + hours.put("06", "Six"); + hours.put("07", "Seven"); + hours.put("08", "Eight"); + hours.put("09", "Nine"); + hours.put("10", "Ten"); + hours.put("11", "Eleven"); + hours.put("12", "Twelve"); + //need 13 to 24....plus will get annoying + + Map mins = new HashMap(); + mins.put("00", "Zero Zero"); + mins.put("01", "Zero One Hours"); + mins.put("02", "Zero Two Hours"); + mins.put("03", "Zero Three Hours"); + mins.put("04", "Zero Four Hours"); + mins.put("05", "Zero Five Hours"); + mins.put("06", "Zero Six Hours"); + mins.put("07", "Zero Seven Hours"); + mins.put("08", "Zero Eight Hours"); + mins.put("09", "Zero Nine Hours"); + mins.put("10", "Zero Ten Hours"); + mins.put("11", "Eleven Hours"); + mins.put("12", "Twelve Hours"); + //etc....but this wil get annoying + + String regex = "^([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])$"; + Pattern regexPattern = Pattern.compile(regex); + Matcher regexMatcher = regexPattern.matcher(format); + if (regexMatcher.find()) { + for (Map.Entry entryHour : hours.entrySet()) { + if (regexMatcher.group(1).equals(entryHour.getKey())) { + sb.append(entryHour.getValue() + " and"); + } + } + //does not want to append this part.... + for (Map.Entry entryMin: mins.entrySet()) { + if (regexMatcher.group(2).equals(entryMin.getKey())) { + sb.append(entryMin.getValue() + "Hours"); + } + } + } + return sb.toString(); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index d262e88..7e54cfd 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -1,4 +1,28 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + public class Problem6Test { + + Problem6 prob6; + @Before + public void setUp() { + prob6 = new Problem6(); + } + + @Test + public void convertToMilitaryTimeZeroHundredsTest() throws Exception { + String output = "Zero One Hundred and Thirty Hours"; + String actual = prob6.convertToMilitaryTime("1:30am"); + Assert.assertEquals(output, actual); + } + + @Test + public void convertToMilitaryTimeTenHundredsTest() throws Exception { + String output = "Thirteen Hundred and Thirty Hours"; + String actual = prob6.convertToMilitaryTime("1:30pm"); + Assert.assertEquals(output, actual); + } }