diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 4ee4e64..1258460 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -1,4 +1,65 @@ package io.zipcoder; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; + public class Problem6 { + + String[] words = {"Zero One", "Zero Two", "Zero Three", "Zero Four", + "Zero Five", "Zero Six", "Zero Seven", "Zero Eight", "Zero Nine", + "Ten", "Eleven", "Twelve", "Thirteen", + "Fourteen", "Fifteen", "Sixteen", "Seventeen", + "Eighteen", "Nineteen", "Twenty", "Twenty One", + "Twenty Two", "Twenty Three", "Twenty Four","Twenty Five", "Twenty Six", + "Twenty Seven", "Twenty Eight", "Twenty Nine", "Thirty", "Thirty One", "Thirty Two", + "Thirty Three", "Thirty Four", "Thirty Five", "Thirty Six", "Thirty Seven", "Thirty Eight", + "Thirty Nine", "Forty", "Forty One", "Forty Two", "Forty Three", "Forty Four", "Forty Five", "Forty Six", + "Forty Seven", "Forty Eight", "Forty Nine", "Fifty", "Fifty One", "Fifty Two", "Fifty Three", "Fifty Four", + "Fifty Five", "Fifty Six", "Fifty Seven", "Fifty Eight", "Fifty Nine", "Sixty"}; + + + public String timeConverter(String inputTime){ + DateFormat inputString= new SimpleDateFormat("hh:mmaa"); + DateFormat outputString = new SimpleDateFormat("HH:mm"); + try { + String date = outputString.format(inputString.parse(inputTime)); + return date; + }catch (ParseException e) { + e.printStackTrace(); + } + return null; + } + + public String[] splitTime(String inputTime){ + String[] timeIsSplit = inputTime.split(":"); + return timeIsSplit; + } + + public String convertIndexToWord(String input){ + int inputAsNumber = Integer.parseInt(input); + String actualWord = words[inputAsNumber-1]; + return actualWord; + } + + + public String formatWords(String hundreds, String Hours){ + StringBuilder sb = new StringBuilder(); + sb.append(hundreds); + sb.append(" "); + sb.append("Hundred and "); + sb.append(Hours); + sb.append(" "); + sb.append("Hours"); + return sb.toString(); + } + + public String seeIfWholeThingWorks(String input){ + String convert = timeConverter(input); + String[] split = splitTime(convert); + String indexOne = convertIndexToWord(split[0]); + String indexTwo = convertIndexToWord(split[1]); + return formatWords(indexOne, indexTwo); + } + } diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index d262e88..75da65a 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -1,4 +1,121 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + public class Problem6Test { + + Problem6 newProblem6; + String testDate1 = "1:00pm"; + String testDate = "10:00am"; + String testDate2 = "3:30pm"; + String testDate3 = "3:30am"; + String militaryTime = "15:30"; + String[] militaryTimeArray = {"15", "30"}; + + @Before + public void setUp(){ + this.newProblem6 = new Problem6(); + } + + @Test + public void timeConverterTest(){ + //Given + String expected = "13:00"; + //When + String actual = newProblem6.timeConverter(testDate1); + //Then + Assert.assertEquals(expected,actual); + } + @Test + public void timeConverterTest1(){ + //Given + String expected = "10:00"; + //When + String actual = newProblem6.timeConverter(testDate); + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void timeConverterTest2(){ + //Given + String expected = "15:30"; + //When + String actual = newProblem6.timeConverter(testDate2); + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void timeConverterTest3(){ + //Given + String expected = "03:30"; + //When + String actual = newProblem6.timeConverter(testDate3); + //Then + Assert.assertEquals(expected,actual); + } + + + @Test + public void splitTimeTest(){ + //Given + Integer expected = militaryTimeArray.length; + //When + String[] splitTimeTest = newProblem6.splitTime(militaryTime); + Integer actual = splitTimeTest.length; + //Then + Assert.assertEquals(expected,actual); + } + @Test + public void splitTimeTest1(){ + //Given + String expected = militaryTimeArray[0]; + //When + String[] splitTimeTest = newProblem6.splitTime(militaryTime); + String actual = splitTimeTest[0]; + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void covertIndexOneToWordTest(){ + //Given + String expected = "Fifteen"; + //When + String actual = newProblem6.convertIndexToWord(militaryTimeArray[0]); + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void covertIndexTwoToWordTest(){ + //Given + String expected = "Thirty"; + //When + String actual = newProblem6.convertIndexToWord(militaryTimeArray[1]); + //Then + Assert.assertEquals(expected, actual); + } + + @Test + public void formatWordsTest(){ + //Given + String expected = "Fifteen Hundred and Thirty Hours"; + //When + String actual = newProblem6.formatWords("Fifteen", "Thirty"); + //Then + Assert.assertEquals(expected, actual); + } + + @Test + public void seeIfWholeThingWorksTest(){ + String expected = "Fifteen Hundred and Thirty Hours"; + String actual = newProblem6.seeIfWholeThingWorks(testDate2); + Assert.assertEquals(expected, actual); + + } + }