fosterbl

Item.java

Feb 18th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.63 KB | None | 0 0
  1. public class Item {
  2.    private String name;
  3.    private double price;
  4.  
  5.    public Item(){
  6.       this.name = "";
  7.       this.price = 0.0;
  8.    }
  9.  
  10.    public Item(String name, double price) {
  11.       this.name = name;
  12.       this.price = price;
  13.    }
  14.  
  15.    public double getPrice() {
  16.       return price;
  17.    }
  18.  
  19.    public String valueToString(double value) {
  20.       String result = "" + Math.abs(value);
  21.       if(result.indexOf(".") == result.length() - 2) {
  22.          result += "0";
  23.       }
  24.       result = "$" + result;
  25.       return result;
  26.    }
  27.  
  28.    public String toString() {
  29.       return name + " " + valueToString(price);
  30.    }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment