[BAEL-7632] Convert a phone number in words to number with Java (#16248)
* [BAEL-7632] Convert a phone number in words to number with Java * Updated [BAEL-7632] Convert a phone number in words to number with Java * Updated [BAEL-7632] Convert a phone number in words to number with Java
This commit is contained in:
parent
2c91616f87
commit
bea3105f57
|
@ -0,0 +1,43 @@
|
|||
import java.util.Map;
|
||||
|
||||
public class UseHashMapToConvertPhoneNumberInWordsToNumber {
|
||||
private static Map<String, Integer> multipliers = Map.of("double",2,
|
||||
"triple", 3,
|
||||
"quadruple", 4);
|
||||
private static Map<String, String> digits = Map.of("zero","1",
|
||||
"one", "1",
|
||||
"two", "2",
|
||||
"three", "3",
|
||||
"four", "4",
|
||||
"five", "5",
|
||||
"six", "6",
|
||||
"seven", "7",
|
||||
"eight", "8",
|
||||
"nine", "9");
|
||||
|
||||
|
||||
public static String convertPhoneNumberInWordsToNumber(String phoneNumberInWord) {
|
||||
|
||||
StringBuilder output = new StringBuilder();
|
||||
Integer currentMultiplier = null;
|
||||
String[] words = phoneNumberInWord.split(" ");
|
||||
|
||||
for (String word : words) {
|
||||
Integer multiplier = multipliers.get(word);
|
||||
if (multiplier != null) {
|
||||
if (currentMultiplier != null) {
|
||||
throw new IllegalArgumentException("Cannot have consecutive multipliers, at: " + word);
|
||||
}
|
||||
currentMultiplier = multiplier;
|
||||
} else {
|
||||
String digit = digits.get(word);
|
||||
if (digit == null) {
|
||||
throw new IllegalArgumentException("Invalid word: " + word);
|
||||
}
|
||||
output.append(digit.repeat(currentMultiplier != null ? currentMultiplier : 1));
|
||||
currentMultiplier = null;
|
||||
}
|
||||
}
|
||||
return output.toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
public class UseSwitchToConvertPhoneNumberInWordsToNumber {
|
||||
|
||||
public static String convertPhoneNumberInWordsToNumber(String phoneNumberInWord) {
|
||||
|
||||
StringBuilder output = new StringBuilder();
|
||||
Integer currentMultiplier = null;
|
||||
String[] words = phoneNumberInWord.split(" ");
|
||||
|
||||
for (String word : words) {
|
||||
Integer multiplier = getWordAsMultiplier(word);
|
||||
if (multiplier != null) {
|
||||
if (currentMultiplier != null) {
|
||||
throw new IllegalArgumentException("Cannot have consecutive multipliers, at: " + word);
|
||||
}
|
||||
currentMultiplier = multiplier;
|
||||
} else {
|
||||
output.append(getWordAsDigit(word).repeat(currentMultiplier != null ? currentMultiplier : 1));
|
||||
currentMultiplier = null;
|
||||
}
|
||||
}
|
||||
return output.toString();
|
||||
}
|
||||
|
||||
public static Integer getWordAsMultiplier(String word) {
|
||||
switch (word) {
|
||||
case "double":
|
||||
return 2;
|
||||
case "triple":
|
||||
return 3;
|
||||
case "quadruple":
|
||||
return 4;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getWordAsDigit(String word) {
|
||||
switch (word) {
|
||||
case "zero":
|
||||
return "0";
|
||||
case "one":
|
||||
return "1";
|
||||
case "two":
|
||||
return "2";
|
||||
case "three":
|
||||
return "3";
|
||||
case "four":
|
||||
return "4";
|
||||
case "five":
|
||||
return "5";
|
||||
case "six":
|
||||
return "6";
|
||||
case "seven":
|
||||
return "7";
|
||||
case "eight":
|
||||
return "8";
|
||||
case "nine":
|
||||
return "9";
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid word: " + word);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class UseHashMapToConvertPhoneNumberInWordsToNumberUnitTest {
|
||||
|
||||
@Test
|
||||
void givenStringWithWhiteSpaces_WhenConvertPhoneNumberInWordsToNumber_ThenEquivalentNumber() {
|
||||
|
||||
assertEquals("5248888",
|
||||
UseHashMapToConvertPhoneNumberInWordsToNumber
|
||||
.convertPhoneNumberInWordsToNumber("five two four quadruple eight"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringEndingWithConseutiveMultipliers_WhenConvertPhoneNumberInWordsToNumber_ThenThrowException() {
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
UseHashMapToConvertPhoneNumberInWordsToNumber
|
||||
.convertPhoneNumberInWordsToNumber("five eight three double triple");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringWithInvalidWords_WhenConvertPhoneNumberInWordsToNumber_ThenThrowException() {
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
UseHashMapToConvertPhoneNumberInWordsToNumber
|
||||
.convertPhoneNumberInWordsToNumber("five eight three two four penta null eight");
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class UseSwitchToConvertPhoneNumberInWordsToNumberUnitTest {
|
||||
|
||||
@Test
|
||||
void givenStringWithWhiteSpaces_WhenConvertPhoneNumberInWordsToNumber_ThenEquivalentNumber() {
|
||||
|
||||
assertEquals("5248888",
|
||||
UseSwitchToConvertPhoneNumberInWordsToNumber
|
||||
.convertPhoneNumberInWordsToNumber("five two four quadruple eight"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringEndingWithConseutiveMultipliers_WhenConvertPhoneNumberInWordsToNumber_ThenThrowException() {
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
UseSwitchToConvertPhoneNumberInWordsToNumber
|
||||
.convertPhoneNumberInWordsToNumber("five eight three double triple");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringWithInvalidWords_WhenConvertPhoneNumberInWordsToNumber_ThenThrowException() {
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
UseSwitchToConvertPhoneNumberInWordsToNumber
|
||||
.convertPhoneNumberInWordsToNumber("five eight three two four penta null eight");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void givenString_WhenGetWordAsMultiplier_ThenEquivalentNumber() {
|
||||
assertEquals(2, UseSwitchToConvertPhoneNumberInWordsToNumber
|
||||
.getWordAsMultiplier("double"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenInvalidString_WhenGetWordAsMultiplier_ThenReturnNull() {
|
||||
assertEquals(null, UseSwitchToConvertPhoneNumberInWordsToNumber
|
||||
.getWordAsMultiplier("hexa"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenString_WhenMapIndividualDigits_ThenEquivalentNumber() {
|
||||
assertEquals("5",
|
||||
UseSwitchToConvertPhoneNumberInWordsToNumber
|
||||
.getWordAsDigit("five"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenInvalidString_WhenMapIndividualDigits_ThenThrowException() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
UseSwitchToConvertPhoneNumberInWordsToNumber
|
||||
.convertPhoneNumberInWordsToNumber("penta");
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue