Advertisement
iamaamir

Calculator with lambdas

Oct 28th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.32 KB | None | 0 0
  1. package calcwithlambda;
  2.  
  3. /**
  4.  *
  5.  * @author Aamir khan
  6.  */
  7. import java.util.Arrays;
  8. import java.util.function.BinaryOperator;
  9. import javafx.application.Application;
  10. import javafx.scene.Group;
  11. import javafx.scene.Scene;
  12. import javafx.scene.control.Button;
  13. import javafx.scene.control.ComboBox;
  14. import javafx.scene.control.Label;
  15. import javafx.scene.control.TextField;
  16. import javafx.scene.layout.HBox;
  17. import javafx.scene.paint.Color;
  18. import javafx.stage.Stage;
  19. import javafx.stage.StageStyle;
  20.  
  21. public class Calculator extends Application {
  22.  
  23.     public static void main(String[] args) {
  24.         launch(args);
  25.     }
  26.  
  27.     @Override
  28.     public void start(Stage stage) {
  29.         stage.setTitle("A Simple Calculator");
  30.  
  31.         ComboBox<Operation> operations = new ComboBox<>();
  32.         operations.getItems().addAll(Arrays.asList(Operation.values()));
  33.         operations.setValue(Operation.values()[0]);
  34.  
  35.         TextField[] input = new TextField[2];
  36.         for (int i = 0; i < input.length; i++) {
  37.             input[i] = new TextField();
  38.             input[i].setPromptText("Enter a number");
  39.         }
  40.  
  41.         Label result = new Label("Click calculate for result");
  42.  
  43.         Button calculate = new Button("Calculate");
  44.         calculate.setOnAction(
  45.                 e -> result.setText(
  46.                         computeResult(
  47.                                 (Operation) operations.getValue(),
  48.                                 input[0].getText(),
  49.                                 input[1].getText()
  50.                         )
  51.                 )
  52.         );
  53.  
  54.         HBox box = new HBox();
  55.         box.getChildren().addAll(
  56.                 input[0], operations, input[1], calculate, result
  57.         );
  58.  
  59.         Group root = new Group();
  60.         root.getChildren().add(box);
  61.  
  62.         final Scene scene = new Scene(root);
  63.         //apply your css for the styling
  64.         scene.getStylesheets().add("http://pastebin.com/raw.php?i=wxn9WiVf");
  65.         scene.setFill(Color.WHITE);
  66.  
  67.         stage.initStyle(StageStyle.UTILITY);
  68.         stage.setScene(scene);
  69.         stage.sizeToScene();
  70.         stage.centerOnScreen();
  71.         stage.show();
  72.     }
  73.  
  74.     private  String computeResult(Operation op, String val1, String val2) {
  75.         double result = op.apply(
  76.                 Double.parseDouble(val1),
  77.                 Double.parseDouble(val2)
  78.         );
  79.  
  80.         return result == (int) result
  81.                 ? String.valueOf((int) result)
  82.                 : String.format("%.2f", result);
  83.     }
  84.  
  85.     enum Operation implements BinaryOperator<Double>{
  86.  
  87.         ADD((x, y) -> x + y),
  88.         SUBTRACT((x, y) -> x - y),
  89.         MULTIPLY((x, y) -> x * y),
  90.         DIVIDE((x, y) -> x / y),
  91.         MOD((x, y) -> x % y),
  92.         HYPOTENUSE(Math::hypot),
  93.         EXPONENTIATE(Math::pow);
  94.  
  95.         private final BinaryOperator<Double> equation;
  96.  
  97.         Operation(BinaryOperator<Double> equation) {
  98.             this.equation = equation;
  99.         }
  100.  
  101. //        public Equation getEquation() {
  102. //            return this.equation;
  103. //        }
  104.  
  105.         @Override
  106.         public String toString() {
  107.             return this.name().charAt(0) + this.name().substring(1).toLowerCase();
  108.         }
  109.  
  110.         @Override
  111.         public Double apply(Double t, Double u) {
  112.             return equation.apply(t, u);
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement