diff --git a/Calculator.java b/Calculator.java new file mode 100644 index 0000000..79615c5 --- /dev/null +++ b/Calculator.java @@ -0,0 +1,330 @@ +/******************************************************************** +// Calculator.java Author: Snubiss +// +// Date: March 17, 2019 +// Modified: March 19, 2019 +// +// Defines the instance data, constructors and associated +// methods of a calculator class object. Creates a calculator +// upon being invoked. This uses extensive amounts of JavaFX to +// produce the GUI. +// +//********************************************************************/ + +package basiccalculator; + +import javafx.beans.value.ObservableValue; +import javafx.geometry.Insets; +import javafx.geometry.Pos; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.TextField; +import javafx.scene.input.KeyCode; +import javafx.scene.layout.GridPane; +import javafx.scene.layout.HBox; +import javafx.scene.layout.Pane; +import javafx.scene.layout.VBox; +import javafx.scene.text.Font; + + +public class Calculator { + + TextField bigText = new TextField(); + TextField A = new TextField(); + TextField action = new TextField();; + TextField B = new TextField();; + Label equals = new Label(); + TextField answer = new TextField();; + VBox calcWindow; + Button[][] button = new Button[4][4]; + GridPane buttonGrid = new GridPane(); + String[] buttonText = {"7","4","1","CLR","8","5","2","0","9","6","3","=","+","-","x","/"}; + + Calculator(){ + + bigText.setText("0"); + initiateListeners(); + this.calcWindow = (VBox)getCalcWindow(); + } + + public VBox getCalculator(){ + return calcWindow; + } + + // CREATE THE CALCULATOR BOX FRAME. + private Pane getCalcWindow(){ + calcWindow = new VBox(); + calcWindow.setMaxWidth(401); + calcWindow.setMinWidth(401); + calcWindow.setMaxHeight(594); + calcWindow.setMinHeight(594); + calcWindow.setStyle("-fx-background-color: #3f3f3f;" + + "-fx-border-color: grey;" + + "-fx-border-width: 1;" + + "-fx-border-radius: 5 5 5 5;" + + "-fx-background-radius: 5 5 5 5;" + + "-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.8), 10, 0, 0, 0);"); + calcWindow.getChildren().addAll(textFieldLabels(), textFieldBoxes(), bigTextField(), buttonLayout()); + //calcWindow.setAlignment(Pos.CENTER); + return calcWindow; + } + + // CREATE THE LABELS ABOVE THE TEXTFIELDS. + private Pane textFieldLabels(){ + + Label firstNumber = new Label(); + firstNumber.setText("A"); + firstNumber.setFont(new Font(15)); + firstNumber.setMaxSize(66, 20); + firstNumber.setMinSize(66, 20); + firstNumber.setStyle("-fx-text-fill: orange;"); + firstNumber.setAlignment(Pos.CENTER); + + Label actionLabel = new Label(); + actionLabel.setText("?"); + actionLabel.setFont(new Font(15)); + actionLabel.setMaxSize(70, 20); + actionLabel.setMinSize(70, 20); + actionLabel.setStyle("-fx-text-fill: orange;"); + actionLabel.setAlignment(Pos.CENTER); + + Label secondNumber = new Label(); + secondNumber.setText("B"); + secondNumber.setFont(new Font(15)); + secondNumber.setMaxSize(70, 20); + secondNumber.setMinSize(70, 20); + secondNumber.setStyle("-fx-text-fill: orange;"); + secondNumber.setAlignment(Pos.CENTER); + + Label answerLabel = new Label(); + answerLabel.setText("Answer"); + answerLabel.setFont(new Font(15)); + answerLabel.setMaxSize(120, 20); + answerLabel.setMinSize(120, 20); + answerLabel.setStyle("-fx-text-fill: orange;"); + answerLabel.setAlignment(Pos.CENTER_RIGHT); + + + HBox textFieldLabels = new HBox(); + textFieldLabels.setPadding(new Insets(20,0,0,20)); + textFieldLabels.setSpacing(10); + textFieldLabels.getChildren().addAll(firstNumber, actionLabel, secondNumber, answerLabel); + return textFieldLabels; + } + + // CREATE THE TEXTFIELD BOXES. + private Pane textFieldBoxes(){ + + A.setMaxSize(80, 40); + A.setMinSize(80, 40); + A.setAlignment(Pos.CENTER); + + action.setMaxSize(40, 40); + action.setMinSize(40, 40); + action.setAlignment(Pos.CENTER); + + B.setMaxSize(80, 40); + B.setMinSize(80, 40); + B.setAlignment(Pos.CENTER); + + equals.setText("="); + equals.setFont(new Font(27)); + equals.setStyle("-fx-text-fill: orange;"); + + answer.setMaxSize(80, 40); + answer.setMinSize(80, 40); + answer.setAlignment(Pos.CENTER); + + + HBox textBox = new HBox(); + textBox.getChildren().addAll(A, action, B, equals, answer); + textBox.setPadding(new Insets(10, 0, 0, 2)); + textBox.setSpacing(20); + textBox.setAlignment(Pos.CENTER); + return textBox; + } + + // CREATE THE BIGTEXTFIELD BOX. + private Pane bigTextField(){ + + bigText.setAlignment(Pos.CENTER_RIGHT); + bigText.setFont(new Font(70)); + bigText.setPadding(new Insets(0,10,0,0)); + bigText.setMaxSize(399, 100); + bigText.setMinSize(399, 100); + + + HBox bigTextBox = new HBox(); + bigTextBox.setMaxSize(399, 100); + bigTextBox.setMinSize(399, 100); + bigTextBox.setPadding(new Insets(25, 0, 0, 0)); + bigTextBox.getChildren().add(bigText); + bigTextBox.setAlignment(Pos.CENTER); + return bigTextBox; + } + + // CREATE THE BUTTON GRID AND BUTTONS FOR THE CALCULATOR. + private Pane buttonLayout(){ + + int textCounter = 0; + + for (int i = 0; i < 4; i++){ + for (int j = 0; j < 4; j++){ + + button[i][j] = new Button(); + button[i][j].setText(buttonText[textCounter]); + button[i][j].setFont(new Font(20)); + button[i][j].setMinSize(95+5, 95); + button[i][j].setMaxSize(95+5, 95); + buttonGrid.add(button[i][j], i, j); + textCounter++; + } + } + buttonGrid.setAlignment(Pos.BOTTOM_CENTER); + buttonGrid.setPadding(new Insets(21, 0, 0, 0)); + return buttonGrid; + } + + // Add listeners to disallow the user to extend numbers out of bounds. + private void initiateListeners(){ + + bigText.lengthProperty().addListener((ObservableValue observable, Number oldValue, Number newValue) -> { + if (newValue.intValue() > oldValue.intValue()) { + // Check if the new character is greater than LIMIT + if (bigText.getText().length() >= 10) { + bigText.setText(bigText.getText().substring(0, 10)); + } + } + }); + + A.lengthProperty().addListener((ObservableValue observable, Number oldValue, Number newValue) -> { + if (newValue.intValue() > oldValue.intValue()) { + // Check if the new character is greater than LIMIT + if (A.getText().length() >= 10) { + A.setText(A.getText().substring(0, 10)); + } + } + }); + + B.lengthProperty().addListener((ObservableValue observable, Number oldValue, Number newValue) -> { + if (newValue.intValue() > oldValue.intValue()) { + // Check if the new character is greater than LIMIT + if (B.getText().length() >= 10) { + B.setText(B.getText().substring(0, 10)); + } + } + }); + } + + // APPLY THE MOUSE AND KEYBOARD ACTIONS TO OUR CALCULATOR. + public void addKeyBindings(Scene scene){ + + ///////////////////////////////////////// + // // + // CLICK CONTROLS // + // // + ///////////////////////////////////////// + + LogicEvents logicanator = new LogicEvents(); + + button[0][2].setOnAction(logicanator.event1(this)); + button[1][2].setOnAction(logicanator.event2(this)); + button[2][2].setOnAction(logicanator.event3(this)); + button[3][2].setOnAction(logicanator.eventMultiply(this)); + button[0][1].setOnAction(logicanator.event4(this)); + button[1][1].setOnAction(logicanator.event5(this)); + button[2][1].setOnAction(logicanator.event6(this)); + button[3][1].setOnAction(logicanator.eventMinus(this)); + button[0][0].setOnAction(logicanator.event7(this)); + button[1][0].setOnAction(logicanator.event8(this)); + button[2][0].setOnAction(logicanator.event9(this)); + button[3][0].setOnAction(logicanator.eventPlus(this)); + button[0][3].setOnAction(logicanator.eventClear(this)); + button[1][3].setOnAction(logicanator.event0(this)); + button[2][3].setOnAction(logicanator.eventEquals(this)); + button[3][3].setOnAction(logicanator.eventDivide(this)); + + ///////////////////////////////////////// + // // + // KEYBOARD CONTROLS // + // // + ///////////////////////////////////////// + + scene.setOnKeyPressed(e -> { + if (e.getCode() == KeyCode.DIGIT1 || e.getCode() == KeyCode.NUMPAD1){ + button[0][2].fire(); + } + if (e.getCode() == KeyCode.DIGIT2 || e.getCode() == KeyCode.NUMPAD2){ + button[1][2].fire(); + } + if (e.getCode() == KeyCode.DIGIT3 || e.getCode() == KeyCode.NUMPAD3){ + button[2][2].fire(); + } + if (e.getCode() == KeyCode.DIGIT4 || e.getCode() == KeyCode.NUMPAD4){ + button[0][1].fire(); + } + if (e.getCode() == KeyCode.DIGIT5 || e.getCode() == KeyCode.NUMPAD5){ + button[1][1].fire(); + } + if (e.getCode() == KeyCode.DIGIT6 || e.getCode() == KeyCode.NUMPAD6){ + button[2][1].fire(); + } + if (e.getCode() == KeyCode.DIGIT7 || e.getCode() == KeyCode.NUMPAD7){ + button[0][0].fire(); + } + if (e.getCode() == KeyCode.DIGIT8 || e.getCode() == KeyCode.NUMPAD8){ + button[1][0].fire(); + } + if (e.getCode() == KeyCode.DIGIT9 || e.getCode() == KeyCode.NUMPAD9){ + button[2][0].fire(); + } + if (e.getCode() == KeyCode.DIGIT0 || e.getCode() == KeyCode.NUMPAD0){ + button[1][3].fire(); + } + if (e.getCode() == KeyCode.ADD || e.getCode() == KeyCode.PLUS){ + button[3][0].fire(); + } + if (e.getCode() == KeyCode.SUBTRACT) { + button[3][1].fire(); + } + if (e.getCode() == KeyCode.DIVIDE || e.getCode() == KeyCode.SLASH) { + button[3][3].fire(); + } + if (e.getCode() == KeyCode.MULTIPLY || e.getCode() == KeyCode.X) { + button[3][2].fire(); + } + if (e.getCode() == KeyCode.ESCAPE || e.getCode() == KeyCode.SPACE) { + button[0][3].fire(); + } + if (e.getCode() == KeyCode.ENTER) { + button[2][3].fire(); + } + if (e.getCode() == KeyCode.BACK_SPACE) { + if (logicanator.cursor == 0 && bigText.getText() != null){ + try{ + int length = bigText.getLength(); + bigText.setText(bigText.getText().substring(0,length-1)); + A.setText(bigText.getText()); + } + catch(StringIndexOutOfBoundsException ex){ + + } + } + if (logicanator.cursor == 1 && bigText.getText() != null){ + try{ + int length = bigText.getLength(); + bigText.setText(bigText.getText().substring(0,length-1)); + B.setText(bigText.getText()); + } + catch(StringIndexOutOfBoundsException ex){ + + } + } + } + }); + + // Remove the default prompt from all textareas + equals.requestFocus(); + } +} diff --git a/LogicEvents.java b/LogicEvents.java new file mode 100644 index 0000000..ffd26bb --- /dev/null +++ b/LogicEvents.java @@ -0,0 +1,682 @@ +/******************************************************************** +// LogicEvents.java Author: Snubiss +// +// Date: March 17, 2019 +// Modified: March 19, 2019 +// +// Defines the instance data, constructors and associated +// methods of a LogicEvents class object. This class contains +// all of the logical events and mathematical calculations +// relating to the Calculator class. This class is designed to +// be present when creating a Calculator class. +// +//********************************************************************/ + +package basiccalculator; + + +import javafx.event.ActionEvent; +import javafx.event.EventHandler; + +public class LogicEvents { + + // Create a couple flags to watch for events. + int cursor; + boolean equaled; + + LogicEvents(){ + cursor = 0; + equaled = false; + } + + + /************************************ + * * + * BUTTON EVENTS * + * * + *************************************/ + public EventHandler event0(Calculator calc){ + + EventHandler test = new EventHandler(){ + @Override + public void handle(ActionEvent event) { + if(equaled){ + calc.bigText.setText("0"); + calc.A.setText(null); + calc.B.setText(null); + calc.action.setText(null); + calc.answer.setText(null); + equaled = false; + } + + if (cursor == 0){ + if(calc.bigText.getText().equals("0") || calc.bigText.getText().isEmpty()){ + calc.bigText.setText("0"); + calc.A.setText("0"); + } + else{ + calc.bigText.setText(calc.bigText.getText() + "0"); + calc.A.setText(calc.A.getText() + "0"); + } + } + if (cursor == 1){ + if(calc.bigText.getText().equals("0")){ + calc.bigText.setText("0"); + calc.B.setText("0"); + } + else{ + calc.B.setText(calc.B.getText() + ("0")); + calc.bigText.setText(calc.bigText.getText() + "0"); + } + } + } + }; + return test; + } + + public EventHandler event1(Calculator calc){ + + EventHandler test = new EventHandler(){ + @Override + public void handle(ActionEvent event) { + if(equaled){ + calc.bigText.setText("0"); + calc.A.setText(null); + calc.B.setText(null); + calc.action.setText(null); + calc.answer.setText(null); + equaled = false; + } + + if (cursor == 0){ + if(calc.bigText.getText().equals("0") || calc.bigText.getText().isEmpty()){ + calc.bigText.setText("1"); + calc.A.setText("1"); + } + else{ + calc.bigText.setText(calc.bigText.getText() + "1"); + calc.A.setText(calc.A.getText() + "1"); + } + } + if (cursor == 1){ + if(calc.bigText.getText().equals("0")){ + calc.bigText.setText("1"); + calc.B.setText("1"); + } + else{ + calc.B.setText(calc.B.getText() + ("1")); + calc.bigText.setText(calc.bigText.getText() + "1"); + } + } + } + }; + return test; + } + + public EventHandler event2(Calculator calc){ + + EventHandler test = new EventHandler(){ + @Override + public void handle(ActionEvent event) { + if(equaled){ + calc.bigText.setText("0"); + calc.A.setText(null); + calc.B.setText(null); + calc.action.setText(null); + calc.answer.setText(null); + equaled = false; + } + + if (cursor == 0){ + if(calc.bigText.getText().equals("0") || calc.bigText.getText().isEmpty()){ + calc.bigText.setText("2"); + calc.A.setText("2"); + } + else{ + calc.bigText.setText(calc.bigText.getText() + "2"); + calc.A.setText(calc.A.getText() + "2"); + } + } + if (cursor == 1){ + if(calc.bigText.getText().equals("0")){ + calc.bigText.setText("2"); + calc.B.setText("2"); + } + else{ + calc.B.setText(calc.B.getText() + ("2")); + calc.bigText.setText(calc.bigText.getText() + "2"); + } + } + } + }; + return test; + } + + public EventHandler event3(Calculator calc){ + + EventHandler test = new EventHandler(){ + @Override + public void handle(ActionEvent event) { + if(equaled){ + calc.bigText.setText("0"); + calc.A.setText(null); + calc.B.setText(null); + calc.action.setText(null); + calc.answer.setText(null); + equaled = false; + } + + if (cursor == 0){ + if(calc.bigText.getText().equals("0") || calc.bigText.getText().isEmpty()){ + calc.bigText.setText("3"); + calc.A.setText("3"); + } + else{ + calc.bigText.setText(calc.bigText.getText() + "3"); + calc.A.setText(calc.A.getText() + "3"); + } + } + if (cursor == 1){ + if(calc.bigText.getText().equals("0")){ + calc.bigText.setText("3"); + calc.B.setText("3"); + } + else{ + calc.B.setText(calc.B.getText() + ("3")); + calc.bigText.setText(calc.bigText.getText() + "3"); + } + } + } + }; + return test; + } + + public EventHandler event4(Calculator calc){ + + EventHandler test = new EventHandler(){ + @Override + public void handle(ActionEvent event) { + if(equaled){ + calc.bigText.setText("0"); + calc.A.setText(null); + calc.B.setText(null); + calc.action.setText(null); + calc.answer.setText(null); + equaled = false; + } + + if (cursor == 0){ + if(calc.bigText.getText().equals("0") || calc.bigText.getText().isEmpty()){ + calc.bigText.setText("4"); + calc.A.setText("4"); + } + else{ + calc.bigText.setText(calc.bigText.getText() + "4"); + calc.A.setText(calc.A.getText() + "4"); + } + } + if (cursor == 1){ + if(calc.bigText.getText().equals("0")){ + calc.bigText.setText("4"); + calc.B.setText("4"); + } + else{ + calc.B.setText(calc.B.getText() + ("4")); + calc.bigText.setText(calc.bigText.getText() + "4"); + } + } + } + }; + return test; + } + + public EventHandler event5(Calculator calc){ + + EventHandler test = new EventHandler(){ + @Override + public void handle(ActionEvent event) { + if(equaled){ + calc.bigText.setText("0"); + calc.A.setText(null); + calc.B.setText(null); + calc.action.setText(null); + calc.answer.setText(null); + equaled = false; + } + + if (cursor == 0){ + if(calc.bigText.getText().equals("0") || calc.bigText.getText().isEmpty()){ + calc.bigText.setText("5"); + calc.A.setText("5"); + } + else{ + calc.bigText.setText(calc.bigText.getText() + "5"); + calc.A.setText(calc.A.getText() + "5"); + } + } + if (cursor == 1){ + if(calc.bigText.getText().equals("0")){ + calc.bigText.setText("5"); + calc.B.setText("5"); + } + else{ + calc.B.setText(calc.B.getText() + ("5")); + calc.bigText.setText(calc.bigText.getText() + "5"); + } + } + } + }; + return test; + } + + public EventHandler event6(Calculator calc){ + + EventHandler test = new EventHandler(){ + @Override + public void handle(ActionEvent event) { + if(equaled){ + calc.bigText.setText("0"); + calc.A.setText(null); + calc.B.setText(null); + calc.action.setText(null); + calc.answer.setText(null); + equaled = false; + } + + if (cursor == 0){ + if(calc.bigText.getText().equals("0") || calc.bigText.getText().isEmpty()){ + calc.bigText.setText("6"); + calc.A.setText("6"); + } + else{ + calc.bigText.setText(calc.bigText.getText() + "6"); + calc.A.setText(calc.A.getText() + "6"); + } + } + if (cursor == 1){ + if(calc.bigText.getText().equals("0")){ + calc.bigText.setText("6"); + calc.B.setText("6"); + } + else{ + calc.B.setText(calc.B.getText() + ("6")); + calc.bigText.setText(calc.bigText.getText() + "6"); + } + } + } + }; + return test; + } + + public EventHandler event7(Calculator calc){ + + EventHandler test = new EventHandler(){ + @Override + public void handle(ActionEvent event) { + if(equaled){ + calc.bigText.setText("0"); + calc.A.setText(null); + calc.B.setText(null); + calc.action.setText(null); + calc.answer.setText(null); + equaled = false; + } + + if (cursor == 0){ + if(calc.bigText.getText().equals("0") || calc.bigText.getText().isEmpty()){ + calc.bigText.setText("7"); + calc.A.setText("7"); + } + else{ + calc.bigText.setText(calc.bigText.getText() + "7"); + calc.A.setText(calc.A.getText() + "7"); + } + } + if (cursor == 1){ + if(calc.bigText.getText().equals("0")){ + calc.bigText.setText("7"); + calc.B.setText("7"); + } + else{ + calc.B.setText(calc.B.getText() + ("7")); + calc.bigText.setText(calc.bigText.getText() + "7"); + } + } + } + }; + return test; + } + + public EventHandler event8(Calculator calc){ + + EventHandler test = new EventHandler(){ + @Override + public void handle(ActionEvent event) { + if(equaled){ + calc.bigText.setText("0"); + calc.A.setText(null); + calc.B.setText(null); + calc.action.setText(null); + calc.answer.setText(null); + equaled = false; + } + + if (cursor == 0){ + if(calc.bigText.getText().equals("0") || calc.bigText.getText().isEmpty()){ + calc.bigText.setText("8"); + calc.A.setText("8"); + } + else{ + calc.bigText.setText(calc.bigText.getText() + "8"); + calc.A.setText(calc.A.getText() + "8"); + } + } + if (cursor == 1){ + if(calc.bigText.getText().equals("0")){ + calc.bigText.setText("8"); + calc.B.setText("8"); + } + else{ + calc.B.setText(calc.B.getText() + ("8")); + calc.bigText.setText(calc.bigText.getText() + "8"); + } + } + } + }; + return test; + } + + public EventHandler event9(Calculator calc){ + + EventHandler test = new EventHandler(){ + @Override + public void handle(ActionEvent event) { + if(equaled){ + calc.bigText.setText("0"); + calc.A.setText(null); + calc.B.setText(null); + calc.action.setText(null); + calc.answer.setText(null); + equaled = false; + } + + if (cursor == 0){ + if(calc.bigText.getText().equals("0") || calc.bigText.getText().isEmpty()){ + calc.bigText.setText("9"); + calc.A.setText("9"); + } + else{ + calc.bigText.setText(calc.bigText.getText() + "9"); + calc.A.setText(calc.A.getText() + "9"); + } + } + if (cursor == 1){ + if(calc.bigText.getText().equals("0")){ + calc.bigText.setText("9"); + calc.B.setText("9"); + } + else{ + calc.B.setText(calc.B.getText() + ("9")); + calc.bigText.setText(calc.bigText.getText() + "9"); + } + } + } + }; + return test; + } + + + /************************************ + * * + * CALCULATION EVENTS * + * * + *************************************/ + + public EventHandler eventPlus(Calculator calc){ + + EventHandler test = new EventHandler(){ + @Override + public void handle(ActionEvent event) { + + Long digTemp; + + try{ + digTemp = Long.parseLong(calc.bigText.getText()); + eventClear(calc); + String temp = String.valueOf(digTemp); + + if ( temp.length() > 10){ + temp = String.valueOf(digTemp).substring(0,9); + } + + equaled = false; + calc.A.setText(temp); + calc.bigText.setText("0"); + calc.action.setText("+"); + calc.B.setText(null); + calc.answer.setText(null); + cursor = 1; + } + catch(NumberFormatException ex){ + + calc.bigText.setText("0"); + calc.A.setText("0"); + calc.B.setText(null); + calc.answer.setText(null); + cursor = 0; + } + } + }; + return test; + } + + public EventHandler eventMinus(Calculator calc){ + + EventHandler test = new EventHandler(){ + @Override + public void handle(ActionEvent event) { + + Long digTemp; + + try{ + digTemp = Long.parseLong(calc.bigText.getText()); + eventClear(calc); + String temp = String.valueOf(digTemp); + + if ( temp.length() > 10){ + temp = String.valueOf(digTemp).substring(0,9); + } + + equaled = false; + calc.A.setText(temp); + calc.bigText.setText("0"); + calc.action.setText("-"); + calc.B.setText(null); + calc.answer.setText(null); + cursor = 1; + } + catch(NumberFormatException ex){ + + calc.bigText.setText("0"); + calc.A.setText("0"); + calc.B.setText(null); + calc.answer.setText(null); + cursor = 0; + } + } + }; + return test; + } + + public EventHandler eventMultiply(Calculator calc){ + + EventHandler test = new EventHandler(){ + @Override + public void handle(ActionEvent event) { + + Long digTemp; + + try{ + digTemp = Long.parseLong(calc.bigText.getText()); + eventClear(calc); + String temp = String.valueOf(digTemp); + + if ( temp.length() > 10){ + temp = String.valueOf(digTemp).substring(0,9); + } + + equaled = false; + calc.A.setText(temp); + calc.bigText.setText("0"); + calc.action.setText("x"); + calc.B.setText(null); + calc.answer.setText(null); + cursor = 1; + } + catch(NumberFormatException ex){ + + calc.bigText.setText("0"); + calc.A.setText("0"); + calc.B.setText(null); + calc.answer.setText(null); + cursor = 0; + } + } + }; + return test; + } + + public EventHandler eventDivide(Calculator calc){ + + EventHandler test = new EventHandler(){ + @Override + public void handle(ActionEvent event) { + + Long digTemp; + + try{ + + digTemp = Long.parseLong(calc.bigText.getText()); + eventClear(calc); + String temp = String.valueOf(digTemp); + + if ( temp.length() > 10){ + temp = String.valueOf(digTemp).substring(0,9); + } + + equaled = false; + calc.A.setText(temp); + calc.bigText.setText("0"); + calc.action.setText("/"); + calc.B.setText(null); + calc.answer.setText(null); + cursor = 1; + } + catch(NumberFormatException ex){ + + calc.bigText.setText("0"); + calc.A.setText("0"); + calc.B.setText(null); + calc.answer.setText(null); + cursor = 0; + } + } + }; + return test; + } + + public EventHandler eventEquals(Calculator calc){ + + EventHandler test = new EventHandler(){ + @Override + public void handle(ActionEvent event) { + + + Long digTempA; + Long digTempB; + char temp; + + try{ + digTempA = Long.parseLong(calc.A.getText()); + } + catch(NumberFormatException ex){ + //calc.bigText.setText("0"); + return; + } + + try{ + digTempA = Long.parseLong(calc.A.getText()); + digTempB = Long.parseLong(calc.B.getText()); + } + catch(NumberFormatException ex){ + calc.bigText.setText("ERROR"); + calc.answer.setText("ERROR"); + return; + } + + try{ + //digTempA = Long.parseLong(calc.A.getText()); + //digTempB = Long.parseLong(calc.B.getText()); + temp = calc.action.getText().charAt(0); + + switch(temp){ + case 'x': calc.answer.setText(String.valueOf(digTempA * digTempB)); + calc.bigText.setText(String.valueOf(digTempA * digTempB)); + if (String.valueOf(digTempA * digTempB).length() > 10){ + calc.bigText.setText("ERROR"); + calc.answer.setText("ERROR"); + } + + break; + case '+': calc.answer.setText(String.valueOf(digTempA + digTempB)); + calc.bigText.setText(String.valueOf(digTempA + digTempB)); + if (String.valueOf(digTempA + digTempB).length() > 10){ + calc.bigText.setText("ERROR"); + calc.answer.setText("ERROR"); + } + break; + case '/': calc.answer.setText(String.valueOf(digTempA / digTempB)); + calc.bigText.setText(String.valueOf(digTempA / digTempB)); + if (String.valueOf(digTempA / digTempB).length() > 10){ + calc.bigText.setText("ERROR"); + calc.answer.setText("ERROR"); + } + break; + case '-': calc.answer.setText(String.valueOf(digTempA - digTempB)); + calc.bigText.setText(String.valueOf(digTempA - digTempB)); + if (String.valueOf(digTempA - digTempB).length() > 9){ + calc.bigText.setText("ERROR"); + calc.answer.setText("ERROR"); + } + break; + default: + break; + } + cursor = 0; + equaled = true; + } + catch(NumberFormatException ex){ + //calc.bigText.setText("0"); + } + } + }; + return test; + } + + public EventHandler eventClear(Calculator calc){ + + EventHandler test = (EventHandler) (ActionEvent event) -> { + calc.bigText.setText("0"); + calc.A.setText(null); + calc.B.setText(null); + calc.action.setText(null); + calc.answer.setText(null); + equaled = false; + cursor = 0; + }; + return test; + } + + +}// END OF CLASS + diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..443e3cf --- /dev/null +++ b/Main.java @@ -0,0 +1,53 @@ +/******************************************************************** +// Main.java Author: Snubiss +// +// Date: March 17, 2019 +// Modified: March 19, 2019 +// +// Purpose: +// The purpose of this application is provide the user with a working +// calculator. The calculator can carry out basic tasks, such as addition +// subtraction, multiplication and division. The calculator allows input +// from the user by keyboard, by click and by direct input being typed +// into the text fields. If a calculation is higher then 9999999999 then +// the calculator will display an error message to the user. +// +//********************************************************************/ + +package basiccalculator; + +import javafx.application.Application; +import javafx.geometry.Pos; +import javafx.scene.Scene; +import javafx.scene.layout.VBox; +import javafx.stage.Stage; + + +public class Main extends Application { + + @Override + public void start(Stage primaryStage) { + + // Create a calculator. + Calculator calc = new Calculator(); + + // Prep the background Pane. + VBox mainPain = new VBox(); + mainPain.getChildren().addAll(calc.getCalculator()); + mainPain.setAlignment(Pos.CENTER); + mainPain.setStyle("-fx-background-color: #2f2f2f"); + + // Prep the Scene. + Scene scene = new Scene(mainPain, 600, 680); + calc.addKeyBindings(scene); + + // Prep the Stage. + primaryStage.setTitle("Snub's Calculator"); + primaryStage.setScene(scene); + primaryStage.show(); + } + + public static void main(String[] args) { + launch(args); + } +}