Advertisement
mattparks5855

Dank Java Calculator

Jun 28th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. package com.mattparks.calculator;
  2.  
  3. import java.util.*;
  4.  
  5. /**
  6.  * A class that takes inputs from the console and runs calculations.
  7.  */
  8. public class Calculator {
  9.     private Scanner scanner;
  10.  
  11.     /**
  12.      * Creates a new calculator.
  13.      */
  14.     public Calculator() {
  15.         scanner = new Scanner(System.in);
  16.     }
  17.  
  18.     /**
  19.      * Runs the inputs and calculations.
  20.      */
  21.     public void run() {
  22.         // Gets the first value.
  23.         System.out.println("Please enter the first number:");
  24.         double a = scanner.nextDouble();
  25.  
  26.         // Gets the second value.
  27.         System.out.println("Please enter the second number:");
  28.         double b = scanner.nextDouble();
  29.  
  30.         // Gets the operation.
  31.         System.out.println("Please enter the operation type:");
  32.         String type = scanner.nextLine();
  33.         if (type.isEmpty()) {
  34.             type = scanner.nextLine();
  35.         }
  36.  
  37.         // Gets the enum and operation interface,
  38.         Operations operations = getOperation(type);
  39.  
  40.         if (operations == null) {
  41.             System.out.println("Could not find a operation for " + type);
  42.             System.exit(-1);
  43.         }
  44.  
  45.         // Runs the calculations and displays results.
  46.         Operation operation = operations.operation;
  47.         double result = operation.calculate(a, b);
  48.         System.out.println(a + " " + operations.shortType + " " + b + " = " + result);
  49.     }
  50.  
  51.     /**
  52.      * A munch of mathematical operations.
  53.      */
  54.     @SuppressWarnings("unused")
  55.     private enum Operations {
  56.         ADDITION((a, b) -> a + b, "+"), SUBTRACTION((a, b) -> a - b, "-"),
  57.         MULTIPLICATION((a, b) -> a * b, "*"), DIVISION((a, b) -> a / b, "/"),
  58.         POWER((a, b) -> Math.pow(a, b), "^");
  59.  
  60.         public final Operation operation;
  61.         public final String shortType;
  62.  
  63.         Operations(Operation operation, String shortType) {
  64.             this.operation = operation;
  65.             this.shortType = shortType;
  66.         }
  67.     }
  68.  
  69.     /**
  70.      * Gets the operations from a short name.
  71.      *
  72.      * @param shortType The short name.
  73.      *
  74.      * @return The operations found with the short name.
  75.      */
  76.     private static Operations getOperation(String shortType) {
  77.         for (Operations o : Operations.values()) {
  78.             if (o.shortType.equals(shortType)) {
  79.                 return o;
  80.             }
  81.         }
  82.  
  83.         return null;
  84.     }
  85.  
  86.     /**
  87.      * A interface used to define mathematical operations.
  88.      */
  89.     private interface Operation {
  90.         /**
  91.          * Runs a calculation between two inputs.
  92.          *
  93.          * @param a The first value.
  94.          * @param b The second value.
  95.          *
  96.          * @return The resulting value.
  97.          */
  98.         double calculate(double a, double b);
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement