Skip to content
Merged
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
38 changes: 26 additions & 12 deletions exercises/phone-number/.meta/src/reference/java/PhoneNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,48 @@ private String extractDigits(String dirtyNumber) {
// Remove spaces, dots, parentheses, hyphens and pluses
continue;
}
if ("-@:!".indexOf(c) > -1) {
throw new IllegalArgumentException("punctuations not permitted");
}
if (!Character.isDigit(c)) {
throw new IllegalArgumentException("Illegal character in phone number. "
+ "Only digits, spaces, parentheses, hyphens or dots accepted.");
throw new IllegalArgumentException("letters not permitted");
}
stringBuilder.append(c);
}
return stringBuilder.toString();
}

private String normalize(String number) {
if (number.length() > 11 || number.length() < 10) {
throw new IllegalArgumentException("Number must be 10 or 11 digits");
if (number.length() < 10) {
throw new IllegalArgumentException("incorrect number of digits");
}

if (number.length() > 11) {
throw new IllegalArgumentException("more than 11 digits");
}

if (number.length() == 11) {
if (number.startsWith("1")) {
number = number.substring(1, number.length());
} else {
throw new IllegalArgumentException("Can only have 11 digits if number starts with '1'");
throw new IllegalArgumentException("11 digits must start with 1");
}
}

if (number.startsWith("0") || number.startsWith("1")) {
throw new IllegalArgumentException("Illegal Area Or Exchange Code. "
+ "Only 2-9 are valid digits");
} else if (number.charAt(3) == '0' || number.charAt(3) == '1') {
throw new IllegalArgumentException("Illegal Area Or Exchange Code. "
+ "Only 2-9 are valid digits");

if (number.startsWith("0")) {
throw new IllegalArgumentException("area code cannot start with zero");
}

if (number.startsWith("1")) {
throw new IllegalArgumentException("area code cannot start with one");
}

if (number.charAt(3) == '0') {
throw new IllegalArgumentException("exchange code cannot start with zero");
}

if (number.charAt(3) == '1') {
throw new IllegalArgumentException("exchange code cannot start with one");
}

return number;
Expand Down
2 changes: 1 addition & 1 deletion exercises/phone-number/.meta/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.6.0
1.7.0
41 changes: 25 additions & 16 deletions exercises/phone-number/src/test/java/PhoneNumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@
import static org.junit.Assert.assertEquals;

public class PhoneNumberTest {
private static String wrongLengthExceptionMessage = "Number must be 10 or 11 digits";
private static String wrongLengthExceptionMessage = "incorrect number of digits";
private static String moreThan11DigitsExceptionMessage = "more than 11 digits";
private static String numberIs11DigitsButDoesNotStartWith1ExceptionMessage =
"Can only have 11 digits if number starts with '1'";
"11 digits must start with 1";
private static String illegalCharacterExceptionMessage =
"Illegal character in phone number. Only digits, spaces, parentheses, hyphens or dots accepted.";
private static String illegalAreaOrExchangeCodeMessage =
"Illegal Area Or Exchange Code. Only 2-9 are valid digits";

"letters not permitted";
private static String illegalPunctuationExceptionMessage =
"punctuations not permitted";
private static String areaCodeStartsWithZeroExceptionMessage =
"area code cannot start with zero";
private static String areaCodeStartsWithOneExceptionMessage =
"area code cannot start with one";
private static String exchangeCodeStartsWithZeroExceptionMessage =
"exchange code cannot start with zero";
private static String exchangeCodeStartsWithOneExceptionMessage =
"exchange code cannot start with one";

@Rule
public ExpectedException expectedException = ExpectedException.none();

Expand Down Expand Up @@ -91,7 +100,7 @@ public void validWhen11DigitsAndStartingWith1EvenWithPunctuation() {
@Test
public void invalidWhenMoreThan11Digits() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(wrongLengthExceptionMessage);
expectedException.expectMessage(moreThan11DigitsExceptionMessage);
new PhoneNumber("321234567890");
}

Expand All @@ -107,71 +116,71 @@ public void invalidWithLetters() {
@Test
public void invalidWithPunctuations() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(illegalCharacterExceptionMessage);
expectedException.expectMessage(illegalPunctuationExceptionMessage);
new PhoneNumber("123-@:!-7890");
}

@Ignore("Remove to run test")
@Test
public void invalidIfAreaCodeStartsWith0() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
expectedException.expectMessage(areaCodeStartsWithZeroExceptionMessage);
new PhoneNumber("(023) 456-7890");
}

@Ignore("Remove to run test")
@Test
public void invalidIfAreaCodeStartsWith1() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
expectedException.expectMessage(areaCodeStartsWithOneExceptionMessage);
new PhoneNumber("(123) 456-7890");
}

@Ignore("Remove to run test")
@Test
public void invalidIfExchangeCodeStartsWith0() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
expectedException.expectMessage(exchangeCodeStartsWithZeroExceptionMessage);
new PhoneNumber("(223) 056-7890");
}

@Ignore("Remove to run test")
@Test
public void invalidIfExchangeCodeStartsWith1() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
expectedException.expectMessage(exchangeCodeStartsWithOneExceptionMessage);
new PhoneNumber("(223) 156-7890");
}

@Ignore("Remove to run test")
@Test
public void invalidIfAreaCodeStartsWith0OnValid11DigitNumber() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
expectedException.expectMessage(areaCodeStartsWithZeroExceptionMessage);
new PhoneNumber("1 (023) 456-7890");
}

@Ignore("Remove to run test")
@Test
public void invalidIfAreaCodeStartsWith1OnValid11DigitNumber() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
expectedException.expectMessage(areaCodeStartsWithOneExceptionMessage);
new PhoneNumber("1 (123) 456-7890");
}

@Ignore("Remove to run test")
@Test
public void invalidIfExchangeCodeStartsWith0OnValid11DigitNumber() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
expectedException.expectMessage(exchangeCodeStartsWithZeroExceptionMessage);
new PhoneNumber("1 (223) 056-7890");
}

@Ignore("Remove to run test")
@Test
public void invalidIfExchangeCodeStartsWith1OnValid11DigitNumber() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
expectedException.expectMessage(exchangeCodeStartsWithOneExceptionMessage);
new PhoneNumber("1 (223) 156-7890");
}
}