@Nonnull public Money subtract (final @Nonnull Money other) { checkCurrencies(other); return new Money(amount.subtract(other.amount), currency); }
@Nonnull public static Money parse (final @Nonnull String string) throws ParseException { final String[] parts = string.split(" "); return new Money((BigDecimal)getFormat().parse(parts[0]), parts[1]); }
@Nonnull public Money add (final @Nonnull Money other) { checkCurrencies(other); return new Money(amount.add(other.amount), currency); }
@Nonnull public Money getMoney (final @Nonnull String key) { return new Money(delegate.getBigDecimal(key).round(ROUNDING), "EUR"); }
@Test public void toString_must_be_properly_computed() { final Money m1 = new Money(4053, "EUR"); assertThat(m1.toString(), is("4053.00 EUR")); } @Test
@Test public void greaterThan_and_lowerThan_must_work_at_the_opposite() { final Money greater = new Money(349, "EUR"); final Money lower = new Money(130, "EUR"); assertThat(greater.greaterThan(lower), is(true)); assertThat(greater.lowerThan(lower) , is(false)); assertThat(lower.greaterThan(greater), is(false)); assertThat(lower.lowerThan(greater), is(true)); }
@Test public void isEqualTo_must_work() { final Money m1 = new Money(349, "EUR"); final Money m2 = new Money(349, "EUR"); final Money m3 = new Money(495, "EUR"); assertThat(m1.isEqualTo(m2), is(true)); assertThat(m2.isEqualTo(m1), is(true)); assertThat(m1.isEqualTo(m3), is(false)); assertThat(m3.isEqualTo(m1), is(false)); }
@Test public void greaterThan_and_lowerThan_must_be_false_when_equality() { final Money m1 = new Money(349, "EUR"); final Money m2 = new Money(349, "EUR"); assertThat(m1.greaterThan(m2), is(false)); assertThat(m1.lowerThan(m2), is(false)); assertThat(m2.greaterThan(m1), is(false)); assertThat(m2.lowerThan(m1), is(false)); } }
@Nonnull private static List<JobEvent> createJobEvents (final @Nonnull LocalDate startDate, final @Nonnull LocalDate endDate, final @Nonnull Money rate) { final List<JobEvent> result = new ArrayList<>(); final long days = startDate.until(endDate, ChronoUnit.DAYS); for (int i = 1; i <= days; i++) { final LocalDateTime s = startDate.plusDays(i - 1).atTime(8, 0).plusMinutes(i * 3); final LocalDateTime e = s.plusMinutes(60 + i * 4); final double hours = s.until(e, ChronoUnit.MINUTES) / 60.0; final BigDecimal earnings = rate.getAmount().multiply(new BigDecimal(hours)); result.add(JobEvent.builder().withId(new Id("" + nextId++)) .withName("Event #" + i) .withDescription("Description of Event #" + i) .withType(JobEvent.Type.TIMED) .withEarnings(new Money(earnings, "EUR")) .withStartDateTime(s) .withEndDateTime(e) .withHourlyRate(rate) .create()); } return result; }
.create(); final Project p = Project.builder().withId(new Id("2")) .withBudget(new Money(10500, "EUR")) .withCustomer(c1) .withName("Project 1") .withNotes("Notes for project 1") .withNumber("1") .withHourlyRate(new Money(43, "EUR")) .create(); assertThat(p.toString(), is("InMemoryProject(id=2, customer=InMemoryCustomer(id=1, name=Acme Corp., billingAddress="
final Money rate1 = new Money(120, "EUR"); final Money rate2 = new Money(150, "EUR"); final Money rate3 = new Money(210, "EUR"); final Money rate4 = new Money(240, "EUR"); .withEvents(je1) .withHourlyRate(rate1) .withBudget(new Money(123456, "EUR")) .create(); final Project acmeConsultingProject2 = .withEvents(je2) .withHourlyRate(rate2) .withBudget(new Money(234567, "EUR")) .create(); final Project acmeFinancingProject1 = .withHourlyRate(rate3) .withEvents(je3) .withBudget(new Money(345678, "EUR")) .create(); final Project acmeFinancingProject2 = .withHourlyRate(rate4) .withEvents(je4) .withBudget(new Money(456789, "EUR")) .create();