Advertisement
Diego1764

Interfaz Cedula Ecuatoriana

Jan 20th, 2025 (edited)
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.31 KB | Software | 0 0
  1. /*
  2.  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  3.  * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JPanel.java to edit this template
  4.  */
  5. package republicavalidacionec;
  6.  
  7. /*
  8.     * Nombre: Diego Villacreses
  9.     * Fecha: 21 de enero del 2024
  10.     * Asunto: Validacion de la Cedula Ecuatoriana con Interfaz Gráfica
  11. */
  12.  
  13. import javax.swing.JOptionPane;
  14. import javax.swing.SwingUtilities;
  15.  
  16. public class main extends javax.swing.JPanel {
  17.  
  18.     /**
  19.      * Creates new form main
  20.      */
  21.     public main() {
  22.         initComponents();
  23.     }
  24.  
  25.     /**
  26.      * This method is called from within the constructor to initialize the form.
  27.      * WARNING: Do NOT modify this code. The content of this method is always
  28.      * regenerated by the Form Editor.
  29.      */
  30.     @SuppressWarnings("unchecked")
  31.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  32.     private void initComponents() {
  33.  
  34.         jButton1 = new javax.swing.JButton();
  35.         Bg = new javax.swing.JPanel();
  36.         CarandoletImage = new javax.swing.JLabel();
  37.         jPanel1 = new javax.swing.JPanel();
  38.         IconEc = new javax.swing.JLabel();
  39.         Title1 = new javax.swing.JLabel();
  40.         Title2 = new javax.swing.JLabel();
  41.         NoboaLogo = new javax.swing.JLabel();
  42.         jPanel2 = new javax.swing.JPanel();
  43.         jFieldCedula = new javax.swing.JTextField();
  44.         jLabel1 = new javax.swing.JLabel();
  45.         BtnValidar = new javax.swing.JButton();
  46.  
  47.         jButton1.setText("jButton1");
  48.  
  49.         Bg.setBackground(new java.awt.Color(204, 204, 204));
  50.  
  51.         CarandoletImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/attractive_129 (2).jpg"))); // NOI18N
  52.  
  53.         jPanel1.setBackground(new java.awt.Color(255, 255, 255));
  54.  
  55.         IconEc.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  56.         IconEc.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Ecuador-Flag-icon (1).png"))); // NOI18N
  57.  
  58.         Title1.setFont(new java.awt.Font("Sitka Display", 1, 24)); // NOI18N
  59.         Title1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  60.         Title1.setText("VALIDACION DE LA CEDULA");
  61.  
  62.         Title2.setFont(new java.awt.Font("Sitka Display", 1, 24)); // NOI18N
  63.         Title2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  64.         Title2.setText("ECUATORIANA");
  65.  
  66.         NoboaLogo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  67.         NoboaLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/nuevo-ecuador-fc2 (1).png"))); // NOI18N
  68.  
  69.         jPanel2.setBackground(new java.awt.Color(204, 204, 204));
  70.  
  71.         jFieldCedula.addActionListener(new java.awt.event.ActionListener() {
  72.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  73.                 jFieldCedulaActionPerformed(evt);
  74.             }
  75.         });
  76.  
  77.         jLabel1.setFont(new java.awt.Font("Segoe UI", 2, 18)); // NOI18N
  78.         jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  79.         jLabel1.setText("Ingrese su cedula ciudadanía:");
  80.  
  81.         BtnValidar.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
  82.         BtnValidar.setText("Validar");
  83.         BtnValidar.addActionListener(new java.awt.event.ActionListener() {
  84.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  85.                 BtnValidarActionPerformed(evt);
  86.             }
  87.         });
  88.  
  89.         javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
  90.         jPanel2.setLayout(jPanel2Layout);
  91.         jPanel2Layout.setHorizontalGroup(
  92.             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  93.             .addGroup(jPanel2Layout.createSequentialGroup()
  94.                 .addContainerGap()
  95.                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  96.                     .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  97.                     .addComponent(jFieldCedula)
  98.                     .addComponent(BtnValidar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  99.                 .addContainerGap())
  100.         );
  101.         jPanel2Layout.setVerticalGroup(
  102.             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  103.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
  104.                 .addContainerGap()
  105.                 .addComponent(jLabel1)
  106.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  107.                 .addComponent(jFieldCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  108.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  109.                 .addComponent(BtnValidar, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  110.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  111.         );
  112.  
  113.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  114.         jPanel1.setLayout(jPanel1Layout);
  115.         jPanel1Layout.setHorizontalGroup(
  116.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  117.             .addGroup(jPanel1Layout.createSequentialGroup()
  118.                 .addContainerGap()
  119.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  120.                     .addComponent(NoboaLogo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  121.                     .addComponent(IconEc, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  122.                     .addComponent(Title1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  123.                     .addComponent(Title2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  124.                     .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  125.                 .addContainerGap())
  126.         );
  127.         jPanel1Layout.setVerticalGroup(
  128.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  129.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
  130.                 .addContainerGap()
  131.                 .addComponent(IconEc)
  132.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  133.                 .addComponent(Title1)
  134.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  135.                 .addComponent(Title2)
  136.                 .addGap(18, 18, 18)
  137.                 .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  138.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE)
  139.                 .addComponent(NoboaLogo)
  140.                 .addContainerGap())
  141.         );
  142.  
  143.         javax.swing.GroupLayout BgLayout = new javax.swing.GroupLayout(Bg);
  144.         Bg.setLayout(BgLayout);
  145.         BgLayout.setHorizontalGroup(
  146.             BgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  147.             .addGroup(BgLayout.createSequentialGroup()
  148.                 .addContainerGap()
  149.                 .addComponent(CarandoletImage, javax.swing.GroupLayout.PREFERRED_SIZE, 594, javax.swing.GroupLayout.PREFERRED_SIZE)
  150.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  151.                 .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  152.                 .addContainerGap())
  153.         );
  154.         BgLayout.setVerticalGroup(
  155.             BgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  156.             .addGroup(BgLayout.createSequentialGroup()
  157.                 .addContainerGap()
  158.                 .addGroup(BgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  159.                     .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  160.                     .addComponent(CarandoletImage, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
  161.                 .addContainerGap())
  162.         );
  163.  
  164.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
  165.         this.setLayout(layout);
  166.         layout.setHorizontalGroup(
  167.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  168.             .addComponent(Bg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  169.         );
  170.         layout.setVerticalGroup(
  171.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  172.             .addComponent(Bg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  173.         );
  174.     }// </editor-fold>                        
  175.  
  176.     private void jFieldCedulaActionPerformed(java.awt.event.ActionEvent evt) {                                            
  177.         // Nada se rellena
  178.     }                                            
  179.  
  180.     private void BtnValidarActionPerformed(java.awt.event.ActionEvent evt) {                                          
  181.     if (this.jFieldCedula.getText().isEmpty()) {
  182.         JOptionPane.showMessageDialog(null, "Estimado Usuario, recuerde que ese campo es obligatorio para validar su cédula");
  183.     } else {
  184.         String cedula = this.jFieldCedula.getText();
  185.        
  186.         // Verificar que la cédula tenga 10 dígitos
  187.         if (cedula.length() != 10 || !cedula.matches("\\d+")) {
  188.             JOptionPane.showMessageDialog(null, "Estimado Usuario, la cédula debe tener exactamente 10 dígitos numéricos", "Error", JOptionPane.ERROR_MESSAGE);
  189.             return;
  190.         }
  191.        
  192.         int digitoVerificador = Integer.parseInt(cedula.substring(9, 10));
  193.         int acumPar = 0;
  194.         int acumImPar = 0;
  195.  
  196.         for (int x = 0; x <= 8; x++) {
  197.             int dato = Integer.parseInt(cedula.substring(x, x + 1));
  198.             if (x % 2 == 0) {
  199.                 int opera = dato * 2;
  200.                 if (opera > 9) {
  201.                     opera = opera - 9;
  202.                 }
  203.                 acumPar = acumPar + opera;
  204.             } else {
  205.                 acumImPar = acumImPar + dato;
  206.             }
  207.         }
  208.  
  209.         int total = acumPar + acumImPar;
  210.         int calculoDigitoVerificador = (10 - (total % 10)) % 10;
  211.  
  212.         if (digitoVerificador == calculoDigitoVerificador) {
  213.             JOptionPane.showMessageDialog(null, "Estimado Usuario, su cédula es correcta");
  214.         } else {
  215.             JOptionPane.showMessageDialog(null, "Estimado Usuario, su cédula es incorrecta", "Error", JOptionPane.ERROR_MESSAGE);
  216.         }
  217.     }
  218.     }                                          
  219.    
  220. public static void main(String[] args) {
  221.     SwingUtilities.invokeLater(new Runnable() {
  222.         @Override
  223.         public void run() {
  224.             javax.swing.JFrame frame = new javax.swing.JFrame("Validación de Cédula");
  225.             frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
  226.            
  227.             main app = new main();
  228.             frame.getContentPane().add(app);
  229.  
  230.             frame.setSize(1000, 400);
  231.             frame.setLocationRelativeTo(null);
  232.             frame.setVisible(true);
  233.         }
  234.     });
  235. }
  236.  
  237.    
  238.     // Variables declaration - do not modify                    
  239.     private javax.swing.JPanel Bg;
  240.     private javax.swing.JButton BtnValidar;
  241.     private javax.swing.JLabel CarandoletImage;
  242.     private javax.swing.JLabel IconEc;
  243.     private javax.swing.JLabel NoboaLogo;
  244.     private javax.swing.JLabel Title1;
  245.     private javax.swing.JLabel Title2;
  246.     private javax.swing.JButton jButton1;
  247.     private javax.swing.JTextField jFieldCedula;
  248.     private javax.swing.JLabel jLabel1;
  249.     private javax.swing.JPanel jPanel1;
  250.     private javax.swing.JPanel jPanel2;
  251.     // End of variables declaration                  
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement