
/**
 *
 * a simple task organiser
 *
 */

import java.lancs.* ;

public class TaskOrganizer
    {
    public static void main(String[] args) throws Exception
        {
        PriorityQueue p = new PriorityQueue() ;
        QueueElement temp = new QueueElement() ;

        BasicIo.prompt("type name of file containing tasks ") ;
        String fileName = BasicIo.readString() ;
        BasicFileIo fileIn = new BasicFileIo(BasicFileIo.INPUT, fileName) ;
        while (true)
	    {
    	    int priority = fileIn.readInteger() ;
    	    if (priority == 0)
		break ;
    	    String task = fileIn.readString() ;
    	    temp.setPriority(priority) ;
    	    temp.setData(task) ;
    	    p.insert(temp) ;
    	    }
        fileIn.closeFile() ;
        System.out.println("there are " + p.length() + " tasks") ;

	int priority ;
	String data ;
	String response ;
	boolean continueLoop = true ;

	while (continueLoop)
   	    {
   	    displayMenu() ;

   	    response = BasicIo.readString().toLowerCase() ;
   	    if (response.length() == 0)
      	        response = "x" ;

   	    switch (response.charAt(0))
   		{
   		case 'a' :
      	    	    BasicIo.prompt("task to add ") ;
      		    data = BasicIo.readString() ;
      		    BasicIo.prompt("priority? ") ;
      		    priority = BasicIo.readInteger() ;
      		    temp.setPriority(priority) ;
      		    temp.setData(data) ;
      		    p.insert(temp) ;
      		    break ;

   		case 'f' :
      		    temp = p.first() ;
      		    System.out.print("'" + temp.getData()) ;
      		    System.out.print("' at head of list with priority ") ;
      		    System.out.println(temp.getPriority()) ;
      		    break ;

   		case 'r' :
      		    temp = p.remove() ;
      		    System.out.print("'" + temp.getData()) ;
      		    System.out.print("' removed from list with priority ") ;
      		    System.out.println(temp.getPriority()) ;
      		    break ;

   		case 'q' :
      		    continueLoop = false ;
      		    break ;

   		default :
      		    System.out.println("invalid response") ;
      		    break ;
   	        }

   	    if (continueLoop)
      		{
      		BasicIo.prompt("press RETURN to continue ") ;
      		response = BasicIo.readString() ;
      		}
	    }

	BasicFileIo fileOut = new BasicFileIo(BasicFileIo.OUTPUT, fileName) ;
	while (!p.isEmpty())
	    {
	    temp = p.remove() ;
	    fileOut.println(temp.getPriority()) ;
	    fileOut.println(temp.getData()) ;
	    }
	fileOut.println(0) ;
	fileOut.closeFile() ;
	} // end of method main

    private static void displayMenu()
        {
        System.out.println() ;
        System.out.println("Options to Manipulate Task List") ;
        System.out.println() ;
        System.out.println("a : add new task") ;
        System.out.println("f : look at first task") ;
        System.out.println("r : remove first task") ;
        System.out.println() ;
        System.out.println("q : quit") ;
        } // end of method displayMenu

    } // end of class TaskOrganizer

