import java.awt.*;
import java.applet.*;
import java.awt.event.*;


public class Calculator extends Applet implements ActionListener
{
	Panel ButtonPanel = new Panel();
	Panel DisplayPanel = new Panel();
	Button b[];
	TextField Display = new TextField(25);
	int NumericValue = 0;
	int OldNumericValue = 0;
	String ProcessType;
	boolean NumButton = false;
	String ConcatButton;
	String ShowDisplay = null;
	
	
	String ButtonNames[] = {"Off", "7", "8", "9", "*", "/", "Pct",
							"4", "5", "6", "-", "Mc", "Sqr", "1",
							"2", "3", "+", "M-", "On", "0", ".", "=",
							"Clr", "M+"};
	
	
	public void init()
	{
		//Display.addActionListener(this);
		b = new Button[ButtonNames.length];
		ButtonPanel.setLayout(new GridLayout(4,6,5,5));
		
		for (int i = 0; i < ButtonNames.length; i++)
		{
			b[i] = new Button(ButtonNames[i]);
			b[i].addActionListener(this);
			b[i].setActionCommand(ButtonNames[i]);
			b[i].addMouseListener(new MouseHandler());
			ButtonPanel.add(b[i]);
		}
		
		
		setLayout( new BorderLayout());
		DisplayPanel.add(Display);
		
		add(DisplayPanel, BorderLayout.NORTH);
		add(ButtonPanel, BorderLayout.CENTER);
	} // end init
	
	
	public void actionPerformed(ActionEvent e)
	{
		String ButtonPushed;
		Integer ButtonValue = null;
		Integer FinalValue = null;

		//Display.setText(e.toString());
		//Display.setText(e.paramString().toString());
		ButtonPushed = e.getActionCommand().toString().trim();
		char MyButton = ButtonPushed.charAt(0);
		
		switch (MyButton)
		{
			case '1':
				{
					if (NumButton == true)
					{
						ConcatButton = ConcatButton + "1";
						int TheValue = Integer.parseInt(ConcatButton);
						NumericValue = TheValue;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					else 
					{
						int TheValue = 1;
						NumericValue = TheValue;
						ConcatButton = "1";
						NumButton = true;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					
					ButtonValue.valueOf(ButtonPushed);
				//	OldNumericValue = NumericValue;
					break;
				}
			case '2':
				{
					if (NumButton == true)
					{
						ConcatButton = ConcatButton + "2";
						int TheValue = Integer.parseInt(ConcatButton);
						NumericValue = TheValue;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					else 
					{
						int TheValue = 2;
						NumericValue = TheValue;
						ConcatButton = "2";
						NumButton = true;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					
					ButtonValue.valueOf(ButtonPushed);
				//	OldNumericValue = NumericValue;
					break;
				}
			case '3':
				{
					if (NumButton == true)
					{
						ConcatButton = ConcatButton + "3";
						int TheValue = Integer.parseInt(ConcatButton);
						NumericValue = TheValue;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					else 
					{
						int TheValue = 3;
						NumericValue = TheValue;
						ConcatButton = "3";
						NumButton = true;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					
					ButtonValue.valueOf(ButtonPushed);
				//	OldNumericValue = NumericValue;
					break;
				}
			case '4':
				{
					if (NumButton == true)
					{
						ConcatButton = ConcatButton + "4";
						int TheValue = Integer.parseInt(ConcatButton);
						NumericValue = TheValue;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					else 
					{
						int TheValue = 4;
						NumericValue = TheValue;
						ConcatButton = "4";
						NumButton = true;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					
					ButtonValue.valueOf(ButtonPushed);
				//	OldNumericValue = NumericValue;
					break;
				}
			case '5':
				{
					if (NumButton == true)
					{
						ConcatButton = ConcatButton + "5";
						int TheValue = Integer.parseInt(ConcatButton);
						NumericValue = TheValue;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					else 
					{
						int TheValue = 5;
						NumericValue = TheValue;
						ConcatButton = "5";
						NumButton = true;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					
					ButtonValue.valueOf(ButtonPushed);
					//OldNumericValue = NumericValue;
					break;
				}
			case '6':
				{
					if (NumButton == true)
					{
						ConcatButton = ConcatButton + "6";
						int TheValue = Integer.parseInt(ConcatButton);
						NumericValue = TheValue;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					else 
					{
						int TheValue = 6;
						NumericValue = TheValue;
						ConcatButton = "6";
						NumButton = true;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					
					ButtonValue.valueOf(ButtonPushed);
					//OldNumericValue = NumericValue;
					break;
				}
			case '7':
				{
					if (NumButton == true)
					{
						ConcatButton = ConcatButton + "7";
						int TheValue = Integer.parseInt(ConcatButton);
						NumericValue = TheValue;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					else 
					{
						int TheValue = 7;
						NumericValue = TheValue;
						ConcatButton = "7";
						NumButton = true;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					
					ButtonValue.valueOf(ButtonPushed);
					//OldNumericValue = NumericValue;
					break;
				}
			case '8':
				{
					if (NumButton == true)
					{
						ConcatButton = ConcatButton + "8";
						int TheValue = Integer.parseInt(ConcatButton);
						NumericValue = TheValue;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					else 
					{
						int TheValue = 8;
						NumericValue = TheValue;
						ConcatButton = "8";
						NumButton = true;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					
					ButtonValue.valueOf(ButtonPushed);
					//OldNumericValue = NumericValue;
					break;
				}
			case '9':
				{
					if (NumButton == true)
					{
						ConcatButton = ConcatButton + "9";
						int TheValue = Integer.parseInt(ConcatButton);
						NumericValue = TheValue;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					else 
					{
						int TheValue = 9;
						NumericValue = TheValue;
						ConcatButton = "9";
						NumButton = true;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					
					ButtonValue.valueOf(ButtonPushed);
					//OldNumericValue = NumericValue;
					break;
				}
			case '0':
				{
					if (NumButton == true)
					{
						ConcatButton = ConcatButton + "0";
						int TheValue = Integer.parseInt(ConcatButton);
						NumericValue = TheValue;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					else 
					{
						int TheValue = 0;
						NumericValue = TheValue;
						ConcatButton = "0";
						NumButton = true;
						Display.setText(ShowDisplay.valueOf(TheValue));
						ShowDisplay = null;
					}
					
					ButtonValue.valueOf(ButtonPushed);
				//	OldNumericValue = NumericValue;
					break;
				}
			case '*':
				{
					ProcessType = "*";
					NumButton = false;
					ConcatButton = null;
					OldNumericValue = NumericValue;
					break;
				}
			case '-':
				{
					ProcessType = "-";
					NumButton = false;
					ConcatButton = null;
					OldNumericValue = NumericValue;
					break;
				}
			case '+':
				{
					ProcessType = "+";
					NumButton = false;
					ConcatButton = null;
					OldNumericValue = NumericValue;
					break;
				}
			case '/':
				{
					ProcessType = "/";
					NumButton = false;
					ConcatButton = null;
					OldNumericValue = NumericValue;
					break;
				}
			case '=':
				{
					NumButton = false;
					String TheFinalValue = " ";
					int FinalInt = 0;
					int Brad = 0;
					int Brian = 0;
					Brad = NumericValue;
					Brian = OldNumericValue;
					if (ProcessType == "*") FinalInt = Brian * Brad;
					if (ProcessType == "-") FinalInt = Brian - Brad;
					if (ProcessType == "+") FinalInt = Brian + Brad;
					if (ProcessType == "/") FinalInt = Brian / Brad;
					Display.setText("FinalValue = " + TheFinalValue.valueOf(FinalInt));
					Brad = 0; Brian = 0; NumericValue = 0; OldNumericValue = 0;
					ConcatButton = null;
					break;
				}
			default:
				{
					Display.setText("The Button Pushed: " + ButtonPushed);
					NumButton = false;
					break;
				}
				
		} // end switch
			
	
	} // end actionPerformed
	
	
	
	class MouseHandler extends MouseAdapter
	{
		Cursor handCursor = new Cursor(Cursor.HAND_CURSOR);
		Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
		
		public void mouseEntered(MouseEvent e)
		{
			e.getComponent().setCursor(handCursor);
		}
		
		public void mouseExited(MouseEvent e)
		{
			e.getComponent().setCursor(defaultCursor);
		}
										  
	} //end MouseHandler
	
	

} // end Calculator class