Created
August 22, 2018 23:58
-
-
Save domgeorg/aac7af294d78833843f2cadfac58a3cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Main { | |
| public static void main(String[] args) { | |
| Person androidDeveloper = | |
| new PersonWithoutPhoneNumber("Kyriakos", "Georgiopoulos"); | |
| Person personWithPhoneNumber = | |
| new PersonWithPhoneNumber(androidDeveloper, "(123)-456-7890"); | |
| Person androidDeveloperWithChangedPhoneNumber = personWithPhoneNumber.anotherOnePersonWithNewPhoneNumber("(030)-311-5222"); | |
| Person androidDeveloperWithNewPhoneNumber = | |
| new PersonWithPhoneNumber(new PersonWithPhoneNumber(new PersonWithoutPhoneNumber("Dom", null), "(000)-333-5555"), "(123)-456-7890"); | |
| Person personNull = | |
| new PersonWithPhoneNumber(null, null); | |
| Person unknown = | |
| new PersonWithPhoneNumber(null, "(123)-456-7890"); | |
| Person nameless = new PersonWithoutPhoneNumber(null, ""); | |
| Person namelessWithPhoneNumber = new PersonWithPhoneNumber(nameless, "(123)-456-7890"); | |
| System.out.println(personWithPhoneNumber.firstName() + " " + personWithPhoneNumber.lastName() + " has phone number " + personWithPhoneNumber.phoneNumber()); | |
| System.out.println(personNull.hasName() ? personNull.phoneNumber() : "Person is null"); | |
| System.out.println(unknown.hasName() ? unknown.phoneNumber() : "Person is unknown"); | |
| System.out.println(namelessWithPhoneNumber.hasName() ? namelessWithPhoneNumber.phoneNumber() : "Nameless"); | |
| System.out.println(androidDeveloperWithNewPhoneNumber.hasName() ? | |
| androidDeveloperWithNewPhoneNumber.firstName() + " " + androidDeveloperWithNewPhoneNumber.lastName() + " has phone number " + androidDeveloperWithNewPhoneNumber.phoneNumber() : | |
| "Person has null lastName"); | |
| System.out.println(androidDeveloperWithChangedPhoneNumber.firstName() + " " + androidDeveloperWithChangedPhoneNumber.lastName() + " has a new phone number " + androidDeveloperWithChangedPhoneNumber.phoneNumber()); | |
| } | |
| } | |
| interface Person { | |
| String firstName(); | |
| String lastName(); | |
| String phoneNumber(); | |
| boolean hasName(); | |
| Person anotherOnePersonWithNewPhoneNumber(String newPhoneNumber); | |
| } | |
| class PersonWithoutPhoneNumber implements Person { | |
| private final String firstName; | |
| private final String lastName; | |
| PersonWithoutPhoneNumber(String firstName, String lastName) { | |
| this.firstName = firstName; | |
| this.lastName = lastName; | |
| } | |
| public String firstName() { | |
| return firstName != null && firstName.length() > 0 ? | |
| firstName : ""; | |
| } | |
| public String lastName() { | |
| return lastName != null && lastName.length() > 0 ? | |
| lastName : ""; | |
| } | |
| public String phoneNumber() { | |
| return ""; | |
| } | |
| public boolean hasName() { | |
| return firstName().length() > 0 && lastName().length() > 0; | |
| } | |
| public Person anotherOnePersonWithNewPhoneNumber(String newPhoneNumber) { | |
| return this; | |
| } | |
| } | |
| class PersonWithPhoneNumber implements Person { | |
| private final Person person; | |
| private final String phoneNumber; | |
| PersonWithPhoneNumber(Person unknown, String phoneNumber) { | |
| this.person = unknown; | |
| this.phoneNumber = phoneNumber; | |
| } | |
| public String phoneNumber() { | |
| return person != null && person.hasName() ? | |
| validPhoneNumberOrEmpty(phoneNumber) : ""; | |
| } | |
| public PersonWithPhoneNumber anotherOnePersonWithNewPhoneNumber(String newPhoneNumber) { | |
| return (phoneNumber.equals(newPhoneNumber) ? this : new PersonWithPhoneNumber(person, newPhoneNumber)); | |
| } | |
| public String firstName() { | |
| return person != null ? | |
| person.firstName() : ""; | |
| } | |
| public String lastName() { | |
| return person != null ? | |
| person.lastName() : ""; | |
| } | |
| public boolean hasName() { | |
| return person != null && person.hasName(); | |
| } | |
| private String validPhoneNumberOrEmpty(String phoneNumber) { | |
| if (phoneNumber != null && phoneNumber.length() > 0 && ( | |
| phoneNumber.matches("\\d{10}") || | |
| phoneNumber.matches("\\d{3}[-\\.\\s]\\d{3}[-\\.\\s]\\d{4}") || | |
| phoneNumber.matches("\\d{3}-\\d{3}-\\d{4}\\s(x|(ext))\\d{3,5}") || | |
| phoneNumber.matches("\\(\\d{3}\\)-\\d{3}-\\d{4}"))) { | |
| return phoneNumber; | |
| } | |
| return ""; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment