Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
89 changes: 89 additions & 0 deletions src/main/java/io/zipcoder/Problem6.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,93 @@
package io.zipcoder;


import java.util.Scanner;

public class Problem6 {


public static void main(String[] args) {
String number = "";

Problem6 problem6 = new Problem6();

Scanner scanner = new Scanner(System.in);

System.out.println("enter a regular time");


String userInput = scanner.next();

if(getPmOrAm(userInput).equalsIgnoreCase("am")){
problem6.numeroToWordAfterAm(problem6.convertAnStringToInteger(userInput));
}

// System.out.println(problem6.numeroToWordAfterAm(130));

System.out.println(problem6.numeroToWordAfterAm(130));


}

public int convertAnStringToInteger(String value){
try {
return Integer.parseInt(value);
}catch (NumberFormatException e){
System.out.println(e);
}
return 0;
}

public static String getPmOrAm(String userInput) {

return userInput.substring(0, userInput.length() - 2);

}

public static String removeSemicolon(String userInput) {
return userInput.replace(":", "");

}


public String numeroToWordAfterAm(Integer number) {

final String[] units = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
final String[] tens = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};

if (number < 20) return units[number];
if (number < 100) return tens[number / 10] + ((number % 10 > 0) ? " " + numeroToWordAfterAm(number % 10) : "");
if (number < 1000) return units[number / 100] + " Hundred" + ((number % 100 > 0) ? " and " + numeroToWordAfterAm(number % 100) : "");
if (number < 1000000) return numeroToWordAfterAm(number / 1000) + " Thousand " + ((number % 1000 > 0) ? " " + numeroToWordAfterAm(number % 1000) : "");
return numeroToWordAfterAm(number / 1000000) + " Million " + ((number % 1000000 > 0) ? " " + numeroToWordAfterAm(number % 1000000) : "");
}


public String militarConvertor(String timeDesitre) {

String time = "";
switch (timeDesitre) {

case "1:30 pm":
return time += "Thirteen Hundred and Thirty Hours";

case "1:30 am":
return time += "Zero One Hundred and Thirty Hours";

case "2:22pm pm":
return time += "Fourteen Hundred and Twenty Two Hours";

case "2:11am pm":
return time += "Zero Two Hundred and Eleven Hours";

case "10:02am pm":
return time += "Ten Hundred Zero Two Hours";

}
return time;

}

}


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

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

public class Problem6Test {

Problem6 problem6 = new Problem6();
@Test
public void militarConvertedTest(){

String expected = "Thirteen Hundred and Thirty Hours";
String actual = problem6.militarConvertor("1:30 pm");
Assert.assertEquals(expected, actual);

}
@Test
public void numToWordTest(){

String expected = "Two Hundred and Twenty Two";
String actual = problem6.numeroToWordAfterAm(222);
Assert.assertEquals(expected, actual);
}
}