Java – Comparator Class for same attributes from different object types

I have an odd question:

I have two classes, Book and Newspaper. They share the attribute price. I have to implement a PriceComparator which obviously compares price from Book and Newspaper. I’m not too experienced -especially in Java but my top off the head solution would be to make an interface which would get me the common values and I could do my comparison based on those but that is not possible for two reasons:

1) I can only have the classes Book, Newspaper, Main and PriceComparator and I’m not allowed to create another class/interface within the same .java file.

2) I have to use one list which contains both Book and Newspaper

My solution for this would be to maybe create a List<Object> and then compare the generic objects using casting to get their value via their getter, but this seems so clunky to me and I think there must be a better solution, though I can’t think of one. I hope my question makes sense. Thanks!

Answer

If you’re not allowed to create another class/interface within the same .java file then create your interface seperately. For example,

Priceable.java

public interface Priceable {
    double getPrice();
}

Book.java

public class Book implements Priceable{
    // ...
    int price;

    @Override
    public double getPrice() {
        return price;
    }
}

Newspaper.java

public class Newspaper implements Priceable{
    // ... other
    private double price;

    @Override
    public double getPrice() {
        return price;
    }
}

Now you can define a Comparator<Pricable> which compares the prices. For example,

Comparator<Priceable> products = (o1, o2) -> (int) (o2.getPrice() o1.getPrice());

Leave a Reply

Your email address will not be published. Required fields are marked *