Created
February 14, 2015 04:52
-
-
Save jthmiranda/5ca14483f0065f1cd06c to your computer and use it in GitHub Desktop.
primertarea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package primertarea; | |
/** | |
* | |
* @author Jonathan Miranda | |
*/ | |
public class Calculadora { | |
public static int agregar(String nums) { | |
int total = 0; | |
String delimitador = "\n|,"; | |
if (null == nums || nums.isEmpty()) { | |
return 0; | |
} | |
if (nums.startsWith("//")) { | |
int fin = nums.indexOf("\n"); | |
delimitador = nums.substring(0, fin); | |
delimitador = delimitador.replace("//", "\\"); | |
nums = nums.substring(fin + 1); | |
} | |
for (String i : nums.split(delimitador)) { | |
int cantidad = Integer.parseInt(i); | |
if (cantidad < 0) { | |
throw new IllegalArgumentException(String.format("string contiene [{0}], el cual no " | |
+ "corresponde con las reglas. Los numeros ingresados" | |
+ " no pueden ser negativos.", cantidad)); | |
} | |
if (cantidad > 1000) { | |
continue; | |
} | |
total += cantidad; | |
} | |
return total; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment