Part 3. Java Programming with String obejcts. Create a class to deal with teleph
ID: 3794018 • Letter: P
Question
Part 3. Java Programming with String obejcts.
Create a class to deal with telephone numbers that your customers enter.
User’s aren’t very disciplined, so even if you indicate that you want the number entered like this:
407 582 2213
They sometimes enter the number like any of these:
(407)582-2213
1 (407) 582-2213
407.582.2213
4075822213
1 407 582-2213
Or other combinations that I can’t think of, but they will.
The constructor for your class takes a String as its only parameter.
If the number they pass to your constructor does not have 10 digits, (or 11 digits if it starts with a 1), you should throw an Exception from the constructor.
InvalidTelephoneException. It’s the Telephone constructor method that will throw this exception.
A test of your class can be though of as: try to make a phone number out of a String.
Telephone phone;
String testString = “407.582.2213”;
try {
phone = new Telephone(testString);
}catch(InvalidTelephoneException ite) {
Send some information to the console about why that string is not a phone number
}
Your class must have a getAreaCode, getExchange, getLocalNumber methods. For the example above, the area code is 407, the exchange is 582, and the local number is 2213.
Follow good practices for OO programming.
A toString method will present the number in a canonical form: (407) 582-2213
Create a test class that demonstrates that your code will work correctly for each of the documented cases above.
Include all of the code, and the output from running your test class.
Use JavaDoc comments to document the class. Include examples IN THE JAVADOC of what format of phone numbers will be acceptable by the class (service provider) that you have created.
Explanation / Answer
TelephoneValidate.java
package telephone;
public class TelephoneValidate {
private String number;
private String areaCode;
private String exchange;
private String localNumber;
/**
*
* @param number
* @throws InvalidTelephoneException
*/
public TelephoneValidate(String number) throws InvalidTelephoneException{
super();
this.number = number;
}
/**
* This method is used to get Area code
* @return areaCode
*/
public String getAreaCode() {
areaCode = "Area code is :"+" "+number.trim().substring(0, number.trim().length() - 7) ;
return areaCode;
}
/**
* This method is used to get Exchange
* @return exchange
*/
public String getExchange() {
exchange = "Exchange is :"+" "+number.trim().substring(3, number.trim().length() - 4);
return exchange;
}
/**
* This method is used to get Local number
* @return localNumber
*/
public String getLocalNumber() {
if(number.length() == 10){
localNumber = "Local number is :"+" "+number.trim().substring(6, number.trim().length() - 0);
}
else{
localNumber = "Local number is :"+" "+number.trim().substring(6, number.trim().length() - 1);
}
return localNumber;
}
@Override
public String toString() {
return "TelephoneValidate [number=" + number + ", areaCode=" + areaCode + ", exchange=" + exchange
+ ", localNumber=" + localNumber + "]";
}
}
InvalidTelephoneException.java
package telephone;
public class InvalidTelephoneException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public InvalidTelephoneException(String message){
super(message);
}
}
Test class : Test.java
package telephone;
import java.util.Scanner;
public class Test {
@SuppressWarnings("resource")
public static void main(String[] args) throws InvalidTelephoneException {
Scanner scan = new Scanner(System.in);
System.out.println("Enter telephone number :");
String input = scan.nextLine();
// Extract only digits from string
input = input.replaceAll("\D+","");
if(input.length() != 10 && input.length() != 11 ){
throw new InvalidTelephoneException("Number shuold have 10 or 11 digit");
}
else{
TelephoneValidate tValidate = new TelephoneValidate(input);
System.out.println(tValidate.getAreaCode());
System.out.println(tValidate.getExchange());
System.out.println(tValidate.getLocalNumber());
}
scan.close();
}
}
Output if you not enter 10 or 11 digit :
Enter telephone number :
123-456-789
Exception in thread "main" telephone.InvalidTelephoneException: Number shuold have 10 or 11 digit
at telephone.Test.main(Test.java:15)
Output if you enter 10 or 11 digit :
Enter telephone number :
123-456-7891
Area code is : 123
Exchange is : 456
Local number is : 7891