From 07ecf15dd6b98dc6d95a397de92b475e31bed073 Mon Sep 17 00:00:00 2001 From: linda Date: Mon, 4 Dec 2017 11:31:11 -0500 Subject: [PATCH 1/2] interview poblem 6, standard time to military time in words --- src/main/java/io/zipcoder/Problem6.java | 114 ++++++++++++++++++++ src/test/java/io/zipcoder/Problem6Test.java | 60 +++++++++++ 2 files changed, 174 insertions(+) diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 4ee4e64..a9c8adc 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -1,4 +1,118 @@ 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 One Hundred and Thirty Hours + 2:22 pm 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 +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 militaryTime) { + //if passing 13:30 or 01:30 + //use substring to get (0,1) if it is 0 return Zero, if not get substring(0,2) and convert to Thirteen +//or split military time into hours and minutes + + + + String hour = ""; +//13 to Thirteen + + + + return hour; + } + + + public static int parseMilitaryMinutes(String militaryTime) { + + String minutes = ""; + + + + return minutes; + } + + + + + + public static String convertToMilitaryTimeInWords(int hour, int minutes) { +// String hundredWord = "Hundred"; +// String andWord = "and"; +// String hoursWord = "Hours"; + + return null; +// return hour + hundredWord + andWord + minutes + hoursWord; +// output: Thirteen Hundred and Thirty Hours + } } + diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index d262e88..5ceb1d8 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -1,4 +1,64 @@ 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 parseMilitaryHour() { + String expected = "Thirteen"; + String actual = problem.parseMilitaryHour(); + Assert.assertEquals(expected, actual); + } + + + +// @Test +// public void convertToMilitaryTimeInWordsTest(){ +// String expected = "Thirteen Hundred and Thirty Hours"; +// String actual = problem.convertToMilitaryTimeInWords(); +// Assert.assertEquals(expected, actual); + + } + } + + From 85fa5924120a500cfb7940e7957ba371ac1ebe35 Mon Sep 17 00:00:00 2001 From: linda Date: Tue, 5 Dec 2017 19:10:13 -0500 Subject: [PATCH 2/2] completed --- src/main/java/io/zipcoder/MilitaryHour.java | 21 ++++++ src/main/java/io/zipcoder/MilitaryMinute.java | 27 +++++++ src/main/java/io/zipcoder/Problem6.java | 64 +++++++++-------- src/test/java/io/zipcoder/Problem6Test.java | 72 ++++++++++++++----- 4 files changed, 136 insertions(+), 48 deletions(-) create mode 100644 src/main/java/io/zipcoder/MilitaryHour.java create mode 100644 src/main/java/io/zipcoder/MilitaryMinute.java 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 a9c8adc..6b3be79 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -14,8 +14,8 @@ public class Problem6 { /* First Breakdown: 1:30 pm to Thirteen Hundred and Thirty Hours - 1:30 am to One Hundred and Thirty Hours - 2:22 pm Fourteen Hundred and Twenty Two 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" @@ -37,6 +37,7 @@ public class Problem6 { 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; @@ -46,17 +47,13 @@ public class Problem6 { add "Hours" at the end of the output string String wordHour = "Hours" - output: Thirteen Hundred and Thirty 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"))); - + //System.out.println(convertToMilitaryTimeInWords(convertToMilitaryTime("1:30 pm"))); } public static String convertToMilitaryTime(String standardTime) { @@ -70,49 +67,54 @@ public static String convertToMilitaryTime(String standardTime) { } DateFormat displayMilitaryTime = new SimpleDateFormat("HH:mm"); - String militaryOutputString = displayMilitaryTime.format(d); - return militaryOutputString; } - public static int parseMilitaryHour(String militaryTime) { - //if passing 13:30 or 01:30 - //use substring to get (0,1) if it is 0 return Zero, if not get substring(0,2) and convert to Thirteen -//or split military time into hours and minutes + 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; + } - String hour = ""; -//13 to Thirteen + public static String convertHourToWord(int hourInt) { + String hourWord = ""; + for (MilitaryHour m : MilitaryHour.values()) + if (hourInt == m.hourToMatch) + hourWord = m.toString(); - return hour; + return hourWord; } - public static int parseMilitaryMinutes(String militaryTime) { - - String minutes = ""; + public static String convertMinuteToWord(int minutesInt) { + String minuteWord = ""; + for (MilitaryMinute m : MilitaryMinute.values()) + if (minutesInt == m.minutesToMatch) + minuteWord = m.toString(); - - return minutes; + 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; - public static String convertToMilitaryTimeInWords(int hour, int minutes) { -// String hundredWord = "Hundred"; -// String andWord = "and"; -// String hoursWord = "Hours"; - - return null; -// return hour + hundredWord + andWord + minutes + hoursWord; -// output: Thirteen Hundred and Thirty 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 5ceb1d8..2bb0ebf 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -15,50 +15,88 @@ public void setUp() { } @Test - public void convertToMilitaryTimeTestOne(){ + public void convertToMilitaryTimeTestOne() { String expected = "13:30"; - String actual = problem.convertToMilitaryTime("1:30 pm"); + String actual = problem.convertToMilitaryTime("1:30 pm"); Assert.assertEquals(expected, actual); } @Test - public void convertToMilitaryTimeTestTwo(){ + public void convertToMilitaryTimeTestTwo() { String expected = "01:30"; - String actual = problem.convertToMilitaryTime("1:30 am"); + String actual = problem.convertToMilitaryTime("1:30 am"); Assert.assertEquals(expected, actual); } @Test - public void convertToMilitaryTimeTestThree(){ + public void convertToMilitaryTimeTestThree() { String expected = "14:22"; - String actual = problem.convertToMilitaryTime("2:22 pm"); + String actual = problem.convertToMilitaryTime("2:22 pm"); Assert.assertEquals(expected, actual); } @Test - public void convertToMilitaryTimeTestFour(){ + public void convertToMilitaryTimeTestFour() { String expected = "02:22"; - String actual = problem.convertToMilitaryTime("2:22 am"); + String actual = problem.convertToMilitaryTime("2:22 am"); Assert.assertEquals(expected, actual); } @Test - public void parseMilitaryHour() { - String expected = "Thirteen"; - String actual = problem.parseMilitaryHour(); + 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 convertToMilitaryTimeInWordsTest(){ -// String expected = "Thirteen Hundred and Thirty Hours"; -// String actual = problem.convertToMilitaryTimeInWords(); -// 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