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/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 4ee4e64..a6f2c95 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -1,4 +1,67 @@ package io.zipcoder; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.HashMap; +import java.util.Map; + public class Problem6 { -} + //Calendar is a singleton and you HAVE to call .getInstance *protected constructor* + private Calendar calendar = Calendar.getInstance(); + private String hour; + private String minute; + + public String convertToMilitary(String input) { + //Separate Hour & Minute + calendar.set(Calendar.HOUR, Integer.parseInt(input.substring(0, input.indexOf(":")))); + calendar.set(Calendar.MINUTE, + //if it is a double digit hour then you index at 5 else index at 4 + Integer.parseInt(input.substring(input.indexOf(":") + 1 , calendar.get(Calendar.HOUR)>9?5:4))); + //If I want to check seconds, too - I would need to change the formatter to reflect HH:mm:ss + //calendar.set(Calendar.SECOND, 00); + + //Check if AM or PM + calendar.set(Calendar.AM_PM, input.substring(input.length() - 2).equalsIgnoreCase("am") ? Calendar.AM : Calendar.PM); + + SimpleDateFormat formatter = new SimpleDateFormat("HH:mm"); + return formatter.format(calendar.getTime()); + + } + + public Map minuteMap = new HashMap(){ + { + put(1, "One"); put(2, "Two"); put(3, "Three"); put(4, "Four"); put(5, "Five"); + put(6, "Six"); put(7, "Seven"); put(8, "Eight"); put(9, "Nine"); put(10, "Ten"); + put(11, "Eleven"); put(12, "Twelve"); put(13, "Thirteen"); put(14, "Fourteen"); + put(15, "Fifteen"); put(16, "Sixteen"); put(17, "Seventeen"); put(18, "Eighteen"); + put(19, "Nineteen"); put(20, "Twenty"); put(21, "Twenty One"); put(22, "Twenty Two"); + put(23, "Twenty Three"); put(24, "Twenty Four"); put(25, "Twenty Five");put(26, "Twenty Six"); + put(27, "Twenty Seven"); put(28, "Twenty Eight"); put(29, "Twenty Nine"); put(30, "Thirty"); + put(31, "Thirty One"); put(32, "Thirty Two"); put(33, "Thirty Three"); put(34, "Thirty Four"); + put(35, "Thirty Five"); put(36, "Thirty Six"); put(37, "Thirty Seven"); put(38, "Thirty Eight"); + put(39, "Thirty Nine"); put(40, "Forty"); put(41, "Forty One"); put(42, "Forty Two"); + put(43, "Forty Three"); put(44, "Forty Four"); put(45, "Forty Five");put(46, "Forty Six"); + put(47, "Forty Seven"); put(48, "Forty Eight"); put(49, "Forty Nine"); put(50, "Fifty"); + put(51, "Fifty One"); put(52, "Fifty Two"); put(53, "Fifty Three"); put(54, "Fifty Four"); + put(55, "Fifty Five");put(56, "Fifty Six"); put(57, "Fifty Seven"); put(58, "Fifty Eight"); put(59, "Fifty Nine"); + } + }; + + public Map hourMap = new HashMap(){ + { + put(1, "Zero One"); put(2, "Zero Two"); put(3, "Zero Three"); put(4, "Zero Four"); put(5, "Zero Five"); + put(6, "Zero Six"); put(7, "Zero Seven"); put(8, "Zero Eight"); put(9, "Zero Nine"); put(10, "Ten"); + put(11, "Eleven"); put(12, "Twelve"); put(13, "Thirteen"); put(14, "Fourteen"); + put(15, "Fifteen"); put(16, "Sixteen"); put(17, "Seventeen"); put(18, "Eighteen"); + put(19, "Nineteen"); put(20, "Twenty"); put(21, "Twenty One"); put(22, "Twenty Two"); + put(23, "Twenty Three"); put(24, "Twenty Four"); + } + }; + + public String parseToString(String input) { + String[] time = convertToMilitary(input).split(":"); + time[1] = minuteMap.get(calendar.get(Calendar.MINUTE)); + time[0] = hourMap.get(calendar.get(Calendar.HOUR_OF_DAY)); + return time[0] + " Hundred and " + time[1] + " Hours"; + } +} \ 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..4c55614 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -1,4 +1,147 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + public class Problem6Test { -} + + @Test + public void splitStringTest() { + // Given + Problem6 problem6 = new Problem6(); + String time = "1:30pm"; + + // When + String expected = "13:30"; + String actual = problem6.convertToMilitary(time); + + // Then + Assert.assertEquals(expected, actual); + } + + @Test + public void splitStringTest2() { + // Given + Problem6 problem6 = new Problem6(); + String time = "1:30am"; + + // When + String expected = "01:30"; + String actual = problem6.convertToMilitary(time); + + // Then + Assert.assertEquals(expected, actual); + } + + @Test + public void splitStringTest3() { + // Given + Problem6 problem6 = new Problem6(); + String time = "2:22pm"; + + // When + String expected = "14:22"; + String actual = problem6.convertToMilitary(time); + + // Then + Assert.assertEquals(expected, actual); + } + + @Test + public void splitStringTest4() { + // Given + Problem6 problem6 = new Problem6(); + String time = "2:11am"; + + // When + String expected = "02:11"; + String actual = problem6.convertToMilitary(time); + + // Then + Assert.assertEquals(expected, actual); + } + + @Test + public void splitStringTest5() { + // Given + Problem6 problem6 = new Problem6(); + String time = "10:02am"; + + // When + String expected = "10:02"; + String actual = problem6.convertToMilitary(time); + + // Then + Assert.assertEquals(expected, actual); + } + + @Test + public void parseToStringTest() { + // Given + Problem6 problem6 = new Problem6(); + String time = "1:30pm"; + + // When + String expected = "Thirteen Hundred and Thirty Hours"; + String actual = problem6.parseToString(time); + + // Then + Assert.assertEquals(expected, actual); + } + + @Test + public void parseToStringTest2() { + // Given + Problem6 problem6 = new Problem6(); + String time = "1:30am"; + + // When + String expected = "Zero One Hundred and Thirty Hours"; + String actual = problem6.parseToString(time); + + // Then + Assert.assertEquals(expected, actual); + } + + @Test + public void parseToStringTest3() { + // Given + Problem6 problem6 = new Problem6(); + String time = "2:22pm"; + + // When + String expected = "Fourteen Hundred and Twenty Two Hours"; + String actual = problem6.parseToString(time); + + // Then + Assert.assertEquals(expected, actual); + } + + @Test + public void parseToStringTest4() { + // Given + Problem6 problem6 = new Problem6(); + String time = "2:11am"; + + // When + String expected = "Zero Two Hundred and Eleven Hours"; + String actual = problem6.parseToString(time); + + // Then + Assert.assertEquals(expected, actual); + } + + @Test + public void parseToStringTest5() { + // Given + Problem6 problem6 = new Problem6(); + String time = "10:02am"; + + // When + String expected = "Ten Hundred and Two Hours"; + String actual = problem6.parseToString(time); + + // Then + Assert.assertEquals(expected, actual); + } +} \ No newline at end of file