testing a Money object
I want to test a money object to ensure it is not negative and if it is then throw an exception. This comes up with an error, can you figure it out for me?
publicstaticvoid isnotNegative(Money cash1)
{
if (cash1 < 0)
{
thrownew IllegalArgumentException();
}
}
operator < cannot be applied to Money,int
I know. An int is a primitive, a Money is an object. You can't compare the two, although there is probably a method in the Money class that returns an int, which can be compared to another int. It's hard to say without seeing the code for the Money class.
Ok, I got this to work:
public static void isnotNegative(Money cash1)
{
if (cash1.isNegative())
{
throw new IllegalArgumentException();
}
}
It didn't compile the first time I tried it. I guess I typed something wrong before.