Advertisement
dimipan80

Class Product (Using for Some of the Next Solutions)

Aug 20th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.94 KB | None | 0 0
  1. /* Create a class Product to hold products, which have name (string)
  2.  * and price (decimal number).
  3.  * Each product must been written in format price + space + name. */
  4.  
  5. import java.math.BigDecimal;
  6.  
  7. public class Product implements Comparable<Product> {
  8.     private String name;
  9.  
  10.     private BigDecimal price;
  11.  
  12.     public Product(String name, String priceStr) {
  13.         setName(name);
  14.         setPrice(priceStr);
  15.     }
  16.  
  17.     public String getName() {
  18.         return name;
  19.     }
  20.  
  21.     public void setName(String name) {
  22.         this.name = name;
  23.     }
  24.  
  25.     public BigDecimal getPrice() {
  26.         return price;
  27.     }
  28.  
  29.     public void setPrice(BigDecimal price) {
  30.         this.price = price;
  31.     }
  32.  
  33.     public void setPrice(String priceStr) {
  34.         this.price = new BigDecimal(priceStr);
  35.     }
  36.  
  37.     @Override
  38.     public int compareTo(Product nextProduct) {
  39.         return (this.price.compareTo(nextProduct.getPrice()));
  40.     }
  41.  
  42.     @Override
  43.     public String toString() {
  44.         return String.format("%.2f %s", price, name);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement