diff --git a/src/main/java/io/zipcoder/AnotherWay.java b/src/main/java/io/zipcoder/AnotherWay.java new file mode 100644 index 0000000..939eb2a --- /dev/null +++ b/src/main/java/io/zipcoder/AnotherWay.java @@ -0,0 +1,141 @@ +package io.zipcoder; + +public class AnotherWay { + + public String convert(String aTime) { + + String time = ""; + + if (hasNoMinutes(aTime)) { + time += hours(aTime) + " Hundred Hours"; + } else if (tenMinutes(aTime).equalsIgnoreCase("zero")) { + time += hours(aTime) + " Hundred " + tenMinutes(aTime) + minutes(aTime) + " Hours"; + } else { + time += hours(aTime) + " Hundred and " + tenMinutes(aTime) + minutes(aTime) + " Hours"; + } + + + return time; + } + + + // look out below! + + + public String splitHours(String aString) { + return aString.split(":")[0]; + } + + public String splitTens(String aTime) { + String afterColon = aTime.split(":")[1]; + return afterColon.substring(0, 1); + } + + public String splitMinutes(String aTime) { + String afterColon = aTime.split(":")[1]; + return afterColon.substring(1, 2); + } + + public String hours(String aString) { + + String hoursString = splitHours(aString); + Integer hoursInt = Integer.parseInt(hoursString); + if (isPm(aString)) { + hoursInt += 12; + } + String hours = null; + + for (Hour h : Hour.values()) { + if (h.getValue() == hoursInt) { + hours = h.toString(); + } + } + + if (hoursInt < 10) { + return "Zero " + hours.substring(0, 1) + hours.substring(1, hours.length()).toLowerCase(); + } else { + return hours.substring(0, 1) + hours.substring(1, hours.length()).toLowerCase(); + } + } + + public String tenMinutes(String aTime) { + + String tenMinutesString = splitTens(aTime); + Integer tensInt = Integer.parseInt(tenMinutesString); + String tens = ""; + + if (!hasNoMinutes(aTime) && !isTeen(aTime)) { + + for (TenBasedMinute tbm : TenBasedMinute.values()) { + if (tbm.getValue() == tensInt) { + tens = tbm.toString(); + } + } + } + + if (tens.equalsIgnoreCase("")) { + return tens; + } else { + return tens.substring(0, 1) + tens.substring(1, tens.length()).toLowerCase(); + + } + } + + public String minutes(String aTime) { + String minutesString = splitMinutes(aTime); + Integer minutesInt = Integer.parseInt(minutesString); + String minutes = ""; + + if (!hasNoMinutes(aTime) && !isTeen(aTime)) { + + for (SingleMinute sm : SingleMinute.values()) { + if (sm.getValue() == minutesInt) { + minutes = sm.toString(); + } + } + } + + if (isTeen(aTime)) { + for (TeenMinute tm : TeenMinute.values()) { + if (tm.getValue() == minutesInt) { + minutes = tm.toString(); + } + } + } + + if (minutes.equalsIgnoreCase("")) { + return minutes; + } else if (isTeen(aTime)) { + return minutes.substring(0, 1) + minutes.substring(1, minutes.length()).toLowerCase(); + } else { + return " " + minutes.substring(0, 1) + minutes.substring(1, minutes.length()).toLowerCase(); + } + } + + + public boolean isPm(String aTime) { + if (aTime.contains("p")) { + return true; + } else { + return false; + } + } + + public boolean isTeen(String aTime) { + if (aTime.charAt(aTime.length() - 4) == '1') { + return true; + } else { + return false; + } + } + + public boolean hasNoMinutes(String aTime) { + if (aTime.charAt(aTime.length() - 4) == '0' && aTime.charAt(aTime.length() - 3) == '0') { + return true; + } else { + return false; + } + } + + +} diff --git a/src/main/java/io/zipcoder/Hour.java b/src/main/java/io/zipcoder/Hour.java new file mode 100644 index 0000000..bb99c50 --- /dev/null +++ b/src/main/java/io/zipcoder/Hour.java @@ -0,0 +1,41 @@ +package io.zipcoder; + +public enum Hour { + + ONE(1), + TWO(2), + THREE(3), + FOUR(4), + FIVE(5), + SIX(6), + SEVEN(7), + EIGHT(8), + NINE(9), + TEN(10), + ELEVEN(11), + TWELVE(24), + THIRTEEN(13), + FOURTEEN(14), + FIFTEEN(15), + SIXTEEN(16), + SEVENTEEN(17), + EIGHTEEN(18), + NINETEEN(19), + TWENTY(20), + TWENTYONE(21), + TWENTYTWO(22), + TWENTYTHREE(23), + ZERO(12); + + private final int value; + + Hour(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + +} diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 4ee4e64..f4adc2f 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -1,4 +1,263 @@ package io.zipcoder; public class Problem6 { + + public String convertToMilitaryTime(String aTime) { + + String asMilitary = ""; + + if (isPm(aTime)) { + asMilitary += getHoursPm(aTime); + } else { + asMilitary += getHoursAm(aTime); + } + + return asMilitary += getMinutes(aTime); + } + + + + // look below at your own risk + + + + + protected boolean isPm(String aTime) { + if (aTime.charAt(aTime.length() - 2) == 'p') { + return true; + } else { + return false; + } + } + + protected String[] splitHoursAndMinutes(String aTime) { + return aTime.split(":"); + } + + protected String getHoursAm(String aTime) { + + String hours = splitHoursAndMinutes(aTime)[0]; + String militaryHours = ""; + + if (hours.length() == 1) { + militaryHours += "Zero "; + + switch (hours.charAt(0)) { + case '1': + militaryHours += "One"; + break; + case '2': + militaryHours += "Two"; + break; + case '3': + militaryHours += "Three"; + break; + case '4': + militaryHours += "Four"; + break; + case '5': + militaryHours += "Five"; + break; + case '6': + militaryHours += "Six"; + break; + case '7': + militaryHours += "Seven"; + break; + case '8': + militaryHours += "Eight"; + break; + case '9': + militaryHours += "Nine"; + break; + default: + break; + } + } else { + switch (hours.charAt(1)) { + case '0': + militaryHours += "Ten"; + break; + case '1': + militaryHours += "Eleven"; + break; + case '2': + militaryHours += "Zero"; + break; + default: + break; + } + } + + return militaryHours += " Hundred"; + + } + + protected String getHoursPm(String aTime) { + + String hours = splitHoursAndMinutes(aTime)[0]; + String militaryHours = ""; + + if (hours.length() == 1) { + + switch (hours.charAt(0)) { + case '1': + militaryHours += "Thirteen"; + break; + case '2': + militaryHours += "Fourteen"; + break; + case '3': + militaryHours += "Fifteen"; + break; + case '4': + militaryHours += "Sixteen"; + break; + case '5': + militaryHours += "Seventeen"; + break; + case '6': + militaryHours += "Eighteen"; + break; + case '7': + militaryHours += "Nineteen"; + break; + case '8': + militaryHours += "Twenty"; + break; + case '9': + militaryHours += "Twenty One"; + break; + default: + break; + } + } else { + switch (hours.charAt(1)) { + case '0': + militaryHours += "Twenty Two"; + break; + case '1': + militaryHours += "Twenty Three"; + break; + case '2': + militaryHours += "Twelve"; + break; + default: + break; + } + } + + return militaryHours += " Hundred"; + + } + + protected String getMinutes(String aTime) { + String minutesInput = splitHoursAndMinutes(aTime)[1]; + String minutes = ""; + + if (minutesInput.charAt(0) != '0') { + minutes += " and "; + } + + if (minutesInput.charAt(0) == '0') { + minutes += " Zero "; + } + + if (minutesInput.charAt(0) == '2') { + minutes += "Twenty "; + } + + if (minutesInput.charAt(0) == '3') { + minutes += "Thirty "; + } + + if (minutesInput.charAt(0) == '4') { + minutes += "Forty "; + } + + if (minutesInput.charAt(0) == '5') { + minutes += "Fifty "; + } + + if (minutesInput.charAt(0) == '1') { + + switch (minutesInput.charAt(1)) { + case '0': + minutes += "Ten "; + break; + case '1': + minutes += "Eleven "; + break; + case '2': + minutes += "Twelve "; + break; + case '3': + minutes += "Thirteen "; + break; + case '4': + minutes += "Fourteen "; + break; + case '5': + minutes += "Fifteen "; + break; + case '6': + minutes += "Sixteen "; + break; + case '7': + minutes += "Seventeen "; + break; + case '8': + minutes += "Eighteen "; + break; + case '9': + minutes += "Nineteen "; + break; + default: + break; + } + + } else { + + switch (minutesInput.charAt(1)) { + case '0': + minutes += ""; + break; + case '1': + minutes += "One "; + break; + case '2': + minutes += "Two "; + break; + case '3': + minutes += "Three "; + break; + case '4': + minutes += "Four "; + break; + case '5': + minutes += "Five "; + break; + case '6': + minutes += "Six "; + break; + case '7': + minutes += "Seven "; + break; + case '8': + minutes += "Eight "; + break; + case '9': + minutes += "Nine "; + break; + default: + break; + } + + } + + return minutes += "Hours"; + + } + + } diff --git a/src/main/java/io/zipcoder/SingleMinute.java b/src/main/java/io/zipcoder/SingleMinute.java new file mode 100644 index 0000000..0757350 --- /dev/null +++ b/src/main/java/io/zipcoder/SingleMinute.java @@ -0,0 +1,24 @@ +package io.zipcoder; + +public enum SingleMinute { + + ONE(1), + TWO(2), + THREE(3), + FOUR(4), + FIVE(5), + SIX(6), + SEVEN(7), + EIGHT(8), + NINE(9); + + private final int value; + + SingleMinute(int value) { + this.value = value; + } + + public int getValue() { + return this.value; + } +} diff --git a/src/main/java/io/zipcoder/TeenMinute.java b/src/main/java/io/zipcoder/TeenMinute.java new file mode 100644 index 0000000..41787c1 --- /dev/null +++ b/src/main/java/io/zipcoder/TeenMinute.java @@ -0,0 +1,26 @@ +package io.zipcoder; + +public enum TeenMinute { + + TEN(0), + ELEVEN(1), + TWELVE(2), + THIRTEEN(3), + FOURTEEN(4), + FIFTEEN(5), + SIXTEEN(6), + SEVENTEEN(7), + EIGHTEEN(8), + NINETEEN(9); + + private final int value; + + TeenMinute(int value) { + this.value = value; + } + + public int getValue() { + return this.value; + } + +} diff --git a/src/main/java/io/zipcoder/TenBasedMinute.java b/src/main/java/io/zipcoder/TenBasedMinute.java new file mode 100644 index 0000000..f097291 --- /dev/null +++ b/src/main/java/io/zipcoder/TenBasedMinute.java @@ -0,0 +1,22 @@ +package io.zipcoder; + +public enum TenBasedMinute { + + ZERO(0), + TWENTY(2), + THIRTY(3), + FORTY(4), + FIFTY(5); + + private final int value; + + TenBasedMinute(int value) { + this.value = value; + } + + public int getValue() { + return this.value; + } + + +} diff --git a/src/test/java/io/zipcoder/AnotherWayTest.java b/src/test/java/io/zipcoder/AnotherWayTest.java new file mode 100644 index 0000000..780638c --- /dev/null +++ b/src/test/java/io/zipcoder/AnotherWayTest.java @@ -0,0 +1,106 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class AnotherWayTest { + + + private AnotherWay testProblem; + + @Before + public void setup() { + testProblem = new AnotherWay(); + } + + @Test + public void convertTest1() { + String input = "1:30pm"; + String expected = "Thirteen Hundred and Thirty Hours"; + String actual = testProblem.convert(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void convertTest2() { + String input = "1:30am"; + String expected = "Zero One Hundred and Thirty Hours"; + String actual = testProblem.convert(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void convertTest3() { + String input = "2:22pm"; + String expected = "Fourteen Hundred and Twenty Two Hours"; + String actual = testProblem.convert(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void convertTest4() { + String input = "2:11am"; + String expected = "Zero Two Hundred and Eleven Hours"; + String actual = testProblem.convert(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void convertTest5() { + String input = "10:02am"; + String expected = "Ten Hundred Zero Two Hours"; + String actual = testProblem.convert(input); + Assert.assertEquals(expected, actual); + } + + + @Test + public void hoursTest1() { + String input = "2:22pm"; + System.out.println(testProblem.hours(input)); + } + + @Test + public void hoursTest2() { + String input = "2:22am"; + System.out.println(testProblem.hours(input)); + } + + @Test + public void tensTest1() { + String input = "2:50am"; + System.out.println(testProblem.tenMinutes(input)); + } + + @Test + public void tensTest2() { + String input = "2:08am"; + System.out.println(testProblem.tenMinutes(input)); + } + + @Test + public void tensTest3() { + String input = "2:00am"; + System.out.println(testProblem.tenMinutes(input)); + } + + @Test + public void minsTest1() { + String input = "2:29am"; + System.out.println(testProblem.minutes(input)); + } + + @Test + public void minsTest2() { + String input = "2:50am"; + System.out.println(testProblem.minutes(input)); + } + + @Test + public void minsTest3() { + String input = "2:14am"; + System.out.println(testProblem.minutes(input)); + } + +} diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index d262e88..aea6222 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -1,4 +1,149 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + public class Problem6Test { + + private Problem6 testProblem; + + @Before + public void setup() { + testProblem = new Problem6(); + } + + @Test + public void convertTest1() { + String input = "1:30pm"; + String expected = "Thirteen Hundred and Thirty Hours"; + String actual = testProblem.convertToMilitaryTime(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void convertTest2() { + String input = "1:30am"; + String expected = "Zero One Hundred and Thirty Hours"; + String actual = testProblem.convertToMilitaryTime(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void convertTest3() { + String input = "2:22pm"; + String expected = "Fourteen Hundred and Twenty Two Hours"; + String actual = testProblem.convertToMilitaryTime(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void convertTest4() { + String input = "2:11am"; + String expected = "Zero Two Hundred and Eleven Hours"; + String actual = testProblem.convertToMilitaryTime(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void convertTest5() { + String input = "10:02am"; + String expected = "Ten Hundred Zero Two Hours"; + String actual = testProblem.convertToMilitaryTime(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void pmTest1() { + String input = "1:30pm"; + Assert.assertTrue(testProblem.isPm(input)); + } + + @Test + public void pmTest2() { + String input = "1:30am"; + Assert.assertFalse(testProblem.isPm(input)); + } + + @Test + public void splitHoursAndMinutesTest1() { + String input = "1:30pm"; + int expected = 2; + int actual = testProblem.splitHoursAndMinutes(input).length; + Assert.assertEquals(expected, actual); + } + + @Test + public void getHoursAmTest1() { + String input = "3:30am"; + String expected = "Zero Three Hundred"; + String actual = testProblem.getHoursAm(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void getHoursAmTest2() { + String input = "11:30am"; + String expected = "Eleven Hundred"; + String actual = testProblem.getHoursAm(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void getHoursAmTest3() { + String input = "12:30am"; + String expected = "Zero Hundred"; + String actual = testProblem.getHoursAm(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void getHoursPmTest1() { + String input = "7:30pm"; + String expected = "Nineteen Hundred"; + String actual = testProblem.getHoursPm(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void getHoursPmTest2() { + String input = "11:30pm"; + String expected = "Twenty Three Hundred"; + String actual = testProblem.getHoursPm(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void getHoursPmTest3() { + String input = "12:30pm"; + String expected = "Twelve Hundred"; + String actual = testProblem.getHoursPm(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void getMinutesTest1() { + String input = "8:14am"; + String expected = " and Fourteen Hours"; + String actual = testProblem.getMinutes(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void getMinutesTest2() { + String input = "2:52pm"; + String expected = " and Fifty Two Hours"; + String actual = testProblem.getMinutes(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void getMinutesTest3() { + String input = "5:04pm"; + String expected = " Zero Four Hours"; + String actual = testProblem.getMinutes(input); + Assert.assertEquals(expected, actual); + } + + }