//v 1.3
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator implements ActionListener {
    JFrame calculatorFrame;
    JPanel calculatorPanel;
    JPanel funcPanel;
    JPanel erPanel;
    JTextField equation;
    JButton multi, divi, add, sub, enter, erase, erLine;
    
    JMenuBar menuBar;
    JMenu file;
    JMenuItem exit;
    
    double values[];
    double endVal;
    double value;
    
	int curPlace;
	
    // Constructor
    public Calculator() {
	// Create the frame and container.
	calculatorFrame = new JFrame("SuperCalculator");
	calculatorPanel = new JPanel();
	calculatorPanel.setLayout(new BorderLayout());
	funcPanel = new JPanel();
	funcPanel.setLayout(new GridLayout(0,1));
	erPanel = new JPanel();
	erPanel.setLayout(new GridLayout(1, 0));
	
	values = new double[100];
	curPlace = 0;
	
	// Add the widgets.
	addWidgets();

	// Add the panel to the frame.
	calculatorFrame.getContentPane().add(calculatorPanel, BorderLayout.NORTH);
	
	// Exit when the window is closed.
        calculatorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	// Show the calculator.
	calculatorFrame.pack();
	calculatorFrame.setVisible(true);
    }

    // Create and add the widgets for calculator.
    private void addWidgets() {
    
    ImageIcon icon = new ImageIcon("middle.gif", 
                               "Erase");
    	
	menuBar = new JMenuBar();
	calculatorFrame.setJMenuBar(menuBar);

	file = new JMenu("File");
	menuBar.add(file);
	
	exit = new JMenuItem("Exit", new ImageIcon("middle.gif"));
	exit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        leave();
    }
});
	file.add(exit);
	
	// Create widgets.
	equation = new JTextField("0", 10);
	
	erLine = new JButton("Erase Line");
	erase = new JButton(icon);
	enter = new JButton("Enter");
	multi = new JButton("X");
	divi = new JButton("/");
	add = new JButton("+");
	sub = new JButton("-");
		
	makeActions();
	
	// Add widgets to container.
		
	calculatorPanel.add(equation, BorderLayout.NORTH);
	calculatorPanel.add(enter, BorderLayout.CENTER);
	calculatorPanel.add(funcPanel, BorderLayout.EAST);
	calculatorPanel.add(erPanel, BorderLayout.SOUTH);
	funcPanel.add(multi);
	funcPanel.add(divi);
	funcPanel.add(add);
	funcPanel.add(sub);
	erPanel.add(erLine);
	erPanel.add(erase);
	
	
    }

    void makeActions(){
    	
	erase.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        equation.setText("0");
        curPlace = 0;
        value = 0;
       
       for(int i = 100; i == 0; i--){
       	values[i] = 0;
       }
               
       //debug code
       System.out.println("Current Place = "+curPlace);
       System.out.println("values["+curPlace+"] = "+values[curPlace]);
       System.out.println("value = "+value);
       //end debug code
   
    }
});

	//input the value into the variables
	enter.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      value = Double.valueOf(equation.getText()).doubleValue();
       values[curPlace] = value;
       
       //debug code
       System.out.println("\nCurrent Place = "+curPlace);
       System.out.println("values["+curPlace+"] = "+values[curPlace]);
       System.out.println("value = "+value);
       //end debug code
       
       equation.setText("0");
       curPlace++;
    }
});
	multi.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
       
    }
});
	divi.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
       
    }
});
	add.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
       if(curPlace != 0){
       	double workVal = values[curPlace - 1] + value;
       	equation.setText(Double.toString(workVal));
       	curPlace--;
       	values[curPlace] = workVal;
       	value = workVal;
       } else {
       	equation.setText("No value to add");
       	}
       	      
       //debug code
       System.out.println("\nCurrent Place = "+curPlace);
       System.out.println("values["+curPlace+"] = "+values[curPlace]);
       System.out.println("value = "+value);
       //end debug code
       
    }
});
	sub.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
       
    }
});
}
	
	public void actionPerformed(ActionEvent e) { 
	
} 

void leave(){
	System.exit(0);
}

	JFrame frame = new JFrame();
    // main method
    public static void main(String[] args) {
	// Set the look and feel.
	try {
	    UIManager.setLookAndFeel(
		UIManager.getCrossPlatformLookAndFeelClassName());
	} catch(Exception e) {}

	Calculator calculator = new Calculator();
	
	
    }
}



