Skip to content

Commit d44e37f

Browse files
committed
refactor: Enhance docs, add more tests in PhoneticAlphabetConverter
1 parent 2f9f75a commit d44e37f

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

src/main/java/com/thealgorithms/conversions/PhoneticAlphabetConverter.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,25 @@ private PhoneticAlphabetConverter() {
5858
PHONETIC_MAP.put('9', "Nine");
5959
}
6060

61+
/**
62+
* Converts text to the NATO phonetic alphabet.
63+
* Steps:
64+
* 1. Convert the text to uppercase.
65+
* 2. Iterate over each character in the text.
66+
* 3. Get the phonetic equivalent of the character from the map.
67+
* 4. Append the phonetic equivalent to the result.
68+
* 5. Append a space to separate the phonetic equivalents.
69+
* 6. Return the result.
70+
*
71+
* @param text the text to convert
72+
* @return the NATO phonetic alphabet
73+
*/
6174
public static String textToPhonetic(String text) {
6275
StringBuilder phonetic = new StringBuilder();
6376
for (char c : text.toUpperCase().toCharArray()) {
77+
if (Character.isWhitespace(c)) {
78+
continue;
79+
}
6480
phonetic.append(PHONETIC_MAP.getOrDefault(c, String.valueOf(c))).append(" ");
6581
}
6682
return phonetic.toString().trim();

src/test/java/com/thealgorithms/conversions/PhoneticAlphabetConverterTest.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
67

78
public class PhoneticAlphabetConverterTest {
89

9-
@Test
10-
public void testTextToPhonetic() {
11-
assertEquals("Alpha Bravo", PhoneticAlphabetConverter.textToPhonetic("AB"));
12-
assertEquals("Alpha Bravo Charlie", PhoneticAlphabetConverter.textToPhonetic("ABC"));
13-
assertEquals("Alpha One Bravo Two Charlie Three", PhoneticAlphabetConverter.textToPhonetic("A1B2C3"));
14-
assertEquals("Hotel Echo Lima Lima Oscar", PhoneticAlphabetConverter.textToPhonetic("Hello"));
15-
assertEquals("One Two Three", PhoneticAlphabetConverter.textToPhonetic("123"));
16-
assertEquals("Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine",
17-
PhoneticAlphabetConverter.textToPhonetic("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"));
18-
assertEquals("Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine",
19-
PhoneticAlphabetConverter.textToPhonetic("abcdefghijklmnopqrstuvwxyz0123456789"));
10+
@ParameterizedTest
11+
@CsvSource({
12+
"'AB', 'Alpha Bravo'", "'ABC', 'Alpha Bravo Charlie'", "'A1B2C3', 'Alpha One Bravo Two Charlie Three'", "'Hello', 'Hotel Echo Lima Lima Oscar'", "'123', 'One Two Three'",
13+
"'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 'Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine'",
14+
"'abcdefghijklmnopqrstuvwxyz0123456789', 'Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine'",
15+
"'', ''", // Empty string case
16+
"'A B C', 'Alpha Bravo Charlie'", // String with spaces
17+
"'A@B#C', 'Alpha @ Bravo # Charlie'", // Special characters
18+
"'A B C 123', 'Alpha Bravo Charlie One Two Three'", // Mixed letters, digits, and spaces
19+
"'a b c', 'Alpha Bravo Charlie'", // Lowercase letters with spaces
20+
"'123!@#', 'One Two Three ! @ #'", // Numbers with special characters
21+
"'HELLO WORLD', 'Hotel Echo Lima Lima Oscar Whiskey Oscar Romeo Lima Delta'" // Words with space
22+
})
23+
public void
24+
testTextToPhonetic(String input, String expectedOutput) {
25+
assertEquals(expectedOutput, PhoneticAlphabetConverter.textToPhonetic(input));
2026
}
2127
}

0 commit comments

Comments
 (0)