From 546f05d7d12c848bcdffa4a05f4ada0dfbf63b86 Mon Sep 17 00:00:00 2001 From: Zachary Date: Mon, 4 Dec 2017 11:06:02 -0500 Subject: [PATCH 1/5] saving,almost there --- src/main/java/io/zipcoder/Problem6.java | 94 +++++++++++++++++++++ src/test/java/io/zipcoder/Problem6Test.java | 59 +++++++++++++ 2 files changed, 153 insertions(+) diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 4ee4e64..40cb783 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -1,4 +1,98 @@ package io.zipcoder; +import sun.font.CompositeGlyphMapper; + +import javax.swing.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class Problem6 { + private static ArrayList amStrings = new ArrayList (Arrays.asList("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve")); + + + public String breakInput(String input) { + + String regexPattern = "^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9][AaPp][Mm]$"; + + ArrayList time = new ArrayList (); + Pattern checkRegex = Pattern.compile(regexPattern); + Matcher regexMatcher = checkRegex.matcher(input); + + if (regexMatcher.find()) { + + int index = regexMatcher.start(); + int end = regexMatcher.end(); + + String HH = input.substring(index, end - 5); + String MM = input.substring(index + 3, end - 2); + String meridiem = input.substring(index + 5, end); + + String newHHString = createMilitaryTimeHH(HH, meridiem); + String newMMString = createMilitaryTimeMM(MM); + + return newHHString+newMMString; + + + } else { + System.out.println("INVALID"); + return null; + } + + + } + + + + public String createMilitaryTimeHH(String input, String meridiem) { + ArrayList pmStrings = new ArrayList (Arrays.asList("", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twentyone", "Twentytwo", "TwentyThree", "Twentyfour")); + StringBuilder stringBuilder = new StringBuilder(); + + + if (meridiem.equalsIgnoreCase("am")) { + if (Integer.valueOf(input) < 10) stringBuilder.append(amStrings.get(0) + " "); + + stringBuilder.append(amStrings.get(Integer.valueOf(input)) + " "); + } else { + stringBuilder.append(pmStrings.get(Integer.valueOf(input)) + " "); + } + + stringBuilder.append("Hundred"); + + return stringBuilder.toString(); + } + + private String createMilitaryTimeMM(String mm) { + ArrayList minutes = new ArrayList (Arrays.asList("","Twenty","Thirty","Fourty","Fifty")); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(" and "); + if (Integer.valueOf(mm)< 10) stringBuilder.append("Zero "); + + char[] chars = mm.toCharArray(); + + + if(mm.charAt(0) == '2'){ + stringBuilder.append(minutes.get(1)); + } + if(mm.charAt(0) == '3'){ + stringBuilder.append(minutes.get(2)); + } + if(mm.charAt(0) == '4'){ + stringBuilder.append(minutes.get(3)); + } + if(mm.charAt(0) == '5'){ + stringBuilder.append(minutes.get(4)); + } + + + + + + + + stringBuilder.append(" Hours"); + return stringBuilder.toString(); + } } diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index d262e88..d128c41 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -1,4 +1,63 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; + public class Problem6Test { + + @Test + public void test1() { + Problem6 test = new Problem6(); + + String expected = "Thirteen Hundred and Thirty Hours"; + + String actual = test.breakInput("01:30pm").toString(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void test2() { + Problem6 test = new Problem6(); + String expected = "Zero One Hundred and Thirty Hours"; + + String actual = test.breakInput("01:30am").toString(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void test3(){ + Problem6 test = new Problem6(); + String expected = "Fourteen Hundred and Twenty Two Hours"; + + String actual = test.breakInput("02:22pm").toString(); + + Assert.assertEquals(expected,actual); + } + @Test + public void test4(){ + Problem6 test = new Problem6(); + String expected = "Zero Two Hundred and Eleven Hours"; + + String actual = test.breakInput("02:11am").toString(); + + Assert.assertEquals(expected,actual); + } + @Test + public void test5(){ + Problem6 test = new Problem6(); + String expected = "Ten Hundred and Zero Two Hours"; + + String actual = test.breakInput("10:02am").toString(); + + Assert.assertEquals(expected,actual); + } + + + } From 062e2951807c84d4505f97360e18f7d5affbdc74 Mon Sep 17 00:00:00 2001 From: Zachary Date: Mon, 4 Dec 2017 11:06:20 -0500 Subject: [PATCH 2/5] saving,almost there --- src/main/java/io/zipcoder/Problem6.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 40cb783..f09c6ed 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -69,10 +69,8 @@ private String createMilitaryTimeMM(String mm) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(" and "); if (Integer.valueOf(mm)< 10) stringBuilder.append("Zero "); - - char[] chars = mm.toCharArray(); - + if(mm.charAt(0) == '2'){ stringBuilder.append(minutes.get(1)); } From 1879ed5b869c71700224d6bf7a653aa58d7eb9e6 Mon Sep 17 00:00:00 2001 From: Zachary Date: Mon, 4 Dec 2017 11:09:49 -0500 Subject: [PATCH 3/5] saving,almost there --- src/main/java/io/zipcoder/Problem6.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index f09c6ed..87fd9bd 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -16,8 +16,7 @@ public class Problem6 { public String breakInput(String input) { String regexPattern = "^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9][AaPp][Mm]$"; - - ArrayList time = new ArrayList (); + Pattern checkRegex = Pattern.compile(regexPattern); Matcher regexMatcher = checkRegex.matcher(input); @@ -69,7 +68,7 @@ private String createMilitaryTimeMM(String mm) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(" and "); if (Integer.valueOf(mm)< 10) stringBuilder.append("Zero "); - + if(mm.charAt(0) == '2'){ stringBuilder.append(minutes.get(1)); From 0b24feae717df267beaf2484121ec1d51583365a Mon Sep 17 00:00:00 2001 From: Zachary Date: Mon, 4 Dec 2017 11:48:41 -0500 Subject: [PATCH 4/5] saving,almost there --- src/main/java/io/zipcoder/Problem6.java | 34 ++++++++++++++++++------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 87fd9bd..4e61991 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -16,7 +16,7 @@ public class Problem6 { public String breakInput(String input) { String regexPattern = "^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9][AaPp][Mm]$"; - + Pattern checkRegex = Pattern.compile(regexPattern); Matcher regexMatcher = checkRegex.matcher(input); @@ -64,11 +64,26 @@ public String createMilitaryTimeHH(String input, String meridiem) { } private String createMilitaryTimeMM(String mm) { - ArrayList minutes = new ArrayList (Arrays.asList("","Twenty","Thirty","Fourty","Fifty")); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(" and "); if (Integer.valueOf(mm)< 10) stringBuilder.append("Zero "); + stringBuilder.append(tenMinutes(mm)).append(secMinutes(mm)); + + + + + + + + + stringBuilder.append(" Hours"); + return stringBuilder.toString(); + } + + public String tenMinutes(String mm){ + ArrayList minutes = new ArrayList (Arrays.asList("","Twenty","Thirty","Fourty","Fifty")); + StringBuilder stringBuilder = new StringBuilder(); if(mm.charAt(0) == '2'){ stringBuilder.append(minutes.get(1)); @@ -82,14 +97,15 @@ private String createMilitaryTimeMM(String mm) { if(mm.charAt(0) == '5'){ stringBuilder.append(minutes.get(4)); } + return stringBuilder.toString(); + } + public String secMinutes(String mm){ + StringBuilder stringBuilder = new StringBuilder(); - - - - - - - stringBuilder.append(" Hours"); + if(mm.charAt(1) == '2'){ + stringBuilder.append(" " + amStrings.get(2)); + } return stringBuilder.toString(); + } } From 71530c11a7099442161cb745cc67c9ceaaee20f2 Mon Sep 17 00:00:00 2001 From: Zachary Date: Mon, 4 Dec 2017 12:00:30 -0500 Subject: [PATCH 5/5] saving,working --- src/main/java/io/zipcoder/Problem6.java | 57 ++++++++++++++------- src/test/java/io/zipcoder/Problem6Test.java | 5 +- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 4e61991..f2ce52d 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -32,7 +32,7 @@ public String breakInput(String input) { String newHHString = createMilitaryTimeHH(HH, meridiem); String newMMString = createMilitaryTimeMM(MM); - return newHHString+newMMString; + return newHHString + newMMString; } else { @@ -44,7 +44,6 @@ public String breakInput(String input) { } - public String createMilitaryTimeHH(String input, String meridiem) { ArrayList pmStrings = new ArrayList (Arrays.asList("", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twentyone", "Twentytwo", "TwentyThree", "Twentyfour")); StringBuilder stringBuilder = new StringBuilder(); @@ -52,8 +51,7 @@ public String createMilitaryTimeHH(String input, String meridiem) { if (meridiem.equalsIgnoreCase("am")) { if (Integer.valueOf(input) < 10) stringBuilder.append(amStrings.get(0) + " "); - - stringBuilder.append(amStrings.get(Integer.valueOf(input)) + " "); + stringBuilder.append(amStrings.get(Integer.valueOf(input)) + " "); } else { stringBuilder.append(pmStrings.get(Integer.valueOf(input)) + " "); } @@ -66,45 +64,66 @@ public String createMilitaryTimeHH(String input, String meridiem) { private String createMilitaryTimeMM(String mm) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(" and "); - if (Integer.valueOf(mm)< 10) stringBuilder.append("Zero "); + if (Integer.valueOf(mm) < 10) stringBuilder.append("Zero"); stringBuilder.append(tenMinutes(mm)).append(secMinutes(mm)); - - - - - - stringBuilder.append(" Hours"); return stringBuilder.toString(); } - public String tenMinutes(String mm){ - ArrayList minutes = new ArrayList (Arrays.asList("","Twenty","Thirty","Fourty","Fifty")); + public String tenMinutes(String mm) { + ArrayList minutes = new ArrayList (Arrays.asList("", "Twenty", "Thirty", "Fourty", "Fifty")); StringBuilder stringBuilder = new StringBuilder(); - if(mm.charAt(0) == '2'){ + if (mm.charAt(0) == '2') { stringBuilder.append(minutes.get(1)); } - if(mm.charAt(0) == '3'){ + if (mm.charAt(0) == '3') { stringBuilder.append(minutes.get(2)); } - if(mm.charAt(0) == '4'){ + if (mm.charAt(0) == '4') { stringBuilder.append(minutes.get(3)); } - if(mm.charAt(0) == '5'){ + if (mm.charAt(0) == '5') { stringBuilder.append(minutes.get(4)); } return stringBuilder.toString(); } - public String secMinutes(String mm){ + + public String secMinutes(String mm) { StringBuilder stringBuilder = new StringBuilder(); - if(mm.charAt(1) == '2'){ + + + if (mm.charAt(1) == '1') { + stringBuilder.append("Eleven"); + } + if (mm.charAt(1) == '2') { stringBuilder.append(" " + amStrings.get(2)); } + if (mm.charAt(1) == '3') { + stringBuilder.append(" " + amStrings.get(3)); + } + if (mm.charAt(1) == '4') { + stringBuilder.append(" " + amStrings.get(4)); + } + if (mm.charAt(1) == '5') { + stringBuilder.append(" " + amStrings.get(5)); + } + if (mm.charAt(1) == '6') { + stringBuilder.append(" " + amStrings.get(6)); + } + if (mm.charAt(1) == '7') { + stringBuilder.append(" " + amStrings.get(7)); + } + if (mm.charAt(1) == '8') { + stringBuilder.append(" " + amStrings.get(8)); + } + if (mm.charAt(1) == '9') { + stringBuilder.append(" " + amStrings.get(9)); + } return stringBuilder.toString(); } diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index d128c41..46b5007 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -1,11 +1,10 @@ package io.zipcoder; import org.junit.Assert; -import org.junit.Before; + import org.junit.Test; -import java.util.ArrayList; -import java.util.Arrays; + public class Problem6Test {