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
59 changes: 56 additions & 3 deletions src/main/java/com/github/archarithms/App.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,68 @@
package com.github.archarithms;

import java.util.Date;
import java.text.SimpleDateFormat;

public class App
{
/*
* Converts a string to title case, replacing non-alphanumeric characters with spaces
*/
public static String convertToTitleCase(final String inpStr)
{
return inpStr;
//throw an exception if input string is null
if (inpStr==null) {
throw new IllegalArgumentException("Error: Input string must not be null");
}
//replace non-alphanumeric characters with spaces
String alphanumeric = inpStr.replaceAll("[^A-Za-z0-9]", " ");

//split into words
String[] words = alphanumeric.split(" ");

String result = "";

for (int i = 0; i < words.length; i++) {
//capitalize the first letter, and make the rest of the letters lowercase
result += Character.toUpperCase(words[i].charAt(0))
+ words[i].substring(1).toLowerCase();
//add a space after each word, except the last one
if (i != words.length-1) result += " ";
}

return result;
}


/*
* Converts a Unix epoch time in seconds and returns the "Month Day, Year" string representation"
*/
public static String convertUnixToDateString(final Long inpUnixSeconds)
{
return String.valueOf(inpUnixSeconds);
//Throw an exception if the input is null
if (inpUnixSeconds == null) {
throw new IllegalArgumentException("Error: Input seconds must not be null");
}

//Create date object based on Unix milliseconds
Date date = new Date(inpUnixSeconds*1000);

//Create text format for the data
SimpleDateFormat format = new SimpleDateFormat("MMMM d, yyyy");

//return the formatted date
return format.format(date);
}

/*
* Gets the current time and returns the Month Day, Year string representation"
*/
public static String convertUnixToDateString()
{
//Get the current time
Date date = new Date();
long current_ms = date.getTime();

//Call the matching function that takes the seconds argument
return convertUnixToDateString(current_ms/1000);
}
}
29 changes: 21 additions & 8 deletions src/test/java/com/github/archarithms/test/DateStringTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
*/
public class DateStringTests
{

/*
* UNCOMMENT THE UNIT TESTS TO RUN THEM. FEEL FREE TO ADD MORE!
*/

/**
* Test the testConvertUnixToDateString method
*/
Expand All @@ -25,12 +20,30 @@ public void testConvertUnixToDateString() {
String testStr = "July 4, 2017";
assertTrue(testStr.equals(App.convertUnixToDateString(1499144400L)));
}


/**
* Test the testNullCase method
* Test that a null value throws the custom exception
*/
@Test
public void testNullCase() {
assertThrows(Exception.class, () -> App.convertUnixToDateString(null));
public void testNull() {
Exception e = assertThrows(IllegalArgumentException.class,
() -> App.convertUnixToDateString(null),
"msg");
assertTrue(e.getMessage().contains("Error: Input seconds must not be null"));
}

/**
* Test that converting without passing an argument gives today's date.
*
* NOTE: testStr needs to be properly set here to match today's date string
* in order to pass, so this test is commented out for now. To test, set the
* testStr properly and then uncomment.
*/
/* @Test
public void testToday() {
String testStr = "January 31, 2021";
assertTrue(testStr.equals(App.convertUnixToDateString()));
} */

}
29 changes: 26 additions & 3 deletions src/test/java/com/github/archarithms/test/TitleCaseTests.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.github.archarithms.test;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

import java.lang.IllegalArgumentException;

import com.github.archarithms.App;

/**
Expand All @@ -17,7 +20,7 @@ public class TitleCaseTests
*/

/**
* Test the testConvertToTitleCase method
* Test an all-caps input with an underscore
*/
@Test
public void testConvertToTitleCase() {
Expand All @@ -26,7 +29,7 @@ public void testConvertToTitleCase() {
}

/**
* Test the testNumbers method
* Test an all-caps input with an underscore and number
*/
@Test
public void testNumbers() {
Expand All @@ -35,11 +38,31 @@ public void testNumbers() {
}

/**
* Test the testOtherChars method
* Test an all-caps input with a dash
*/
@Test
public void testOtherChars() {
String testStr = "Truth Track";
assertTrue(testStr.equals(App.convertToTitleCase("TRUTH-TRACK")));
}

/**
* Test mix of characters with non-alphanumeric at the end
*/
@Test
public void testComplexStr() {
String testStr = "Case Three Extra Chars";
assertTrue(testStr.equals(App.convertToTitleCase("CASE-THREE_extra[chars]///")));
}

/**
* Test that a null string throws the custom exception
*/
@Test
public void testNull() {
Exception e = assertThrows(IllegalArgumentException.class,
() -> App.convertToTitleCase(null),
"msg");
assertTrue(e.getMessage().contains("Error: Input string must not be null"));
}
}