-1

I'm trying to create a method in java which compares some products. I need to compare them by unit of measure and then by quantity, but I don't know how. I need to use object.equals ? I'll put my code here.

public abstract class Produs implements Comparable {
    private String tipProdus;
    private String unitateMasura;
    private int cantitate;

    public Produs(String tipProdus, String unitateMasura, int cantitate) {
        super();
        this.tipProdus = tipProdus;
        this.unitateMasura = unitateMasura;
        this.cantitate = cantitate;
    }

    public Object genereazaDescriere() {
        Object String = null;
        return String;

    }
    public void compareTo() {
        tipProdus.equals(unitateMasura);{
            
        }
    }
}
AlyAly12
  • 31
  • 7
  • Does this answer your question? [How to compare objects by multiple fields](https://stackoverflow.com/questions/369512/how-to-compare-objects-by-multiple-fields) – Hülya Dec 06 '20 at 13:24
  • I don't think so, I have some products, so they are string, and I think I need to use .equals – AlyAly12 Dec 06 '20 at 13:29
  • What are you trying to achieve in our code by checking if `tipProdus.equals(unitateMasura)`?? Have you looked at [`Comparable` javadoc](https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html) -- your code does NOT implement comparable interface at all. [This link](https://stackoverflow.com/questions/3718383/why-should-a-java-class-implement-comparable) gives some examples of proper implementations. Also it seems that you need to realize _how_ you should compare objects with different `unitateMasura`, most likely there should be some conversion (e.g. inch to mm, etc.) – Nowhere Man Dec 06 '20 at 13:51

1 Answers1

0

First of all, you can not create objects from an abstract class so you need to change that or just forget about that constructor because you won't be able to use it. To compare two objects you need to create a comparator class so you would be able to compare objects according to the attributte you want to. So in this case you need to create two comparators, one to compare them by unit of measure and another to compare it by quantity. So it would be something like this:

Comparator by unit of measure:

public class UnitOfMeasureProdusComparator implements Comparator<Produs> {
    @Override
    public int compare(Produs p1, Produs p2) {
      return p1.getUnitateMasura().compareTo(p2.getUnitateMasura());
    }

Comparator by quantity:

public class QuantityProdusComparator implements Comparator<Produs> {
    @Override
    public int compare(Produs p1, Produs p2) {
      return p1.getCantitate().compareTo(p2.getCantitate());
    }

So now for example if you have an arraylist of Produs objects you can compare them like this:

ArrayList<Produs> products = new ArrayList<>();
Produs p1 = new Produs("x", "centimeters", 5);
Produs p2 = new Produs("y", "meters", 4);
products.add(p1,p2);
//now you have two objects created so if you want to sort them by there quantity u can do it like this:
Collections.sort(productos, new QuantityProdusComparator());

It will sort the list according to the comparator you use. Internally the comparator class will send a 0 if the objects are equal, a -1 if the object is smaller than or a 1 if the object is bigger than.

If you comparing string attributes it will do it in alphabetical order.

MadMax
  • 91
  • 3
  • 14
  • "comparing" by unit of measure is pretty meaningless if these units are compared as strings in alphabetic order: `"centimeter" < "inch" < "kilometer" < "meter" < "millimeter" < "nanometer"` :) – Nowhere Man Dec 06 '20 at 15:08
  • 1
    I know, that's why i said it. I think it should just be ordered by quantity with different arrays per each unit of measure or something similar. – MadMax Dec 06 '20 at 16:50