3

Today I am working on a new program to find the cheapest pizza in a file full of data. i am completely stuck on one issue though, it is using the compareTo method. I created it in my resource class but I am not getting errors saying a double cannot be dereferenced, I looked this issue up and still have had no help. I am not the most advanced programmer and do not understand many of the complicated answers on other resources. My specifications for the program are as follows:

B. CheapPizza.java a. Objective – implement the Comparable interface in Pizza.java. b. Assignment – Use the Pizza.java file you copied into Chapter 10. Implement the Comparable interface and add a compareTo() method. This compareTo() should be used to help you find the cheapest pizza in pizza.txt. Write CheapPizza.java client class to test the new version of Pizza.java. CheapPizza will read in the pizzas from the file and will keep track of the cheapest pizza by using the compareTo() method. Create a Pizza object to hold the cheapest pizza and a Pizza object to read in the pizzas from the file. Use compareTo() to compare the cheapest pizza to the pizza read from the file. If the pizza read from the file is cheaper, change the cheapest pizza. a. Input – pizzas.txt file on Blackboard. b. Output – should appear exactly in the format shown below: The cheapest pizza: The 9 inch olive pizza will cost $7.99

The code for the compareTo method is at the bottom of my code, can someone please explain to me what I am doing incorrect? Thank you for your time, have a great day! ~

public class Pizza   {   
    private int size;
    private double cost;
    private String topping;

    public Pizza(int s, double c, String t)
    {
       size = s;
       cost = c;
       topping = t;
    }
    public Pizza(String t, int s, double c) //.equals Method For Comparing Pizza for ''PizzaMatch''
    {
       topping = t;
       size = s;
       cost = c;

    }

    public Pizza()
    {
       size = 10;
       cost = 9.00;
       topping = "Cheese";
    }

    public String toString()
    {
       return String.format("%d inch %s pizza will cost $%.2f", size, topping, cost);
    }
    public int getSize()
    {
       return size;
    }
    public void setSize(int s)
    {
       size = s;
    }

    public double getCost()
    {
       return cost;
    }
    public void setCost(Double c)
    {
       cost = c;  
    }
    public String getTopping()
    {
       return topping;   
    }
    public void setTopping(String t)
    {
       topping = t;
    }

    public boolean equals(Object obj) //.equals Method For Comparing Pizza in "PizzaMatch"
    {
       if(!(obj instanceof Pizza)) 
          throw new IllegalArgumentException("Parameter must be of Pizza!");

       Pizza temp = (Pizza) obj;

       if (this.topping.equals(temp.topping) && this.size == temp.size  && this.cost == temp.cost)
          return true;
       else 
          return false;

    }
    //============================================================================================
    public int compareTo(Object obj){
        if(!(obj instanceof Pizza))
            throw new IllegalArgumentException
                ("Parameter must be a Pizza");
        Pizza temp = (Pizza) obj;  
        if (this.cost.compareTo(temp.cost) < 0) //this comes 1st
            return -1;
        else if(this.cost.compareTo(temp.cost) > 0) //temp comes 1st
            return 1;
        else //this and temp are equal
             return 0;
    }
}
Ryan Artecona
  • 5,953
  • 3
  • 19
  • 18
  • You don't use `compareTo` with primitives, which `double`s are. Just compare them directly, e.g. `this.cost < temp.cost`. – GriffeyDog Dec 17 '14 at 16:36

3 Answers3

3
       if(!(obj instanceof Pizza))
            throw new IllegalArgumentException
            ("Parameter must be a Pizza");
        Pizza temp = (Pizza) obj;  
        return Double.valueOf(this.cost).compareTo(Double.valueOf(temp.cost));

cost is primitive double. CompareTo is a method in object. So you can use Double object instead of double or You simple use arithmetic operation.

Siva Kumar
  • 1,983
  • 3
  • 14
  • 26
  • I appreciate the help, I was never taught this and I'm glad someone was able to explain this to me! –  Dec 18 '14 at 14:53
2

cost is a field of the primitive type double and you cannot call methods (in your case compareTo()) on primitive types, only on objects. To compare two doubles, just compare them directly

 if (this.cost < temp.cost)
    return -1;
 else if(this.cost > temp.cost)
    return 1;
 else
    return 0;
Marv
  • 3,517
  • 2
  • 22
  • 47
2

You have to implement java.lang.Comparable interface to Pizza class

public class Pizza implements Comparator<Pizza> {
    // Enter other codes
    // with toString

   @Override
   public int compareTo(Pizza o) {
        return  Double.valueOf(this.cost).compareTo(Double.valueOf(o.cost));
   }
}

Then use Collections.sort( listOfPizzas ) to sort out pizzas according to the cost.

public static void main(String[] args) {
    List l = new ArrayList();
    l.add(new Pizza(9, 7.99, "Olive Pizza"));
    l.add(new Pizza(9, 8.99, "Hot & Spicy Chicken Pizza"));
    l.add(new Pizza(9, 9.99, "Corned Mutton Sensation Pizza"));
    Collections.sort(l);
    System.out.println(" " + l.get(0));
}
sandaru.ny
  • 105
  • 7
  • Thank you for letting me know this new method of sorting the data, I appreciate it! –  Dec 18 '14 at 14:52