/*
 * Created on Sep 18, 2004
 * Modified to illustrate exception handling: Oct 25, 2004. Then Oct 28, 2004 for the finally clause.
 *
 * CS 178 Programming with Multimedia Objects
 * 
 * Objective: Learn to input data  via an Applet,  process data internally, and output data to screen...all with Exception handling.
 */

/**
 * @author Aditya Mathur
 *
 * A chemical reaction is taking place. You are measuring the temperature
 * of the reaction at regular intervals. The mesaurement is being entered into the computer via your applet.
 * Your Java applet keeps track of the current minimum, maximum, and average  temperature,
 * and the number of  observations. Handle exceptions!
 */

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.math.*;

public class DataAnalysisFinally extends Applet implements ActionListener{

	
	// Create a button for the user to indicate data is ready for input.
	
Button recordData=new Button("Record");
Button reset=new Button("Reset");

// Create text fields for data input and output.

TextField dataInField=new TextField("Enter data", 50);
TextField maxField=new TextField("No data", 6);
TextField minField=new TextField("No data", 6);
TextField avgField=new TextField("No data", 6);
TextField numObsField=new TextField("0", 6); // Number of correct observations.
TextField numIncObsField=new TextField("0", 6);// Number of incorrect observations.

Label maxLabel=new Label("Max");
Label minLabel=new Label("Min");
Label avgLabel=new Label("Avg");

// Initialize sum, min,  max, and average.

double sum=0.0;
double min=Double.MAX_VALUE;  // Min  possible integer.
double max=Double.MIN_VALUE; // Max possible integer.
double avg=0.0;


// Initialize the number of correct and incorrect observations entered.

int numObservations=0;
int numIncObservations=0;
boolean correct=true; // Set to false if data input is incorrect.




public void init(){
	
	
	// Add a listener to the recordData button.
	
recordData.addActionListener(this);
reset.addActionListener(this);

// Add the two buttons first to the applet screen.

add(recordData);

// Next add the data in/out widgets.


add(dataInField);
add(numObsField);
add(numIncObsField);
add(maxLabel); add(maxField);
add(minLabel); add(minField);
add(avgLabel); add(avgField);



// Add the reset buton to the end of the screen.

add(reset);
setBackground(Color.orange);
	
} // End of init() method.

public void actionPerformed(ActionEvent e){
	
	Object source=e.getSource();
	
	
	double dataEntered;
	
	if(source==reset)resetScreen();
	
	else
		if(source==recordData)
		{
		// Get string from the dataInField.
		
		String dataString=dataInField.getText();
		
		// Convert it to a double value.
		
		try{
		
			dataEntered=Double.parseDouble(dataString);
			//			If no exception raised then the entered data is correct. 
			// 		Update sum, and average.
			
			correct=true;
			sum=sum+dataEntered;
			numObservations=numObservations+1;
			avg=sum/numObservations;
			
			// Update min and max values.
			
			if(min>dataEntered)min=dataEntered;
			if(max<dataEntered)max=dataEntered;
			
			
		
		}
		catch(NumberFormatException ex){
			dataInField.setText("Number Format Exception: "+ex.getMessage()+" Data not recorded");
			dataEntered=0.0;
			numIncObservations++;
			correct=false;
		}
		finally{
//			 Now update  the various fields on the screen.
			
			updateScreen();
				
			}
		}
		}// End of actionPerformed() method.

public void updateScreen(){
	
	maxField.setText(Double.toString(max));
	minField.setText(Double.toString(min));
	avgField.setText(Double.toString(avg));
	numObsField.setText(Integer.toString(numObservations));
	numIncObsField.setText(Integer.toString(numIncObservations));
	if(correct)dataInField.setText("Enter next value");
	
	

}

// Reset all widgets to their initial state, ready for data entry.
// Data entered is lost.


public void resetScreen(){
	
	
	// Reset display.
	
	dataInField.setText("Enter data");
	maxField.setText("No data");
	minField.setText("No Data");
	avgField.setText("No data");
	numObsField.setText("0");
	
	
	// Reset all values.
	
	sum=0.0;
	min=Double.MAX_VALUE;
	max=Double.MIN_VALUE;
	avg=0.0;
	numObservations=0;
	numIncObservations=0;
} // End of resetScreen.

}
