How to find Sum of digits from the given number ?

This is the complete solution to find the sum of digits from a given integer. The solution consists of a functional interface, super class and three derived classes implementing the interface.

package migrate;

public interface SumofDigitsOfNumIntf {

	public int SumDigits(Integer num);

	static void displayMsgForNum(Integer inpval, int sumd) {

		System.out.println("The sum of digits from this number = " + inpval + " is =" + sumd);
	}
}
package migrate;

import java.util.Arrays;
import java.util.Optional;
import java.util.logging.Logger;

class NumberInput {

	private Integer intValue;

	/* Declare the logger object for the class to display the log messages */
	static Logger logger = Logger.getLogger(NumberInput.class.getName());

	@SuppressWarnings("null")
	public boolean validateInputs(Integer intVal) {

		/* Fetch the class object's name from which this method is called */
		String cName = Thread.currentThread().getStackTrace()[2].getClassName();
		String mName = "validateInputs";
		logger.info("This method '" + mName + "' called from the Class '" + cName + "'");

		/*
		 * Validating the function parameters using Optional Integer data type methods
		 */
		Optional<Integer> intValue = Optional.ofNullable(intVal);

		/*
		 * Fetch the value of the Optional Integer parameters using 'isPresent'
		 * condition check. If the value is available use 'get' method otherwise simply
		 * assign 'null' to the 'intValue' variable using setter method.
		 */
		if (intValue.isPresent())
			setIntValue(intValue.get());
		else {
			setIntValue((Integer) null);
			return false;
		}

		logger.info("The input is validated Successfully");
		return true;
	}

	public int getIntValue() {
		return intValue.intValue();
	}

	public void setIntValue(Integer intValue) {
		this.intValue = intValue;
	}
}

class SumDigits_Using_Corelogic extends NumberInput implements SumofDigitsOfNumIntf {

	@Override
	public int SumDigits(Integer intVal) {

		/* Fetch the class object's name from which this method is called */
		String cName = Thread.currentThread().getStackTrace()[2].getClassName();
		String mName = "SumDigits";
		logger.info("This method '" + mName + "' called from the Class '" + cName + "'");
		int sumDigits = 0;
		Integer duplValue = intVal;

		try {
			if (validateInputs(intVal)) {
				int rem = 0;
				while (intVal != 0) {
					rem = intVal % 10;
					sumDigits = sumDigits + rem;
					intVal = intVal / 10;
				}
			}
		} catch (NullPointerException e) {
			logger.info("The input is validated Successfully");
			logger.warning("The given Input is '" + intVal + "'\n");
		}
		SumofDigitsOfNumIntf.displayMsgForNum(duplValue, sumDigits);
		return sumDigits;
	}
}

class SumDigits_Using_LambdaExpression extends NumberInput implements SumofDigitsOfNumIntf {

	/* Define the Lambda expression for the functional interface 'Prime' */
	SumofDigitsOfNumIntf SdNum = ((numValue) -> {

		boolean minusNum = false;
		/* Initialize the lower and upper boundary values in the for loop */
		System.out.println("The value of numValue is (Before) " + numValue);
		if (numValue < 0) {
			numValue *= -1;
			minusNum = true;
		}
		System.out.println("The value of numValue is (after) " + numValue);
		Integer sumDigits = 0;
		String strValue = String.valueOf(numValue);
		int lenOfStr = strValue.length();
		while (--lenOfStr >= 0)
			sumDigits += Character.getNumericValue(strValue.charAt(lenOfStr));
		System.out.println("The value of sumDigits is (Before) " + sumDigits);
		if (minusNum)
			sumDigits *= -1;
		System.out.println("The value of sumDigits is (after) " + sumDigits);
		return sumDigits;
	});

	@Override
	public int SumDigits(Integer intVal) {

		/* Fetch the class object's name from which this method is called */
		String cName = Thread.currentThread().getStackTrace()[2].getClassName();
		String mName = "SumDigits";
		logger.info("This method '" + mName + "' called from the Class '" + cName + "'");
		int status = 0;

		if (validateInputs(intVal)) {
			status = SdNum.SumDigits(intVal);
		} else {
			System.out.println("The given number is =" + intVal);
		}
		SumofDigitsOfNumIntf.displayMsgForNum(intVal, status);
		return status;
	}
}

class SumDigits_Using_Streams extends NumberInput implements SumofDigitsOfNumIntf {

	@Override
	public int SumDigits(Integer intVal) {

		/* Fetch the class object's name from which this method is called */
		String cName = Thread.currentThread().getStackTrace()[2].getClassName();
		String mName = "SumDigits";
		logger.info("This method '" + mName + "' called from the Class '" + cName + "'");
		Integer sumDigits = 0;
		boolean minusNum = false;
		if (validateInputs(intVal)) {

			if (intVal < 0) {
				intVal *= -1;
				minusNum = true;
			}
			String[] strArray = String.valueOf(intVal).split("\\s*");
			sumDigits = Arrays.stream(strArray).map(c -> Integer.parseInt(c)).reduce(0, (a, b) -> a + b);
			if (minusNum)
				sumDigits *= -1;
		}

		SumofDigitsOfNumIntf.displayMsgForNum(intVal, sumDigits);
		return sumDigits.intValue();
	}
}

public class SumofDigitsOfNum {

	public static void main(String[] args) {

		/*
		 * create the class object PrimeChk_Using_Corelogic, and call the method
		 * SumDigits
		 */
		SumDigits_Using_Corelogic SDUcore = new SumDigits_Using_Corelogic();
		SDUcore.SumDigits(4006);
		SDUcore.SumDigits(null);
		/*
		 * create the class object SumDigits_Using_LambdaExpression, and call the method
		 * SumDigits
		 */
		SumDigits_Using_LambdaExpression SDULexp = new SumDigits_Using_LambdaExpression();
		SDULexp.SumDigits(null);
		SDULexp.SumDigits(-4060);
		/*
		 * create the class object SumDigits_Using_Streams, and call the method
		 * SumDigits
		 */
		SumDigits_Using_Streams SDUstreams = new SumDigits_Using_Streams();
		SDUstreams.SumDigits(null);
		SDUstreams.SumDigits(-4600);
	}
}
OUTPUT:

Mar. 09, 2024 6:22:08 P.M. migrate.SumDigits_Using_Corelogic SumDigits
INFO: This method 'SumDigits' called from the Class 'migrate.SumofDigitsOfNum'
Mar. 09, 2024 6:22:08 P.M. migrate.NumberInput validateInputs
INFO: This method 'validateInputs' called from the Class 'migrate.SumDigits_Using_Corelogic'
Mar. 09, 2024 6:22:08 P.M. migrate.NumberInput validateInputs
INFO: The input is validated Successfully
The sum of digits from this number = 4006 is =10
Mar. 09, 2024 6:22:08 P.M. migrate.SumDigits_Using_Corelogic SumDigits
INFO: This method 'SumDigits' called from the Class 'migrate.SumofDigitsOfNum'
Mar. 09, 2024 6:22:08 P.M. migrate.NumberInput validateInputs
INFO: This method 'validateInputs' called from the Class 'migrate.SumDigits_Using_Corelogic'
The sum of digits from this number = null is =0
Mar. 09, 2024 6:22:08 P.M. migrate.SumDigits_Using_LambdaExpression SumDigits
INFO: This method 'SumDigits' called from the Class 'migrate.SumofDigitsOfNum'
Mar. 09, 2024 6:22:08 P.M. migrate.NumberInput validateInputs
INFO: This method 'validateInputs' called from the Class 'migrate.SumDigits_Using_LambdaExpression'
The given number is =null
The sum of digits from this number = null is =0
Mar. 09, 2024 6:22:08 P.M. migrate.SumDigits_Using_LambdaExpression SumDigits
INFO: This method 'SumDigits' called from the Class 'migrate.SumofDigitsOfNum'
Mar. 09, 2024 6:22:08 P.M. migrate.NumberInput validateInputs
INFO: This method 'validateInputs' called from the Class 'migrate.SumDigits_Using_LambdaExpression'
Mar. 09, 2024 6:22:08 P.M. migrate.NumberInput validateInputs
INFO: The input is validated Successfully
The value of numValue is (Before) -4060
The value of numValue is (after) 4060
The value of sumDigits is (Before) 10
The value of sumDigits is (after) -10
The sum of digits from this number = -4060 is =-10
Mar. 09, 2024 6:22:08 P.M. migrate.SumDigits_Using_Streams SumDigits
INFO: This method 'SumDigits' called from the Class 'migrate.SumofDigitsOfNum'
Mar. 09, 2024 6:22:08 P.M. migrate.NumberInput validateInputs
INFO: This method 'validateInputs' called from the Class 'migrate.SumDigits_Using_Streams'
The sum of digits from this number = null is =0
Mar. 09, 2024 6:22:08 P.M. migrate.SumDigits_Using_Streams SumDigits
INFO: This method 'SumDigits' called from the Class 'migrate.SumofDigitsOfNum'
Mar. 09, 2024 6:22:08 P.M. migrate.NumberInput validateInputs
INFO: This method 'validateInputs' called from the Class 'migrate.SumDigits_Using_Streams'
Mar. 09, 2024 6:22:08 P.M. migrate.NumberInput validateInputs
INFO: The input is validated Successfully
The sum of digits from this number = 4600 is =-10

The link to the JUnit5 test for this solution is here – https://atomic-temporary-185308886.wpcomstaging.com/2024/03/10/sumdigits-junittest/