Skip to content

Instantly share code, notes, and snippets.

@jbis9051
Created May 20, 2019 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbis9051/4cb61a642d08eb0403ffc17649c0514d to your computer and use it in GitHub Desktop.
Save jbis9051/4cb61a642d08eb0403ffc17649c0514d to your computer and use it in GitHub Desktop.
java.util.ArrayList;
import javafx.geometry.Pos;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.layout.Pane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.GridPane;
public class Tester extends Application
{
static int first=0;
static boolean firstDone=false;
static int second=0;
static int operation = 0;
static int answer = 0;
static boolean continuing=false;
public static void main(String[] args)
{
Application.launch(args);
}
public void start(Stage primaryStage)
{
GridPane pane = new GridPane();
Button one = new Button("1");
Button two = new Button("2");
Button three = new Button("3");
Button four = new Button("4");
Button five = new Button("5");
Button six = new Button("6");
Button seven = new Button("7");
Button eight = new Button("8");
Button nine = new Button("9");
Button zero = new Button("0");
Button add = new Button("+");
Button subtract = new Button("-");
Button multiply = new Button("x");
Button divide = new Button("/");
Button equals = new Button("=");
Button clear = new Button("C");
BtnHandler1 handler1 = new BtnHandler1 ();
BtnHandler2 handler2 = new BtnHandler2 ();
BtnHandler3 handler3 = new BtnHandler3 ();
BtnHandler4 handler4 = new BtnHandler4 ();
BtnHandler5 handler5 = new BtnHandler5 ();
BtnHandler6 handler6 = new BtnHandler6 ();
BtnHandler7 handler7 = new BtnHandler7 ();
BtnHandler8 handler8 = new BtnHandler8 ();
BtnHandler9 handler9 = new BtnHandler9 ();
BtnHandler0 handler0 = new BtnHandler0 ();
BtnHandlerPlus handlerPlus = new BtnHandlerPlus ();
BtnHandlerMinus handlerMinus = new BtnHandlerMinus ();
BtnHandlerTimes handlerTimes = new BtnHandlerTimes ();
BtnHandlerDivide handlerDivide = new BtnHandlerDivide ();
BtnHandlerEquals handlerEquals = new BtnHandlerEquals ();
BtnHandlerClear handlerClear = new BtnHandlerClear ();
one.setOnAction(handler1);
two.setOnAction(handler2);
three.setOnAction(handler3);
four.setOnAction(handler4);
five.setOnAction(handler5);
six.setOnAction(handler6);
seven.setOnAction(handler7);
eight.setOnAction(handler8);
nine.setOnAction(handler9);
zero.setOnAction(handler0);
add.setOnAction(handlerPlus);
subtract.setOnAction(handlerMinus);
multiply.setOnAction(handlerTimes);
divide.setOnAction(handlerDivide);
equals.setOnAction(handlerEquals);
clear.setOnAction(handlerClear);
pane.add(seven, 0, 0);
pane.add(eight, 1, 0);
pane.add(nine, 2, 0);
pane.add(four, 0, 1);
pane.add(five, 1, 1);
pane.add(six, 2, 1);
pane.add(one, 0, 2);
pane.add(two, 1, 2);
pane.add(three, 2, 2);
pane.add(divide, 3, 0);
pane.add(multiply, 3, 1);
pane.add(add, 3, 2);
pane.add(subtract, 3, 3);
pane.add(equals, 2, 3);
pane.add(clear, 0, 3);
pane.add(zero, 1, 3);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
}
} class BtnHandler1 implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e)
{
if (Tester.continuing)
{
Tester.first=0;
Tester.continuing=false;
Tester.firstDone=false;
}
if (!Tester.firstDone)
Tester.first=Tester.first*10+1;
else
Tester.second=Tester.second*10+1;
System.out.print("1");
}
}
class BtnHandler2 implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e)
{
if (Tester.continuing)
{
Tester.first=0;
Tester.continuing=false;
Tester.firstDone=false;
}
if (!Tester.firstDone)
Tester.first=Tester.first*10+2;
else
Tester.second=Tester.second*10+2;
System.out.print("2");
}
} class BtnHandler3 implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e)
{
if (Tester.continuing)
{
Tester.first=0;
Tester.continuing=false;
Tester.firstDone=false;
}
if (!Tester.firstDone)
Tester.first=Tester.first*10+3;
else
Tester.second=Tester.second*10+3;
System.out.print("3");
}
} class BtnHandler4 implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e)
{
if (Tester.continuing)
{
Tester.first=0;
Tester.continuing=false;
Tester.firstDone=false;
}
if (!Tester.firstDone)
Tester.first=Tester.first*10+4;
else
Tester.second=Tester.second*10+4;
System.out.print("4");
}
} class BtnHandler5 implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e)
{
if (Tester.continuing)
{
Tester.first=0;
Tester.continuing=false;
Tester.firstDone=false;
}
if (!Tester.firstDone)
Tester.first=Tester.first*10+5;
else
Tester.second=Tester.second*10+5;
System.out.print("5");
}
} class BtnHandler6 implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e)
{
if (Tester.continuing)
{
Tester.first=0;
Tester.continuing=false;
Tester.firstDone=false;
}
if (!Tester.firstDone)
Tester.first=Tester.first*10+6;
else
Tester.second=Tester.second*10+6;
System.out.print("6");
}
} class BtnHandler7 implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e)
{
if (Tester.continuing)
{
Tester.first=0;
Tester.continuing=false;
Tester.firstDone=false;
}
if (!Tester.firstDone)
Tester.first=Tester.first*10+7;
else
Tester.second=Tester.second*10+7;
System.out.print("7");
}
} class BtnHandler8 implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e)
{
if (Tester.continuing)
{
Tester.first=0;
Tester.continuing=false;
Tester.firstDone=false;
}
if (!Tester.firstDone)
Tester.first=Tester.first*10+8;
else
Tester.second=Tester.second*10+8;
System.out.print("8");
}
} class BtnHandler9 implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e)
{
if (Tester.continuing)
{
Tester.first=0;
Tester.continuing=false;
Tester.firstDone=false;
}
if (!Tester.firstDone)
Tester.first=Tester.first*10+9;
else
Tester.second=Tester.second*10+9;
System.out.print("9");
}
}
class BtnHandler0 implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e)
{
if (Tester.continuing)
{
Tester.first=0;
Tester.continuing=false;
Tester.firstDone=false;
}
if (!Tester.firstDone)
Tester.first=Tester.first*10;
else
Tester.second=Tester.second*10;
System.out.print("0");
}
} class BtnHandlerPlus implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e)
{
Tester.continuing=false;
Tester.firstDone=true;
Tester.operation = 1;
System.out.println();
System.out.println("+ ");
}
} class BtnHandlerMinus implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e)
{
Tester.continuing=false;
Tester.firstDone=true;
Tester.operation = 2;
System.out.println();
System.out.println("- ");
}
} class BtnHandlerTimes implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e)
{
Tester.continuing=false;
Tester.firstDone=true;
Tester.operation = 3;
System.out.println();
System.out.println("x ");
}
}
class BtnHandlerDivide implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e)
{
Tester.continuing=false;
Tester.firstDone=true;
Tester.operation = 4;
System.out.println();
System.out.println("/ ");
}
} class BtnHandlerEquals implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e)
{
System.out.println();
System.out.println("_____");
if (Tester.operation==1)
{
Tester.answer = Tester.first + Tester.second;
}
else if (Tester.operation==2)
{
Tester.answer = Tester.first - Tester.second;
}
else if (Tester.operation==3)
{
Tester.answer = Tester.first * Tester.second;
}
else if (Tester.operation==4)
{
Tester.answer = Tester.first / Tester.second;
}
System.out.println(Tester.answer);
System.out.println();
System.out.println();
Tester.first = Tester.answer;
Tester.second = 0;
Tester.continuing=true;
Tester.firstDone=true;
}
} class BtnHandlerClear implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e)
{
System.out.println();
System.out.println();
Tester.first=0;
Tester.second=0;
Tester.continuing=false;
Tester.firstDone=false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment