Java Reference
In-Depth Information
public Reservation build() throws BuilderException
{
boolean noHeadcount = (headcount == 0);
boolean noDollarsPerHead = (dollarsPerHead == 0.0);
boolean counteroffer = noHeadcount || noDollarsPerHead;
if (noHeadcount && noDollarsPerHead)
{
headcount = MINHEAD;
dollarsPerHead = MINTOTAL / headcount;
}
else if (noHeadcount)
{
headcount =
(int) Math.ceil(MINTOTAL / dollarsPerHead);
headcount = Math.max(headcount, MINHEAD);
}
else if (noDollarsPerHead)
{
dollarsPerHead = MINTOTAL / headcount;
}
check();
return new Reservation(
date,
headcount,
city,
dollarsPerHead,
hasSite,
counteroffer);
}
This solution factors out a check() method that verifies the validity of a potentially
modified request:
protected void check() throws BuilderException
{
if (date == null)
{
throw new BuilderException("Valid date not found");
}
if (city == null)
{
throw new BuilderException("Valid city not found");
}
if (headcount < MINHEAD)
{
throw new BuilderException(
"Minimum headcount is " + MINHEAD);
}
if (dollarsPerHead * headcount < MINTOTAL)
{
throw new BuilderException(
"Minimum total cost is " + MINTOTAL);
}
}
Search WWH ::




Custom Search