|
| 1 | + |
| 2 | +package unit14.Actividad24_Ampliacion; |
| 3 | + |
| 4 | +import javax.swing.JOptionPane; |
| 5 | +import unit14.Actividad15_Aplicacion.DAO.OficinaDAO; |
| 6 | +import unit14.Actividad15_Aplicacion.Oficina; |
| 7 | + |
| 8 | + |
| 9 | +public class Actividad24 extends javax.swing.JFrame { |
| 10 | + |
| 11 | + |
| 12 | + public Actividad24() { |
| 13 | + initComponents(); |
| 14 | + } |
| 15 | + |
| 16 | + @SuppressWarnings("unchecked") |
| 17 | + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
| 18 | + private void initComponents() { |
| 19 | + |
| 20 | + jlTitulo = new javax.swing.JLabel(); |
| 21 | + jlCiudad = new javax.swing.JLabel(); |
| 22 | + jtfCiudad = new javax.swing.JTextField(); |
| 23 | + jlCiudad1 = new javax.swing.JLabel(); |
| 24 | + jtfSuperficie = new javax.swing.JTextField(); |
| 25 | + jlCiudad2 = new javax.swing.JLabel(); |
| 26 | + jtfVentas = new javax.swing.JTextField(); |
| 27 | + jbCrearOficina = new javax.swing.JButton(); |
| 28 | + |
| 29 | + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); |
| 30 | + |
| 31 | + jlTitulo.setFont(new java.awt.Font("Helvetica Neue", 0, 18)); // NOI18N |
| 32 | + jlTitulo.setText("Crear Oficina"); |
| 33 | + |
| 34 | + jlCiudad.setText("Ciudad:"); |
| 35 | + |
| 36 | + jlCiudad1.setText("Superficie:"); |
| 37 | + |
| 38 | + jlCiudad2.setText("Ventas:"); |
| 39 | + |
| 40 | + jbCrearOficina.setText("Crear"); |
| 41 | + jbCrearOficina.addActionListener(new java.awt.event.ActionListener() { |
| 42 | + public void actionPerformed(java.awt.event.ActionEvent evt) { |
| 43 | + jbCrearOficinaActionPerformed(evt); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); |
| 48 | + getContentPane().setLayout(layout); |
| 49 | + layout.setHorizontalGroup( |
| 50 | + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
| 51 | + .addGroup(layout.createSequentialGroup() |
| 52 | + .addGap(117, 117, 117) |
| 53 | + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) |
| 54 | + .addGroup(layout.createSequentialGroup() |
| 55 | + .addComponent(jlTitulo) |
| 56 | + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) |
| 57 | + .addGroup(layout.createSequentialGroup() |
| 58 | + .addComponent(jlCiudad) |
| 59 | + .addGap(18, 18, 18) |
| 60 | + .addComponent(jtfCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE) |
| 61 | + .addGap(128, 128, 128)))) |
| 62 | + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() |
| 63 | + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) |
| 64 | + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) |
| 65 | + .addGroup(layout.createSequentialGroup() |
| 66 | + .addComponent(jlCiudad2) |
| 67 | + .addGap(18, 18, 18) |
| 68 | + .addComponent(jtfVentas, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)) |
| 69 | + .addGroup(layout.createSequentialGroup() |
| 70 | + .addComponent(jlCiudad1) |
| 71 | + .addGap(18, 18, 18) |
| 72 | + .addComponent(jtfSuperficie, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))) |
| 73 | + .addGap(128, 128, 128)) |
| 74 | + .addGroup(layout.createSequentialGroup() |
| 75 | + .addGap(151, 151, 151) |
| 76 | + .addComponent(jbCrearOficina) |
| 77 | + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) |
| 78 | + ); |
| 79 | + layout.setVerticalGroup( |
| 80 | + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
| 81 | + .addGroup(layout.createSequentialGroup() |
| 82 | + .addContainerGap() |
| 83 | + .addComponent(jlTitulo) |
| 84 | + .addGap(48, 48, 48) |
| 85 | + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) |
| 86 | + .addComponent(jlCiudad) |
| 87 | + .addComponent(jtfCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) |
| 88 | + .addGap(18, 18, 18) |
| 89 | + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) |
| 90 | + .addComponent(jlCiudad1) |
| 91 | + .addComponent(jtfSuperficie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) |
| 92 | + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) |
| 93 | + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) |
| 94 | + .addComponent(jlCiudad2) |
| 95 | + .addComponent(jtfVentas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) |
| 96 | + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE) |
| 97 | + .addComponent(jbCrearOficina) |
| 98 | + .addGap(45, 45, 45)) |
| 99 | + ); |
| 100 | + |
| 101 | + pack(); |
| 102 | + }// </editor-fold>//GEN-END:initComponents |
| 103 | + |
| 104 | + private void jbCrearOficinaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCrearOficinaActionPerformed |
| 105 | + String ciudad = jtfCiudad.getText(); |
| 106 | + String superficieString = jtfSuperficie.getText(); |
| 107 | + String ventasString = jtfVentas.getText(); |
| 108 | + boolean isValidOficina = true; |
| 109 | + String errorMessage = ""; |
| 110 | + |
| 111 | + if (ciudad.isBlank()) { |
| 112 | + isValidOficina = false; |
| 113 | + errorMessage = "La ciudad no puede estar vacía"; |
| 114 | + } else if (!superficieString.matches("\\d+")) { |
| 115 | + isValidOficina = false; |
| 116 | + errorMessage = "La superficie debe de ser numérica"; |
| 117 | + } else if (Integer.parseInt(superficieString) <= 0) { |
| 118 | + isValidOficina = false; |
| 119 | + errorMessage = "La superficie no puede ser negativa"; |
| 120 | + } else if (!ventasString.matches("\\d+")) { |
| 121 | + isValidOficina = false; |
| 122 | + errorMessage = "Las ventas debe de ser numérica"; |
| 123 | + } else if (Integer.parseInt(ventasString) <= 0) { |
| 124 | + isValidOficina = false; |
| 125 | + errorMessage = "Las ventas no puede ser negativa"; |
| 126 | + } |
| 127 | + |
| 128 | + if (!isValidOficina) { |
| 129 | + JOptionPane.showMessageDialog(this, errorMessage,"Error", JOptionPane.ERROR_MESSAGE); |
| 130 | + } else { |
| 131 | + OficinaDAO oficinaDAO = new OficinaDAO(); |
| 132 | + Oficina nuevaOficina = new Oficina(0, ciudad, Integer.parseInt(superficieString), Integer.valueOf(ventasString)); |
| 133 | + |
| 134 | + oficinaDAO.insert(nuevaOficina); |
| 135 | + } |
| 136 | + }//GEN-LAST:event_jbCrearOficinaActionPerformed |
| 137 | + |
| 138 | + /** |
| 139 | + * @param args the command line arguments |
| 140 | + */ |
| 141 | + public static void main(String args[]) { |
| 142 | + /* Set the Nimbus look and feel */ |
| 143 | + //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> |
| 144 | + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. |
| 145 | + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html |
| 146 | + */ |
| 147 | + try { |
| 148 | + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { |
| 149 | + if ("Nimbus".equals(info.getName())) { |
| 150 | + javax.swing.UIManager.setLookAndFeel(info.getClassName()); |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + } catch (ClassNotFoundException ex) { |
| 155 | + java.util.logging.Logger.getLogger(Actividad24.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); |
| 156 | + } catch (InstantiationException ex) { |
| 157 | + java.util.logging.Logger.getLogger(Actividad24.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); |
| 158 | + } catch (IllegalAccessException ex) { |
| 159 | + java.util.logging.Logger.getLogger(Actividad24.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); |
| 160 | + } catch (javax.swing.UnsupportedLookAndFeelException ex) { |
| 161 | + java.util.logging.Logger.getLogger(Actividad24.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); |
| 162 | + } |
| 163 | + //</editor-fold> |
| 164 | + |
| 165 | + /* Create and display the form */ |
| 166 | + java.awt.EventQueue.invokeLater(new Runnable() { |
| 167 | + public void run() { |
| 168 | + new Actividad24().setVisible(true); |
| 169 | + } |
| 170 | + }); |
| 171 | + } |
| 172 | + |
| 173 | + // Variables declaration - do not modify//GEN-BEGIN:variables |
| 174 | + private javax.swing.JButton jbCrearOficina; |
| 175 | + private javax.swing.JLabel jlCiudad; |
| 176 | + private javax.swing.JLabel jlCiudad1; |
| 177 | + private javax.swing.JLabel jlCiudad2; |
| 178 | + private javax.swing.JLabel jlTitulo; |
| 179 | + private javax.swing.JTextField jtfCiudad; |
| 180 | + private javax.swing.JTextField jtfSuperficie; |
| 181 | + private javax.swing.JTextField jtfVentas; |
| 182 | + // End of variables declaration//GEN-END:variables |
| 183 | +} |
0 commit comments