/**
 * Calculate.java calculates the addition and
 * subtraction of two numbers.
 * @author
 * @version
 */

import java.lang.String.*;
import java.lang.Exception.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calculate extends JFrame implements ActionListener{
    // instance variables
    private static final int FRAME_WIDTH = 300;
    private static final int FRAME_HEIGHT = 300;

    private static final int FRAME_X_ORIGIN = 150;
    private static final int FRAME_Y_ORIGIN = 250;

    private static final int BUTTON_WIDTH = 80;
    private static final int BUTTON_HEIGHT = 30;

    private JButton cancelButton;
    private JButton okButton;

    private JTextField inputLine1;
	private JTextField inputLine2;
	private JTextField outputLine;

	private JLabel label1;
	private JLabel label2;
	private JLabel label3;

    public static void main(String[] args){
        Calculate frame = new Calculate();
        frame.setVisible(true);
    }

    /**
     * Constructor for objects of class Calculate
     */
    public Calculate(){
        Container contentPane;

        //Set the frame properties
        setSize         (FRAME_WIDTH, FRAME_HEIGHT);
        setResizable    (false);
        setTitle        ("Calculator");
        setLocation     (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);

        contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.setBackground( Color.white );

        //Create and Place the Buttons on the frame
        okButton = new JButton("ADD");
        okButton.setBounds(70, 175, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(okButton);

        cancelButton = new JButton("SUB");
        cancelButton.setBounds(160, 175, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(cancelButton);

        //Register this frame as an Action listener of the buttons
        okButton.addActionListener(this);
        cancelButton.addActionListener(this);

		  //Create and Place the TextFields on the frame
        inputLine1 = new JTextField();
        inputLine1.setBounds(80, 40, 130, 25);
        contentPane.add(inputLine1);

		inputLine2 = new JTextField();
        inputLine2.setBounds(80, 70, 130, 25);
        contentPane.add(inputLine2);

		outputLine = new JTextField();
        outputLine.setBounds(80, 100, 200, 25);
		outputLine.setEditable( false );
        contentPane.add(outputLine);

		//Action listener of the TextFields
		inputLine1.addActionListener(this);
		inputLine2.addActionListener(this);

		//Create and Place Labels on the frame
		JLabel label1 = new JLabel("Integer 1: ");
		label1.setBounds(10, 0, 100, 100);
		contentPane.add(label1);

		JLabel label2 = new JLabel("Integer 2: ");
		label2.setBounds(10, 30, 100, 100);
		contentPane.add(label2);

		JLabel label3 = new JLabel("Result : ");
		label3.setBounds(10, 60, 100, 100);
		contentPane.add(label3);

        //Exit program when the viewer is closed
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

	 //Event handler
    public void actionPerformed(ActionEvent event){
        if(event.getSource() instanceof JButton){
            JButton clickedButton = (JButton) event.getSource();

            String buttonText = clickedButton.getText();
			  try{

				//Get the integer values of the strings
				int number1 = Integer.valueOf(inputLine1.getText()).intValue();
				int number2 = Integer.valueOf(inputLine2.getText()).intValue();
				int add,sub;

          	    if(buttonText.equals("ADD")){    //Add the two numbers
					add = number1 + number2;

					String addString = Integer.toString(add);
					outputLine.setText(addString);

				}
				else{    //Subtract the two numbers
					sub = number1 - number2;
					String addString = Integer.toString(sub);
					outputLine.setText(addString);
				}
			  }catch(NumberFormatException e){
					outputLine.setText("Enter a valid 10 Digit Integer");
			  }

        }
        else {
		  	setTitle(inputLine1.getText());
    		outputLine.setText("Press the + or - button ");
        }
    }
}
