/* * @topic T02215 Sorting Comparable Objects * @brief class Product implements Comparable */ package arraydemo; public class Product implements Comparable { int code; String description; double price; // Constructors public Product(int cc, String dd, double pp) { code = cc; description = dd; price = pp; } // operations public String getDescription() { return description + " " + code + " " + price; } public int compareTo( Object obj ) { // If sorting by descriptions: Product another = (Product)obj; return description.compareTo(another.description); // If sorting by price: /* Product another = (Product)obj; if ( price < another.price ) { return +1; } else if ( price > another.price ) { return -1; } else { return 0; } */ } }//class Product