diff --git a/pom.xml b/pom.xml index 5fd8efa..7584aab 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ io.zipcoder InterviewProblem5 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.7 + 1.7 + + + + diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 4ee4e64..64a8d61 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -1,4 +1,156 @@ package io.zipcoder; +import java.util.ArrayList; + public class Problem6 { + + private ArrayList hoursArrayList = new ArrayList<>(); + private ArrayList minutesArrayList = new ArrayList<>(); + + public Problem6() { + populateHours(); + populateMinutes(); + } + + public String convertToMilitaryTime(String input) { + String militaryTime = ""; + + boolean isPm = checkIfPm(input); + Integer hours = getHours(input); + Integer minutes = getMinutes(input); + + militaryTime += convertHoursToString(hours, isPm); + militaryTime += " Hundred "; + militaryTime += convertMinutesToString(minutes); + militaryTime += " Hours"; + + return militaryTime; + } + + public boolean checkIfPm(String input) { + input = input.toLowerCase(); + return input.contains("pm"); + } + + public Integer getHours(String input) throws NumberFormatException{ + String[] splitAtColon = input.split(":"); + String hoursString = splitAtColon[0]; + return verifyValidInput(hoursString); + } + + public Integer getMinutes(String input) throws NumberFormatException { + String[] splitAtColon = input.split(":"); + String minutesString = splitAtColon[1].substring(0,2); + return verifyValidInput(minutesString); + } + + public Integer verifyValidInput(String input) throws NumberFormatException { + Integer returnInteger; + try { + returnInteger = Integer.parseInt(input); + return returnInteger; + } + catch (NumberFormatException e) { + throw new NumberFormatException(); + } + } + + public void populateHours() { + hoursArrayList.add("Nothing"); + hoursArrayList.add("One"); + hoursArrayList.add("Two"); + hoursArrayList.add("Three"); + hoursArrayList.add("Four"); + hoursArrayList.add("Five"); + hoursArrayList.add("Six"); + hoursArrayList.add("Seven"); + hoursArrayList.add("Eight"); + hoursArrayList.add("Nine"); + hoursArrayList.add("Ten"); + hoursArrayList.add("Eleven"); + hoursArrayList.add("Twelve"); + hoursArrayList.add("Thirteen"); + hoursArrayList.add("Fourteen"); + hoursArrayList.add("Fifteen"); + hoursArrayList.add("Sixteen"); + hoursArrayList.add("Seventeen"); + hoursArrayList.add("Eighteen"); + hoursArrayList.add("Nineteen"); + hoursArrayList.add("Twenty"); + hoursArrayList.add("Twenty One"); + hoursArrayList.add("Twenty Two"); + hoursArrayList.add("Twenty Three"); + hoursArrayList.add("Twenty Four"); + } + + public void populateMinutes() { + for (String hour : hoursArrayList) { + minutesArrayList.add(hour); + } + minutesArrayList.add("Twenty Five"); + minutesArrayList.add("Twenty Six"); + minutesArrayList.add("Twenty Seven"); + minutesArrayList.add("Twenty Eight"); + minutesArrayList.add("Twenty Nine"); + minutesArrayList.add("Thirty"); + minutesArrayList.add("Thirty One"); + minutesArrayList.add("Thirty Two"); + minutesArrayList.add("Thirty Three"); + minutesArrayList.add("Thirty Four"); + minutesArrayList.add("Thirty Five"); + minutesArrayList.add("Thirty Six"); + minutesArrayList.add("Thirty Seven"); + minutesArrayList.add("Thirty Eight"); + minutesArrayList.add("Thirty Nine"); + minutesArrayList.add("Forty"); + minutesArrayList.add("Forty One"); + minutesArrayList.add("Forty Two"); + minutesArrayList.add("Forty Three"); + minutesArrayList.add("Forty Four"); + minutesArrayList.add("Forty Five"); + minutesArrayList.add("Forty Six"); + minutesArrayList.add("Forty Seven"); + minutesArrayList.add("Forty Eight"); + minutesArrayList.add("Forty Nine"); + minutesArrayList.add("Fifty"); + minutesArrayList.add("Fifty One"); + minutesArrayList.add("Fifty Two"); + minutesArrayList.add("Fifty Three"); + minutesArrayList.add("Fifty Four"); + minutesArrayList.add("Fifty Five"); + minutesArrayList.add("Fifty Six"); + minutesArrayList.add("Fifty Seven"); + minutesArrayList.add("Fifty Eight"); + minutesArrayList.add("Fifty Nine"); + } + + public ArrayList getHoursArrayList() { + return hoursArrayList; + } + + public ArrayList getMinutesArrayList() { + return minutesArrayList; + } + + public String convertHoursToString(Integer hour, boolean isPm) { + String hours = ""; + if (!isPm && hour < 10) { + hours += "Zero "; + } + if (isPm) { + return hours + hoursArrayList.get(hour + 12); + } + return hours + hoursArrayList.get(hour); + } + + public String convertMinutesToString(Integer minute) { + String minutes = ""; + if (minute < 10) { + minutes += "Zero "; + } + else { + minutes += "and "; + } + return minutes + minutesArrayList.get(minute); + } } diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index d262e88..bb53470 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -1,4 +1,146 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + public class Problem6Test { + + Problem6 problem6; + + @Before + public void setUp() { + problem6 = new Problem6(); + } + + @Test + public void convertToMilitaryTimeTest_Input130pm() { + String input = "1:30pm"; + String expected = "Thirteen Hundred and Thirty Hours"; + String actual = problem6.convertToMilitaryTime(input); + + Assert.assertEquals(expected, actual); + } + + @Test + public void convertToMilitaryTimeTest_Input130am() { + String input = "1:30am"; + String expected = "Zero One Hundred and Thirty Hours"; + + String actual = problem6.convertToMilitaryTime(input); + + Assert.assertEquals(expected, actual); + } + + @Test + public void convertToMilitaryTimeTest_Input222pm() { + String input = "2:22pm"; + String expected = "Fourteen Hundred and Twenty Two Hours"; + + String actual = problem6.convertToMilitaryTime(input); + + Assert.assertEquals(expected, actual); + } + + @Test + public void convertToMilitaryTimeTest_Input211am() { + String input = "2:11am"; + String expected = "Zero Two Hundred and Eleven Hours"; + + String actual = problem6.convertToMilitaryTime(input); + + Assert.assertEquals(expected, actual); + } + + @Test + public void convertToMilitaryTimeTest_Input1002am() { + String input = "10:02am"; + String expected = "Ten Hundred Zero Two Hours"; + + String actual = problem6.convertToMilitaryTime(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void checkIfPmTest_expectedTrue() { + String input = "1:30pm"; + Assert.assertTrue(problem6.checkIfPm(input)); + } + + @Test + public void checkIfPmTest_expectedFalse() { + String input = "5:30am"; + Assert.assertFalse(problem6.checkIfPm(input)); + } + + @Test + public void getHoursTest_expected1() { + String input = "1:30pm"; + Integer expected = 1; + Integer actual = problem6.getHours(input); + Assert.assertEquals(expected, actual); + } + + @Test (expected = NumberFormatException.class) + public void verifyValidInputTestFromGetHours_ExpectedNumberFormatException() { + String input = "Tariq:30pm"; + Integer actual = problem6.getHours(input); + } + + @Test + public void getMinutesTest_expected30() { + String input = "1:30pm"; + Integer expected = 30; + Integer actual = problem6.getMinutes(input); + Assert.assertEquals(expected, actual); + } + + @Test (expected = NumberFormatException.class) + public void verifyValidInputTestFromGetMinutes_expectedNumberFormatException() { + String input = "1:lolpm"; + Integer actual = problem6.getMinutes(input); + } + + @Test + public void testPopulateHours_ExpectedSize25() { + int expected = 25; + int actual = problem6.getHoursArrayList().size(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testPopulateMinutes_ExpectedSize60() { + int expected = 60; + int actual = problem6.getMinutesArrayList().size(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void convertHoursToString_expectedFourteen() { + String input = "2:30pm"; + String expected = "Fourteen"; + + Integer hours = problem6.getHours(input); + + boolean isPm = problem6.checkIfPm(input); + + String actual = problem6.convertHoursToString(hours, isPm); + + Assert.assertEquals(expected, actual); + } + + @Test + public void convertMinutesToString_expectedThirty() { + String input = "2:30pm"; + String expected = "and Thirty"; + + Integer minutes = problem6.getMinutes(input); + + String actual = problem6.convertMinutesToString(minutes); + + Assert.assertEquals(expected, actual); + } + }