ew0345

Java calculator

Apr 21st, 2021
632
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class Calculator {
  4.     static double num1, num2, returnValue;
  5.     static boolean debug;
  6.    
  7.     public static void main(String[] args) {
  8.         if (args.length == 3) {
  9.             withArgs(args[0], Double.parseDouble(args[1]), Double.parseDouble(args[2]));
  10.         } else if (args.length <= 0) {
  11.             withoutArgs();
  12.         } else if (args.length == 1 && args[0].equalsIgnoreCase("--debug")) {
  13.             System.out.println("Debug mode enabled");
  14.             debug = true;
  15.             withoutArgs();
  16.         }else {
  17.             error();
  18.         }
  19.     }
  20.    
  21.     private static void withArgs(String type, double num1, double num2) {
  22.         switch (type.toLowerCase()) {
  23.             case "a":case "add":case "addition":
  24.                 returnValue = addition(num1, num2);
  25.                 System.out.println(num1 + " + " + num2 + " = " + returnValue);
  26.                 break;
  27.             case "s":case "sub":case "subtraction":
  28.                 returnValue = subtraction(num1, num2);
  29.                 System.out.println(num1 + " - " + num2 + " = " + returnValue);
  30.                 break;
  31.             case "m":case "mul":case "mult":case "multi":case "multiplication":
  32.                 returnValue = multiplication(num1, num2);
  33.                 System.out.println(num1 + " * " + num2 + " = " + returnValue);
  34.                 break;
  35.             case "d":case "div":case "division":
  36.                 returnValue = division(num1, num2);
  37.                 System.out.println(num1 + " / " + num2 + " = " + returnValue);
  38.                 break;
  39.             default:
  40.                 System.out.println("Invalid math operation."
  41.                     +"\nValid operations: Addition, Subtraction, Multiplication, Division.");
  42.                 System.exit(0);
  43.                 break;
  44.         }
  45.     }
  46.    
  47.     private static void withoutArgs() {
  48.         try (Scanner in = new Scanner(System.in)) {
  49.             String type;
  50.            
  51.             System.out.print("Math Operation: ");
  52.             type = in.nextLine();
  53.            
  54.             System.out.print("First Number: ");
  55.             num1 = in.nextDouble();
  56.            
  57.             System.out.print("Second Number: ");
  58.             num2 = in.nextDouble();
  59.            
  60.             withArgs(type, num1, num2);
  61.         } catch (Exception e) {
  62.             if (debug == true) {
  63.                 e.printStackTrace();
  64.             } else {
  65.                 System.out.println("Invalid input. Inputs types:"
  66.                     +"\n    Math Operation: String"
  67.                     +"\n    First Number: Double"
  68.                     +"\n    Second Number: Double");
  69.             }
  70.         }
  71.     }
  72.    
  73.     private static void error() {
  74.         System.out.println("Invalid usage."
  75.             +"\nValid usage: Calculator [<operation>] [<first number>] [<second number>]");
  76.     }
  77.    
  78.     private static double addition(double num1, double num2) {
  79.         return num1 + num2;
  80.     }
  81.    
  82.     private static double subtraction(double num1, double num2) {
  83.         return num1 - num2;
  84.     }
  85.    
  86.     private static double multiplication(double num1, double num2) {
  87.         return num1 * num2;
  88.     }
  89.    
  90.     private static double division(double num1, double num2) {
  91.         return num1 / num2;
  92.     }
  93. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×