0

So I'm debugging my program and I have to use a few operators. However the values that I'm doing the comparison on are objects of custom classes.

I created this "Money" Class.

public Money(double amount)
{
    if (amount < 0)
    {
        System.out.println(
             "Error: Negative amounts of money are not allowed.");
        System.exit(0);
    }
    else
    {
        long allCents = Math.round(amount*100);
        dollars = allCents/100;
        cents = allCents%100;
    }
}

And I have these errors:

CreditCard.java:55: error: bad operand types for binary operator '<='
     if (balance && amount <= creditLimit)
                           ^
first type:  Money
second type: Money
CreditCard.java:57: error: bad operand types for binary operator '+'
        balance += amount;
                ^
first type:  Money
second type: Money
CreditCard.java:68: error: bad operand types for binary operator '-'
     balance -= amount;
             ^
first type:  Money
second type: Money
3 errors

I'm trying to do this operation:

public void charge(Money amount)
  {
     if (balance && amount <= creditLimit)
     {
        balance += amount;
     }
     else
     {
        System.out.println("The amount to charge exceeds the credit limit and will not be charged.");
     }

  }

What do I use for these kinds of operators with custom objects?

ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37
WillBro
  • 147
  • 4
  • 16

1 Answers1

2

In Java, you cannot overload operators to work with custom objects. You will need to add methods to the Money class to do the operations you want. For instance:

class Money {
    . . .
    public boolean exceeds(Money creditLimit) {
       return dollars > creditLimit.dollars
           || (dollars == creditLimit.dollars && cents > creditLimit.cents);
    }

    public Money incrementBy(Money amount) {
        long allCents = 100 * (dollars + amount.dollars)
            + cents + amount.cents;
        dollars = allCents / 100;
        cents = allCents % 100;
        return this; // for chaining
    }
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 2
    I would recommend having `Money` implement `Comparable` (which can serve many other purposes) as well as `equals()`, in addition to adding specific "operators" (e.g. increment / decrement). – Jason C Mar 17 '14 at 22:27
  • 2
    @JasonC - Yes, that's a good idea. Then `if (x.exceeds(y)) ...` could be replaced by `if (x.compareTo(y) > 0) ...`. It should also implement `hashCode()` as well as `equals()`. – Ted Hopp Mar 17 '14 at 22:28
  • Have you considered using BigDecimal ... since you appear to have created the Money class to handle decimal representation of currency (to "pennies")? – ErstwhileIII Mar 17 '14 at 22:55
  • This won't fix the first error. – tbodt Mar 17 '14 at 23:06
  • @ErstwhileIII - That's also a good idea, but involves more of a change to OP's design (plus a little added complexity to specify the rounding mode). An alternative would be to simply keep everything as cents internally in the `Money` class. – Ted Hopp Mar 17 '14 at 23:10
  • @tbodt - Why not? OP will need to replace the `if` tests with method calls; the nonsense syntax `balance && amount <= creditLimit` would be forced to go away. – Ted Hopp Mar 17 '14 at 23:11
  • @tbodt - I rolled back your edit because the syntax you suggested is still illegal in Java; you can't use `==` or `<=` with custom objects. – Ted Hopp Mar 17 '14 at 23:12
  • Would be useful to understand BigDecimal and rounding if using for money calculations (particular with different world currencies) – ErstwhileIII Mar 17 '14 at 23:16