Advertisement
robeeeert

Untitled

Jun 24th, 2023
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1.     public static boolean validarCui(String cui) {
  2.         int contadorDigitos = 0;
  3.         boolean cuiValido = false;
  4.         String ultimosCuatroDigitos;
  5.         if (cui.length() == 13) {
  6.             for (int i = 0; i < cui.length(); i++) {
  7.                 if (i == 0) {
  8.                     switch (cui.charAt(i)) {
  9.                         case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
  10.                             contadorDigitos++;
  11.                             break;
  12.                     }
  13.                 }
  14.                 switch (i) {
  15.                     case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12:
  16.                         switch (cui.charAt(i)) {
  17.                             case '0':case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
  18.                                 contadorDigitos++;
  19.                                 break;
  20.                         }
  21.                 }
  22.             }
  23.         }
  24.         ultimosCuatroDigitos = cui.substring(9);
  25.         if (contadorDigitos == cui.length()) {
  26.             if (!buscarEnArchivo(ultimosCuatroDigitos)) {
  27.                 cuiValido = false;
  28.             } else {
  29.                 cuiValido = true;
  30.             }
  31.         }
  32.         return cuiValido;
  33.     }
  34.     public static boolean buscarEnArchivo(String ultimosCuatroDigitos) {
  35.         boolean codigoEncontrado = false;
  36.         try {
  37.             BufferedReader br = new BufferedReader(new FileReader("DepartamentosMunicipios.txt"));
  38.             String linea;
  39.             while ((linea = br.readLine()) != null) {
  40.                 if (linea.length() >= 4) {
  41.                     String codigo = linea.substring(0, 4);
  42.                     if (codigo.equals(ultimosCuatroDigitos)) {
  43.                         codigoEncontrado = true;
  44.                         break;
  45.                     }
  46.                 }
  47.             }
  48.         } catch (IOException e) {
  49.             System.out.println("No se encontro el archivo");
  50.         }
  51.         return codigoEncontrado;
  52.     }
  53.     public static boolean validarFecha(String fecha){
  54.         boolean fechaValida = true;
  55.         String[] partesFecha = fecha.split("/");
  56.         int dia = Integer.parseInt(partesFecha[0]);
  57.         int mes = Integer.parseInt(partesFecha[1]);
  58.         int año = Integer.parseInt(partesFecha[2]);
  59.         if (fecha.length() > 10){
  60.             fechaValida = false;
  61.         }
  62.         if(mes < 1 || mes > 12 ){
  63.             fechaValida = false;
  64.         }else{
  65.             switch(mes){
  66.                 case 2:
  67.                     if(esBisiesto(año)){
  68.                         if(dia < 1 || dia > 29){
  69.                             fechaValida = false;
  70.                         }
  71.                     }else{
  72.                         if(dia < 1 || dia > 28){
  73.                             fechaValida = false;
  74.                         }
  75.                     }
  76.                     break;
  77.                 case 4: case 6: case 9: case 11:
  78.                     if(dia < 1 || dia > 30){
  79.                         fechaValida = false;
  80.                     }
  81.                     break;
  82.                 default:
  83.                     if(dia < 1 || dia > 31){
  84.                         fechaValida = false;
  85.                     }
  86.                     break;
  87.             }
  88.         }
  89.         return fechaValida;
  90.     }
  91.     public static boolean esBisiesto(int año){
  92.         return (año % 4 == 0 && año % 100 != 0) || (año % 400 == 0);
  93.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement