Package com.example.orderplatform
Record Class Money
java.lang.Object
java.lang.Record
com.example.orderplatform.Money
- Record Components:
amount- decimal amount, rounded half up to currency-style precisioncurrency- three-letter currency code
Monetary value object used by pricing, orders and payments.
Amounts are normalized to two decimal places and currencies are stored as uppercase ISO-4217 codes. Negative amounts and mixed-currency arithmetic are rejected as business rule violations.
-
Constructor Summary
ConstructorsConstructorDescriptionMoney(BigDecimal amount, String currency) Creates an instance of aMoneyrecord class. -
Method Summary
Modifier and TypeMethodDescriptionAdds two monetary values after verifying they use the same currency.amount()Returns the value of theamountrecord component.currency()Returns the value of thecurrencyrecord component.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.multiply(int factor) Multiplies this value by a positive quantity.static Moneyof(BigDecimal amount, String currency) Creates a normalized money value.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
Method Details
-
of
Creates a normalized money value.- Parameters:
amount- decimal amountcurrency- three-letter currency code- Returns:
- normalized money value
-
add
Adds two monetary values after verifying they use the same currency.- Parameters:
other- value to add- Returns:
- sum of both values
- Throws:
BusinessRuleViolationException- when currencies differ
-
multiply
Multiplies this value by a positive quantity.- Parameters:
factor- positive multiplier- Returns:
- multiplied money value
- Throws:
BusinessRuleViolationException- when the multiplier is less than one
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object). -
amount
Returns the value of theamountrecord component.- Returns:
- the value of the
amountrecord component
-
currency
Returns the value of thecurrencyrecord component.- Returns:
- the value of the
currencyrecord component
-