Skip to content

Instantly share code, notes, and snippets.

@jthmiranda
Created February 14, 2015 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jthmiranda/5ca14483f0065f1cd06c to your computer and use it in GitHub Desktop.
Save jthmiranda/5ca14483f0065f1cd06c to your computer and use it in GitHub Desktop.
primertarea
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