Sha256: 3d9f70328fcd66fe093cefba3a899391419cb2e5062f17306bd06ca2ed6ad0a8

Contents?: true

Size: 1.24 KB

Versions: 19

Compression:

Stored size: 1.24 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.length() < 10){
            throw new IllegalArgumentException("Number must be 10 or 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'");
            }
        } 
        
        return number;
    }

    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

19 entries across 19 versions & 1 rubygems

Version Path
trackler-2.0.6.36 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.35 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.34 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.33 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.32 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.31 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.30 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.29 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.28 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.27 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.26 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.25 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.24 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.23 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.22 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.21 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.20 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.19 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java
trackler-2.0.6.18 tracks/java/exercises/phone-number/src/example/java/PhoneNumber.java