Money implements ValueObject { @NonNull public Money subtract(@NonNull Money subtrahend) { Objects.requireNonNull(subtrahend, "subtrahend must not be null"); if (currency != subtrahend.currency) { throw new IllegalArgumentException("Cannot subtract two Money objects with different currencies"); } return new Money(currency, amount - subtrahend.amount); } @JsonCreator  Money(@NonNull @JsonProperty("currency") Currency currency, @JsonProperty("amount") int amount);  Money(@NonNull Currency currency, double amount); static Money valueOf(Currency currency, Integer value); @NonNull Money add(@NonNull Money augend); @NonNull Money subtract(@NonNull Money subtrahend); @NonNull Money multiply(int multiplicand); @NonNull Currency currency(); int fixedPointAmount(); double doubleAmount(); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString();  }
@Test public void subtract() { var m1 = new Money(Currency.SEK, 300.50); var m2 = new Money(Currency.SEK, 200.50); assertThat(m1.subtract(m2)).isEqualTo(new Money(Currency.SEK, 100.00)); }
@Test(expected = IllegalArgumentException.class) public void subtract_differentCurrencies_exceptionThrown() { var m1 = new Money(Currency.SEK, 300.50); var m2 = new Money(Currency.EUR, 200.50); m1.subtract(m2); }