
import javabook.*;
import java.awt.*;
import java.io.*;

/**
 * The main program to test the Ch 16 algorithms.
 *
 * @author Dr. Caffeine
 *
 */
class TestCh16Algorithms
{

//--------------------------------
//  Data Members
//--------------------------------

    /**
     * The main window of the program
     */
    private MainWindow mainWindow;
         
    /**
     * OutputBox for displaying the test messages
     */
    private OutputBox  outputBox;
    
    /**
     * Input for getting the shape dimension
     */
    private InputBox   inputBox;
   
    /**
     * MessageBox for error messages
     */
    private MessageBox messageBox;
   
    /**
     * ListBox for listing available algorithms
     */
    private ListBox    algorithmListBox;

    /**
     * The algorithm selected by the user
     */
    private int        selectedAlgorithm;
    
    /**
     * The Ch16Algorithms object for running the selected algorithm
     */
    private Ch16Algorithms algorithmRunner;
    
      
//--------------------------------
//  Constructors
//--------------------------------

    /**
     * Default constructor. The default window title is "Chapter 16 Algorithms".
     */
    public TestCh16Algorithms()
    {
        mainWindow  = new MainWindow("Chapter 16 Algorithms");
        outputBox   = new OutputBox ( mainWindow );
        inputBox    = new InputBox  ( mainWindow );
        messageBox  = new MessageBox( mainWindow );
        
        algorithmListBox = new ListBox( mainWindow, "Select Algorithm:" );
        algorithmListBox.addItem( "Factorial"         );
        algorithmListBox.addItem( "Directory Listing" );
        algorithmListBox.addItem( "Anagram"           );
        algorithmListBox.addItem( "Towers of Hanoi"   );
        algorithmListBox.addItem( "Quicksort"         );
        algorithmListBox.addItem( "-----------------" );
        algorithmListBox.addItem( "Quit"              );
        
        mainWindow.setVisible( true );
        outputBox.setVisible( true );
        
        algorithmRunner = new Ch16Algorithms( outputBox );
    }
    
    
    
    public static void main( String[] args )
    {
        TestCh16Algorithms test = new TestCh16Algorithms(  );
        test.start( );
    }
    
    
    public void start( )
    {
        int selection; 
        
        do {
            
            selection = algorithmListBox.getSelectedIndex( );
            
            switch ( selection ) {
                
                case 0: runFactorial( );
                        break;
                        
                case 1: runDirList( );
                        break;
                        
                case 2: runAnagram( );
                        break;
                        
                case 3: runTowersOfHanoi( );
                        break;
                
                case 4: runQuickSort( );
                        break;
                        
            }
        } while ( selection != 6 );
        
    }
    
    private void runFactorial( )
    {
        int N = inputBox.getInteger( "Compute N. Enter N:" );
        
        while ( N <= 0 ) {
            
            messageBox.show( "N must be positive" );
            
            N = inputBox.getInteger( "Enter N:" );
        }
        
        int answer = algorithmRunner.factorial( N );
        
        outputBox.skipLine( 2 );
        outputBox.printLine( "Factorial of N is " + answer );
    }
    
    private void runDirList( )
    {
        String dirName = inputBox.getString( "Enter the folder name");
        
        File file = new File( dirName );
        
        outputBox.skipLine( 2 );
        
        algorithmRunner.directoryListing( file );

    }
    
    private void runAnagram( )
    {
        String word = inputBox.getString( "Generate anagrams. Enter a word" );
        
        outputBox.skipLine( 2 );
        
        algorithmRunner.anagram( "", word );
    }
    
    private void runTowersOfHanoi( )
    {
        int diskCnt = inputBox.getInteger( "Towers of Hanoi. How many disks?");
        
        outputBox.skipLine( 2 );
        
        algorithmRunner.towersOfHanoi( diskCnt, 1, 3, 2 );
        
    }
    
    private void runQuickSort( )
    {
        int N = inputBox.getInteger( "Quicksort. Number of elements to sort:");
        
        outputBox.skipLine( 2 );
        outputBox.printLine( " Unsorted List: " );


        //generate random numbers from 1 to N
        int[] list = new int[N];
        
        for (int i = 1; i < N; i++) {
            list[i] = (int) Math.floor(Math.random( ) * N) + 1;
            outputBox.print("  " + list[i]);
        }
        
        //now sort the list
        algorithmRunner.quickSort( list, 0, N-1 );
        
        outputBox.skipLine( 2 );
        outputBox.printLine( " Sorted List: " );
        
        for (int i = 1; i < N; i++) {
            outputBox.print("  " + list[i] );
        }
    }
    
}
    
    
    
