Question

package prob1;

public class Franchise implements Comparable<Franchise> {
    private String location;
    private double totals = 0;
    private String owner;

    public Franchise(String location,double totals,String owner){
        this.location = location;
        this.totals = totals;
        this.owner = owner;
    }
    public String getLocation(){
        return location;
    }
    public double getTotal(){
        return totals;
    }
    public String getOwner(){
        return owner;
    }
    public int compareTo(Franchise that) {
        int stateComparition = this.getLocation().compareTo(that.getLocation()); 
        Double sales = Double.valueOf(this.getTotal());    
        Double thatSales = Double.valueOf(that.getTotal());
        int salesComparition = sales.compareTo(thatSales);    

        if(stateComparition == 0){
            if(salesComparition > 0)
                 return -1;
            else if(salesComparition < 0)
                 return 1;
            else
                 return 0;
        }
           return stateComparition;         
    }

    public String toString(){
        String result = "";
        result = "Owner =  " + getOwner() + " ,State = "+ getLocation() + " ,Sales = " + getTotal() + "\n";

        return result;
    }

}

This class has a compareTo method.I was wondering if you guys can explain me in detail what is this method doing...basically I need you guys to trace it and explain me step by step.Thanks a lot!

package prob1;
import java.util.ArrayList;
import java.util.Collections;

public class FracnahiseTester {

    /**
     * @param args
     */
    public static void main(String[] args) {
         ArrayList<Franchise> franchises = new ArrayList<Franchise>();

         Franchise franchaise1 = new Franchise("CA",3300,"Corey");
         Franchise franchaise2 = new Franchise("GA",1000,"Pato");
         Franchise franchaise4 = new Franchise("NC",3300,"Travis");
         Franchise franchaise3 = new Franchise("NC",3500,"Roy");

         franchises.add(franchaise1);
         franchises.add(franchaise2);
         franchises.add(franchaise4);
         franchises.add(franchaise3);

         Collections.sort(franchises);
         for(Franchise i : franchises){
             System.out.println(i);
         }
    }

}

This is my tester

Était-ce utile?

La solution

Although you can get this info from javadoc, this answer will help understand the concept of compareTo() method for other users who drop by here.

When you are implementing Comparable interface, your compareTo() method is used to compare different objects,

public int compareTo(Franchise that) {
    int stateComparition = this.getLocation().compareTo(that.getLocation()); 
    Double sales = Double.valueOf(this.getTotal());    
    Double thatSales = Double.valueOf(that.getTotal());
    int salesComparition = sales.compareTo(thatSales);    

    if(stateComparition == 0){
        if(salesComparition > 0)
             return -1;
        else if(salesComparition < 0)
             return 1;
        else
             return 0;
    }
       return stateComparition;         
}
  • public int compareTo() used to compare the object passed as argument with the object that calls this method.
  • If this object is less than that object, a negative integer is returned, in your case its -1. That means this objects comes first before that in sorted manner.
  • If this is equal to that object, then 0 should be equal that means objects are equal.
  • If this is greater than that object than a positive integer is returned, in your case, it is 1. It means this comes AFTER that when objects of Franchise type are sorted.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top