Skip to content
Open

Done #33

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.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

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

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

public class Problem6 {


Map<Integer, String> myMap = new HashMap<Integer, String>();
String[] numberString = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
"Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Ninteen", "Twenty"};

public Map<Integer, String> populateMap() {
for (int i = 0; i < numberString.length; i++) {
myMap.put(i, numberString[i]);
}
myMap.put(21, "Twenty One");
myMap.put(22, "Twenty Two");
myMap.put(23, "Twenty Three");
myMap.put(30, "Thirty");
myMap.put(40, "Forty");
myMap.put(50, "Fifty");
return myMap;
}

public String militaryTimeConverter(String input) {
myMap = populateMap();
String outPut = "";
String inputTime="";
if(input.contains("am")){
inputTime += input.replace("am", "");
}else{
inputTime+=input.replace("pm","");
}

String[] inputTimeArray = inputTime.split(":");
int[] convertedToInt = stringToIntConverter(inputTimeArray);
if(input.contains("am")){
outPut+=amTimeFormat(convertedToInt[0],myMap);
}else{
outPut+=pmTimeFormat(convertedToInt[0],myMap);
}
outPut+=minuteFormat(convertedToInt[1]);

return outPut;
}

public int[] stringToIntConverter(String[] input) {
int[] intArray = new int[0];
for (int i = 0; i < input.length; i++) {
intArray = Arrays.copyOf(intArray, intArray.length + 1);
intArray[i] = Integer.parseInt(String.valueOf(input[i]));
}
return intArray;
}

public String pmTimeFormat(int inputHour,Map<Integer, String> inputMap) {
String outPut = "";
if (inputHour == 12) {
outPut += inputMap.get(inputHour);
} else {
outPut += inputMap.get(12 + inputHour);
}
return outPut;
}

public String amTimeFormat(int inputHour,Map<Integer, String> inputMap) {
String outPut = "";
if (inputHour < 10) {
outPut += "Zero ";
outPut += inputMap.get(inputHour);
} else if (inputHour == 12) {
outPut += "Zero ";
outPut += inputMap.get(0);
} else {
outPut += inputMap.get(inputHour);
}
return outPut;

}


public String minuteFormat(int inputMinute){
String outPut="";
int divisionByTen = inputMinute / 10;
int modulus = inputMinute % 10;
if (inputMinute< 24) {
outPut += " Hundred and ";
if(inputMinute<10){
outPut+="Zero ";
}
outPut += myMap.get(inputMinute);
outPut += " Hours";
} else {
outPut += " Hundred and ";
outPut += myMap.get(10 * (divisionByTen));
outPut += " ";
if (modulus != 0) {
outPut += myMap.get(modulus) +" ";
}
outPut += "Hours";

}
return outPut;
}

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

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

public class Problem6Test {

@Test
public void testMilitaryTimeConverter1(){
Problem6 test = new Problem6();
String input = "1:30pm";
String expected = "Thirteen Hundred and Thirty Hours";

String actual = test.militaryTimeConverter(input);
Assert.assertEquals(expected,actual);
}
@Test
public void testMilitaryTimeConverter2(){
Problem6 test = new Problem6();
String input = "7:30pm";
String expected = "Ninteen Hundred and Thirty Hours";

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

String actual = test.militaryTimeConverter(input);
Assert.assertEquals(expected,actual);
}
@Test
public void testMilitaryTimeConverter4(){
Problem6 test = new Problem6();
String input = "11:55pm";
String expected = "Twenty Three Hundred and Fifty Five Hours";

String actual = test.militaryTimeConverter(input);
Assert.assertEquals(expected,actual);
}

@Test
public void testMilitaryTimeConverter5(){
Problem6 test = new Problem6();
String input = "1:22am";
String expected = "Zero One Hundred and Twenty Two Hours";

String actual = test.militaryTimeConverter(input);
Assert.assertEquals(expected,actual);
}

@Test
public void testMilitaryTimeConverter6(){
Problem6 test = new Problem6();
String input = "12:00am";
String expected = "Zero Zero Hundred and Zero Zero Hours";

String actual = test.militaryTimeConverter(input);
Assert.assertEquals(expected,actual);
}

@Test
public void testMilitaryTimeConverter7(){
Problem6 test = new Problem6();
String input = "12:00pm";
String expected = "Twelve Hundred and Zero Zero Hours";

String actual = test.militaryTimeConverter(input);
Assert.assertEquals(expected,actual);
}







}