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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>io.zipcoder</groupId>
<artifactId>InterviewProblem5</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/io/zipcoder/Militarizer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.zipcoder;

import java.util.HashMap;

public class Militarizer {
private HashMap<String,String> teens = new HashMap<>();
private HashMap<String,String> firstDigits = new HashMap<>();
private HashMap<String,String> secondDigits = new HashMap<>();

public Militarizer(){
buildTeensMap();
buildFirstDigitsMap();
buildSecondDigitsMap();
}


public String speaker(String input){
if(teens.containsKey(input)){
return teens.get(input);
}
else if (input.charAt(1) == '0'){
return firstDigits.get(input.substring(0,1));
}
else{
return firstDigits.get(input.substring(0,1)) + " " + secondDigits.get(input.substring(1));
}
}



private void buildTeensMap(){
teens.put("10", "Ten");
teens.put("11", "Eleven");
teens.put("12", "Twelve");
teens.put("13", "Thirteen");
teens.put("14", "Fourteen");
teens.put("15", "Fifteen");
teens.put("16", "Sixteen");
teens.put("17", "Seventeen");
teens.put("18", "Eighteen");
teens.put("19", "Nineteen");
}

private void buildFirstDigitsMap(){
firstDigits.put("0", "Zero");
firstDigits.put("2","Twenty");
firstDigits.put("3","Thirty");
firstDigits.put("4","Forty");
firstDigits.put("5","Fifty");
}

private void buildSecondDigitsMap(){
secondDigits.put("0", "");
secondDigits.put("1", "One");
secondDigits.put("2", "Two");
secondDigits.put("3", "Three");
secondDigits.put("4", "Four");
secondDigits.put("5", "Five");
secondDigits.put("6", "Six");
secondDigits.put("7", "Seven");
secondDigits.put("8", "Eight");
secondDigits.put("9", "Nine");









}

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

public class Problem6 {

public String timeConverter(String time){
Militarizer converter = new Militarizer();

String [] stepOne = TimeConverter.splitTime(time);
String [] stepTwo = TimeConverter.convertTime(stepOne);

return converter.speaker(stepTwo[0]) + " Hundred and " + converter.speaker(stepTwo[1]) + " Hours";
}

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

import static java.lang.Integer.parseInt;

public class TimeConverter {

public static String[] splitTime(String time){
String[] stepOne = time.split(":");
String[] stepTwo = {stepOne[1].substring(0,2), stepOne[1].substring(2)};
String[] output = {stepOne[0],stepTwo[0],stepTwo[1]};
if(output[0].length()==1){
output[0] = "0" + output[0];
}

return output;
}

public static String[] convertTime(String [] input){
if(input[2].equals("pm")){
Integer workingInt = parseInt(input[0]);
workingInt +=12;
String[] output = {workingInt.toString(),input[1]};
return output;
}

else{
String[] output = {input[0],input[1]};
return output;
}
}

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

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

public class MilitarizerTest {

Militarizer test;

@Before
public void setup(){
test = new Militarizer();
}


@Test
public void SpeakerTest(){
//Given
String input = "11";

//When
String expected = "Eleven";
String actual = test.speaker(input);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void SpeakerTestTwo(){
//Given
String input = "05";

//When
String expected = "Zero Five";
String actual = test.speaker(input);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void SpeakerTestThree(){
//Given
String input = "30";

//When
String expected = "Thirty";
String actual = test.speaker(input);

//Then
Assert.assertEquals(expected,actual);
}

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

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

public class Problem6Test {


@Test
public void timeTestOne(){
//Given
Problem6 tester = new Problem6();
String input = "1:30pm";

//When
String expected = "Thirteen Hundred and Thirty Hours";
String actual = tester.timeConverter(input);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void timeTestTwo(){
//Given
Problem6 tester = new Problem6();
String input = "1:30am";

//When
String expected = "Zero One Hundred and Thirty Hours";
String actual = tester.timeConverter(input);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void timeTestThree(){
//Given
Problem6 tester = new Problem6();
String input = "2:22pm";

//When
String expected = "Fourteen Hundred and Twenty Two Hours";
String actual = tester.timeConverter(input);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void timeTestFour(){
//Given
Problem6 tester = new Problem6();
String input = "2:11am";

//When
String expected = "Zero Two Hundred and Eleven Hours";
String actual = tester.timeConverter(input);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void timeTestFive(){
//Given
Problem6 tester = new Problem6();
String input = "10:02am";

//When
String expected = "Ten Hundred and Zero Two Hours";
String actual = tester.timeConverter(input);

//Then
Assert.assertEquals(expected,actual);
}


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

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

public class TimeConverterTest {

@Test
public void timeSplitTestOne(){
//Given
String input = "1:30pm";

//When
String [] expected = {"01","30","pm"};
String [] actual = TimeConverter.splitTime(input);

//Then
Assert.assertArrayEquals(expected,actual);
}

@Test
public void timeSplitTestTwo(){
//Given
String input = "12:30pm";

//When
String [] expected = {"12","30","pm"};
String [] actual = TimeConverter.splitTime(input);

//Then
Assert.assertArrayEquals(expected,actual);
}

@Test
public void timeConvertTestOne() {
//Given
String [] input = {"01","30","pm"};

//When
String [] expected = {"13","30"};
String [] actual = TimeConverter.convertTime(input);

//Then
Assert.assertArrayEquals(expected,actual);
}

@Test
public void timeConvertTestTwo() {
//Given
String [] input = {"01","30","am"};

//When
String [] expected = {"01","30"};
String [] actual = TimeConverter.convertTime(input);

//Then
Assert.assertArrayEquals(expected,actual);
}

}