import
java.awt.event.ActionListener;
import
java.awt.event.ActionEvent;
import
javax.swing.JOptionPane;
import
javax.swing.JButton;
public
class TestCalcEngine implements ActionListener {
Calculator parent; // a reference to the Calculator
// Constructor stores the reference to the Calculator
// window in the member variable parent...
TestCalcEngine(Calculator parent){
this.parent = parent;
}
publicvoid actionPerformed(ActionEvent e){
// Get the source of this action...
JButton clickedButton=(JButton) e.getSource();
// Get the buttons label...
String clickedButtonLabel=clickedButton.getText();
// Calculator code goes here. Do something
// based on the button pressed...
if (clickedButtonLabel=="0"){
// Get the existing text from the calculators display field...
String dispFieldText =
parent.getDisplayValue();
if (parent.operator ==""){//operator not set, deal with operand1
parent.operand1=dispFieldText + clickedButtonLabel;
}else{ //operator already set, deal with operand2
parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...
parent.setDisplayValue(dispFieldText + clickedButtonLabel);
}
elseif (clickedButtonLabel=="1"){
// Get the existing text from the calculators display field...
String dispFieldText =
parent.getDisplayValue();
if (parent.operator ==""){//operator not set, deal with operand1
parent.operand1=dispFieldText + clickedButtonLabel;
}else{ //operator already set, deal with operand2
parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...
parent.setDisplayValue(dispFieldText + clickedButtonLabel);
}
elseif (clickedButtonLabel=="2"){
// Get the existing text from the calculators display field...
String dispFieldText =
parent.getDisplayValue();
if (parent.operator ==""){//operator not set, deal with operand1
parent.operand1=dispFieldText + clickedButtonLabel;
}else{ //operator already set, deal with operand2
parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...
parent.setDisplayValue(dispFieldText + clickedButtonLabel);
}
elseif (clickedButtonLabel=="3"){
// Get the existing text from the calculators display field...
String dispFieldText =
parent.getDisplayValue();
if (parent.operator ==""){//operator not set, deal with operand1
parent.operand1=dispFieldText + clickedButtonLabel;
}else{ //operator already set, deal with operand2
parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...
parent.setDisplayValue(dispFieldText + clickedButtonLabel);
}
elseif (clickedButtonLabel=="4"){
// Get the existing text from the calculators display field...
String dispFieldText =
parent.getDisplayValue();
if (parent.operator ==""){//operator not set, deal with operand1
parent.operand1=dispFieldText + clickedButtonLabel;
}else{ //operator already set, deal with operand2
parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...
parent.setDisplayValue(dispFieldText + clickedButtonLabel);
}
elseif (clickedButtonLabel=="5"){
// Get the existing text from the calculators display field...
String dispFieldText =
parent.getDisplayValue();
if (parent.operator ==""){//operator not set, deal with operand1
parent.operand1=dispFieldText + clickedButtonLabel;
}else{ //operator already set, deal with operand2
parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...
parent.setDisplayValue(dispFieldText + clickedButtonLabel);
}
elseif (clickedButtonLabel=="6"){
// Get the existing text from the calculators display field...
String dispFieldText =
parent.getDisplayValue();
if (parent.operator ==""){//operator not set, deal with operand1
parent.operand1=dispFieldText + clickedButtonLabel;
}else{ //operator already set, deal with operand2
parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...
parent.setDisplayValue(dispFieldText + clickedButtonLabel);
}
elseif (clickedButtonLabel=="7"){
// Get the existing text from the calculators display field...
String dispFieldText =
parent.getDisplayValue();
if (parent.operator ==""){//operator not set, deal with operand1
parent.operand1=dispFieldText + clickedButtonLabel;
}else{ //operator already set, deal with operand2
parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...
parent.setDisplayValue(dispFieldText + clickedButtonLabel);
}
elseif (clickedButtonLabel=="8"){
// Get the existing text from the calculators display field...
String dispFieldText =
parent.getDisplayValue();
if (parent.operator ==""){//operator not set, deal with operand1
parent.operand1=dispFieldText + clickedButtonLabel;
}else{ //operator already set, deal with operand2
parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...
parent.setDisplayValue(dispFieldText + clickedButtonLabel);
}
elseif (clickedButtonLabel=="9"){
// Get the existing text from the calculators display field...
String dispFieldText =
parent.getDisplayValue();
if (parent.operator ==""){//operator not set, deal with operand1
parent.operand1=dispFieldText + clickedButtonLabel;
}else{ //operator already set, deal with operand2
parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...
parent.setDisplayValue(dispFieldText + clickedButtonLabel);
}
elseif (clickedButtonLabel=="."){
// Get the existing text from the calculators display field...
String dispFieldText =
parent.getDisplayValue();
if (parent.operator ==""){//operator not set, deal with operand1
parent.operand1=dispFieldText + clickedButtonLabel;
}else{ //operator already set, deal with operand2
parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...
parent.setDisplayValue(dispFieldText + clickedButtonLabel);
}
elseif (clickedButtonLabel=="/"){
// Clear Display...
parent.setDisplayValue("");
parent.operator = (clickedButtonLabel);
}
elseif (clickedButtonLabel=="+"){
// Clear Display...
parent.setDisplayValue("");
parent.operator = (clickedButtonLabel);
}
elseif (clickedButtonLabel=="-"){
// Clear Display...
parent.setDisplayValue("");
parent.operator = (clickedButtonLabel);
}
elseif (clickedButtonLabel=="x"){
// Clear Display...
parent.setDisplayValue("");
parent.operator = (clickedButtonLabel);
}
elseif (clickedButtonLabel=="="){
// do the arithmetic...
int resulto = 0;
if (parent.operator == "+"){
int operando1 = Integer.parseInt(parent.operand1);
int operando2 = Integer.parseInt(parent.operand2);
resulto = operando1 + operando2;
}
elseif (parent.operator == "-"){
// do the arithmetic...
}
elseif (parent.operator == "/"){
// do the arithmetic...
}
elseif (parent.operator == "x"){
// do the arithmetic...
}
// Update the display...
String resultoo = Integer.toString(resulto);
parent.setDisplayValue(resultoo);
// Clear the operands...
parent.operand1 = "";
parent.operand2 = "";
parent.operator = "";
}
}
}
</B>