/*
 * Created on Oct 1, 2004
 * 
 * CS 178 Programming with Multimedia Objects
 *
 * Illustrate simple array declaration and operations in Java. 
 * For use during Lectures on Oct 4 and 6, 2004.
 * 
 * This program allows a user to (a) create an in-memory database of atomic weights, (b) search for
 * the atomic weight of a given element.
 * 
 * Sorting of array is not included in this applet. There is another applet which includes sorting also.
 * 
 *
 */

/**
 * @author Aditya P. Mathur
 *
 *
 */

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

public class arraySearch  extends Applet implements ActionListener, ItemListener{

	Button record=new Button("RECORD");
	boolean canAdd=true; // Indicates if there is space in the database to add more elements.
	
	// Element names  and their respective atomic weights 
	// are kept in arrays  and displayed  in a choice box.
	// The two arrays  elementName and elementWeight constitute our in-memory database.
	
	// Upon the start of the applet, the arrays contain no data and hence the Choice box is empty.
	
	final int maxElements=2;  // Max number of elements.
	String [] elementName=new String[maxElements];
	double [] elementWeight=new double[maxElements];
	
	Choice  nameDisp=new Choice();
	
	TextField nameTyped=new TextField ("Enter the element  name.");
	TextField weightTyped=new TextField("Enter the atomic weight.");
	
	
	int totalNames=0;  // Number of names  currently in the database.
	
	
	
	public void init (){
	

	
//		 Add actionListener to the button and ItemL<istener to the choice box.
		
		record.addActionListener(this);
		nameDisp.addItemListener(this);
		nameDisp.addItem("No elements added.");
	
		
	
//		 Add widgets to the screen.
		
		add(record);
		add(nameTyped);
		add (weightTyped);
		add(nameDisp);
		
		setBackground(Color.orange);
	} // End of init

	
	public void actionPerformed(ActionEvent e){
		
		int index;
		Object  buttonPressed=e.getSource();
		
		// Remove default message from Choice if this is the first item to be added.
		
		if(totalNames==0)nameDisp.remove(0); 
		
		// Get the  element name and its atomic weight typed by the user.

		if(!canAdd)
		{
			nameTyped.setText("Sorry, there is no more space in the database.");
		}
		else
		{
			totalNames++;  // Add to total number of element names added.
			String name=nameTyped.getText(); // get the name typed.
			double weight=Double.parseDouble(weightTyped.getText()); // Get its atomic weight.
		
			
			// Add name and weight to the database.
			
			elementName[totalNames-1]=name;
			elementWeight[totalNames-1]=weight;
		
			// Update choice box and weight.
			
			nameDisp.addItem(name); // Add to the choice box for display.
			nameTyped.setText(name+" Added to the database.");
			
			// Update displayed items so user sees the correct infrormation.
			nameDisp.select(totalNames-1);
			nameDisp.setBackground(Color.white);
			weightTyped.setText("Atomic weight of "+name+": "+weight);
		
			// Set canAdd to false if the database is full.
		
			if (totalNames==maxElements)
				{
				canAdd=false;
				nameTyped.setText("Cannot add more to the database.");
				}
		
		}
	}// End of actionPerformed
		
	
	// Process choice box selection.
	
	public void itemStateChanged(ItemEvent e)
	{
		
		// Get the element name selected.
		
		String name=nameDisp.getSelectedItem();
		
		// Search for this name in the elementName array.
		
		int index=find(name);
		
		
		if(index==-1)
		{
			weightTyped.setText("Sorry, this element does not exist in the database.");
		}
		else
		{
			weightTyped.setText("Atomic weight of "+name+": "+Double.toString(elementWeight[index]));
		
		}
	
	} // End of itemStateChanged.
	
	// Search for a name in the database and return its index.
	
	public int find(String name)
	{
		for (int i=0; i<totalNames;i++)
		{
			if(name==elementName[i])
			{
				return(i);
			}
		}
		return(-1);
		} // End of find.
	}// End of applet.
	
