-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathStringCalculator.java
More file actions
38 lines (29 loc) · 880 Bytes
/
StringCalculator.java
File metadata and controls
38 lines (29 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.ArrayList;
import java.util.List;
public class StringCalculator {
private ParserOperands parserOperandos;
private static final int MAX = 1000;
public StringCalculator(){
parserOperandos = new ParserOperands();
}
public int add(String string) {
int result = 0;
List<String> operandos = parserOperandos.parse(string);
checkNegatives(operandos);
for (String operando : operandos) {
if(Integer.parseInt(operando) <= MAX)
result += Integer.parseInt(operando);
}
return result;
}
private void checkNegatives(List<String> operandos) {
List<String> negatives = new ArrayList<String>();
for (String operando : operandos) {
if(Integer.parseInt(operando) < 0)
negatives.add(operando);
}
if(negatives.size() > 0){
throw new NegativesNotAllowedException("Negatives not allowed " + negatives.toString());
}
}
}