Skip to content

Commit

Permalink
add functionality to multiply Quantity by a scalar value
Browse files Browse the repository at this point in the history
  • Loading branch information
bachmanity1 authored Aug 9, 2024
1 parent 6f70710 commit 01eb4ae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,18 @@ public Quantity subtract(Quantity y) {
return op(y, BigDecimal::subtract);
}

/**
* Multiplies the quantity by the specified scalar multiplicand.
*
* @param multiplicand the scalar value to multiply by
* @return a new Quantity resulting from the multiplication of this quantity by the scalar multiplicand
*/
public Quantity multiply(int multiplicand) {
BigDecimal numericalAmount = getNumericalAmount();
numericalAmount = numericalAmount.multiply(BigDecimal.valueOf(multiplicand));
return fromNumericalAmount(numericalAmount, format);
}

Quantity op(Quantity y, BiFunction<BigDecimal, BigDecimal, BigDecimal> func) {
BigDecimal numericalAmount = this.getNumericalAmount();
numericalAmount = func.apply(numericalAmount, y.getNumericalAmount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,12 @@ void testSubtract() {
Quantity quantity = new Quantity("0Mi");
assertThat(quantity.subtract(new Quantity("1Ki"))).isEqualTo(new Quantity("-1Ki"));
}

@Test
void testMultiply() {
Quantity quantity = new Quantity("4Gi");
assertThat(quantity.multiply(0)).isEqualTo(new Quantity("0"));
assertThat(quantity.multiply(3)).isEqualTo(new Quantity("12Gi"));
assertThat(quantity.multiply(-3)).isEqualTo(new Quantity("-12Gi"));
}
}

0 comments on commit 01eb4ae

Please sign in to comment.