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;
}
}