Sha256: af46ccd87fe9ffb282c9fc77b65fd6c52d8f93f9e049db4e812c4529195a6d46

Contents?: true

Size: 1.07 KB

Versions: 73

Compression:

Stored size: 1.07 KB

Contents

public class PhoneNumber {

    private final String number;

    public PhoneNumber(String phoneNumber) {
        number = normalize(extractDigits(phoneNumber));
    }

    private String extractDigits(String dirtyNumber) {
        return dirtyNumber.replaceAll("[^\\d]", "");
    }

    private String normalize(String number) {
        if(number.length() == 11 && number.startsWith("1")) {
            number = number.substring(1, number.length());
        }

        final boolean numberIsValid = (number.length() == 10);
        final String normalizedNumber = numberIsValid ? number : "0000000000";
        return normalizedNumber;
    }

    public String getNumber() {
        return number;
    }

    public String getAreaCode() {
        return number.substring(0, 3);
    }

    public String getExchangeCode() {
        return number.substring(3, 6);
    }

    public String getSubscriberNumber() {
        return number.substring(6, 10);
    }

    public String pretty() {
        return "(" + getAreaCode() + ") " + getExchangeCode() + "-" + getSubscriberNumber();
    }

}

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
trackler-2.0.6.17 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.16 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.15 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.14 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.13 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.12 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.11 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.10 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.9 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.8 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.7 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.6 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.5 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.4 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.3 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.2 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.1 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.0 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.5.18 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.5.17 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java