From a9f5b8270839dde00e75c73eefbefd2d17b44b47 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sun, 18 Aug 2024 20:58:57 +0200 Subject: [PATCH] refactor: `OctalToDecimal` (#5344) --- .../conversions/OctalToDecimal.java | 57 +++++++++---------- .../conversions/OctalToDecimalTest.java | 21 ++++--- 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java b/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java index 187f0ed1..d91ce6eb 100644 --- a/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java +++ b/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java @@ -1,47 +1,42 @@ package com.thealgorithms.conversions; -import java.util.Scanner; - /** - * Converts any Octal Number to a Decimal Number + * Class for converting an octal number to a decimal number. Octal numbers are based on 8, using digits from 0 to 7. * - * @author Zachary Jones */ public final class OctalToDecimal { + private static final int OCTAL_BASE = 8; + private OctalToDecimal() { } /** - * Main method + * Converts a given octal number (as a string) to its decimal representation. + * If the input is not a valid octal number (i.e., contains characters other than 0-7), + * the method throws an IllegalArgumentException. * - * @param args Command line arguments - */ - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.print("Octal Input: "); - String inputOctal = sc.nextLine(); - int result = convertOctalToDecimal(inputOctal); - if (result != -1) { - System.out.println("Result convertOctalToDecimal : " + result); - } - sc.close(); - } - - /** - * This method converts an octal number to a decimal number. - * - * @param inputOctal The octal number - * @return The decimal number + * @param inputOctal The octal number as a string + * @return The decimal equivalent of the octal number + * @throws IllegalArgumentException if the input is not a valid octal number */ public static int convertOctalToDecimal(String inputOctal) { - try { - // Actual conversion of Octal to Decimal: - return Integer.parseInt(inputOctal, 8); - } catch (NumberFormatException ne) { - // Printing a warning message if the input is not a valid octal - // number: - System.out.println("Invalid Input, Expecting octal number 0-7"); - return -1; + if (inputOctal == null || inputOctal.isEmpty()) { + throw new IllegalArgumentException("Input cannot be null or empty"); } + + int decimalValue = 0; + + for (int i = 0; i < inputOctal.length(); i++) { + char currentChar = inputOctal.charAt(i); + + if (currentChar < '0' || currentChar > '7') { + throw new IllegalArgumentException("Incorrect input: Expecting an octal number (digits 0-7)"); + } + + int currentDigit = currentChar - '0'; + decimalValue = decimalValue * OCTAL_BASE + currentDigit; + } + + return decimalValue; } } diff --git a/src/test/java/com/thealgorithms/conversions/OctalToDecimalTest.java b/src/test/java/com/thealgorithms/conversions/OctalToDecimalTest.java index 6e17ea14..20f0fa40 100644 --- a/src/test/java/com/thealgorithms/conversions/OctalToDecimalTest.java +++ b/src/test/java/com/thealgorithms/conversions/OctalToDecimalTest.java @@ -1,14 +1,21 @@ package com.thealgorithms.conversions; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; public class OctalToDecimalTest { - @Test - public void testOctalToDecimal() { - assertEquals(1465, OctalToDecimal.convertOctalToDecimal("2671")); - assertEquals(189, OctalToDecimal.convertOctalToDecimal("275")); + @ParameterizedTest + @CsvSource({"10, 8", "7, 7", "77, 63", "123, 83", "0, 0", "777, 511", "2671, 1465", "275, 189"}) + void testConvertOctalToDecimal(String inputOctal, int expectedDecimal) { + Assertions.assertEquals(expectedDecimal, OctalToDecimal.convertOctalToDecimal(inputOctal)); + } + + @ParameterizedTest + @CsvSource({"'', Input cannot be null or empty", "'8', Incorrect input: Expecting an octal number (digits 0-7)", "'19', Incorrect input: Expecting an octal number (digits 0-7)"}) + void testIncorrectInput(String inputOctal, String expectedMessage) { + IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> OctalToDecimal.convertOctalToDecimal(inputOctal)); + Assertions.assertEquals(expectedMessage, exception.getMessage()); } }