package programming101.calculator;

import java.awt.Frame;
import java.awt.Panel;


/**
	* Defines a simple graphic calculator consisting of a display region
	* for accepting input and displaying results, and a number of buttons
	* for various operations.  The buttons respond "directly" when they are clicked.
	* They receive a standard <CODE>ActionEvent</CODE> from the
	* underlying windowing system.  They also need information about the
	* state of the calculator - among other things, the
	* current cumulated value, the new value to be combined with the
	* cumulated value, the last button clicked, etc., and this must be
	* made accessible separately (in this case, through the constructors,
	* or an accessor method).  The only buttons supplied here are: 
	* <CODE>ClearButton</CODE>, <CODE>EqButton</CODE>, <CODE>PlusButton</CODE>.
	*
	* @author © 1998 Peter T. Sander
	* @version 1.3 (7/04/98)
	* @since 15/03/98
	*/
	
public class Calculator implements java.io.Serializable {
	// visible attributes
	private double cumulator;
	private DisplayField entryField;
	private CalculatorButton lastButton;
	// other variables
	protected Panel buttonPanel = new Panel();
	protected Frame frame = new Frame("Calculator");
	private PlusButton plusButton;
	private EqButton eqButton;
	private ClearButton ACButton;
	
	/**
		* No-args constructor.  Sets up the various calculator GUI components.
		*/
	public Calculator() {
		// instantiates GUI objects
		entryField = new DisplayField();
		// operation buttons
		ACButton = new ClearButton(this);
		plusButton = new PlusButton(this);
		eqButton = new EqButton(this);
		// adds display region and button panel to GUI frame
		frame.add("North", entryField);
		frame.add("Center", buttonPanel);
		// places the buttons into button panel
		buttonPanel.add(plusButton);
		buttonPanel.add(eqButton);
		buttonPanel.add(ACButton);
		// ensures GUI visibility
		frame.pack();
		frame.show();
	}
	
	/**
		* Getter method for cumulator attribute.
		*
		* @return current value of cumulator
		*/
	public final double getCumulator() {
		return cumulator;
	}
	
	/**
		* Setter method for cumulator attribute.
		*
		* @param cumulator new value for cumulator
		*/
	public final void setCumulator(double cumulator) {
		this.cumulator = cumulator;
	}
		
	/**
		* Getter method for entry field attribute.
		*
		* @return current value of entry field
		*/
	public final String getEntryField() {
		return entryField.getText();
	}
	
	/**
		* Setter method for entry field attribute.
		*
		* @param value new value for entry field
		*/
	public final void setEntryField(String value) {
		entryField.setText(value);
	}
	
	/**
		* Getter method for last button clicked attribute.
		*
		* @return last button clicked
		*/
	public final CalculatorButton getLastButton() {
		return lastButton;
	}
	
	/**
		* Setter method for last button clicked attribute.
		*
		* @param lastButton new value for last button clicked
		*/
	public final void setLastButton(CalculatorButton lastButton) {
		this.lastButton = lastButton;
	}
}
