📄 money.java
字号:
package net.java.workeffort.infrastructure.uom;import java.io.Serializable;import java.math.BigDecimal;import java.util.Currency;/** * The money class. * @author Antony Joseph */public class Money implements Serializable { private static final int[] cents = new int[] { 1, 10, 100, 1000 }; private long amount; private Currency currency; public Money(BigDecimal amount, Currency currency) { this.currency = currency; this.amount = Math.round(amount.doubleValue() * centFactor()); } private Money(long amount, Currency currency) { this.currency = currency; this.amount = amount; } public BigDecimal getAmount() { return BigDecimal.valueOf(amount, currency.getDefaultFractionDigits()); } public Currency getCurrency() { return currency; } public boolean equals(Object other) { return (other instanceof Money) && equals((Money) other); } public boolean equals(Money other) { return currency.equals(other.currency) && (amount == other.amount); } public int hashCode() { return (int) (amount ^ (amount >>> 32)); } private int centFactor() { return cents[currency.getDefaultFractionDigits()]; } public Money add(Money other) { assertSameCurrencyAs(other); return newMoney(amount + other.amount); } public Money subtract(Money other) { assertSameCurrencyAs(other); return newMoney(amount - other.amount); } public boolean greaterThan(Money other) { return (compareTo(other) > 0); } public boolean lessThan(Money other) { return (compareTo(other) < 0); } public Money multiply(BigDecimal amount) { return new Money(getAmount().multiply(amount), currency); } public int compareTo(Object other) { return compareTo((Money) other); } public int compareTo(Money other) { assertSameCurrencyAs(other); if (amount < other.amount) return -1; else if (amount == other.amount) return 0; else return 1; } public String toString() { return "amount=" + getAmount() + " currency=" + currency; } private Money newMoney(long amount) { return new Money(amount, this.currency); } private void assertSameCurrencyAs(Money arg) { if (!currency.equals(arg.getCurrency())) throw new CurrencyMismatchException("Mismatch between currencies" + currency + " and " + arg.getCurrency()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -