[Solved] Addition method to java class [duplicate]


Java doesn’t support operator overloading, instead you need to add an add function:

public class Money {
    private BigDecimal amount;
    private Currency currency;

    public Money add(Money m) {
        Money res = new Money();
        if (!currency.equals(m.currency)) {
            throw new UnsupportedOperationException();
        }
        res.currency = currency;
        res.amount = m.amount.add(amount);
        return res;
    }
}

Money result = one.add(two);

6

solved Addition method to java class [duplicate]