Skip to content

Instantly share code, notes, and snippets.

@oshi192
Last active April 17, 2018 12:13
Show Gist options
  • Save oshi192/0c663229d4e4ce632d6ce03e12a12172 to your computer and use it in GitHub Desktop.
Save oshi192/0c663229d4e4ce632d6ce03e12a12172 to your computer and use it in GitHub Desktop.
import java.awt.dnd.InvalidDnDOperationException;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.Scanner;

public class Main {
    private static final char ADD = '+';
    private static final char SUB = '-';
    private static final char MULT = '*';
    private static final char DIV = '/';
    private static Map<Character, Double> operators = new HashMap<>();

    public static void main(String... args) {
        printInfo();
        try {
            calculating();
        } catch (InputMismatchException e) {
            System.out.println("Invalid input, try enter numbers");
        } catch (InvalidDnDOperationException e) {
            System.out.println(e.getMessage());
        } catch (RuntimeException e) {
            System.out.println("Something went wrong" + e);
        }
    }
    private static void calculating(){
        Scanner sc = new Scanner(System.in);
        System.out.print("First  number: ");
        double n1 = sc.nextDouble();
        System.out.print("     operator: ");
        sc.skip("\n");
        char operator = sc.nextLine().charAt(0);
        System.out.print("Second number: ");
        double n2 = sc.nextDouble();
        action(n1, n2);
        operatorValidator(operator);
        System.out.println("result: " + operators.get(operator));
    }
    private static void printInfo() {
        System.out.println("-------------Calculator-------------");
        System.out.println("enter first number and press enter,");
        System.out.println("after that enter operator and press enter,");
        System.out.println("enter second number and press enter");
        System.out.println("operators: + - * /");
    }

    private static void operatorValidator(char operator) {
        if (operators.get(operator)==null) {
            throw new InvalidDnDOperationException("invalid operator: \"" + operator + "\"");
        }
    }

    private static void action(double n1, double n2) {
        operators.put(ADD, addition(n1, n2));
        operators.put(SUB, subtraction(n1, n2));
        operators.put(MULT, multiply(n1, n2));
        operators.put(DIV, division(n1, n2));
    }

    private static double addition(double n1, double n2) {
        return n1 + n2;
    }

    private static double subtraction(double n1, double n2) {
        return n1 - n2;
    }

    private static double multiply(double n1, double n2) {
        return n1 * n2;
    }

    private static double division(double n1, double n2) {
        if (n2 == 0) {
            throw new InvalidDnDOperationException("divided by 0!!");
        }
        return n1 / n2;
    }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment