-1

I seem to be having an issue when comparing doubles inside of an equals method. I receive an error saying java.lang.ClassCastException: and java.lang.Double cannot be cast to Item. Am I not casting correctly? When I change the cast to Double I recieve the error cannot find symbol - method getPrice().

public class Item implements Comparable
{
    private String name, category;
    private int quantity;
    private double price;

    public Item(String nam,String cate,int quant,double pric)
    {
        name = nam;
        category = cate;
        quantity = quant;
        price = pric;
    }

    public String toString()
    {
        return name + ", " + category + ", " + quantity + ", " + price;
    }

    public boolean equals(Object other)
    {
        if (price <= ((Item)other).getPrice()) // Error //
        return true;
        else
        return false;
    }

    public String getName()
    {
        return name;
    }

    public String getCategory()
    {
        return category;
    }

    public int getQuantity()
    {
        return quantity;
    }

    public double getPrice()
    {
        return  price;
    }

    public int compareTo(Object other)
    {
        int result;

        double otherPrice = ((Item)other).getPrice();
        String otherCategory = ((Item)other).getCategory();

        if (this.equals(otherPrice))
            result = category.compareTo(otherCategory);
        else
            result = price > otherPrice ? + 1 : price < otherPrice ? -1 : 0;
        return result;
    }
}
vicvicvic
  • 6,025
  • 4
  • 38
  • 55
Matt
  • 47
  • 5
  • Item.equals casts the passed Object to Item, but you call it with a Double, which can't be cast to anything except Number and Object. See http://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java for how to correctly implement equals and related issues. – Adrian Leonhard Feb 20 '15 at 20:55
  • @AngularLover that won't help... Java autoboxes anyways. – Adrian Leonhard Feb 20 '15 at 20:57
  • Why is this question being asked twice? https://stackoverflow.com/questions/28550438/double-cannot-be-dereferenced-for-equals-and-compareto-java – Zac Feb 20 '15 at 21:18

3 Answers3

0

I assume that other being passed into the compareTo mehtod is an Item.

So you have a Item other, and a double otherPrice.

Then when you call this.equals(otherPrice) in the if statement you are doing a Item.equals(Double).

You should be passing in an item. I think you want to replace double otherPrice = ((Item)other).getPrice(); with ObjectotherPrice = ((Item)other);

See if you pass a double into the equals method you try and cast the double to a Item, witch is not correct.

Zac
  • 2,201
  • 24
  • 48
0

Beware that the equals method in Java takes an Object as argument. This is also true for the compareTo method. That means that the argument can be of any type (String, List, Double...). I suspect this is what is happening here.

In your equals method, you cast the argument other to Item. But what if other is not an Item ?

You should check the type of other before casting with the instanceof operator, like this :

public boolean equals(Object other) {
    if (!(other instanceof Item)) { //this also includes the case where other is null
        return false;
    }
    return price <= ((Item)other).getPrice();
}

The best practice would also be to add a check on this so as to avoid the cast altogether :

public boolean equals(Object other) {
    if (this == other) {
        return true;
    }
    if (!(other instanceof Item)) {
        return false;
    }
    return price <= ((Item)other).getPrice();
}

With this in mind, you should also review your compareTo method.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • I like this advice, very true. I think the real issue here is that he is passing a double into the equals method, then casting it to a Item. – Zac Feb 20 '15 at 21:04
0

Your problem lies in your compareTo method. You are trying to compare your Item Object to a double. Your method should look like this:

public int compareTo(Object other) {
    int result;

    double otherPrice = ((Item) other).getPrice();
    String otherCategory = ((Item) other).getCategory();

    if (this.price==otherPrice)
        result = category.compareTo(otherCategory);
    else
        result = price > otherPrice ? +1 : price < otherPrice ? -1 : 0;
    return result;
}
Johan Prins
  • 112
  • 5