Skip to content

Instantly share code, notes, and snippets.

@theboreddev
Created June 9, 2020 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theboreddev/4965b712db737a024a6482206432e7e9 to your computer and use it in GitHub Desktop.
Save theboreddev/4965b712db737a024a6482206432e7e9 to your computer and use it in GitHub Desktop.
Customer
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