/*********************************************************************
*
*  Class:   Proj3 (Main Module)
*   Course:  COP 3530, Summer C 1999
*
*  Author:  Student Name(cise: login  ) - 123-45-6789
*
*  Summary:  This is the main module for Project 3
*
*/


import ArrayHandler;           // array handling
import DLinkList;              // doubly linked list handling
import DLLMergeSort;           // doubly linked list with merge sorting

public class Proj3
{
	
	/*     DATA                */                           
	private static final int ARRAY = 1;        //these consts for readability
	private static final int DLL = 2;
	
	private static final int HISTO = 1;
	private static final int INSERT = 2;
	private static final int MERGE = 3;
	
		
	/*      METHODS             */
	
	public static void main(String args[])
   							throws java.io.IOException
  {

	int sortMethod;                    //user selected sort method
	ArrayHandler array;                //unitialized instance of array
	DLLMergeSort dlist;                //    "          "      "  DLL	
    String usage = "usage: Proj3 <DataStruct>  <sortMethod>";
	
	if ( (args.length < 2) ||          //check proper amount of parameters
         (!args[0].equals("-a") && !args[0].equals("-d")) ||//check for a or d param	
	     (!args[1].equals("-h") && !args[1].equals("-i") && //check for valid sort method
 		  !args[1].equals("-m")))
	
	{
	  System.out.println(usage);        //notify error and quit
	  return;
	}
	
	if (args[1].equals("-h"))             //set sort method
	  sortMethod = HISTO;
	else if (args[1].equals("-i"))
	  sortMethod = INSERT;
	else
	  sortMethod = MERGE;
	  
 	if (args[0].equals("-a"))                //use array data type
	{  
	  	array = new ArrayHandler();
		array.getValues();                   //get values from stdin
	  	writeHeader(ARRAY, sortMethod, array.getSize()); //print header to screen	
	    array.writeSequence();               //write original seq
	  	if (sortMethod == HISTO)
		  array.sortHistogram();             //do histogram sort
		else if (sortMethod == INSERT)
		  array.sortInsertion();             //do insertion sort
		else
		  array.sortMerge();                  //do merge sort
		  
		System.out.println("Sorted sequence:");  
        array.writeSequence();                //print out sorted sequence
	}
	else if (args[0].equals("-d"))            //use DLL
	{
		dlist = new DLLMergeSort();          //make a DLL with merge sorting
		dlist.getValues();                   //get values from stdin
		writeHeader(DLL, sortMethod,
		  dlist.getSize());                  //write header to screen	
		dlist.displayList();                 //write original list
	  	if (sortMethod == HISTO)
		  dlist.sortHistogram();             //do histogram sort using dll
		else if (sortMethod == INSERT)
		  dlist.sortInsertion();             //do insertion sort using dll
		else
		{
		  dlist.mergeSort();                  //do merge sort using dll
		}
		System.out.println("Sorted sequence:");
        dlist.displayList();                  //show the list
	}
	else                                      //invalid switch used
	{  
	  System.out.println(usage);              //inform user then quit
	  System.out.println("-valid switches: -a (Array impl.) -s (SLL) -d (DLL)");
	  return;
	}
  }
  
    
  private static void writeHeader(int ADT, int sortMethod, int count)  //writes header for each type
  {
  	String ADTString, SortMethodString;
	if (ADT == ARRAY)
	  ADTString = new String("Array");
	else
	  ADTString = new String("Doubly-Linked List");
	
	if (sortMethod == HISTO)
	  SortMethodString = "Histogram-sort";
	else if (sortMethod == INSERT)
	  SortMethodString = "Insertion-sort";
	else
	  SortMethodString = "Merge-sort";
	  
	System.out.println("Student Name     123-45-6789");
	System.out.println("02 July 1999     COP3530-C99-Proj3");
	System.out.println("");
	System.out.println("Numbers read in: " + count);
	System.out.println("");
	System.out.println("Data Structure: " + ADTString);
  	System.out.println("Sorting Algorithm: " + SortMethodString);
	System.out.println("");
	System.out.println("Input sequence:");
  }
}
	  
   
    
