//===========================================================
//  PROJECT:           A simple calculator
//  FILE:              Calculator2.java
//  PURPOSE:           A methodical design of a calculator            
//  VERSION:           1.1
//  TARGET:            java v1.1
//  PROGRAMMER:        Philip N. Geissler                    
//  UPDATE HISTORY:    11/10/00  First version.  No Events
//                     11/24/00  Second Version. With Events
//===========================================================

//-------------------------- NOTES --------------------------
/**
  This calculator design consists of 4 sections:
       1. NumDisp(TextField) - the display field
       2. KeysPan(Panel) - the number buttons
       3. FuncPan(Panel) - the math operation buttons
       4. DividerPan(Panel) - center graphic's panel
*/

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

//==================class Calculator2======================
/**
  a simple calculator that consists of a number pad a set 
  of function buttons and a display panel.  The number pad
  includes four separate panels.  The function buttons 
  incorporate four separate panels also.  
*/
public class Calculator2 extends Applet
                         implements ActionListener
{
   private TextField numDisp = new TextField(20);  
   private Panel keysPan = new Panel();  //number buttons
   private Panel funcPan = new Panel();  //function buttons
   private Panel dividerPan = new Panel(); //center panel
                  
   //for getting, calculating and displaying  number values
   private char op = 0;
   // numbers calculated and displayed in float mode  
   private float memValue = 0; 
   // for clearing display after calculating argument  
   private boolean clrNum = false; 
   
   //button declarations                  
   private Button  b7 = new Button("7"), //number pad     
                   b8 = new Button("8"), //    |
                   b9 = new Button("9"), //    V
                   b4 = new Button("4"),      
                   b5 = new Button("5"),      
                   b6 = new Button("6"),                   
                   b1 = new Button("1"),      
                   b2 = new Button("2"),       
                   b3 = new Button("3"),      
                   b0 = new Button("0"),   
                   bPoint = new Button("."),// function pad
                   bAllclear = new Button("AC"),//   |                        
                   bClear = new Button("C"),    //   V  
                   bPlus = new Button("+"),     
                   bMinus = new Button("-"),      
                   bMult = new Button("*"),    
                   bDiv = new Button("/"),
                   bEqual = new Button("="),      
                   bNeg = new Button("+/-"),
                   bSquare = new Button("x^2");
                   
   public void init()
   {  // class parameters
      Color myColor1 = new Color(0,128,192);
      this.setBackground(myColor1);
      this.setForeground(Color.black);
      this.setLayout(new BorderLayout());
      this.add(BorderLayout.NORTH, numDisp);
      this.add(BorderLayout.WEST, keysPan);
      this.add(BorderLayout.EAST, funcPan);
      this.add(BorderLayout.CENTER, dividerPan);
      
      //numDisp parameters
      numDisp.setFont(new Font("SanSerif",Font.BOLD,15)); 
      Color myColor2 = new Color(106,196,249);
      numDisp.setBackground(myColor2);
      numDisp.setEditable(true);
      numDisp.addActionListener(this);   
      
      //-----------------keysPan Panel---------------------
      keysPan.setLayout(new GridLayout(4, 1, 4, 4));
      //Top Row of keysPan
      Panel topRow = new Panel();
      topRow.setLayout(new GridLayout(1, 3, 3, 3));
      this.keysPan.add(topRow);
      //"7" button
      b7 = new Button("7");
      topRow.add(b7);
      b7.addActionListener(this);
      //"8" button
      b8 = new Button("8");
      topRow.add(b8);
      b8.addActionListener(this);
      //"9" button
      b9 = new Button("9");
      topRow.add(b9);      
      b9.addActionListener(this);      
      //Second row of KeysP includes 4, 5, and 6
      Panel secRow = new Panel();
      secRow.setLayout(new GridLayout(1, 3, 3, 3));
      this.keysPan.add(secRow);
      //"4" button
      b4 = new Button("4");
      secRow.add(b4);
      b4.addActionListener(this);
      //"5" button
      b5 = new Button("5");
      secRow.add(b5);
      b5.addActionListener(this);
      //"6" button
      b6 = new Button("6");
      secRow.add(b6);
      b6.addActionListener(this);      
      //Third row of keysPan includes 1, 2, and 3
      Panel thirdRow = new Panel();
      thirdRow.setLayout(new GridLayout(1, 3, 3, 3));
      this.keysPan.add(thirdRow);
      //"1" button
      b1 = new Button("1");
      thirdRow.add(b1);
      b1.addActionListener(this);
      //"2" button
      b2 = new Button("2");
      thirdRow.add(b2);
      b2.addActionListener(this);
      //"3" button
      b3 = new Button("3");
      thirdRow.add(b3);
      b3.addActionListener(this);      
      //Bottom row of keysPan includes ".", "0", and "+/-"
      Panel botRow = new Panel();
      botRow.setLayout(new GridLayout(1, 3, 3, 3));
      this.keysPan.add(botRow);
      //"." button
      bPoint = new Button(".");
      botRow.add(bPoint);
      bPoint.addActionListener(this);
      //"0" button
      b0 = new Button("0");
      botRow.add(b0);
      b0.addActionListener(this);
      //"+/-" button
      bNeg = new Button("+/-");
      botRow.add(bNeg); 
      bNeg.addActionListener(this);
            
      //------------------funcPan Panel--------------------
      funcPan.setLayout(new GridLayout(4, 2, 4, 4));
      funcPan.setForeground(Color.red);    
      //First row of funcPan includes "AC" and "C" 
      Panel row1 = new Panel();
      row1.setLayout(new GridLayout(1, 2, 3, 3));
      this.funcPan.add(row1);
      //"AC" button
      bAllclear = new Button("AC");
      row1.add(bAllclear);
      bAllclear.addActionListener(this);
      //"C" button
      bClear = new Button("C");
      row1.add(bClear);
      bClear.addActionListener(this);      
      //Second row of funcPan includes "+" and "-"
      Panel row2 = new Panel();
      row2.setLayout(new GridLayout(1, 2, 3, 3));
      this.funcPan.add(row2);
      //"+" button
      bPlus = new Button("+");
      row2.add(bPlus);
      bPlus.addActionListener(this);
      //"-" button
      bMinus = new Button("-");
      row2.add(bMinus);
      bMinus.addActionListener(this);      
      //Third row of funcPan includes "*" and "/"
      Panel row3 = new Panel();
      row3.setLayout(new GridLayout(1, 2, 3, 3));
      this.funcPan.add(row3);
      //"*" button
      bMult = new Button("*");
      row3.add(bMult);
      bMult.addActionListener(this);
      //"/" button
      bDiv = new Button("/");
      row3.add(bDiv);
      bDiv.addActionListener(this);      
      //Fourth row of funcPan includes "=" and "x^2"
      Panel row4 = new Panel();
      row4.setLayout(new GridLayout(1, 2, 3, 3));
      this.funcPan.add(row4);      
      //"=" button     
      bEqual = new Button("=");
      row4.add(bEqual);      
      bEqual.addActionListener(this);
      //"x^2" button
      bSquare = new Button("x^2");
      row4.add(bSquare);      
      bSquare.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e)
   {
      String bttnHit = e.getActionCommand();
      {  // deal with button hits
         if (bttnHit == "+/-")
         {  //gets negative value of entered number
            float neg = getValue(numDisp.getText());
            if (neg == 0) // can't get a "-0" so...
            {
               numDisp.setText("0.0");
               numDisp.setText("");
            }
            else  //put negative sign in front of number
            { 
               neg *= -1; //if button is hit twice value 
                          //becomes positive 
               numDisp.setText("");//clear number display
               addtoNum("" + neg);//adds value to number
            }   
         }
         // deal with "+", "-", "*", and "/" button hits
         else if (bttnHit == "+" | bttnHit == "-" |
                  bttnHit == "*" | bttnHit == "/")
         { 
            memValue = getValue(numDisp.getText());
            op = bttnHit.charAt(0);//reads first char(0) position
            numDisp.setText("");            
            addtoNum(bttnHit); // go to addtoNum method
            clrNum = true;  //clear display
         }
         //deal with the equal button hit
         else if (bttnHit == "=")
         {       
            doMath(); 
            memValue = getValue(numDisp.getText());
            op = 0;            
            clrNum = true;
         }
         //deal with square button hit
         else if (bttnHit == "x^2")
         {
            memValue = getValue(numDisp.getText());
            memValue = memValue * memValue;
            doMath();
            memValue = getValue(numDisp.getText());
            clrNum = true;           
         }
         //deal with clear button hit  
         else if (bttnHit == "C")
         {  //erases second number and displays first
            // number before "=" sign is hit
            numDisp.setText("");
            op = 0;
            doMath();
            memValue = getValue(numDisp.getText());
            clrNum = true;
         }
         //deal with AllClear button hit
         else if (bttnHit == "AC")
         {  // completely erases display field and sets
            // memValue back to "0" value 
            numDisp.setText("");
            op = 0;
            memValue = (float) 0;
            clrNum = true;
         }
         else 
         {     
            if (clrNum) //clear Display
            {
               numDisp.setText("");
               clrNum = false;
               addtoNum(bttnHit);
            }
            else  //add entry to Display
            {
               addtoNum(bttnHit);
            }
         }
      }
   }
   
   public void doMath() //claculate math argument
   {  
      float curValue = getValue(numDisp.getText());
      switch (op)
      {  //Addition function
         case '+':
             memValue += curValue;
             break;
             
         //Subtraction function
         case '-':
             memValue -= curValue;
             break;
             
         //Multiplication function    
         case '*':
             memValue *= curValue;
             break;
         
         //Division function    
         case '/':
             if (curValue != 0) //cannot divide by "0"
             {
                memValue /= curValue;
             }
             else
             {
                memValue = 0;  //result of dividing by "0"  
             }
             break;
      }  
      //get ready for next display entry    
      numDisp.setText("");
      addtoNum("" + memValue);
      clrNum = true;
   }
   
   public void addtoNum(String bttnHit)
   {  //gets, adds and displays number in display field
      String newNum = numDisp.getText();
      newNum += bttnHit;
      int numLength = newNum.length(); //length of number
      numDisp.setText(newNum);
   }   
   
   public float getValue(String val)
   {//converts string value to float and returns value
      if (val.equals("."))
      {
         val = "0";
      }
      Float f1 = Float.valueOf(val);
      return f1.floatValue();
   }
}
      

      

      
 


           

   
   
      
       