// SE 450 S99 programming assignment 1
// 100-point version of the Rational Number
// Calculator Applet
// does addition, subtraction, or multiplication

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class RNTestApplet2 extends Applet implements ActionListener {

  // 2 input TextFields, plus one to display the answer
  // the user will type a rational number (e.g., 1/2)
  // into each input TextField
  private TextField RNField1, RNField2, ansField;
  // This gives the user the choice of +, -, or * 
  private Choice c;

  // the init method adds the components to the Applet,
  // and adds an ActionListener to listen for the user
  // to type Enter into the second TextField
  public void init () {
    Panel p = new Panel();
    RNField1 = new TextField(10);
    p.add(RNField1);
    c = new Choice();
    c.add("+");
    c.add("-");
    c.add("*");
    p.add(c);
    RNField2 = new TextField(10);
    p.add(RNField2);
    RNField2.addActionListener(this);
    p.add(new Label("   ="));
    ansField = new TextField(10);
    p.add(ansField);
    add(p);
  }

  // you'll need to write this method -- it is called
  // when the user hits enter in the second TextField
  public void actionPerformed(ActionEvent e) {
	RN frame1 = new RN (RNField1.getText()),
	   frame2 = new RN (RNField2.getText()),
	   frame3;
	if (c.getSelectedItem() == "+")
		frame3 = frame1.add(frame2);
	else if (c.getSelectedItem() == "-")
		frame3 = frame1.subtract(frame2);
	else frame3 = frame1.multiply(frame2);

	ansField.setText(frame3.toAnswer());
  }
}
