From e2abb090f5ba138d4cbea1cf1316ec3bd46d4619 Mon Sep 17 00:00:00 2001 From: amorozov Date: Wed, 13 Nov 2024 21:46:01 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=B7=D0=B0=20=D0=94=D0=B8=D0=BC=D1=83=20=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D0=BB=D1=8C=D0=BD=D0=BE=D0=B5=20=D1=80=D0=B5=D1=88?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5,=20=D0=B1=D1=83=D0=B4=D0=B5=D0=BC=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=BA=D1=82=D0=B8=D0=BA=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=D1=81=D1=8F=20=D0=B1=D0=BE=D0=BB=D1=8C=D1=88=D0=B5?= =?UTF-8?q?.=20=D0=92=D1=81=D0=B5=20=D0=B7=D0=B0=D0=B1=D1=8B=D0=BB=20(?= =?UTF-8?q?=D1=81=D0=B0=D0=BC=20=D1=81=D0=BC=D0=BE=D0=B3=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D0=B8=D1=82=D1=8C=20=D0=BD=D0=B0=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BD=D1=81=D1=82=D0=B0=D0=BD=D1=82=D1=8B=20=D0=BC=D0=B0?= =?UTF-8?q?=D0=B3=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=B8=D0=B5=20=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=BE=D0=BA=D0=B8,=20=D0=BA=D0=BE=D0=B3=D0=B4=D0=B0=20?= =?UTF-8?q?=D0=B5=D0=BC=D1=83=20=D0=BF=D0=BE=D0=BA=D0=B0=D0=B7=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=20=D0=BA=D0=B0=D0=BA=20=D0=BF=D0=B8=D1=81=D0=B0=D1=82?= =?UTF-8?q?=D1=8C=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D0=B0=D0=BD=D1=82=D1=8B?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/tswf/dima/CalculatorApp.java | 70 +++++++++++++------ 1 file changed, 50 insertions(+), 20 deletions(-) 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); - - } } }