I think I have gotten pretty far, but I am at a logical hang-up maybe some of you smarter fellows can help me out!
public class ItemList{
ItemInfoNode head;
ItemInfoNode tail;
int listCount = 0;
public ItemList(){
head = tail = null;
}
public void insertInfo(String name, String rfidTag, String initPosition, double price){
ItemInfo obj = new ItemInfo(name, rfidTag, initPosition, initPosition, price);
ItemInfoNode temp = new ItemInfoNode();
temp.setInfo(obj);
if(head == null){ head = tail = temp; }
else{
if(head == tail){//BEGIND SECOND OBJECT HANDLING
if(head.getInfo().getTag().compareToIgnoreCase(rfidTag) <= 0){//to see if temp belongs after head
head.setNext(temp);
temp.setPrev(head);
tail = temp;
}
else{
ItemInfoNode nodePtr = head;
head = temp;
tail = nodePtr;
head.setNext(tail);
tail.setPrev(head);
}
}//END SECOND OBJECT HANDLING
else{
if(head.getInfo().getTag().compareToIgnoreCase(rfidTag) > 0){
ItemInfoNode nodePtr = head;
head = temp;
temp.setNext(nodePtr);
temp.getNext().setPrev(head);
}
else if(head.getInfo().getTag().compareToIgnoreCase(rfidTag) <= 0 && tail.getInfo().getTag().compareToIgnoreCase(rfidTag) > 0){
head.setNext(temp);
temp.setPrev(head);
}
else{//item bigger then tail
ItemInfoNode nodePtr = tail;
tail = temp;
tail.setPrev(nodePtr);
tail.getPrev().setNext(tail);
}
}
}
listCount++;
}}
Now the purpose of this method is obviously to insert nodes where they belong, but they need to be sorted by their rfidTag String, which is a hexadecimal number, I'm not sure if it is apparent but I want to do it from least to greatest order. Now as you can see my code has become very convoluted and very difficult to follow and deal with, but I think I am close, anyone have any tips or "logical guidance" they can give to help me better understand how to go about getting this to work properly? In its current state it is destroying my list, somewhat looping, and then throwing a NullPointerException!
EDIT**: So I have revised my code and added comments to show a more concise interpretation of what I am looking to accomplish, maybe someone can help me understand how to hand these methods now?
I am very close now, it works when I put in objects in the order in which they would be in the list, but if I try to insert an object node that belongs somewhere in the middle I destroy my list, I am not seeing my mistake, anyone see it? Main for reference
public class Test{
public static void main(String args[]){
ItemInfo item = new ItemInfo(null, null, null, null, 0);
item.setName("Chocolate");
item.setTag("2");
item.setOrigin("s12345");
item.setCurrent("s12345");
item.setPrice(30.00);
ItemInfo item2 = new ItemInfo(null, null, null, null, 0);
item2.setName("Buzz Lightyear");
item2.setTag("1");
item2.setOrigin("d67890");
item2.setCurrent("d67890");
item2.setPrice(15.99);
ItemInfo item3 = new ItemInfo(null, null, null, null, 0);
item3.setName("Hotwheels");
item3.setTag("000000000");
item3.setOrigin("h34743");
item3.setCurrent("h34743");
item3.setPrice(24.25);
ItemInfo item4 = new ItemInfo(null, null, null, null, 0);
item4.setName("Barbie");
item4.setTag("FFFFFFFFF");
item4.setOrigin("s49862");
item4.setCurrent("s49862");
item4.setPrice(21.22);
ItemInfo item5 = new ItemInfo(null, null, null, null, 0);
item5.setName("Bicycle");
item5.setTag("CCCCCCCCC");
item5.setOrigin("k28475");
item5.setCurrent("k28475");
item5.setPrice(10.99);
ItemInfoNode nood = new ItemInfoNode();
ItemInfoNode nood2 = new ItemInfoNode();
ItemInfoNode nood3 = new ItemInfoNode();
ItemInfoNode nood4 = new ItemInfoNode();
ItemInfoNode nood5 = new ItemInfoNode();
nood.setInfo(item);
nood2.setInfo(item2);
nood3.setInfo(item3);
nood4.setInfo(item4);
nood5.setInfo(item5);
ItemList list = new ItemList();
list.insertInfo(item.getName(), item.getTag(), item.getCurrent(), item.getPrice());
list.insertInfo(item2.getName(), item2.getTag(), item2.getCurrent(), item2.getPrice());
list.insertInfo(item3.getName(), item3.getTag(), item3.getCurrent(), item3.getPrice());
list.insertInfo(item4.getName(), item4.getTag(), item4.getCurrent(), item4.getPrice());
list.insertInfo(item5.getName(), item5.getTag(), item5.getCurrent(), item5.getPrice());
list.printAll();
}
}
And my output as well...
Hotwheels Bicycle
Now if I change the rfidTags of the 5 objects so that the next one is larger then the last, it works, but not if they are put in the way they are now.