Created
June 9, 2020 18:52
-
-
Save theboreddev/4965b712db737a024a6482206432e7e9 to your computer and use it in GitHub Desktop.
Customer
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
import java.util.Optional; | |
public class Customer { | |
private final String firstName; | |
private final String surname; | |
private final Integer age; | |
private final String address; | |
private final String phoneNumber; | |
private final Sex sex; | |
private Customer(Builder builder) { | |
this.firstName = builder.firstName; | |
this.surname = builder.surname; | |
this.age = builder.age; | |
this.address = builder.address; | |
this.phoneNumber = builder.phoneNumber; | |
this.sex = builder.sex; | |
} | |
public static Builder builderOf(String firstName, String surname) { | |
return new Builder(firstName, surname); | |
} | |
static class Builder { | |
private final String firstName; | |
private final String surname; | |
private Integer age; | |
private String address; | |
private String phoneNumber; | |
private Sex sex; | |
private Builder(String firstName, String surname) { | |
this.firstName = firstName; | |
this.surname = surname; | |
} | |
public Builder withAge(Integer age) { | |
this.age = age; | |
return this; | |
} | |
public Builder withAddress(String address) { | |
this.address = address; | |
return this; | |
} | |
public Builder withPhoneNumber(String phoneNumber) { | |
this.phoneNumber = phoneNumber; | |
return this; | |
} | |
public Builder withSex(Sex sex) { | |
this.sex = sex; | |
return this; | |
} | |
public Customer build() { | |
return new Customer(this); | |
} | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public String getSurname() { | |
return surname; | |
} | |
public Optional<Integer> getAge() { | |
return Optional.ofNullable(age); | |
} | |
public Optional<String> getAddress() { | |
return Optional.ofNullable(address); | |
} | |
public Optional<String> getPhoneNumber() { | |
return Optional.ofNullable(phoneNumber); | |
} | |
public Optional<Sex> getSex() { | |
return Optional.ofNullable(sex); | |
} | |
static enum Sex { | |
FEMALE, | |
MALE | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment