java - Node Not Being Added Into Heap in Chronological Order -


the function of program allow user manage list of tasks using heap. current task based on soonest due date. task object contains 2 strings name , due date. have no problem adding task node heap. problem should adding heap , should become current task whenever due date coming up. program works whenever don't add anything, when add task new task becomes last task no matter set due date to. here examples:

do math homework, 05/04/2015 11:00 // read in text file , added heap grocery shopping, 05/03/2015 12:00 // read in text file , added heap write code, 04/30/2015 15:00  // user asked input , wants add "write code" tasklist, notice date.  output when run program: grocery shopping, 05/03/2015 12:00 // correct order math homework, 05/04/2015 11:00 // correct order write code, 04/30/2015 15:00 // problem here // added task gets output last no matter date 

main class:

public static heap addtask( heap h ){     task newtask = new task( "", "" ); // create new empty task.      scanner input = new scanner(system.in);     system.out.println("enter task name: ");     newtask.setname( input.nextline() );     system.out.println("enter due date: ");     newtask.setduedate( input.nextline() );      node n = new node( newtask ); // sets data of new task , adds node.     h.addnode( n );     return h; } 

task class:

public class task {     private string name;     private string duedate;     public task( string n, string d ){     name = n;     duedate = d; } // maybe compareto() wrong? public int compareto( task t ){     return duedate.compareto( t.getduedate() ); } 

heap class:

public class heap { private arraylist<node> heap; public heap(){     heap = new arraylist<node>(); } public int getploc( int ){     return ( - 1 ) / 2; } public node getnodeat( int ){     if( heap.get(i) == null ){         system.out.println("item not exist.");         return null;     }else{         return heap.get(i);     } } public void addnode( node n ){     heap.add(null);     int index = heap.size() - 1;     while( index > 0 && getnodeat(getploc(index)).getdata().compareto( n.getdata() ) > 0 ){         heap.set(index, getnodeat(getploc(index)));         index = getploc(index);     }     heap.set(index, n); } 


Comments