2014-08-09 22:38:07 +03:00
|
|
|
package com.iluwatar;
|
|
|
|
|
2014-08-31 00:33:09 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* The class with many parameters.
|
|
|
|
*
|
|
|
|
*/
|
2014-08-09 22:38:07 +03:00
|
|
|
public class Hero {
|
|
|
|
|
|
|
|
private final Profession profession;
|
|
|
|
private final String name;
|
|
|
|
private final HairType hairType;
|
|
|
|
private final HairColor hairColor;
|
|
|
|
private final Armor armor;
|
|
|
|
private final Weapon weapon;
|
|
|
|
|
|
|
|
public Profession getProfession() {
|
|
|
|
return profession;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public HairType getHairType() {
|
|
|
|
return hairType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public HairColor getHairColor() {
|
|
|
|
return hairColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Armor getArmor() {
|
|
|
|
return armor;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Weapon getWeapon() {
|
|
|
|
return weapon;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.append(profession);
|
|
|
|
sb.append(" named ");
|
|
|
|
sb.append(name);
|
|
|
|
if (hairColor != null || hairType != null) {
|
|
|
|
sb.append(" with ");
|
|
|
|
if (hairColor != null) {
|
|
|
|
sb.append(hairColor);
|
|
|
|
sb.append(" ");
|
|
|
|
}
|
|
|
|
if (hairType != null) {
|
|
|
|
sb.append(hairType);
|
|
|
|
sb.append(" ");
|
|
|
|
}
|
2014-09-25 21:16:48 +03:00
|
|
|
sb.append(hairType != HairType.BALD ? "hair" : "head");
|
2014-08-09 22:38:07 +03:00
|
|
|
}
|
|
|
|
if (armor != null) {
|
|
|
|
sb.append(" wearing ");
|
|
|
|
sb.append(armor);
|
|
|
|
}
|
|
|
|
if (weapon != null) {
|
|
|
|
sb.append(" and wielding ");
|
|
|
|
sb.append(weapon);
|
|
|
|
}
|
|
|
|
sb.append(".");
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private Hero(HeroBuilder builder) {
|
|
|
|
this.profession = builder.profession;
|
|
|
|
this.name = builder.name;
|
|
|
|
this.hairColor = builder.hairColor;
|
|
|
|
this.hairType = builder.hairType;
|
|
|
|
this.weapon = builder.weapon;
|
|
|
|
this.armor = builder.armor;
|
|
|
|
}
|
2014-08-31 00:33:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* The builder class.
|
|
|
|
*
|
|
|
|
*/
|
2014-08-09 22:38:07 +03:00
|
|
|
public static class HeroBuilder {
|
|
|
|
|
|
|
|
private final Profession profession;
|
|
|
|
private final String name;
|
|
|
|
private HairType hairType;
|
|
|
|
private HairColor hairColor;
|
|
|
|
private Armor armor;
|
|
|
|
private Weapon weapon;
|
|
|
|
|
|
|
|
public HeroBuilder(Profession profession, String name) {
|
|
|
|
if (profession == null || name == null) {
|
|
|
|
throw new NullPointerException("profession and name can not be null");
|
|
|
|
}
|
|
|
|
this.profession = profession;
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public HeroBuilder withHairType(HairType hairType) {
|
|
|
|
this.hairType = hairType;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public HeroBuilder withHairColor(HairColor hairColor) {
|
|
|
|
this.hairColor = hairColor;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public HeroBuilder withArmor(Armor armor) {
|
|
|
|
this.armor = armor;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public HeroBuilder withWeapon(Weapon weapon) {
|
|
|
|
this.weapon = weapon;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Hero build() {
|
|
|
|
return new Hero(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|