Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/main/java/io/zipcoder/Problem6.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
package io.zipcoder;

public class Problem6 {

public String buildString(String input) {
Integer hours = 0;
Integer minutes = 0;
String ret = "";
String[] colonSplit = input.split(":");
hours += Integer.parseInt(colonSplit[0]);
minutes += Integer.parseInt(colonSplit[1].substring(0, 2));
if (colonSplit[1].charAt(2)=='p') {
hours += 12;
}
if (hours < 10) {
ret += "Zero ";
}
ret += hoursToWords(hours);
ret += " Hundred ";
if (colonSplit[1].charAt(0)!='0') {
ret += "and ";
} else {
ret += "Zero ";
}
ret += minutesToWords(minutes);
ret += " Hours";
return ret;
}

private String hoursToWords(Integer hours) {
String ret = "";
ret += TimeEnum.parseToWord(hours);
return ret;
}

private String minutesToWords(Integer minutes) {
String ret = "";
ret += TimeEnum.parseToWord(minutes);
return ret;
}

}
92 changes: 92 additions & 0 deletions src/main/java/io/zipcoder/TimeEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package io.zipcoder;

import java.util.HashMap;
import java.util.Map;

public enum TimeEnum {
ZERO(0, "Zero"),
ONE(1, "One"),
TWO(2, "Two"),
THREE(3, "Three"),
FOUR(4, "Four"),
FIVE(5, "Five"),
SIX(6, "Six"),
SEVEN(7, "Seven"),
EIGHT(8, "Eight"),
NINE(9, "Nine"),
TEN(10, "Ten"),
ELEVEN(11, "Eleven"),
TWELVE(12, "Twelve"),
THIRTEEN(13, "Thirteen"),
FOURTEEN(14, "Fourteen"),
FIFTEEN(15, "Fifteen"),
SIXTEEN(16, "Sixteen"),
SEVENTEEN(17, "Seventeen"),
EIGHTEEN(18, "Eighteen"),
NINETEEN(19, "Nineteen"),
TWENTY(20, "Twenty"),
TWENTY_ONE(21, "Twenty One"),
TWENTY_TWO(22, "Twenty Two"),
TWENTY_THREE(23, "Twenty Three"),
TWENTY_FOUR(24, "Twenty Four"),
TWENTY_FIVE(25, "Twenty Five"),
TWENTY_SIX(26, "Twenty Six"),
TWENTY_SEVEN(27, "Twenty Seven"),
TWENTY_EIGHT(28, "Twenty Eight"),
TWENTY_NINE(29, "Twenty Nine"),
THIRTY(30, "Thirty"),
THIRTY_ONE(31, "Thirty One"),
THIRTY_TWO(32, "Thirty Two"),
THIRTY_THREE(33, "Thirty Three"),
THIRTY_FOUR(34, "Thirty Four"),
THIRTY_FIVE(35, "Thirty Five"),
THIRTY_SIX(36, "Thirty Six"),
THIRTY_SEVEN(37, "Thirty Seven"),
THIRTY_EIGHT(38, "Thirty Eight"),
THIRTY_NINE(39, "Thirty Nine"),
FORTY(40, "Forty"),
FORTY_ONE(41, "Forty One"),
FORTY_TWO(42, "Forty Two"),
FORTY_THREE(43, "Forty Three"),
FORTY_FOUR(44, "Forty Four"),
FORTY_FIVE(45, "Forty Five"),
FORTY_SIX(46, "Forty Six"),
FORTY_SEVEN(47, "Forty Seven"),
FORTY_EIGHT(48, "Forty Eight"),
FORTY_NINE(49, "Forty Nine"),
FIFTY(50, "Fifty"),
FIFTY_ONE(51, "Fifty One"),
FIFTY_TWO(52, "Fifty Two"),
FIFTY_THREE(53, "Fifty Three"),
FIFTY_FOUR(54, "Fifty Four"),
FIFTY_FIVE(55, "Fifty Five"),
FIFTY_SIX(56, "Fifty Six"),
FIFTY_SEVEN(57, "Fifty Seven"),
FIFTY_EIGHT(58, "Fifty Eight"),
FIFTY_NINE(59, "Fifty Nine");

final int hourValue;
final String hourWord;
static final Map lookup = new HashMap();

static {
for (TimeEnum t : TimeEnum.values()) {
lookup.put(t.getHourValue(), t.getHourWord());
}
}

TimeEnum(int hourValue, String hourWord) {
this.hourValue = hourValue;
this.hourWord = hourWord;
}

public int getHourValue() {return this.hourValue;}

public String getHourWord() {return this.hourWord;}

public static String parseToWord(int hourValue) {
return (String) lookup.get(hourValue);
}


}
44 changes: 44 additions & 0 deletions src/test/java/io/zipcoder/Problem6Test.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,48 @@
package io.zipcoder;

import org.junit.Assert;
import org.junit.Test;

public class Problem6Test {

@Test
public void testParse1() {
Problem6 problem6 = new Problem6();
String expected = "Thirteen Hundred and Thirty Hours";
String actual = problem6.buildString("1:30pm");
Assert.assertEquals(expected, actual);
}

@Test
public void testParse2() {
Problem6 problem6 = new Problem6();
String expected = "Zero One Hundred and Thirty Hours";
String actual = problem6.buildString("1:30am");
Assert.assertEquals(expected, actual);
}

@Test
public void testParse3() {
Problem6 problem6 = new Problem6();
String expected = "Fourteen Hundred and Twenty Two Hours";
String actual = problem6.buildString("2:22pm");
Assert.assertEquals(expected, actual);
}

@Test
public void testParse4() {
Problem6 problem6 = new Problem6();
String expected = "Zero Two Hundred and Eleven Hours";
String actual = problem6.buildString("2:11am");
Assert.assertEquals(expected, actual);
}

@Test
public void testParse5() {
Problem6 problem6 = new Problem6();
String expected = "Ten Hundred Zero Two Hours";
String actual = problem6.buildString("10:02am");
Assert.assertEquals(expected, actual);
}

}