I have a class Item, which implements Comparable, and has a compareTo method. I want to compare the object o to other Items. I casted o to Item.
In a separate class, Inventory, I have a method for inserting items into the inventory. But I only want to insert if their product numbers are different. So I try to call the compareTo() method to compare item numbers but get a stackoverflow error.
I've tried p.compareTo(iter.next), because I want it to cycle through all of the items in the list. Sorry the formatting isn't perfect. first post here.
public class item{
public int compareTo(Object o){
result = compareTo((Product)o);
if (result < 0){
return -1;
}
else if (result == 0){
return 0;
}
else{
return 1;
}
}
}
public class ProductInventory extends Product {
private void insert(Product p){
Iterator iter = list.iterator();
if (list.size() == 0) {
list.addFirst(p);
}
while(iter.hasNext()) {
p.compareTo(iter.next());
//if (p.getNumber() != iter.next().getNumber()) {
System.out.print("RESULT:" + result);
if (result != 0) {
list.addFirst(p);
}
else {
System.out.print("DUPLICATE");
}
iter.next();
}
}
I want it to print duplicate if result = 0 (the numbers are the same), otherwise add it to list.
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)