diff --git a/src/main/java/io/zipcoder/MilitaryTime.java b/src/main/java/io/zipcoder/MilitaryTime.java new file mode 100644 index 0000000..a9f4c9a --- /dev/null +++ b/src/main/java/io/zipcoder/MilitaryTime.java @@ -0,0 +1,105 @@ +package io.zipcoder; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class MilitaryTime { + + private static String[] timeWords = { + "Zero", + "One", + "Two", + "Three", + "Four", + "Five", + "Six", + "Seven", + "Eight", + "Nine", + "Ten", + "Eleven", + "Twelve", + "Thirteen", + "Fourteen", + "Fifteen", + "Sixteen", + "Seventeen", + "Eighteen", + "Nineteen", + }; + + private static String[] tensWords = { + "Twenty", + "Thirty", + "Forty", + "Fifty" + }; + + public String convertToMilitaryTime(String time) throws Exception{ + time = convertNumerically(time); + String[] splitTime = time.split(":"); + Integer hours = Integer.parseInt(splitTime[0]); + Integer minutes = Integer.parseInt(splitTime[1]); + + String output = new StringBuilder() + .append(convertHours(hours)) + .append(convertMinutes(minutes)) + .toString(); + + return output; + } + + public String convertHours(Integer hours) { + StringBuilder output = new StringBuilder(); + if(hours >= 10 && hours < 20) { + output.append(timeWords[hours] + " Hundred and "); + } else if(hours < 10) { + output.append(timeWords[hours / 10]); + output.append(" "); + output.append(timeWords[hours % 10] + " Hundred and "); + } else if(hours > 19) { + output.append(tensWords[(hours / 10) - 2]); + output.append(" "); + output.append(timeWords[hours % 10] + " Hundred and "); + } + return output.toString(); + } + + public String convertMinutes(Integer minutes) { + StringBuilder output = new StringBuilder(); + if(minutes > 10 && minutes < 20) { + output.append(timeWords[minutes] + " Hours"); + } else if(minutes < 10) { + output.append(timeWords[minutes / 10]); + output.append(" "); + output.append(timeWords[minutes % 10] + " Hours"); + } else if(minutes > 19) { + output.append(tensWords[(minutes / 10) - 2]); + output.append(" "); + if(minutes % 10 != 0) { + output.append(timeWords[minutes % 10]); + output.append(" "); + } + output.append("Hours"); + } + return output.toString(); + } + + public String convertNumerically(String time) throws Exception { + SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm"); + SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a"); + Date date = parseFormat.parse(formatTime(time)); + return displayFormat.format(date); + } + + public String formatTime(String time) { + String[] splitTime = time.split(""); + for(int i = splitTime.length - 1; i > splitTime.length - 4; i--) { + if(i == splitTime.length - 3) { + splitTime[i] = splitTime[i] + " "; + } + splitTime[i] = splitTime[i].toUpperCase(); + } + return String.join("", splitTime); + } +} diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java deleted file mode 100644 index 4ee4e64..0000000 --- a/src/main/java/io/zipcoder/Problem6.java +++ /dev/null @@ -1,4 +0,0 @@ -package io.zipcoder; - -public class Problem6 { -} diff --git a/src/test/java/io/zipcoder/MilitaryTimeTest.java b/src/test/java/io/zipcoder/MilitaryTimeTest.java new file mode 100644 index 0000000..a02ad38 --- /dev/null +++ b/src/test/java/io/zipcoder/MilitaryTimeTest.java @@ -0,0 +1,156 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class MilitaryTimeTest { + + private MilitaryTime militaryTime; + + @Before + public void setup() { + militaryTime = new MilitaryTime(); + } + + @Test + public void formatTest() { + String input = "11:30pm"; + String expected = "11:30 PM"; + + String actual = militaryTime.formatTime(input); + + Assert.assertEquals(expected, actual); + } + + @Test + public void convertNumericallyPMTest() throws Exception{ + String input = "1:30pm"; + String expected = "13:30"; + + String actual = militaryTime.convertNumerically(input); + + Assert.assertEquals(expected, actual); + } + + @Test + public void convertNumericallyAMTest() throws Exception { + String input = "1:30am"; + String expected = "01:30"; + + String actual = militaryTime.convertNumerically(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void convertHoursTest() { + Integer input = 11; + String expected = "Eleven Hundred and "; + + String actual = militaryTime.convertHours(input); + + Assert.assertEquals(expected, actual); + } + + @Test + public void convertHoursTest2() { + Integer input = 23; + String expected = "Twenty Three Hundred and "; + + String actual = militaryTime.convertHours(input); + + Assert.assertEquals(expected, actual); + } + + @Test + public void convertHoursTest3() { + Integer input = 58; + String expected = "Fifty Eight Hundred and "; + + String actual = militaryTime.convertHours(input); + + Assert.assertEquals(expected, actual); + } + + @Test + public void convertHoursTest4() { + Integer input = 3; + String expected = "Zero Three Hundred and "; + + String actual = militaryTime.convertHours(input); + + Assert.assertEquals(expected, actual); + } + + @Test + public void convertMinutesTest() { + Integer input = 3; + String expected = "Zero Three Hours"; + + String actual = militaryTime.convertMinutes(input); + + Assert.assertEquals(expected, actual); + } + + @Test + public void convertMinutesTest2() { + Integer input = 14; + String expected = "Fourteen Hours"; + + String actual = militaryTime.convertMinutes(input); + + Assert.assertEquals(expected, actual); + } + + @Test + public void militaryTimePM() throws Exception { + String input = "1:30pm"; + String expected = "Thirteen Hundred and Thirty Hours"; + + String actual = militaryTime.convertToMilitaryTime(input); + + Assert.assertEquals(expected, actual); + } + + @Test + public void militaryTimePM2() throws Exception { + String input = "2:22pm"; + String expected = "Fourteen Hundred and Twenty Two Hours"; + + String actual = militaryTime.convertToMilitaryTime(input); + + Assert.assertEquals(expected, actual); + } + + @Test + public void militaryTimeAM() throws Exception { + String input = "1:30am"; + String expected = "Zero One Hundred and Thirty Hours"; + + String actual = militaryTime.convertToMilitaryTime(input); + + Assert.assertEquals(expected, actual); + } + + @Test + public void militaryTimeAM2() throws Exception { + String input = "2:11am"; + String expected = "Zero Two Hundred and Eleven Hours"; + + String actual = militaryTime.convertToMilitaryTime(input); + + Assert.assertEquals(expected, actual); + } + + @Test + public void militaryTimeAM3() throws Exception { + String input = "10:02am"; + String expected = "Ten Hundred and Zero Two Hours"; + + String actual = militaryTime.convertToMilitaryTime(input); + + Assert.assertEquals(expected, actual); + } + +} diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java deleted file mode 100644 index d262e88..0000000 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ /dev/null @@ -1,4 +0,0 @@ -package io.zipcoder; - -public class Problem6Test { -}