Advertisement
SamuilPetrow

ProductClass

May 15th, 2014
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.54 KB | None | 0 0
  1.  
  2. public class Product implements Comparable<Product>{
  3.     private String name;
  4.     private double price;
  5.    
  6.     public Product(String name, double price){
  7.         this.name = name;
  8.         this.price = price;
  9.     }
  10.    
  11.     public String getName(){
  12.         return name;
  13.     }
  14.    
  15.     public double getPrice(){
  16.         return price;
  17.     }
  18.    
  19.     public int compareTo(Product compareFruit) {
  20.          
  21.         double otherPrice = ((Product) compareFruit).getPrice();
  22.  
  23.         //ascending order
  24.         if(this.price>otherPrice) return 1;
  25.         else
  26.         if(this.price==otherPrice) return 0;
  27.         return -1;
  28.     }  
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement