Good day!

There is a factory for the creation of different varieties of coffee (Arabica or Robusta).

They, in turn, are inherited (that is, Arabica and Robusta classes) from the abstract class Coffee.

At the moment, the creation of objects using the factory.

Then you need to shove these objects into a collection called the Van of coffee.

Great, everything turns out, the created objects are added to the CoffeeVan collection (this is a separate class).

Now I create a class CoffeeWeightComparator (I want to sort by weight).

public class CoffeeWeightComparator implements Comparator<Coffee> { public int compare(Coffee o1, Coffee o2) { Double weight1 = o1.getWeight(); Double weight2 = o2.getWeight(); return weight1.compareTo(weight2); } } 

Next comes the Main method.

 public class Runner { public static void main(String[] args) { CoffeeFactory arabica = CoffeeFactory.create(0); CoffeeFactory robusta = CoffeeFactory.create(1); CoffeeVan coffeeVan = new CoffeeVan(new ArrayList<CoffeeFactory>()); coffeeVan.fillVan(arabica); coffeeVan.fillVan(robusta); System.out.println(coffeeVan.toString()); Collections.sort(coffeeVan, new CoffeeWeightComparator()); } } 

And when I call the sort on the last line, he swears at

Wrong 1st argument type. Found: CoffeeVan, requid Coffee

I can't pass coffee, because the collection is in CoffeeVan; objects are simply created in the Coffee class.

    2 answers 2

    Swears because you pass incorrect parameters to the sort method:

     public static <T> void sort(List<T> list, Comparator<? super T> c) 

    The first parameter should be a list, and your CoffeeVan obviously not. For this code to work, you need to either change the logic, or make CoffeeVan class that implements the java.util.List interface, or a descendant of one of its implementations:

      private static class CoffeeVan extends ArrayList<Coffee> {} 
    • It turned out, only now he does not add to the collection. - D.Mark
    • This is how the add method should now appear public void fillVan (Coffee coffee) {this.add (coffee); } - Artem Konovalov
    • The van is already a list and the need for an additional list disappears, so there is no need to transfer anything to the constructor now. - Artem Konovalov
    • In this line: coffeeVan.fillVan (arabica); he swears now on fillVan in CoffeeVan't be able to apply - Coffee Mark D.
    • Why do you pass the factory in fillVan? I don’t know how you’re thinking, but usually with the help of some factories, instances of a certain class are created and then they are worked with. (see answer @iksuy) Logically, the fillVan method already needs to transfer coffee. - Artem Konovalov

    Of course he curses, CoffeeVan is transferred to your sort , and the comparator is defined for Coffee .

    The list of factories is a very dubious thing. Probably you need (or you tried to do) something like this hierarchy:

     class Coffee {/*...*/} class Arabica extends Coffee {/*...*/} class Robusta extends Coffee {/*...*/} class CoffeeFactory { public enum CoffeeCode { ARABICA, ROBUSTA }; public Coffee create(CoffeeCode cc){ if(cc == CoffeeCode.ARABICA){ return new Arabica(); } else { return new Robusta(); } } } 

    Well, your main code:

     Coffee arabica = CoffeeFactory.create(ARABICA); Coffee robusta = CoffeeFactory.create(ROBUSTA); List<Coffee> coffeeVan = new ArrayList<Coffee>(); coffeeVan.add(arabica); coffeeVan.add(robusta); Collections.sort(coffeeVan, new CoffeeWeightComparator()); 

    UPD

    If you need to use CoffeeVan , then either it must implement the List<Coffee> interface, or sort it in another way.

    • Is CoffeeVan not used at all? I just need exactly what the entire collection would be stored in CoffeeVan. And since your code works, Thank you very much, the only thing that remains is what to do with CoffeeVan, the collection is needed there. - D.Mark
    • one
      @ D.Mark, updated the answer - iksuy
    • Thank you very much! Quest decided! - D.Mark