diff --git a/pom.xml b/pom.xml index 5fd8efa..7b5cfe2 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.8 + 1.8 + + + + diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 4ee4e64..43a03de 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -1,4 +1,95 @@ package io.zipcoder; +import java.util.Arrays; + + public class Problem6 { + + public static String[] militaryNum = new String[]{"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", + "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", + "Nineteen", "Twenty", "Thirty", "Forty", "Fifty"}; + + + public String militaryConverter(String time) { + Integer[] fullClock = splitHourAndMinutes(time); + if(militaryPhrasing(fullClock).charAt(0) == militaryPhrasing(fullClock).charAt(5)){ + return militaryPhrasing(fullClock).substring(5); + } + return militaryPhrasing(fullClock); + } + + public Integer[] splitHourAndMinutes(String time) { + String[] clockGroup = time.split(":"); + String amOrPm = clockGroup[1].substring(2); + + int hours = Integer.parseInt(clockGroup[0]); + if (hours != 12) { + if (amOrPm.equals("pm")) { + hours += 12; + } + } else { + if (amOrPm.equals("am")) { + hours -= hours; + } + } + int minutes = Integer.parseInt(clockGroup[1].substring(0, 2)); + + Integer[] fullClock = new Integer[2]; + fullClock[0] = hours; + fullClock[1] = minutes; + + return fullClock; + } + + public String militaryPhrasing(Integer[] fullClock){ + int hours = fullClock[0]; + int minutes = fullClock[1]; + StringBuilder stringBuilder = new StringBuilder(); + + if (hours < 10){ + stringBuilder.append(militaryNum[0] + " "); + } + if (hours <= 20){ + stringBuilder.append(militaryNum[hours] + " "); + } + else{ + stringBuilder.append(militaryNum[20] + (militaryNum[hours - 20]) + " "); + } + stringBuilder.append("Hundred "); + + if (minutes > 0) { + stringBuilder.append("and "); + if (minutes <= 20) { + if (minutes < 10) { + stringBuilder.append(militaryNum[0] + " "); + } + stringBuilder.append(militaryNum[minutes] + " "); + } else { + int tenBase = minutes / 10; + int oneBase = minutes % 10; + stringBuilder.append(militaryNum[18 + tenBase] + " "); + + if (oneBase > 0) { + stringBuilder.append(militaryNum[oneBase] + " "); + } + } + } + stringBuilder.append("Hours"); + return stringBuilder.toString(); + } + + public static void main(String[] args) { + Problem6 test = new Problem6(); + + String time = "1:35am"; + test.splitHourAndMinutes(time); + System.out.println(Arrays.toString(test.splitHourAndMinutes(time))); + System.out.println(militaryNum[20]); + + } } +//1:30pm +//1 = index[0] +//30pm = index[1] substring(2) = pm +//30 = index[1] substring(0,2) + diff --git a/src/main/java/io/zipcoder/Problem6Redux.java b/src/main/java/io/zipcoder/Problem6Redux.java new file mode 100644 index 0000000..4211dac --- /dev/null +++ b/src/main/java/io/zipcoder/Problem6Redux.java @@ -0,0 +1,4 @@ +package io.zipcoder; + +public class Problem6Redux { +} diff --git a/src/test/java/io/zipcoder/Problem6ReduxTest.java b/src/test/java/io/zipcoder/Problem6ReduxTest.java new file mode 100644 index 0000000..e71c293 --- /dev/null +++ b/src/test/java/io/zipcoder/Problem6ReduxTest.java @@ -0,0 +1,4 @@ +package io.zipcoder; + +public class Problem6ReduxTest { +} diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index d262e88..fda9c92 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -1,4 +1,65 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + public class Problem6Test { + + + @Test + public void militaryTimeTest(){ + String time = "1:30pm"; + String militaryTime = "Thirteen Hundred and Thirty Hours"; + Problem6 testProblem6 = new Problem6(); + String actual = testProblem6.militaryConverter(time); + Assert.assertEquals(militaryTime, actual); + } + @Test + public void militaryTimeTest1(){ + String time = "1:30am"; + String militaryTime = "Zero One Hundred and Thirty Hours"; + Problem6 testProblem6 = new Problem6(); + String actual = testProblem6.militaryConverter(time); + Assert.assertEquals(militaryTime, actual); + } + @Test + public void militaryTimeTest2(){ + String time = "2:22pm"; + String militaryTime = "Fourteen Hundred and Twenty Two Hours"; + Problem6 testProblem6 = new Problem6(); + String actual = testProblem6.militaryConverter(time); + Assert.assertEquals(militaryTime, actual); + } + @Test + public void militaryTimeTest3(){ + String time = "2:11am"; + String militaryTime = "Zero Two Hundred and Eleven Hours"; + Problem6 testProblem6 = new Problem6(); + String actual = testProblem6.militaryConverter(time); + Assert.assertEquals(militaryTime, actual); + } + @Test + public void militaryTimeTest4(){ + String time = "10:02am"; + String militaryTime = "Ten Hundred and Zero Two Hours"; + Problem6 testProblem6 = new Problem6(); + String actual = testProblem6.militaryConverter(time); + Assert.assertEquals(militaryTime, actual); + } + @Test + public void militaryTimeTest5(){ + String time = "10:22am"; + String militaryTime = "Ten Hundred and Twenty Two Hours"; + Problem6 testProblem6 = new Problem6(); + String actual = testProblem6.militaryConverter(time); + Assert.assertEquals(militaryTime, actual); + } + @Test + public void militaryTimeTest6(){ + String time = "12:22am"; + String militaryTime = "Zero Hundred and Twenty Two Hours"; + Problem6 testProblem6 = new Problem6(); + String actual = testProblem6.militaryConverter(time); + Assert.assertEquals(militaryTime, actual); + } }