import javabook.*;

/*
 * Introduction to OOP with Java 2nd Edition, McGraw-Hill
 *	
 *  A sample program from Section 9.1 to compute the 
 *  annual rainfall and variations for monthly average.
 *
 *  In this version, the string values are used for month names.
 *  
 * @author Dr. Caffeine
 *
 */
class Rainfall2
{
    
    public static void main (String[] args)
    {
        MainWindow  mainWindow;
        OutputBox 	outputBox;
        InputBox    inputBox;  
        
        mainWindow = new MainWindow("Rainfall Program");
        outputBox  = new OutputBox( mainWindow );
        inputBox   = new InputBox ( mainWindow );
                
        mainWindow.setVisible( true ); 
        outputBox.setVisible( true );
        
        String[] monthName = { "January", "February", "March",
                               "April", "May", "June", "July",
                               "August", "September", "October",
                               "November", "December"  };
        
        double[]  rainfall = new double[12];
        
        double[]  quarterAverage = new double[4];
        
        double    annualAverage,
                  sum,
                  difference;
                  
        double    oddMonthSum, oddMonthAverage, 
                  evenMonthSum, evenMonthAverage;
                  
        
        sum = 0.0;
        
        for (int i = 0; i < 12; i++) {
        
            rainfall[i] = inputBox.getDouble("Rainfall for " 
                                                + monthName[i]);
            sum += rainfall[i];
        }
        
        annualAverage = sum / 12.0;
        
        outputBox.printLine( "Annual Average Rainfall: " +
                             Format.rightAlign( 10, 2, annualAverage ) );
        outputBox.skipLine( 2 );          
        

        oddMonthSum  = 0.0;
        evenMonthSum = 0.0;
        
        ///////////// Odd and Even Month Averages //////////////////
  
        //----------------- Variation ---------------------/
        /*
        for (int i = 0; i < rainfall.length; i += 2 ) {
            oddMonthSum += rainfall[i];
            evenMonthSum += rainfall[i+1];
        }
        */
        //-------------------------------------------------/
        
        //compute the average for the odd months
        for (int i = 0; i < rainfall.length; i += 2) {
            
            oddMonthSum += rainfall[i];
        }
        
        oddMonthAverage = oddMonthSum / 6.0;
        
        //compute the average for the even months
        for (int i = 1; i < rainfall.length; i += 2) {
            
            evenMonthSum += rainfall[i];
        }
        
        evenMonthAverage = evenMonthSum / 6.0;
        
        outputBox.printLine( "Odd Month Rainfall Average: " +
                             Format.rightAlign( 10, 2, oddMonthAverage ) );
                             
        outputBox.skipLine( 1 );
        
        outputBox.printLine( "Even Month Rainfall Average: " +
                             Format.rightAlign( 10, 2, evenMonthAverage ) );
                   
        outputBox.skipLine( 1 );
        
        
        /////////////////// Quarter Averages ////////////////////// 
        
        for (int i = 0; i < 4; i++) {

            sum = 0;
        
            for (int j = 0; j < 3; j++) {    //compute the sum of
                sum += rainfall[3*i + j];    //one quarter
            }
        
            quarterAverage[i] = sum / 3.0;   //average for Quarter i+1
            
            outputBox.printLine( "Rainfall Average Qtr " + (i+1) + ": " +
                             Format.rightAlign( 10, 2, quarterAverage[i] ) );
    
        }
   }
    
}