Repository: TheAlgorithms/Java
Pull request #5752 (2024-10-13 03:55:44)

feat: Add `PhoneticAlphabetConverter` new algorithm with Junit tests
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5752?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 64.01%. Comparing base [(`4a03f42`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/4a03f420616a6773e9a5cdc304b1681e96d945bd?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`2cd5ce8`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/2cd5ce8a3e91dafdf52a9ba2bc626f64b02678cb?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5752      +/-   ##
============================================
+ Coverage     63.91%   64.01%   +0.09%     
- Complexity     4167     4170       +3     
============================================
  Files           582      583       +1     
  Lines         16302    16344      +42     
  Branches       3148     3149       +1     
============================================
+ Hits          10419    10462      +43     
+ Misses         5457     5456       -1     
  Partials        426      426              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5752?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index eeb50764..80e61f58 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -100,6 +100,7 @@
             * [OctalToBinary](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToBinary.java)
             * [OctalToDecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java)
             * [OctalToHexadecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToHexadecimal.java)
+            * [PhoneticAlphabetConverter](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/PhoneticAlphabetConverter.java)
             * [RgbHsvConversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/RgbHsvConversion.java)
             * [RomanToInteger](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/RomanToInteger.java)
             * [TurkishToLatinConversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/TurkishToLatinConversion.java)
@@ -740,6 +741,7 @@
             * [OctalToBinaryTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToBinaryTest.java)
             * [OctalToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToDecimalTest.java)
             * [OctalToHexadecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToHexadecimalTest.java)
+            * [PhoneticAlphabetConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/PhoneticAlphabetConverterTest.java)
             * [RomanToIntegerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/RomanToIntegerTest.java)
             * [UnitConversionsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/UnitConversionsTest.java)
             * [UnitsConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/UnitsConverterTest.java)
diff --git a/src/main/java/com/thealgorithms/conversions/PhoneticAlphabetConverter.java b/src/main/java/com/thealgorithms/conversions/PhoneticAlphabetConverter.java
new file mode 100644
index 00000000..bcea1862
--- /dev/null
+++ b/src/main/java/com/thealgorithms/conversions/PhoneticAlphabetConverter.java
@@ -0,0 +1,68 @@
+package com.thealgorithms.conversions;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Converts text to the NATO phonetic alphabet.
+ * Examples:
+ * "ABC" -> "Alpha Bravo Charlie"
+ * "Hello" -> "Hotel Echo Lima Lima Oscar"
+ * "123" -> "One Two Three"
+ * "A1B2C3" -> "Alpha One Bravo Two Charlie Three"
+ *
+ * @author Hardvan
+ */
+public final class PhoneticAlphabetConverter {
+    private PhoneticAlphabetConverter() {
+    }
+
+    private static final Map<Character, String> PHONETIC_MAP = new HashMap<>();
+
+    static {
+        PHONETIC_MAP.put('A', "Alpha");
+        PHONETIC_MAP.put('B', "Bravo");
+        PHONETIC_MAP.put('C', "Charlie");
+        PHONETIC_MAP.put('D', "Delta");
+        PHONETIC_MAP.put('E', "Echo");
+        PHONETIC_MAP.put('F', "Foxtrot");
+        PHONETIC_MAP.put('G', "Golf");
+        PHONETIC_MAP.put('H', "Hotel");
+        PHONETIC_MAP.put('I', "India");
+        PHONETIC_MAP.put('J', "Juliett");
+        PHONETIC_MAP.put('K', "Kilo");
+        PHONETIC_MAP.put('L', "Lima");
+        PHONETIC_MAP.put('M', "Mike");
+        PHONETIC_MAP.put('N', "November");
+        PHONETIC_MAP.put('O', "Oscar");
+        PHONETIC_MAP.put('P', "Papa");
+        PHONETIC_MAP.put('Q', "Quebec");
+        PHONETIC_MAP.put('R', "Romeo");
+        PHONETIC_MAP.put('S', "Sierra");
+        PHONETIC_MAP.put('T', "Tango");
+        PHONETIC_MAP.put('U', "Uniform");
+        PHONETIC_MAP.put('V', "Victor");
+        PHONETIC_MAP.put('W', "Whiskey");
+        PHONETIC_MAP.put('X', "X-ray");
+        PHONETIC_MAP.put('Y', "Yankee");
+        PHONETIC_MAP.put('Z', "Zulu");
+        PHONETIC_MAP.put('0', "Zero");
+        PHONETIC_MAP.put('1', "One");
+        PHONETIC_MAP.put('2', "Two");
+        PHONETIC_MAP.put('3', "Three");
+        PHONETIC_MAP.put('4', "Four");
+        PHONETIC_MAP.put('5', "Five");
+        PHONETIC_MAP.put('6', "Six");
+        PHONETIC_MAP.put('7', "Seven");
+        PHONETIC_MAP.put('8', "Eight");
+        PHONETIC_MAP.put('9', "Nine");
+    }
+
+    public static String textToPhonetic(String text) {
+        StringBuilder phonetic = new StringBuilder();
+        for (char c : text.toUpperCase().toCharArray()) {
+            phonetic.append(PHONETIC_MAP.getOrDefault(c, String.valueOf(c))).append(" ");
+        }
+        return phonetic.toString().trim();
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/conversions/PhoneticAlphabetConverterTest.java b/src/test/java/com/thealgorithms/conversions/PhoneticAlphabetConverterTest.java
new file mode 100644
index 00000000..07847302
--- /dev/null
+++ b/src/test/java/com/thealgorithms/conversions/PhoneticAlphabetConverterTest.java
@@ -0,0 +1,21 @@
+package com.thealgorithms.conversions;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+public class PhoneticAlphabetConverterTest {
+
+    @Test
+    public void testTextToPhonetic() {
+        assertEquals("Alpha Bravo", PhoneticAlphabetConverter.textToPhonetic("AB"));
+        assertEquals("Alpha Bravo Charlie", PhoneticAlphabetConverter.textToPhonetic("ABC"));
+        assertEquals("Alpha One Bravo Two Charlie Three", PhoneticAlphabetConverter.textToPhonetic("A1B2C3"));
+        assertEquals("Hotel Echo Lima Lima Oscar", PhoneticAlphabetConverter.textToPhonetic("Hello"));
+        assertEquals("One Two Three", PhoneticAlphabetConverter.textToPhonetic("123"));
+        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",
+            PhoneticAlphabetConverter.textToPhonetic("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"));
+        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",
+            PhoneticAlphabetConverter.textToPhonetic("abcdefghijklmnopqrstuvwxyz0123456789"));
+    }
+}