ItEr29S06CUAsignacionGrupoRecursosAPlanificacionItEr28S06: Adding method to calculate the resources per day from the working hours and the workable hours

This commit is contained in:
Óscar González Fernández 2009-10-07 23:47:34 +02:00
parent fc4e76821c
commit 4283cf6582
2 changed files with 32 additions and 3 deletions

View file

@ -27,7 +27,10 @@ import org.apache.commons.lang.Validate;
public class ResourcesPerDay {
private final BigDecimal amount;
public static ResourcesPerDay calculateFrom(int hoursWorking, int workableHours) {
return amount(new BigDecimal(hoursWorking).divide(new BigDecimal(
workableHours), 2, RoundingMode.HALF_UP));
}
public static ResourcesPerDay amount(int amount) {
return new ResourcesPerDay(new BigDecimal(amount));
@ -37,6 +40,8 @@ public class ResourcesPerDay {
return new ResourcesPerDay(decimal);
}
private final BigDecimal amount;
private ResourcesPerDay(BigDecimal amount) {
Validate.isTrue(amount.intValue() >= 0);
this.amount = amount.setScale(2, RoundingMode.HALF_UP);
@ -73,4 +78,10 @@ public class ResourcesPerDay {
return withoutDecimalpart.intValue() == 0;
}
@Override
public String toString() {
return amount.toString();
}
}

View file

@ -72,7 +72,7 @@ public class ResourcesPerDayTest {
@Override
public void describeTo(Description description) {
description.appendText("must have an integer part of"
+ integerPart);
+ integerPart + " and ");
description.appendText("must have " + decimalPart
+ " as decimal part");
}
@ -105,7 +105,6 @@ public class ResourcesPerDayTest {
}
}
@Test
public void canBeConvertedToHoursGivenTheWorkingDayHours() {
ResourcesPerDay units = ResourcesPerDay.amount(2);
@ -146,4 +145,23 @@ public class ResourcesPerDayTest {
}
}
@SuppressWarnings("unchecked")
@Test
public void canCalculateTheResourcesPerDayFromTheHoursWorkingAndTheWorkableHours() {
Object[] periodicalNumber = { ResourcesPerDay.calculateFrom(10, 3),
readsAs(3, 33) };
Object[][] examples = {
{ ResourcesPerDay.calculateFrom(1000, 1000), readsAs(1, 00) },
{ ResourcesPerDay.calculateFrom(2000, 1000), readsAs(2, 00) },
{ ResourcesPerDay.calculateFrom(500, 1000), readsAs(0, 50) },
{ ResourcesPerDay.calculateFrom(651, 1000), readsAs(0, 65) },
{ ResourcesPerDay.calculateFrom(1986, 1000), readsAs(1, 99) },
periodicalNumber };
for (Object[] pair : examples) {
ResourcesPerDay first = (ResourcesPerDay) pair[0];
Matcher<ResourcesPerDay> matcher = (Matcher<ResourcesPerDay>) pair[1];
assertThat(first, matcher);
}
}
}