From edacb5b3bf73733f540717aa46ee6e372d04c808 Mon Sep 17 00:00:00 2001 From: Mitch Taylor Date: Fri, 13 Apr 2018 10:16:26 -0400 Subject: [PATCH 1/3] Groundwork --- src/main/java/io/zipcoder/HourEnum.java | 4 ++ src/main/java/io/zipcoder/MinuteEnum.java | 4 ++ src/main/java/io/zipcoder/Problem6.java | 44 +++++++++++++++++ src/test/java/io/zipcoder/Problem6Test.java | 52 +++++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 src/main/java/io/zipcoder/HourEnum.java create mode 100644 src/main/java/io/zipcoder/MinuteEnum.java diff --git a/src/main/java/io/zipcoder/HourEnum.java b/src/main/java/io/zipcoder/HourEnum.java new file mode 100644 index 0000000..619d377 --- /dev/null +++ b/src/main/java/io/zipcoder/HourEnum.java @@ -0,0 +1,4 @@ +package io.zipcoder; + +public enum HourEnum { +} diff --git a/src/main/java/io/zipcoder/MinuteEnum.java b/src/main/java/io/zipcoder/MinuteEnum.java new file mode 100644 index 0000000..758a652 --- /dev/null +++ b/src/main/java/io/zipcoder/MinuteEnum.java @@ -0,0 +1,4 @@ +package io.zipcoder; + +public enum MinuteEnum { +} diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 4ee4e64..5490b50 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -1,4 +1,48 @@ package io.zipcoder; public class Problem6 { + + // Count hours + // am = 0, pm = 12 + + public String parse(String input) { + Integer hours = 0; + Integer minutes = 0; + String ret = ""; + String[] colonSplit = input.split(":"); + hours += Integer.parseInt(colonSplit[0]); + minutes += Integer.parseInt(colonSplit[1].substring(0, 2)); + if (colonSplit[1].charAt(2)=='p') { + hours += 12; + } + if (hours < 10) { + ret += "Zero "; + } + ret += hoursToWords(hours); + ret += " Hundred "; + if (colonSplit[1].charAt(0)!='0') { + ret += "and "; + } else { + ret += "Zero "; + } + ret += minutesToWords(minutes); + ret += " Hours"; + return ret; + } + + public String hoursToWords(Integer hours) { + String ret = ""; + return ret; + } + + public String minutesToWords(Integer minutes) { + String ret = ""; + return ret; + } + + public Integer testMethod(String string) { + Integer ret = Integer.parseInt(string.substring(0, 2)); + return ret; + } + } diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index d262e88..6fdba42 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -1,4 +1,56 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + public class Problem6Test { + + @Test + public void testParse1() { + Problem6 problem6 = new Problem6(); + String expected = "Thirteen Hundred and Thirty Hours"; + String actual = problem6.parse("1:30pm"); + Assert.assertEquals(expected, actual); + } + + @Test + public void testParse2() { + Problem6 problem6 = new Problem6(); + String expected = "Zero One Hundred and Thirty Hours"; + String actual = problem6.parse("1:30am"); + Assert.assertEquals(expected, actual); + } + + @Test + public void testParse3() { + Problem6 problem6 = new Problem6(); + String expected = "Fourteen Hundred and Twenty Two Hours"; + String actual = problem6.parse("2:22pm"); + Assert.assertEquals(expected, actual); + } + + @Test + public void testParse4() { + Problem6 problem6 = new Problem6(); + String expected = "Zero Two Hundred and Eleven Hours"; + String actual = problem6.parse("2:11am"); + Assert.assertEquals(expected, actual); + } + + @Test + public void testParse5() { + Problem6 problem6 = new Problem6(); + String expected = "Ten Hundred Zero Two Hours"; + String actual = problem6.parse("10:02am"); + Assert.assertEquals(expected, actual); + } + + @Test + public void testTest() { + Problem6 problem6 = new Problem6(); + Integer expected = 2; + Integer actual = problem6.testMethod("02pm"); + Assert.assertEquals(expected, actual); + } + } From cfe87f8f03ec5f5708ee7c415cb0117c768c1902 Mon Sep 17 00:00:00 2001 From: Mitch Taylor Date: Fri, 13 Apr 2018 10:46:47 -0400 Subject: [PATCH 2/3] Horrible enum --- src/main/java/io/zipcoder/HourEnum.java | 4 -- src/main/java/io/zipcoder/MinuteEnum.java | 4 -- src/main/java/io/zipcoder/TimeEnum.java | 77 +++++++++++++++++++++++ 3 files changed, 77 insertions(+), 8 deletions(-) delete mode 100644 src/main/java/io/zipcoder/HourEnum.java delete mode 100644 src/main/java/io/zipcoder/MinuteEnum.java create mode 100644 src/main/java/io/zipcoder/TimeEnum.java diff --git a/src/main/java/io/zipcoder/HourEnum.java b/src/main/java/io/zipcoder/HourEnum.java deleted file mode 100644 index 619d377..0000000 --- a/src/main/java/io/zipcoder/HourEnum.java +++ /dev/null @@ -1,4 +0,0 @@ -package io.zipcoder; - -public enum HourEnum { -} diff --git a/src/main/java/io/zipcoder/MinuteEnum.java b/src/main/java/io/zipcoder/MinuteEnum.java deleted file mode 100644 index 758a652..0000000 --- a/src/main/java/io/zipcoder/MinuteEnum.java +++ /dev/null @@ -1,4 +0,0 @@ -package io.zipcoder; - -public enum MinuteEnum { -} diff --git a/src/main/java/io/zipcoder/TimeEnum.java b/src/main/java/io/zipcoder/TimeEnum.java new file mode 100644 index 0000000..0501c11 --- /dev/null +++ b/src/main/java/io/zipcoder/TimeEnum.java @@ -0,0 +1,77 @@ +package io.zipcoder; + +public enum TimeEnum { + ZERO(0, "Zero"), + ONE(1, "One"), + TWO(2, "Two"), + THREE(3, "Three"), + FOUR(4, "Four"), + FIVE(5, "Five"), + SIX(6, "Six"), + SEVEN(7, "Seven"), + EIGHT(8, "Eight"), + NINE(9, "Nine"), + TEN(10, "Ten"), + ELEVEN(11, "Eleven"), + TWELVE(12, "Twelve"), + THIRTEEN(13, "Thirteen"), + FOURTEEN(14, "Fourteen"), + FIFTEEN(15, "Fifteen"), + SIXTEEN(16, "Sixteen"), + SEVENTEEN(17, "Seventeen"), + EIGHTEEN(18, "Eighteen"), + NINETEEN(19, "Nineteen"), + TWENTY(20, "Twenty"), + TWENTY_ONE(21, "Twenty One"), + TWENTY_TWO(22, "Twenty Two"), + TWENTY_THREE(23, "Twenty Three"), + TWENTY_FOUR(24, "Twenty Four"), + TWENTY_FIVE(25, "Twenty Five"), + TWENTY_SIX(26, "Twenty Six"), + TWENTY_SEVEN(27, "Twenty Seven"), + TWENTY_EIGHT(28, "Twenty Eight"), + TWENTY_NINE(29, "Twenty Nine"), + THIRTY(30, "Thirty"), + THIRTY_ONE(31, "Thirty One"), + THIRTY_TWO(32, "Thirty Two"), + THIRTY_THREE(33, "Thirty Three"), + THIRTY_FOUR(34, "Thirty Four"), + THIRTY_FIVE(35, "Thirty Five"), + THIRTY_SIX(36, "Thirty Six"), + THIRTY_SEVEN(37, "Thirty Seven"), + THIRTY_EIGHT(38, "Thirty Eight"), + THIRTY_NINE(39, "Thirty Nine"), + FORTY(40, "Forty"), + FORTY_ONE(41, "Forty One"), + FORTY_TWO(42, "Forty Two"), + FORTY_THREE(43, "Forty Three"), + FORTY_FOUR(44, "Forty Four"), + FORTY_FIVE(45, "Forty Five"), + FORTY_SIX(46, "Forty Six"), + FORTY_SEVEN(47, "Forty Seven"), + FORTY_EIGHT(48, "Forty Eight"), + FORTY_NINE(49, "Forty Nine"), + FIFTY(50, "Fifty"), + FIFTY_ONE(51, "Fifty One"), + FIFTY_TWO(52, "Fifty Two"), + FIFTY_THREE(53, "Fifty Three"), + FIFTY_FOUR(54, "Fifty Four"), + FIFTY_FIVE(55, "Fifty Five"), + FIFTY_SIX(56, "Fifty Six"), + FIFTY_SEVEN(57, "Fifty Seven"), + FIFTY_EIGHT(58, "Fifty Eight"), + FIFTY_NINE(59, "Fifty Nine"); + + final int hourValue; + final String hourWord; + + TimeEnum(int hourValue, String hourWord) { + this.hourValue = hourValue; + this.hourWord = hourWord; + } + + public int getHourValue() {return this.hourValue;} + + public String getHourWord() {return this.hourWord;} + +} From ffa33dc2b2d5cd3195389638bbc197429e7efbeb Mon Sep 17 00:00:00 2001 From: Mitch Taylor Date: Fri, 13 Apr 2018 11:29:43 -0400 Subject: [PATCH 3/3] Reverse enum lookup --- src/main/java/io/zipcoder/Problem6.java | 16 +++++----------- src/main/java/io/zipcoder/TimeEnum.java | 15 +++++++++++++++ src/test/java/io/zipcoder/Problem6Test.java | 18 +++++------------- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 5490b50..2e51ef3 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -2,10 +2,7 @@ public class Problem6 { - // Count hours - // am = 0, pm = 12 - - public String parse(String input) { + public String buildString(String input) { Integer hours = 0; Integer minutes = 0; String ret = ""; @@ -30,18 +27,15 @@ public String parse(String input) { return ret; } - public String hoursToWords(Integer hours) { + private String hoursToWords(Integer hours) { String ret = ""; + ret += TimeEnum.parseToWord(hours); return ret; } - public String minutesToWords(Integer minutes) { + private String minutesToWords(Integer minutes) { String ret = ""; - return ret; - } - - public Integer testMethod(String string) { - Integer ret = Integer.parseInt(string.substring(0, 2)); + ret += TimeEnum.parseToWord(minutes); return ret; } diff --git a/src/main/java/io/zipcoder/TimeEnum.java b/src/main/java/io/zipcoder/TimeEnum.java index 0501c11..da299a1 100644 --- a/src/main/java/io/zipcoder/TimeEnum.java +++ b/src/main/java/io/zipcoder/TimeEnum.java @@ -1,5 +1,8 @@ package io.zipcoder; +import java.util.HashMap; +import java.util.Map; + public enum TimeEnum { ZERO(0, "Zero"), ONE(1, "One"), @@ -64,6 +67,13 @@ public enum TimeEnum { final int hourValue; final String hourWord; + static final Map lookup = new HashMap(); + + static { + for (TimeEnum t : TimeEnum.values()) { + lookup.put(t.getHourValue(), t.getHourWord()); + } + } TimeEnum(int hourValue, String hourWord) { this.hourValue = hourValue; @@ -74,4 +84,9 @@ public enum TimeEnum { public String getHourWord() {return this.hourWord;} + public static String parseToWord(int hourValue) { + return (String) lookup.get(hourValue); + } + + } diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index 6fdba42..badc404 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -9,7 +9,7 @@ public class Problem6Test { public void testParse1() { Problem6 problem6 = new Problem6(); String expected = "Thirteen Hundred and Thirty Hours"; - String actual = problem6.parse("1:30pm"); + String actual = problem6.buildString("1:30pm"); Assert.assertEquals(expected, actual); } @@ -17,7 +17,7 @@ public void testParse1() { public void testParse2() { Problem6 problem6 = new Problem6(); String expected = "Zero One Hundred and Thirty Hours"; - String actual = problem6.parse("1:30am"); + String actual = problem6.buildString("1:30am"); Assert.assertEquals(expected, actual); } @@ -25,7 +25,7 @@ public void testParse2() { public void testParse3() { Problem6 problem6 = new Problem6(); String expected = "Fourteen Hundred and Twenty Two Hours"; - String actual = problem6.parse("2:22pm"); + String actual = problem6.buildString("2:22pm"); Assert.assertEquals(expected, actual); } @@ -33,7 +33,7 @@ public void testParse3() { public void testParse4() { Problem6 problem6 = new Problem6(); String expected = "Zero Two Hundred and Eleven Hours"; - String actual = problem6.parse("2:11am"); + String actual = problem6.buildString("2:11am"); Assert.assertEquals(expected, actual); } @@ -41,15 +41,7 @@ public void testParse4() { public void testParse5() { Problem6 problem6 = new Problem6(); String expected = "Ten Hundred Zero Two Hours"; - String actual = problem6.parse("10:02am"); - Assert.assertEquals(expected, actual); - } - - @Test - public void testTest() { - Problem6 problem6 = new Problem6(); - Integer expected = 2; - Integer actual = problem6.testMethod("02pm"); + String actual = problem6.buildString("10:02am"); Assert.assertEquals(expected, actual); }