diff --git a/src/main/java/ch/hearc/ig/odi/stringcalculatortdd/StringCalculator.java b/src/main/java/ch/hearc/ig/odi/stringcalculatortdd/StringCalculator.java index 53c991c..3dcf00f 100644 --- a/src/main/java/ch/hearc/ig/odi/stringcalculatortdd/StringCalculator.java +++ b/src/main/java/ch/hearc/ig/odi/stringcalculatortdd/StringCalculator.java @@ -9,80 +9,83 @@ import org.apache.logging.log4j.Logger; public class StringCalculator { - /* private static final Logger logger = LogManager.getLogger(StringCalculator.class());*/ - public StringCalculator() { - } - private static final Logger logger = LogManager.getLogger(StringCalculator.class); + /* private static final Logger logger = LogManager.getLogger(StringCalculator.class());*/ + public StringCalculator() { + } + private static final Logger logger = LogManager.getLogger(StringCalculator.class); - public int add(final String numbers) { - //default delimiter - String delimiter = ",|\n"; - String numbersUpdated = numbers; - //if it begins by '//' then it's a personalize delimiter(s) - if (numbers.startsWith("//")) { - delimiter = this.extractDelimiter(numbers); - logger.info("délimiteur sprécial"); - //"cut" the beginning of the string so that the personalize delimiter is removed - numbersUpdated = numbers.substring(numbers.indexOf("\n") + 1); - } - return add(numbersUpdated, delimiter); + public int add(final String numbers) { + //default delimiter + String delimiter = ",|\n"; + String numbersUpdated = numbers; + + //if it begins by '//' then it's a personalize delimiter(s) + if (numbers.startsWith("//")) { + delimiter = this.extractDelimiter(numbers); + logger.info("délimiteur sprécial"); + //"cut" the beginning of the string so that the personalize delimiter is removed + numbersUpdated = numbers.substring(numbers.indexOf("\n") + 1); } + return add(numbersUpdated, delimiter); + } - private int add(final String numbersWithoutDelimiter, final String delimiter) { - int returnValue = 0; - List negativeNumbers = new ArrayList(); - - //a string tab containing the numbers without the delimiter(s) is created - String[] numbersArray = numbersWithoutDelimiter.split(delimiter); - - for (String number : numbersArray) { - //test if the numbers array is not empty - if (!(number.trim().length() == 0)) { - Integer numberInt = Integer.parseInt(number); - if (numberInt < 0) - negativeNumbers.add(numberInt); - else if (numberInt <= 1000) - returnValue += numberInt; - else { - logger.warn("Nombre plus grand que 1000"); - } - } - } + private int add(final String numbersWithoutDelimiter, final String delimiter) { + int returnValue = 0; + List negativeNumbers = new ArrayList(); - if (negativeNumbers.size() > 0) { - logger.fatal("Attention nombre négatif :" +negativeNumbers.toString()); - throw new RuntimeException("Negative not allowed: " + negativeNumbers.toString()); + //a string tab containing the numbers without the delimiter(s) is created + String[] numbersArray = numbersWithoutDelimiter.split(delimiter); + for (String number : numbersArray) { + //test if the numbers array is not empty + if (!(number.trim().length() == 0)) { + Integer numberInt = Integer.parseInt(number); + if (numberInt < 0) { + negativeNumbers.add(numberInt); + } else if (numberInt <= 1000) { + returnValue += numberInt; + } else { + logger.warn("Nombre plus grand que 1000"); } + } + } + + if (negativeNumbers.size() > 0) { + logger.fatal("Attention nombre négatif :" + negativeNumbers.toString()); + throw new RuntimeException("Negative not allowed: " + negativeNumbers.toString()); - return returnValue; } - private String extractDelimiter(String numbers) { - String[] delimiters; - String returnDelimiter; - int beginDelimiterIndex = numbers.indexOf("//") + 2; - int endDelimiterIndex = numbers.indexOf("\n"); - //if it begin with '[' and end by ']' there are multiple delimiters - if(numbers.charAt(beginDelimiterIndex) == '[' && numbers.charAt(endDelimiterIndex - 1) == ']') { - //use an Apache library to get each delimiters between '[' and ']' - delimiters = StringUtils.substringsBetween(numbers.substring(beginDelimiterIndex, endDelimiterIndex), "[", "]"); - returnDelimiter = ""; - //iterate through the delimiters list and concatenate the delimiter with a '|' between - //them so that the split method is OK with the regex - for (String element: delimiters) { - returnDelimiter += element; - returnDelimiter += "|"; - } - returnDelimiter = StringUtils.removeEnd(returnDelimiter, "|"); - } else { - //there is only one delimiter - returnDelimiter = numbers.substring(beginDelimiterIndex, endDelimiterIndex); - } + return returnValue; + } - return returnDelimiter; + private String extractDelimiter(String numbers) { + String[] delimiters; + String returnDelimiter; + int beginDelimiterIndex = numbers.indexOf("//") + 2; + int endDelimiterIndex = numbers.indexOf("\n"); + //if it begin with '[' and end by ']' there are multiple delimiters + if (numbers.charAt(beginDelimiterIndex) == '[' + && numbers.charAt(endDelimiterIndex - 1) == ']') { + //use an Apache library to get each delimiters between '[' and ']' + delimiters = StringUtils + .substringsBetween(numbers.substring(beginDelimiterIndex, endDelimiterIndex), "[", "]"); + returnDelimiter = ""; + //iterate through the delimiters list and concatenate the delimiter with a '|' between + //them so that the split method is OK with the regex + for (String element : delimiters) { + returnDelimiter += element; + returnDelimiter += "|"; + } + returnDelimiter = StringUtils.removeEnd(returnDelimiter, "|"); + } else { + //there is only one delimiter + returnDelimiter = numbers.substring(beginDelimiterIndex, endDelimiterIndex); } + + return returnDelimiter; + } } diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml index 55fe20c..28ce90c 100644 --- a/src/main/resources/log4j2.xml +++ b/src/main/resources/log4j2.xml @@ -10,12 +10,12 @@ + pattern="%highlight{%d{HH:mm:ss} [%t] %-5level %c - %msg%n}" disableAnsi="false"/> - +