diff --git a/src/main/java/io/tswf/dima/CalculatorApp.java b/src/main/java/io/tswf/dima/CalculatorApp.java index b48ad5c..c9bfff3 100644 --- a/src/main/java/io/tswf/dima/CalculatorApp.java +++ b/src/main/java/io/tswf/dima/CalculatorApp.java @@ -3,23 +3,55 @@ package io.tswf.dima; import java.util.Scanner; public class CalculatorApp { - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - float number1 = scanner.nextFloat(); - String operation = scanner.next(); - float number2 = scanner.nextFloat(); - System.out.print("Ответ:"); - String a = "+"; - String b = "-"; - String c = "*"; - String d = ":"; - String e = "^"; - if( operation != a && operation != b && operation != c && operation != d && operation != e) { - System.out.println("ошибка"); + public static final String OPERATION_PLUS = "+"; + public static final String OPERATION_MINUS = "-"; + public static final String OPERATION_MULTIPLY = "*"; + public static final String OPERATION_DIVISION = ":"; + public static final String OPERATION_POWER = "^"; + + public static final String[] AVAILABLE_OPERATIONS = { OPERATION_DIVISION, OPERATION_MINUS, OPERATION_PLUS, OPERATION_MULTIPLY, OPERATION_POWER }; + + public static final Scanner SCANNER = new Scanner(System.in); + + // Проверяет поддерживается ли операция + public static boolean isOperationSupported(String operationDigit) { + for (String supportedOperationDigit : AVAILABLE_OPERATIONS) { + if (supportedOperationDigit.equals(operationDigit)) { + return true; + } } - if (":".equals(operation)) { + return false; + } + + public static void printInputNameText(String inputName) { + System.out.print("Введите " + inputName + " >>> "); + } + + public static float readNumberFromConsole(String numberName) { + printInputNameText(numberName); + return SCANNER.nextFloat(); + } + + public static String readStringFromConsole(String stringName) { + printInputNameText(stringName); + return SCANNER.next(); + } + + public static void main(String[] args) { + float number1 = readNumberFromConsole("первое число"); + String operation = readStringFromConsole("операцию"); + float number2 = readNumberFromConsole("второе число"); + + System.out.print("Ответ: "); + + if(!isOperationSupported(operation)) { + System.out.println("Неизвестная операция!"); + return; + } + + if (OPERATION_DIVISION.equals(operation)) { if (number2 != 0) { float division = number1 / number2; System.out.println(division); @@ -28,25 +60,23 @@ public class CalculatorApp { } } - if ("+".equals(operation)) { + if (OPERATION_PLUS.equals(operation)) { float addition = number1 + number2; System.out.println(addition); } - if ("-".equals(operation)) { + if (OPERATION_MINUS.equals(operation)) { float subtraction = number1 - number2; System.out.println(subtraction); } - if ("*".equals(operation)) { + if (OPERATION_MULTIPLY.equals(operation)) { float multiplication = number1 * number2; System.out.println(multiplication); } - if ("^".equals(operation)) { + if (OPERATION_POWER.equals(operation)) { double power = Math.pow(number1, number2); System.out.println(power); - - } } }