diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 4ee4e64..1bbbaa1 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -1,4 +1,58 @@ package io.zipcoder; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + public class Problem6 { + //Step 1: convert given 12 hour numerical format into 24 hours format + //use SimpleDateFormat to format input to 24 hours + + String[] units = {"Zero", "One", "Two", "Three", "Four", + "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", + "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; + String[] tens = {"", "", "Twenty", "Thirty", "Forty", "Fifty"}; + public String convertTo24Hours(String input) { + SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm"); + SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a"); + Date date = null; + try { + date = parseFormat.parse(input); + } catch (ParseException e) { + e.printStackTrace(); + } + return displayFormat.format(date); + } + + //Step 2: convert the 24 hour format into military time phrase in string format + //after the first two digit, add "Hundred and" to the end + //add Hours at the end of each string + public String convertToMilitaryTime(String input){ + String formattedInput = convertTo24Hours(input); + String[] time= formattedInput.split(":"); + String hour = convertToWords(time[0]); + String minute=convertToWords(time[1]); + return hour + " Hundred and " + minute + " Hours"; + + + } + public String convertToWords(String input){ + int num = Integer.parseInt(input); + String word=""; + if(num<10){ + word+=units[0]+" "+units[num]; + } + if(num<20 && num>=10){ + word+=units[num]; + } + if(num>=20){ + if(num%10==0){ + word+=tens[num/10]; + } + else { + word += tens[num / 10] + " " + units[num % 10]; + } + } + return word; + } } diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index d262e88..0bdd0ff 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -1,4 +1,41 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + public class Problem6Test { + Problem6 problem6 = new Problem6(); + @Test + public void testConvertTo24Hours(){ + String expected= "14:30"; + String actual = problem6.convertTo24Hours("2:30 pm"); + Assert.assertEquals(expected,actual); + System.out.println(actual); + } + //output should Fourteen Hundred and Thirty Hours + @Test + public void testConvert() { + String expected = "Fourteen Hundred and Thirty Hours"; + String actual = problem6.convertToMilitaryTime("2:30 pm"); + Assert.assertEquals(expected, actual); + } + @Test + public void testConvert2(){ + String expected="Fourteen Hundred and Zero Hours"; + String actual = problem6.convertToMilitaryTime("2:00 pm"); + Assert.assertEquals(expected,actual); + System.out.println(actual); + } + @Test + public void testConvertTo24Hours2(){ + String expected = "09:00"; + String actual = problem6.convertTo24Hours("9:00 am"); + Assert.assertEquals(expected,actual); + } + @Test + public void testConvert3(){ + String expected ="Zero Two Hundred and Eleven Hours"; + String actual = problem6.convertToMilitaryTime("2:11 am"); + Assert.assertEquals(expected,actual); + } }