diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 4ee4e64..db99bf2 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -1,4 +1,112 @@ package io.zipcoder; public class Problem6 { + + private String militaryHour; + private String militaryMinute; + + private String[] hoursArray = {"Zero Hundred ", "Zero One Hundred ", "Zero Two Hundred ", "Zero Three Hundred ", + "Zero Four Hundred ", "Zero Five Hundred ", "Zero Six Hundred ", " Zero Seven Hundred ", + "Zero Eight Hundred ", "Zero Nine Hundred ", "Ten Hundred ", "Eleven Hundred ", + "Twelve Hundred ", "Thirteen Hundred ", "Fourteen Hundred ", "Fifteen Hundred ", + "Sixteen Hundred ", "Seventeen Hundred ", "Eighteen Hundred ", "Nineteen Hundred ", + "Twenty Hundred ", "Twenty One Hundred ", "Twenty Two Hundred ", "Twenty Three Hundred ", + "Twenty Four Hundred "}; + private String[] minutesArray = {"", "and One", "and Two", "and Three", "and Four", "and Five", "and Six", "and Seven", "and Eight", "and Nine", "and Ten", + "and Eleven", "and Twelve", "and Thirteen", "and Fourteen", "and Fifteen", "and Sixteen", "and Seventeen", "and Eighteen", "and Nineteen", "and Twenty", + "and Twenty One", "and Twenty Two", "and Twenty Three", "and Twenty Four", "and Twenty Five", "and Twenty Six", "and Twenty Seven", "and Twenty Eight", "and Twenty Nine", "and Thirty", + "and Thirty One", "and Thirty Two", "and Thirty Three", "and Thirty Four", "and Thirty Five", "and Thirty Six", "and Thirty Seven", "and Thirty Eight", "and Thirty Nine", "and Fourty", + "and Fourty One", "and Fourty Two", "and Fourty Three", "and Fourty Four", "and Fourty Five", "and Fourty Six", "and Fourty Seven", "and Fourty Eight", "and Fourty Nine", "and Fifty", + "and Fifty One", "and Fifty Two", "and Fifty Three", "and Fifty Four", "and Fifty Five", "and Fifty Six", "and Fifty Seven", "and Fifty Eight", "and Fifty Nine"}; + + public String[] splitTime(String timeInput) { + String[] splitHourAndMinute = timeInput.split(":"); + return splitHourAndMinute; + } + + public Integer parseForHour(String timeInput) { + String[] hour = splitTime(timeInput); + Integer intHour = Integer.parseInt(hour[0]); + + if (hour[1].contains("p") || hour[1].contains("P")) { + return intHour += 12; + } else { + return intHour; + } + } + + public Integer parseForMinute(String timeInput) { + String[] minute = splitTime(timeInput); + String minutePortion = minute[1]; + + //Using Stringbuilder to append minute without the am or pm + StringBuilder noAmOrPm = new StringBuilder(); + noAmOrPm.append(minutePortion.charAt(0)); + noAmOrPm.append(minutePortion.charAt(1)); + + //StringBuilder to String + String minuteToString = noAmOrPm.toString(); + + //String parsed to Integer + Integer minuteOnly = Integer.parseInt(minuteToString); + + return minuteOnly; + } + + public String getMilitaryHour(Integer hour) { + militaryHour = hoursArray[hour]; + return militaryHour; + } + + public String getMilitaryMinute(Integer minute) { + militaryMinute = minutesArray[minute]; + return militaryMinute; + + } + + //Testing this method in main method below + public String getFullMilitaryTime(String hour, String minute) { + return militaryHour + "Hundred " + militaryMinute + " Hours"; + } + + public static void main(String[] args) + { + String timeInput = "1:30pm"; + String militaryHour; + String militaryMinutes; + + Problem6 problem6 = new Problem6(); + militaryHour = problem6.getMilitaryHour(problem6.parseForHour(timeInput)); + militaryMinutes = problem6.getMilitaryMinute(problem6.parseForMinute(timeInput)); + problem6.getFullMilitaryTime(militaryHour, militaryMinutes); + System.out.println(problem6.getFullMilitaryTime(militaryHour, militaryMinutes)); + } + } + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index d262e88..39136fa 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -1,4 +1,282 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + public class Problem6Test { + + @Test + public void testSplitTime() + { + //Given + Problem6 problem6 = new Problem6(); + String timeInput = "1:30pm"; + String hour = "1"; + String minute = "30"; + String [] expected = {"1", "30pm"}; + + //When + String [] actual = problem6.splitTime(timeInput); + + //Then + Assert.assertEquals(expected, actual); + + + } + + @Test + public void testParseForHour1() + { + //Given + Problem6 problem6 = new Problem6(); + String timeInput = "1:30pm"; + Integer expected = 13; + + //When + Integer actual = problem6.parseForHour(timeInput); + + //Then + Assert.assertEquals(expected, actual); + + + } + + @Test + public void testParseForHour2() + { + //Given + Problem6 problem6 = new Problem6(); + String timeInput = "2:30pm"; + Integer expected = 14; + + //When + Integer actual = problem6.parseForHour(timeInput); + + //Then + Assert.assertEquals(expected, actual); + } + + @Test + public void testParseForHour3() + { + //Given + Problem6 problem6 = new Problem6(); + String timeInput = "3:30pm"; + Integer expected = 15; + + //When + Integer actual = problem6.parseForHour(timeInput); + + //Then + Assert.assertEquals(expected, actual); + } + + @Test + public void testParseForHour4() + { + //Given + Problem6 problem6 = new Problem6(); + String timeInput = "7:00pm"; + Integer expected = 19; + + //When + Integer actual = problem6.parseForHour(timeInput); + + //Then + Assert.assertEquals(expected, actual); + } + + @Test + public void testParseForHour5() + { + //Given + Problem6 problem6 = new Problem6(); + String timeInput = "1:30am"; + Integer expected = 1; + + //When + Integer actual = problem6.parseForHour(timeInput); + + //Then + Assert.assertEquals(expected, actual); + + + } + + @Test + public void testParseForHour6() + { + //Given + Problem6 problem6 = new Problem6(); + String timeInput = "2:30am"; + Integer expected = 2; + + //When + Integer actual = problem6.parseForHour(timeInput); + + //Then + Assert.assertEquals(expected, actual); + } + + @Test + public void testParseForHour7() + { + //Given + Problem6 problem6 = new Problem6(); + String timeInput = "3:30am"; + Integer expected = 3; + + //When + Integer actual = problem6.parseForHour(timeInput); + + //Then + Assert.assertEquals(expected, actual); + } + + @Test + public void testParseForHour8() + { + //Given + Problem6 problem6 = new Problem6(); + String timeInput = "7:00am"; + Integer expected = 7; + + //When + Integer actual = problem6.parseForHour(timeInput); + + //Then + Assert.assertEquals(expected, actual); + } + + @Test + public void testParseForMinute1() + { + //Given + Problem6 problem6 = new Problem6(); + String timeInput = "4:45am"; + Integer expected = 45; + + //When + Integer actual = problem6.parseForMinute(timeInput); + + //Then + Assert.assertEquals(expected, actual); + + + } + + @Test + public void testParseForMinute2() + { + //Given + Problem6 problem6 = new Problem6(); + String timeInput = "4:03am"; + Integer expected = 03; + + //When + Integer actual = problem6.parseForMinute(timeInput); + + //Then + Assert.assertEquals(expected, actual); + + } + + @Test + public void testgetMilitaryHour1() + { + //Given + Problem6 problem6 = new Problem6(); + Integer hour = 16; + String expected = "Sixteen Hundred "; + + //When + String actual = problem6.getMilitaryHour(hour); + + //Then + Assert.assertEquals(expected, actual); + } + + @Test + public void testgetMilitaryHour2() + { + //Given + Problem6 problem6 = new Problem6(); + Integer hour = 1; + String expected = "Zero One Hundred "; + + //When + String actual = problem6.getMilitaryHour(hour); + + //Then + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetMilitaryMinute1() + { + //Given + Problem6 problem6 = new Problem6(); + Integer minute = 00; + String expected = ""; + + //When + String actual = problem6.getMilitaryMinute(minute); + + //Then + Assert.assertEquals(expected, actual); + + + } + + @Test + public void testGetMilitaryMinute2() + { + //Given + Problem6 problem6 = new Problem6(); + Integer minute = 01; + String expected = "and One"; + + //When + String actual = problem6.getMilitaryMinute(minute); + + //Then + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetMilitaryTime3() + { + //Given + Problem6 problem6 = new Problem6(); + Integer minute = 02; + String expected = "and Two"; + + //When + String actual = problem6.getMilitaryMinute(minute); + + //Then + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetMilitaryTime4() + { + //Given + Problem6 problem6 = new Problem6(); + Integer minute = 59; + String expected = "and Fifty Nine"; + + //When + String actual = problem6.getMilitaryMinute(minute); + + //Then + Assert.assertEquals(expected, actual); + } + + + + + + + }