Skip to content

Commit a5c8c21

Browse files
Merge pull request exercism#1637 from mirkoperillo/upgrade_version_phone-number
upgrade phone-number to v1.7.0
2 parents 4a3c650 + ee5bc1e commit a5c8c21

3 files changed

Lines changed: 52 additions & 29 deletions

File tree

exercises/phone-number/.meta/src/reference/java/PhoneNumber.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,48 @@ private String extractDigits(String dirtyNumber) {
1313
// Remove spaces, dots, parentheses, hyphens and pluses
1414
continue;
1515
}
16+
if ("-@:!".indexOf(c) > -1) {
17+
throw new IllegalArgumentException("punctuations not permitted");
18+
}
1619
if (!Character.isDigit(c)) {
17-
throw new IllegalArgumentException("Illegal character in phone number. "
18-
+ "Only digits, spaces, parentheses, hyphens or dots accepted.");
20+
throw new IllegalArgumentException("letters not permitted");
1921
}
2022
stringBuilder.append(c);
2123
}
2224
return stringBuilder.toString();
2325
}
2426

2527
private String normalize(String number) {
26-
if (number.length() > 11 || number.length() < 10) {
27-
throw new IllegalArgumentException("Number must be 10 or 11 digits");
28+
if (number.length() < 10) {
29+
throw new IllegalArgumentException("incorrect number of digits");
2830
}
2931

32+
if (number.length() > 11) {
33+
throw new IllegalArgumentException("more than 11 digits");
34+
}
35+
3036
if (number.length() == 11) {
3137
if (number.startsWith("1")) {
3238
number = number.substring(1, number.length());
3339
} else {
34-
throw new IllegalArgumentException("Can only have 11 digits if number starts with '1'");
40+
throw new IllegalArgumentException("11 digits must start with 1");
3541
}
3642
}
37-
38-
if (number.startsWith("0") || number.startsWith("1")) {
39-
throw new IllegalArgumentException("Illegal Area Or Exchange Code. "
40-
+ "Only 2-9 are valid digits");
41-
} else if (number.charAt(3) == '0' || number.charAt(3) == '1') {
42-
throw new IllegalArgumentException("Illegal Area Or Exchange Code. "
43-
+ "Only 2-9 are valid digits");
43+
44+
if (number.startsWith("0")) {
45+
throw new IllegalArgumentException("area code cannot start with zero");
46+
}
47+
48+
if (number.startsWith("1")) {
49+
throw new IllegalArgumentException("area code cannot start with one");
50+
}
51+
52+
if (number.charAt(3) == '0') {
53+
throw new IllegalArgumentException("exchange code cannot start with zero");
54+
}
55+
56+
if (number.charAt(3) == '1') {
57+
throw new IllegalArgumentException("exchange code cannot start with one");
4458
}
4559

4660
return number;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.6.0
1+
1.7.0

exercises/phone-number/src/test/java/PhoneNumberTest.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@
66
import static org.junit.Assert.assertEquals;
77

88
public class PhoneNumberTest {
9-
private static String wrongLengthExceptionMessage = "Number must be 10 or 11 digits";
9+
private static String wrongLengthExceptionMessage = "incorrect number of digits";
10+
private static String moreThan11DigitsExceptionMessage = "more than 11 digits";
1011
private static String numberIs11DigitsButDoesNotStartWith1ExceptionMessage =
11-
"Can only have 11 digits if number starts with '1'";
12+
"11 digits must start with 1";
1213
private static String illegalCharacterExceptionMessage =
13-
"Illegal character in phone number. Only digits, spaces, parentheses, hyphens or dots accepted.";
14-
private static String illegalAreaOrExchangeCodeMessage =
15-
"Illegal Area Or Exchange Code. Only 2-9 are valid digits";
16-
14+
"letters not permitted";
15+
private static String illegalPunctuationExceptionMessage =
16+
"punctuations not permitted";
17+
private static String areaCodeStartsWithZeroExceptionMessage =
18+
"area code cannot start with zero";
19+
private static String areaCodeStartsWithOneExceptionMessage =
20+
"area code cannot start with one";
21+
private static String exchangeCodeStartsWithZeroExceptionMessage =
22+
"exchange code cannot start with zero";
23+
private static String exchangeCodeStartsWithOneExceptionMessage =
24+
"exchange code cannot start with one";
25+
1726
@Rule
1827
public ExpectedException expectedException = ExpectedException.none();
1928

@@ -91,7 +100,7 @@ public void validWhen11DigitsAndStartingWith1EvenWithPunctuation() {
91100
@Test
92101
public void invalidWhenMoreThan11Digits() {
93102
expectedException.expect(IllegalArgumentException.class);
94-
expectedException.expectMessage(wrongLengthExceptionMessage);
103+
expectedException.expectMessage(moreThan11DigitsExceptionMessage);
95104
new PhoneNumber("321234567890");
96105
}
97106

@@ -107,71 +116,71 @@ public void invalidWithLetters() {
107116
@Test
108117
public void invalidWithPunctuations() {
109118
expectedException.expect(IllegalArgumentException.class);
110-
expectedException.expectMessage(illegalCharacterExceptionMessage);
119+
expectedException.expectMessage(illegalPunctuationExceptionMessage);
111120
new PhoneNumber("123-@:!-7890");
112121
}
113122

114123
@Ignore("Remove to run test")
115124
@Test
116125
public void invalidIfAreaCodeStartsWith0() {
117126
expectedException.expect(IllegalArgumentException.class);
118-
expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
127+
expectedException.expectMessage(areaCodeStartsWithZeroExceptionMessage);
119128
new PhoneNumber("(023) 456-7890");
120129
}
121130

122131
@Ignore("Remove to run test")
123132
@Test
124133
public void invalidIfAreaCodeStartsWith1() {
125134
expectedException.expect(IllegalArgumentException.class);
126-
expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
135+
expectedException.expectMessage(areaCodeStartsWithOneExceptionMessage);
127136
new PhoneNumber("(123) 456-7890");
128137
}
129138

130139
@Ignore("Remove to run test")
131140
@Test
132141
public void invalidIfExchangeCodeStartsWith0() {
133142
expectedException.expect(IllegalArgumentException.class);
134-
expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
143+
expectedException.expectMessage(exchangeCodeStartsWithZeroExceptionMessage);
135144
new PhoneNumber("(223) 056-7890");
136145
}
137146

138147
@Ignore("Remove to run test")
139148
@Test
140149
public void invalidIfExchangeCodeStartsWith1() {
141150
expectedException.expect(IllegalArgumentException.class);
142-
expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
151+
expectedException.expectMessage(exchangeCodeStartsWithOneExceptionMessage);
143152
new PhoneNumber("(223) 156-7890");
144153
}
145154

146155
@Ignore("Remove to run test")
147156
@Test
148157
public void invalidIfAreaCodeStartsWith0OnValid11DigitNumber() {
149158
expectedException.expect(IllegalArgumentException.class);
150-
expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
159+
expectedException.expectMessage(areaCodeStartsWithZeroExceptionMessage);
151160
new PhoneNumber("1 (023) 456-7890");
152161
}
153162

154163
@Ignore("Remove to run test")
155164
@Test
156165
public void invalidIfAreaCodeStartsWith1OnValid11DigitNumber() {
157166
expectedException.expect(IllegalArgumentException.class);
158-
expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
167+
expectedException.expectMessage(areaCodeStartsWithOneExceptionMessage);
159168
new PhoneNumber("1 (123) 456-7890");
160169
}
161170

162171
@Ignore("Remove to run test")
163172
@Test
164173
public void invalidIfExchangeCodeStartsWith0OnValid11DigitNumber() {
165174
expectedException.expect(IllegalArgumentException.class);
166-
expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
175+
expectedException.expectMessage(exchangeCodeStartsWithZeroExceptionMessage);
167176
new PhoneNumber("1 (223) 056-7890");
168177
}
169178

170179
@Ignore("Remove to run test")
171180
@Test
172181
public void invalidIfExchangeCodeStartsWith1OnValid11DigitNumber() {
173182
expectedException.expect(IllegalArgumentException.class);
174-
expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
183+
expectedException.expectMessage(exchangeCodeStartsWithOneExceptionMessage);
175184
new PhoneNumber("1 (223) 156-7890");
176185
}
177186
}

0 commit comments

Comments
 (0)