//import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
* <p>Title: Calculator </p>
* <p>Description: The GUI and listener classes of the calculator </p>
* <p>Copyright: Copyright (c) 2014</p>
* <p>Date: 14-03-2014 </p>
* <p>@author John Hermes </p>
* <p>@version 1.0 </p>
*
**/
public class Calculator extends JPanel {
boolean numericValue,opperatorValue;
String buttonText;
String value1 = "";
String memory;
String valueAfterOp;
double result1;
String operator;
JPanel mainPanel = new JPanel();
BoxLayout boxLayout = new BoxLayout(mainPanel , 1);
JPanel displayPanel = new JPanel();
JTextField display = new JTextField();
JPanel topButtonRow = new JPanel();
JPanel calcPanel = new JPanel();
GridLayout topButtonRowGrid = new GridLayout();
GridLayout calcPanelGrid = new GridLayout();
JButton bMemoryClear = new JButton("MC");
JButton b0 = new JButton("0");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton bDivide = new JButton("/");
JButton bSqrt = new JButton("sqrt");
JButton bMemoryRecall = new JButton("MR");
JButton bMultiply = new JButton("*");
JButton bPercent = new JButton("%");
JButton bMemoryStore = new JButton("MS");
JButton bMinus = new JButton("-");
JButton bReciprocal = new JButton("1/x");
JButton bMemoryAdd = new JButton("M+");
JButton bSign = new JButton("+/-");
JButton bPoint = new JButton(".");
JButton bAddition = new JButton("+");
JButton bEquals = new JButton("=");
JButton bDisplayMem = new JButton("");
JButton bBackspace = new JButton("Backspace");
JButton bCE = new JButton("CE");
JButton bC = new JButton("C");
public Calculator(){
add( mainPanel);
mainPanel.setLayout(boxLayout);
mainPanel.add(displayPanel);
displayPanel.add(display);
display.setColumns(40);
display.setText("0");
display.setHorizontalAlignment(SwingConstants.RIGHT);
mainPanel.add(topButtonRow);
topButtonRow.setLayout(topButtonRowGrid);
topButtonRowGrid.setRows(1);
topButtonRowGrid.setColumns(4);
topButtonRow.add(bDisplayMem);
topButtonRow.add(bBackspace);
topButtonRow.add(bCE);
topButtonRow.add(bC);
mainPanel.add(calcPanel);
calcPanel.setName("JCalcPanel");
calcPanel.setLayout(calcPanelGrid);
calcPanelGrid.setRows(4);
calcPanelGrid.setColumns(6);
// Add buttons to panel
JButton buttons [] = {bMemoryClear,b7,b8,b9,bDivide,bSqrt,bMemoryRecall,b4,b5,b6,bMultiply,bPercent,bMemoryStore,b1,b2,b3,bMinus,bReciprocal,bMemoryAdd,b0,bSign,bPoint,bAddition,bEquals};
for(int i = 0; i < buttons.length; i++){
calcPanel.add(buttons[i]);
}
// Add listeners
bBackspace.addActionListener(new bBackspaceListener());
bC.addActionListener(new bCListener());
bCE.addActionListener(new bCeListener());
bMemoryClear.addActionListener(new bMemoryClearListener());
bDivide.addActionListener(new bDivideListener());
bSqrt.addActionListener(new bSqrtListener());
bMemoryRecall.addActionListener(new bMemoryRecallListener());
bMultiply.addActionListener(new bMultiplyListener());
bPercent.addActionListener(new bPercentListener());
bMemoryStore.addActionListener(new bMemoryStoreListener());
bAddition.addActionListener(new bAdditionListener());
bMinus.addActionListener(new bMinusListener());
bReciprocal.addActionListener(new bReciprocalListener());
bMemoryAdd.addActionListener(new bMemoryAddListener());
bSign.addActionListener(new bSignListener());
bPoint.addActionListener(new bPointListener());
bAddition.addActionListener(new bAdditionListener());
bEquals.addActionListener(new bEqualsListener());
JButton numButtons [] = {b0,b1,b2,b3,b4,b5,b6,b7,b8,b9};
for(int i = 0; i < numButtons.length; i++){
numButtons[i].addActionListener(new numberListener(i+""));
}
}
/////////////////////////////////////////////////////////////////
/**
* Description: In the case a numeric value is pressed the value should be congatenated with value1 and shown in the display.
*/
public void displayNumbers(String valueNum) {
value1 += valueNum;
display.setText(value1);
numericValue = true;
opperatorValue = false;
}
/**
* Description: In the case a numeric value is pressed the value should be congatenated with value1 and shown in the display.
* <p>
* Fill the variable 'operator' with the operator button that has been pressed.
* <p>
* @Param getOperator the operator that is pressed
**/
public void operator (String getOparator) {
numericValue = false;
opperatorValue = true;
valueAfterOp = display.getText();
value1 = "";
operator = getOparator;
}
/**
* Description: Listener inner class for the button bMemoryClear.
**/
class bMemoryClearListener implements ActionListener{
/**
* Description: represents the activation of the button bMemoryClear.
* <p>
* Clear the variable 'memory' and the display.
* <p>
* @Param event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
memory = "";
bDisplayMem.setText("");
}
}
/**
* Description: Listener inner class for the button bMemoryRecall.
**/
class bMemoryRecallListener implements ActionListener{
/**
* Description: represents the activation of the button bMemoryRecall.
* <p>
* Show the value of the variable 'memory' on the display.
* <p>
* @Param event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
display.setText(memory);
value1 = memory;
}
}
/**
* Description: Listener inner class for the button bMemoryStore.
**/
class bMemoryStoreListener implements ActionListener{
/**
* Description: represents the activation of the button bMemoryStore.
* <p>
* Save the value on the display to the variable 'memory' and set the text of the empty button to 'M'.
* <p>
* @Param event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
bDisplayMem.setText("M");
memory = display.getText();
}
}
/**
* Description: Listener inner class for the button bMemoryAdd.
**/
class bMemoryAddListener implements ActionListener{
/**
* Description: represents the activation of the button bMemoryAdd.
* <p>
* Add the value on the display to the value that is stored in the variable 'memory'.
* <p>
* @Param event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
double result;
result = Double.parseDouble(memory) + Double.parseDouble(display.getText());
memory = result + "";
}
}
/**
* Description: Listener inner class for the button bReciprocal.
**/
class bReciprocalListener implements ActionListener{
/**
* Description: represents the activation of the button bReciprocal.
* <p>
* Create a reference variable to Calculations.
* Call the function divideByX and pass the value in the display.
* Then show the result of the function in the display
* <p>
* @Param event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
Calculations calc = new Calculations();
value1 = calc.divideByX(display);
display.setText(value1);
}
}
/**
* Description: Listener inner class for the button bSqrt.
**/
class bSqrtListener implements ActionListener{
/**
* Description: represents the activation of the button bSqrt.
* <p>
* Create a reference variable to Calculations.
* Call the function sqrt and pass the value in the display.
* Then show the result of the function in the display
* <p>
* @Param event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
Calculations calc = new Calculations();
value1 = calc.sqrt(display);
display.setText(value1);
}
}
/**
* Description: Listener inner class for the button bPercent.
**/
class bPercentListener implements ActionListener{
/**
* Description: represents the activation of the button bPercent.
* <p>
* Create a reference variable to Calculations.
* Call the function percent and pass the value in the display.
* Then show the result of the function in the display
* <p>
* @Param event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
Calculations calc = new Calculations();
value1 = calc.percent(display, valueAfterOp);
display.setText(value1);
}
}
/**
* Description: Listener inner class for the button bC.
**/
class bCListener implements ActionListener{
/**
* Description: represents the activation of the button bC.
* <p>
* Clear variables value1,valueAfterOp,operator and the display when the C button is pressed
* <p>
* @Param event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
value1 = "";
display.setText("0");
valueAfterOp = "";
operator = "";
}
}
// Listerner classes
/**
* Description: Listener inner class for the button bCe.
**/
class bCeListener implements ActionListener{
/**
* Description: represents the activation of the button bCe.
* <p>
* Clear the last numeric entrance
* <p>
* @Param event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
value1 = "";
display.setText("0");
}
}
/**
* Description: Listener inner class for the button bPoint.
**/
class bPointListener implements ActionListener{
/**
* Description: represents the activation of the button bPoint.
* <p>
* Call the method 'displayNumbers' and add a decimal to the number on the display.
* <p>
* @Param event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
displayNumbers(bPoint.getText());
}
}
/**
* Description: Listener inner class for the numeric button.
**/
class numberListener implements ActionListener{
private String number;
/**
* Description: Constructor of the class numberListener.
* <p>
* @Param number contstructs an ActionEvent
**/
public numberListener(String number) {
this.number = number;
}
/**
* Description: represents the activation of the numeric buttons.
* <p>
* Call the method 'displayNumbers' and add the number on the button to the display.
* <p>
* @event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
displayNumbers(number);
}
}
/**
* Description: Listener inner class for the button bAddition.
**/
class bAdditionListener implements ActionListener{
/**
* Description: represents the activation of the button bAddition.
* <p>
* Call the method 'operator' and show the value of the operator in the display.
* <p>
* @Param event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
operator(bAddition.getText());
}
}
/**
* Description: Listener inner class for the button bSign.
**/
class bSignListener implements ActionListener{
/**
* Description: represents the activation of the button bSign.
* <p>
* Create a reference variable to Calculations.
* Call the function signChange and pass the value in the display.
* <p>
* @Param event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
Calculations calc = new Calculations();
value1 = calc.signChange(display);
}
}
/**
* Description: Listener inner class for the button bMinus.
**/
class bMinusListener implements ActionListener{
/**
* Description: represents the activation of the button bMinus.
* <p>
* Call the method 'operator' and show the value of the operator in the display.
* <p>
* @Param event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
operator(bMinus.getText());
}
}
/**
* Description: Listener inner class for the button bMultiply.
**/
class bMultiplyListener implements ActionListener{
/**
* Description: represents the activation of the button bMultiply.
* <p>
* Call the method 'operator' and show the value of the operator in the display.
* <p>
* @Param event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
operator(bMultiply.getText());
}
}
/**
* Description: Listener inner class for the button bDivide.
**/
class bDivideListener implements ActionListener{
/**
* Description: represents the activation of the button bDivide.
* <p>
* Call the method 'operator' and show the value of the operator in the display.
* <p>
* @Param event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
operator(bDivide.getText());
}
}
/**
* Description: Listener inner class for the button bBackspace.
**/
class bBackspaceListener implements ActionListener{
/**
* Description: represents the activation of the button bBackspace.
* <p>
* Create a reference variable to Calculations.
* Call the function backspace and pass the value in the display.
* <p>
* @Param event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
//backspace();
Calculations calc = new Calculations();
calc.backspace(display, numericValue);
}
}
/**
* Description: Listener inner class for the button bEquals.
**/
class bEqualsListener implements ActionListener{
/**
* Description: represents the activation of the button bEquals.
* <p>
* Create a reference variable to Calculations when there is not a division by zero.
* Call the function calc and pass the values 'valueAfterOp' and 'value1'.
* <p>
* @Param event contstructs an ActionEvent
**/
public void actionPerformed(ActionEvent event){
numericValue = false;
opperatorValue = true;
//Check if there is a division by zero.
if (value1.equals("0") && operator.equals("/")) {
display.setText("Cannot divide by zero.");
} else {
Calculations calc = new Calculations();
display.setText(calc.resultCalc(Double.parseDouble(valueAfterOp), Double.parseDouble(value1),operator) + "");
}
}
}
// ***** End listener classes *****
}