Advertisement
Guest User

Product

a guest
May 21st, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2.  
  3.  
  4. public class Product implements Comparable <Product>{
  5.     private String name;
  6.     private double price;
  7.     Product (String name, double price) {
  8.         this.name = name;
  9.         this.price = price;
  10.     }
  11.     public double getPrice() {
  12.         return this.price;
  13.     }
  14.     @Override
  15.     public String toString() {
  16.         DecimalFormat formatter = new DecimalFormat("#0.00");
  17.         return formatter.format(this.price) + " " + this.name;
  18.     }
  19.     @Override
  20.     public int compareTo(Product arg) {
  21.         if(this.price < arg.price)
  22.             return -1;
  23.         else if (this.price > arg.price)
  24.             return 1;
  25.         return 0;
  26.     }
  27.     @Override
  28.     public boolean equals(Object obj) {
  29.         if (this == obj)
  30.             return true;
  31.         if (obj == null)
  32.             return false;
  33.         if (getClass() != obj.getClass())
  34.             return false;
  35.         Product other = (Product) obj;
  36.         if (name == null) {
  37.             if (other.name != null)
  38.                 return false;
  39.         } else if (!name.equals(other.name))
  40.             return false;
  41.         return true;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement