diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 4ee4e64..60cf5c0 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -1,4 +1,140 @@ package io.zipcoder; +import java.util.HashMap; public class Problem6 { + + private HashMap hours = new HashMap(); + private HashMap minutes = new HashMap(); + private HashMap multiplesOfTen = new HashMap(); + private String[] input = {}; + + public Problem6(){ + populateHours(); + populateMinutes(); + populateMultiplesOfTen(); + } + + // Converts String input to Military Time result + public String convertToMilitaryTime(String input) { + this.input = splitTime(input); + + if(inTheMorning(this.input)){ + return getHourInMilitaryAM(this.input[0]) + + " Hundred and " + + getMinutesInMilitary(this.input[1]) + + " Hours"; + } else { + return getHourInMilitaryPM(this.input[0]) + + " Hundred and " + + getMinutesInMilitary(this.input[1]) + + " Hours"; + } + } + + // Splits input into array // example: {10, 45, am} + protected String[] splitTime(String time){ + String[] splitTimes = time.split(":"); + String[] endSplitTime = { + splitTimes[0], + splitTimes[1].substring(0, 2), + time.substring(time.length() - 2) + }; + return endSplitTime; + } + + // Checks if the input contains (am) - morning time + protected boolean inTheMorning(String[] testArray){ + return (testArray[2].equalsIgnoreCase("am")); + } + + // Checks what number the minutes starts with and feeds to getMinutesInMilitary + private String minuteNumberStartsWith(String[] timeArray){ + String firstMinuteNumber = ""; + for(int i = 0; i < 6; i++){ + if(timeArray[1].substring(0, 1).equals(String.valueOf(i))){ + firstMinuteNumber += String.valueOf(i + "0"); + break; + } + } + return firstMinuteNumber; + } + + protected String getHourInMilitaryAM(String hour){ + if(Integer.parseInt(hour) == 12){ + return hours.get("0"); + } else { + return hours.get(hour); + } + } + + protected String getHourInMilitaryPM(String hour){ + Integer pmHour = Integer.parseInt(hour) + 12; + if(Integer.parseInt(hour) == 12){ + return hours.get("12"); + } else { + return hours.get(String.valueOf(pmHour)); + } + } + + // Converts standard minutes to military minutes + protected String getMinutesInMilitary(String inputMinutes){ + + if (Integer.parseInt(inputMinutes.substring(0, 2)) < 10) { + return hours.get(inputMinutes.substring(inputMinutes.length() - 1)); + } else if (Integer.parseInt(inputMinutes.substring(0, 2)) < 20){ + return hours.get(inputMinutes); + } else if (Integer.parseInt(inputMinutes.substring(0, 2)) % 10 == 0) { + return multiplesOfTen.get(minuteNumberStartsWith(this.input)); + } else { + return multiplesOfTen.get(minuteNumberStartsWith(this.input)) + " " + + minutes.get(inputMinutes.substring(inputMinutes.length() - 1)); + } + } + + private void populateHours(){ + hours.put("0", "Zero Zero"); + hours.put("1", "Zero One"); + hours.put("2", "Zero Two"); + hours.put("3", "Zero Three"); + hours.put("4", "Zero Four"); + hours.put("5", "Zero Five"); + hours.put("6", "Zero Six"); + hours.put("7", "Zero Seven"); + hours.put("8", "Zero Eight"); + hours.put("9", "Zero Nine"); + hours.put("10", "Ten"); + hours.put("11", "Eleven"); + hours.put("12", "Twelve"); + hours.put("13", "Thirteen"); + hours.put("14", "Fourteen"); + hours.put("15", "Fifteen"); + hours.put("16", "Sixteen"); + hours.put("17", "Seventeen"); + hours.put("18", "Eighteen"); + hours.put("19", "Nineteen"); + hours.put("20", "Twenty"); + hours.put("21", "Twenty One"); + hours.put("22", "Twenty Two"); + hours.put("23", "Twenty Three"); + } + + private void populateMinutes(){ + minutes.put("1", "One"); + minutes.put("2", "Two"); + minutes.put("3", "Three"); + minutes.put("4", "Four"); + minutes.put("5", "Five"); + minutes.put("6", "Six"); + minutes.put("7", "Seven"); + minutes.put("8", "Eight"); + minutes.put("9", "Nine"); + } + + private void populateMultiplesOfTen(){ + multiplesOfTen.put("10", "Ten"); + multiplesOfTen.put("20", "Twenty"); + multiplesOfTen.put("30", "Thirty"); + multiplesOfTen.put("40", "Forty"); + multiplesOfTen.put("50", "Fifty"); + } } diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index d262e88..3938949 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -1,4 +1,59 @@ package io.zipcoder; + +import org.junit.Assert; +import org.junit.Test; + public class Problem6Test { + + @Test + public void convertToMilitaryTimeAMTest(){ + Problem6 problem6 = new Problem6(); + + String expected = "Eleven Hundred and Thirty Three Hours"; + String actual = problem6.convertToMilitaryTime("11:33am"); + + Assert.assertEquals(expected, actual); + } + + @Test + public void convertToMilitaryTimePMTest(){ + Problem6 problem6 = new Problem6(); + + String expected = "Twenty Three Hundred and Thirty Three Hours"; + String actual = problem6.convertToMilitaryTime("11:33pm"); + + Assert.assertEquals(expected, actual); + } + + @Test + public void splitTimeTest(){ + Problem6 problem6 = new Problem6(); + + String[] expected = {"10", "35", "pm"}; + String[] actual = problem6.splitTime("10:35pm"); + + Assert.assertEquals(expected, actual); + } + + @Test + public void isItMorningTest(){ + Problem6 problem6 = new Problem6(); + String[] timeArray = {"10", "35", "am"}; + + boolean actual = problem6.inTheMorning(timeArray); + + Assert.assertTrue(actual); + } + + @Test + public void isItAfternoonTest(){ + Problem6 problem6 = new Problem6(); + String[] timeArray = {"10", "35", "pm"}; + + boolean actual = problem6.inTheMorning(timeArray); + + Assert.assertFalse(actual); + } + }