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;
}
}