Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed midnight time zeroes #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

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

import java.util.Arrays;


public class Problem6 {

public static String[] militaryNum = new String[]{"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
"Nineteen", "Twenty", "Thirty", "Forty", "Fifty"};


public String militaryConverter(String time) {
Integer[] fullClock = splitHourAndMinutes(time);
if(militaryPhrasing(fullClock).charAt(0) == militaryPhrasing(fullClock).charAt(5)){
return militaryPhrasing(fullClock).substring(5);
}
return militaryPhrasing(fullClock);
}

public Integer[] splitHourAndMinutes(String time) {
String[] clockGroup = time.split(":");
String amOrPm = clockGroup[1].substring(2);

int hours = Integer.parseInt(clockGroup[0]);
if (hours != 12) {
if (amOrPm.equals("pm")) {
hours += 12;
}
} else {
if (amOrPm.equals("am")) {
hours -= hours;
}
}
int minutes = Integer.parseInt(clockGroup[1].substring(0, 2));

Integer[] fullClock = new Integer[2];
fullClock[0] = hours;
fullClock[1] = minutes;

return fullClock;
}

public String militaryPhrasing(Integer[] fullClock){
int hours = fullClock[0];
int minutes = fullClock[1];
StringBuilder stringBuilder = new StringBuilder();

if (hours < 10){
stringBuilder.append(militaryNum[0] + " ");
}
if (hours <= 20){
stringBuilder.append(militaryNum[hours] + " ");
}
else{
stringBuilder.append(militaryNum[20] + (militaryNum[hours - 20]) + " ");
}
stringBuilder.append("Hundred ");

if (minutes > 0) {
stringBuilder.append("and ");
if (minutes <= 20) {
if (minutes < 10) {
stringBuilder.append(militaryNum[0] + " ");
}
stringBuilder.append(militaryNum[minutes] + " ");
} else {
int tenBase = minutes / 10;
int oneBase = minutes % 10;
stringBuilder.append(militaryNum[18 + tenBase] + " ");

if (oneBase > 0) {
stringBuilder.append(militaryNum[oneBase] + " ");
}
}
}
stringBuilder.append("Hours");
return stringBuilder.toString();
}

public static void main(String[] args) {
Problem6 test = new Problem6();

String time = "1:35am";
test.splitHourAndMinutes(time);
System.out.println(Arrays.toString(test.splitHourAndMinutes(time)));
System.out.println(militaryNum[20]);

}
}
//1:30pm
//1 = index[0]
//30pm = index[1] substring(2) = pm
//30 = index[1] substring(0,2)

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

public class Problem6Redux {
}
4 changes: 4 additions & 0 deletions src/test/java/io/zipcoder/Problem6ReduxTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.zipcoder;

public class Problem6ReduxTest {
}
61 changes: 61 additions & 0 deletions src/test/java/io/zipcoder/Problem6Test.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,65 @@
package io.zipcoder;

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

public class Problem6Test {


@Test
public void militaryTimeTest(){
String time = "1:30pm";
String militaryTime = "Thirteen Hundred and Thirty Hours";
Problem6 testProblem6 = new Problem6();
String actual = testProblem6.militaryConverter(time);
Assert.assertEquals(militaryTime, actual);
}
@Test
public void militaryTimeTest1(){
String time = "1:30am";
String militaryTime = "Zero One Hundred and Thirty Hours";
Problem6 testProblem6 = new Problem6();
String actual = testProblem6.militaryConverter(time);
Assert.assertEquals(militaryTime, actual);
}
@Test
public void militaryTimeTest2(){
String time = "2:22pm";
String militaryTime = "Fourteen Hundred and Twenty Two Hours";
Problem6 testProblem6 = new Problem6();
String actual = testProblem6.militaryConverter(time);
Assert.assertEquals(militaryTime, actual);
}
@Test
public void militaryTimeTest3(){
String time = "2:11am";
String militaryTime = "Zero Two Hundred and Eleven Hours";
Problem6 testProblem6 = new Problem6();
String actual = testProblem6.militaryConverter(time);
Assert.assertEquals(militaryTime, actual);
}
@Test
public void militaryTimeTest4(){
String time = "10:02am";
String militaryTime = "Ten Hundred and Zero Two Hours";
Problem6 testProblem6 = new Problem6();
String actual = testProblem6.militaryConverter(time);
Assert.assertEquals(militaryTime, actual);
}
@Test
public void militaryTimeTest5(){
String time = "10:22am";
String militaryTime = "Ten Hundred and Twenty Two Hours";
Problem6 testProblem6 = new Problem6();
String actual = testProblem6.militaryConverter(time);
Assert.assertEquals(militaryTime, actual);
}
@Test
public void militaryTimeTest6(){
String time = "12:22am";
String militaryTime = "Zero Hundred and Twenty Two Hours";
Problem6 testProblem6 = new Problem6();
String actual = testProblem6.militaryConverter(time);
Assert.assertEquals(militaryTime, actual);
}
}