Skip to content

Commit

Permalink
Relates #6 logue fonctionnent avec les spécifications demandé
Browse files Browse the repository at this point in the history
  • Loading branch information
rascho1 committed Mar 17, 2019
1 parent 50b3a88 commit fbfa895
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> 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<Integer> 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;
}
}
4 changes: 2 additions & 2 deletions src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
<Console name="console" target="SYSTEM_OUT">
<LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout
pattern="%highlight{%d{HH:mm:ss} [%t] %-5level %c - %msg%n}{FATAL=red, WARN=yellow, INFO=black}" disableAnsi="false"/>
pattern="%highlight{%d{HH:mm:ss} [%t] %-5level %c - %msg%n}" disableAnsi="false"/>
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Console"/>
<AppenderRef ref="console"/>
<AppenderRef ref="warning"/>
<AppenderRef ref="fatal"/>
</Root>
Expand Down

0 comments on commit fbfa895

Please sign in to comment.