diff --git a/src/main/java/io/zipcoder/MilitaryHour.java b/src/main/java/io/zipcoder/MilitaryHour.java new file mode 100644 index 0000000..d9e6282 --- /dev/null +++ b/src/main/java/io/zipcoder/MilitaryHour.java @@ -0,0 +1,21 @@ +package io.zipcoder; + +public enum MilitaryHour { + + Zero (0), One (1), Two (2), Three (3), Four (4), + Five (5), Six (6), Seven (7), Eight (8),Nine (9), + Ten (10), Eleven (11), Twelve (12), Thirteen (13), Fourteen (14), + Fifteen (15), Sixteen (16), Seventeen (17), Eighteen (18), Nineteen (19), + Twenty (20), Twentyone (21), Twentytwo (22), Twentythree (23), Twentyfour(24); + + + Integer hourToMatch; + + MilitaryHour(Integer hourToMatch){ + + this.hourToMatch = hourToMatch; + } +} + + + diff --git a/src/main/java/io/zipcoder/MilitaryMinute.java b/src/main/java/io/zipcoder/MilitaryMinute.java new file mode 100644 index 0000000..d35a05e --- /dev/null +++ b/src/main/java/io/zipcoder/MilitaryMinute.java @@ -0,0 +1,27 @@ +package io.zipcoder; + +public enum MilitaryMinute { + + + Zero (0), One (1), Two (2), Three (3), Four (4), + Five (5), Six (6), Seven (7), Eight (8),Nine (9), + Ten (10), Eleven (11), Twelve (12), Thirteen (13), Fourteen (14), + Fifteen (15), Sixteen (16), Seventeen (17), Eighteen (18), Nineteen (19), + Twenty (20), Twentyone (21), Twentytwo (22), Twentythree (23), Twentyfour(24), + Twentyfive (25), Twentysix (26), Twentyseven (27), Twentyeight (28), Twentynine (29), + Thirty (30), Thirtyone (31), Thirtytwo (32), Thirtythree (33), Thirtyfour (34), + Thirtyfive (35), Thirtysix (36), Thirtyseven (37), Thirtyeight (38), Thirtynine (39), + Forty (40), Fortyone (41), Fortytwo (42), Fortythree (43), Fortyfour (44), + Fortyfive (45), Fortysix (46), Fortyseven (47), Fortyeight (48),Fortynine (49), + Fifty (50), Fiftyone (51), Fiftytwo (52), Fiftythree (53), Fiftyfour (54), + Fiftyfive (55), Fiftysix (56), Fiftyseven (57), Fiftyeight (58),Fiftynine (59); + + Integer minutesToMatch; + + MilitaryMinute(Integer minutesToMatch){ + + this.minutesToMatch = minutesToMatch; + } + + +} diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 4ee4e64..6b3be79 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -1,4 +1,120 @@ package io.zipcoder; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; +import java.util.Date; + public class Problem6 { -} + + // Given a time given as a String in numerical format, convert that value into its military time phrase. + +/* First Breakdown: + + 1:30 pm to Thirteen Hundred and Thirty Hours + 1:30 am to Zero One Hundred and Thirty Hours + 2:22 pm to Fourteen Hundred and Twenty Two Hours + + 1 - one o'clock (1:00) when it is "am" OR thirteen o clock (13:00) when it is a "pm" + + : - convert to "and" + 30 - minutes, only output name for the number + pm - indicates if one(1) or thirteen(13), or another two(2) or fourteenth(14) + +add "Hours" at the end of the output string + +string to int + + Second Breakdown: +found SimpleDateFormat class + +1:30 pm to 13:30 - Thirteen Hundred and Thirty Hours +if "am" do nothing to it just make it into and int +1:30 am to 1:30 - Zero One Hundred and Thirty Hours +2:22 pm to 14:22 - Fourteen Hundred and Twenty Two Hours +2:11 am to 2:11 - Zero Two Hundred and Eleven Hours + +convert from standard to military time 1:30 pm to 13:30 +//"13:30" to "13" +then convert military time in numbers to word 1 to Thirteen , 30 to Thirty +split number in front of : into a new variable String hour; +the number after the split put into a new variable String minutes; + + String hundredWord = "Hundred +then add "and" after the words split String andWord = "and" or just concatenate later +add "Hours" at the end of the output string String wordHour = "Hours" + + + output: Thirteen Hundred and Thirty Hours +*/ + + public static void main(String[] args) throws Exception { + + System.out.println(convertToMilitaryTime("1:30 pm")); + //System.out.println(convertToMilitaryTimeInWords(convertToMilitaryTime("1:30 pm"))); + } + + public static String convertToMilitaryTime(String standardTime) { + DateFormat parseStandardTime = new SimpleDateFormat("hh:mm a"); + Date d = null; + + try { + d = parseStandardTime.parse(standardTime); + } catch (ParseException e) { + e.printStackTrace(); + } + + DateFormat displayMilitaryTime = new SimpleDateFormat("HH:mm"); + String militaryOutputString = displayMilitaryTime.format(d); + return militaryOutputString; + } + + public static int parseMilitaryHour(String militaryOutputString) { + + String hourString = militaryOutputString.substring(0, 2); + int hourInt = Integer.parseInt(hourString); + return hourInt; + } + + public static int parseMilitaryMinutes(String militaryOutputString) { + String minutesString = militaryOutputString.substring(3); + int minutesInt = Integer.parseInt(minutesString); + return minutesInt; + } + + public static String convertHourToWord(int hourInt) { + + String hourWord = ""; + + for (MilitaryHour m : MilitaryHour.values()) + if (hourInt == m.hourToMatch) + hourWord = m.toString(); + + return hourWord; + } + + + public static String convertMinuteToWord(int minutesInt) { + String minuteWord = ""; + + for (MilitaryMinute m : MilitaryMinute.values()) + if (minutesInt == m.minutesToMatch) + minuteWord = m.toString(); + + return minuteWord; + } + + + public static String convertToMilitaryTimeInWords(String hourWord, String minuteWord) { + String hundredWord = "Hundred"; + String andWord = "and"; + String hoursWord = "Hours"; + + return hourWord + " " + hundredWord + " " + andWord + " " + minuteWord + " " + hoursWord; + + + } + +} \ 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..2bb0ebf 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -1,4 +1,102 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.theories.suppliers.TestedOn; + public class Problem6Test { -} + + private Problem6 problem; + + @Before + public void setUp() { + problem = new Problem6(); + } + + @Test + public void convertToMilitaryTimeTestOne() { + String expected = "13:30"; + String actual = problem.convertToMilitaryTime("1:30 pm"); + Assert.assertEquals(expected, actual); + } + + @Test + public void convertToMilitaryTimeTestTwo() { + String expected = "01:30"; + String actual = problem.convertToMilitaryTime("1:30 am"); + Assert.assertEquals(expected, actual); + } + + @Test + public void convertToMilitaryTimeTestThree() { + String expected = "14:22"; + String actual = problem.convertToMilitaryTime("2:22 pm"); + Assert.assertEquals(expected, actual); + } + + @Test + public void convertToMilitaryTimeTestFour() { + String expected = "02:22"; + String actual = problem.convertToMilitaryTime("2:22 am"); + Assert.assertEquals(expected, actual); + } + + @Test + public void parseMilitaryHourTest() { + int expected = 13; + int actual = problem.parseMilitaryHour("13:30"); + Assert.assertEquals(expected, actual); + } + + @Test + public void parseMilitaryMinutesTest() { + int expected = 30; + int actual = problem.parseMilitaryMinutes("13:30"); + Assert.assertEquals(expected, actual); + } + + @Test + public void convertHourToWordTestONe() { + String expected = "Thirteen"; + String actual = problem.convertHourToWord(13); + Assert.assertEquals(expected, actual); + } + + @Test + public void convertHourToWordTestTwo() { + String expected = "Twentyfour"; + String actual = problem.convertHourToWord(24); + Assert.assertEquals(expected, actual); + } + + @Test + public void convertMinuteToWordTestOne() { + String expected = "Seven"; + String actual = problem.convertMinuteToWord(7); + Assert.assertEquals(expected, actual); + } + + @Test + public void convertMinuteToWordTestTwo() { + String expected = "Fortyone"; + String actual = problem.convertMinuteToWord(41); + Assert.assertEquals(expected, actual); + } + + + @Test + public void convertToMilitaryTimeInWordsTestOne() { + String expected = "Thirteen Hundred and Thirty Hours"; + String actual = problem.convertToMilitaryTimeInWords("Thirteen", "Thirty"); + Assert.assertEquals(expected, actual); + } + + @Test + public void convertToMilitaryTimeInWordsTestTwo() { + String expected = "Fourteen Hundred and Twentytwo Hours"; + String actual = problem.convertToMilitaryTimeInWords("Fourteen", "Twentytwo"); + Assert.assertEquals(expected, actual); + } + +} \ No newline at end of file