How to use CompareTo() and TreeSet()

857 Views Asked by At

I have two files with lines like this: ProductName(String);ProductCode(String);SellData(Int). There are some ProductCode that are appeared in the two files. I need to get rid of duplicates and sort the items by ProductCode. Finally, the output should be the names sorted by the codes. I want to use CompareTo() and TreeSet() to solve this problem, but something wrong with my program. Here it is:

class Gyumi implements Comparable<Gyumi> {
   String termek = "";
   String termekkod = "";
   int[] evek = new int[3];

  @Override
  public String toString() {
      return termek + ", " + termekkod + ", " + Arrays.toString(evek);
  }

  @Override
  public int compareTo(Gyumi t) {
      if(termekkod >((Gyumi)t.termekkod){
          return 1;
      }
      if(termekkod <((Gyumi)t.termekkod){
        return -1;
      }
        return 0;
    }
 }
}

public class Gyumolcs {

static ArrayList<Gyumi> lista = new ArrayList<Gyumi>();
static ArrayList<Gyumi> lista2 = new ArrayList<Gyumi>();
static TreeSet<Gyumi> halmaz = new TreeSet<Gyumi>();

public static void Feltolt() {
    File r = new File("termekkod_uzlet2.csv");
    File f = new File("termekkod_uzlet1.csv");
    try {
        Scanner scan = new Scanner(f, "iso-8859-2");
        while (scan.hasNextLine()) {
            String sor = scan.nextLine();
            String[] tomb = sor.split(";");
            Gyumi gy = new Gyumi();
            gy.termek = tomb[0];
            gy.termekkod = tomb[1];
            gy.evek[0] = Integer.parseInt(tomb[2]);
            gy.evek[1] = Integer.parseInt(tomb[3]);
            gy.evek[2] = Integer.parseInt(tomb[4]);
            lista.add(gy);
            halmaz.add(gy);
        }
    } catch (Exception e) {
        System.out.println("Hiba " + e.getMessage());
    }
    try {
        Scanner scan2 = new Scanner(r, "iso-8859-2");
        while (scan2.hasNextLine()) {
            String sor = scan2.nextLine();
            String[] tomb = sor.split(";");
            Gyumi gy = new Gyumi();
            gy.termek = tomb[0];
            gy.termekkod = tomb[1];
            gy.evek[0] = Integer.parseInt(tomb[2]);
            gy.evek[1] = Integer.parseInt(tomb[3]);
            gy.evek[2] = Integer.parseInt(tomb[4]);
            lista2.add(gy);
            halmaz.add(gy);
        }
    } catch (Exception e) {
        System.out.println("valami" + e.getMessage());
    }
}

public static void main(String[] args) {
    Feltolt();
    System.out.println(halmaz);
  }
}
1

There are 1 best solutions below

0
On BEST ANSWER

Your compareTo method is wrong, change it to below:

  @Override
  public int compareTo(Gyumi t) {
     if(termekkod.compareTo(t.termekkod)>0){
         return 1;
     }
     if(termekkod.compareTo(t.termekkod)<0){
         return -1;
     }
         return 0;
   }

Also please follow general OOPS design in your code. Give private scope for your member variables and retrieve/set them using getters/setters.

This looks more neat and precise to OO design:

@Override
public int compareTo(Gyumi t) {
   if(termekkod.compareTo(t.gettermekkod())>0){
       return 1;
   }
   if(termekkod.compareTo(t.gettermekkod())<0){
       return -1;
   }
       return 0;
 }