Skip to content

Instantly share code, notes, and snippets.

@sahoosunilkumar
Created March 19, 2019 06:54
Show Gist options
  • Save sahoosunilkumar/9784f4bea2284d31969640ca7428c135 to your computer and use it in GitHub Desktop.
Save sahoosunilkumar/9784f4bea2284d31969640ca7428c135 to your computer and use it in GitHub Desktop.
final class Party {
private final String place;
private final String name;
private final String starter;
private final String maincourse;
private final String desert;
private final String danceType;
private Party(PartyBuilder builder) {
this.place = builder.place;
this.name = builder.name;
this.maincourse = builder.maincourse;
this.starter = builder.starter;
this.danceType = builder.danceType;
this.desert = builder.desert;
}
public String getplace() {
return place;
}
public String getName() {
return name;
}
public String getstarter() {
return starter;
}
public String getmaincourse() {
return maincourse;
}
public String getdesert() {
return desert;
}
public String getdanceType() {
return danceType;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(" Party : ").append(name).append(" at ").append(place);
if (danceType != null) {
sb.append(" lets dance on " + danceType);
}
if (starter != null) {
sb.append(" start with " + starter);
}
if (maincourse != null) {
sb.append(" followed by ").append(maincourse);
}
if (desert != null) {
sb.append(" ends with ").append(desert);
}
return sb.toString();
}
/**
*
* The builder class.
*
*/
public static class PartyBuilder {
private final String place;
private final String name;
private String starter;
private String maincourse;
private String desert;
private String danceType;
/**
* Constructor
*/
public PartyBuilder(String name, String place) {
if (place == null || name == null) {
throw new IllegalArgumentException(
"place and name can not be null");
}
this.place = place;
this.name = name;
}
public PartyBuilder withstarter(String starter) {
this.starter = starter;
return this;
}
public PartyBuilder withmaincourse(String maincourse) {
this.maincourse = maincourse;
return this;
}
public PartyBuilder withdesert(String desert) {
this.desert = desert;
return this;
}
public PartyBuilder withdanceType(String danceType) {
this.danceType = danceType;
return this;
}
public Party build() {
return new Party(this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment