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.
 *  
 * @author Dr. Caffeine
 *
 */
class Rainfall
{
    
    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 );
        
        double[]  rainfall = new double[12];
        
        double    annualAverage,
                  sum,
                  difference;
                  
        sum = 0.0;
        
        for (int i = 0; i < 12; i++) {
        
            rainfall[i] = inputBox.getDouble("Rainfall for month " 
                              + (i+1));
            sum += rainfall[i];
        }
        
        annualAverage = sum / 12.0;
        
        outputBox.printLine( "Annual Average Rainfall: " +
                             Format.rightAlign( 10, 2, annualAverage ) );
        outputBox.skipLine( 2 );
        
                                
        for (int i = 0; i < 12; i++) {
            outputBox.print( Format.rightAlign(3,i+1) ); //month #
        
            //average rainfall for the month
            outputBox.print( Format.rightAlign(17,1,rainfall[i]) );
        
            //difference between the monthly and annual averages
            difference = Math.abs( rainfall[i] - annualAverage );
            outputBox.printLine( Format.rightAlign(17,2,difference) );
        }
        
    }
    
}