Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Create a class Product to hold products, which have name (string)
- * and price (decimal number).
- * Each product must been written in format price + space + name. */
- import java.math.BigDecimal;
- public class Product implements Comparable<Product> {
- private String name;
- private BigDecimal price;
- public Product(String name, String priceStr) {
- setName(name);
- setPrice(priceStr);
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public BigDecimal getPrice() {
- return price;
- }
- public void setPrice(BigDecimal price) {
- this.price = price;
- }
- public void setPrice(String priceStr) {
- this.price = new BigDecimal(priceStr);
- }
- @Override
- public int compareTo(Product nextProduct) {
- return (this.price.compareTo(nextProduct.getPrice()));
- }
- @Override
- public String toString() {
- return String.format("%.2f %s", price, name);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement