/* Program:  DonnaApplet
   Date:     Nov. 4, 2009
   Programmer:Ms. Bell
   */

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

   public class DonnaApplet extends Applet implements ActionListener
   {

   // declare variables

   String  firstName, lastName;
   int     size, content, delivery, organization, rank;
   int     i = 1;

   float totalContent = 0;
   float totalDelivery = 0;
   float totalOrganization = 0;
   float totalRank = 0;

   Person[] debateGroup = new Person[50];

   // creating components for applet

   Label headerLabel = new Label("Please enter forensic score data");

   Label firstNameLabel = new Label("First Name:");
     TextField firstNameField = new TextField(15);

   Label lastNameLabel = new Label("Last Name:");
     TextField lastNameField = new TextField(15);

   Label contentLabel = new Label("Content Score:");
     Choice contentChoice = new Choice();

   Label deliveryLabel = new Label("Delivery Score:");
     Choice deliveryChoice = new Choice();

   Label organizationLabel = new Label("Organization Score:");
     Choice organizationChoice = new Choice();

   Label rankLabel = new Label("Rank:");
     TextField rankField = new TextField(4);

   Button submitButton = new Button("Submit Scores");
   Button displayButton = new Button("Display Averages");

   Label avgContentLabel = new Label("Average Content");
     Label displayAvgContentLabel = new Label("");

   Label avgDeliveryLabel = new Label("Average Delivery");
     Label displayAvgDeliveryLabel = new Label("");

   Label avgOrganizationLabel = new Label("Average Organization");
     Label displayAvgOrganizationLabel = new Label("");

   Label avgRankLabel = new Label("Average Rank");
     Label displayAvgRankLabel = new Label("");


   public void init()
   {
	 // Add components to window and set colors

	 setBackground(Color.green);
	 add(headerLabel);

	 add(firstNameLabel);
	 add(firstNameField);
	     firstNameField.requestFocus();

	 add(lastNameLabel);
	 add(lastNameField);

	 add(contentLabel);
	 add(contentChoice);
	     contentChoice.addItem("1");
	     contentChoice.addItem("2");
	     contentChoice.addItem("3");
	     contentChoice.addItem("4");
         contentChoice.addItem("5");

     add(deliveryLabel);
	 add(deliveryChoice);
	 	     deliveryChoice.addItem("1");
	 	     deliveryChoice.addItem("2");
	 	     deliveryChoice.addItem("3");
	 	     deliveryChoice.addItem("4");
             deliveryChoice.addItem("5");

     add(organizationLabel);
	 add(organizationChoice);
	 	     organizationChoice.addItem("1");
	 	     organizationChoice.addItem("2");
	 	     organizationChoice.addItem("3");
	 	     organizationChoice.addItem("4");
             organizationChoice.addItem("5");

     add(rankLabel);
     add(rankField);

     add(submitButton);
         submitButton.addActionListener(this);

     add(displayButton);
         displayButton.addActionListener(this);

     add(avgContentLabel);
     add(displayAvgContentLabel);

     add(displayAvgContentLabel);
     add(displayAvgDeliveryLabel);

     add(avgOrganizationLabel);
     add(displayAvgOrganizationLabel);

     add(avgRankLabel);
     add(displayAvgRankLabel);
 }

public void actionPerformed(ActionEvent e)
{
	String arg = e.getActionCommand();

	if ( arg == "Submit Scores")
	{
		// assigning data

		firstName = firstNameField.getText();
		lastName = lastNameField.getText();
		content = Integer.parseInt(contentChoice.getSelectedItem());
		delivery = Integer.parseInt(deliveryChoice.getSelectedItem());
		organization = Integer.parseInt(organizationChoice.getSelectedItem());
		rank = Integer.parseInt(rankField.getText());

		size = i++;

		debateGroup[i] = new Person(firstName, lastName, content, delivery, organization, rank);

        // accumulate totals for each category

        totalContent += debateGroup[i].getContent();
        totalDelivery += debateGroup[i].getDelivery();
        totalOrganization += debateGroup[i].getOrganization();
        totalRank += debateGroup[i].getRank();

        firstNameField.setText("");
        lastNameField.setText("");
        contentChoice.select(0);
        deliveryChoice.select(0);
        organizationChoice.select(0);
        rankField.setText("");
        firstNameField.requestFocus();
	}

	if ( arg == "Display Averages")
	{
		displayAvgContentLabel.setText(String.valueOf(totalContent/size));
		displayAvgDeliveryLabel.setText(String.valueOf(totalDelivery / size));
		displayAvgOrganizationLabel.setText(String.valueOf(totalOrganization / size));
		displayAvgRankLabel.setText(String.valueOf(totalRank / size));
		repaint();
	}
  }
}
