Gestione errori

This commit is contained in:
La Programmatrice Verde
2026-02-17 17:55:07 +01:00
parent c55e158df2
commit 4e576c49ef
9 changed files with 159 additions and 29 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -139,14 +139,29 @@
</Properties> </Properties>
</Component> </Component>
<Component class="javax.swing.JTextField" name="txtNome"> <Component class="javax.swing.JTextField" name="txtNome">
<Events>
<EventHandler event="focusGained" listener="java.awt.event.FocusListener" parameters="java.awt.event.FocusEvent" handler="txtNomeFocusGained"/>
</Events>
</Component> </Component>
<Component class="javax.swing.JTextField" name="txtCognome"> <Component class="javax.swing.JTextField" name="txtCognome">
<Events>
<EventHandler event="focusGained" listener="java.awt.event.FocusListener" parameters="java.awt.event.FocusEvent" handler="txtCognomeFocusGained"/>
</Events>
</Component> </Component>
<Component class="javax.swing.JTextField" name="txtCodiceFiscale"> <Component class="javax.swing.JTextField" name="txtCodiceFiscale">
<Events>
<EventHandler event="focusGained" listener="java.awt.event.FocusListener" parameters="java.awt.event.FocusEvent" handler="txtCodiceFiscaleFocusGained"/>
</Events>
</Component> </Component>
<Component class="javax.swing.JTextField" name="txtDataDiNascita"> <Component class="javax.swing.JTextField" name="txtDataDiNascita">
<Events>
<EventHandler event="focusGained" listener="java.awt.event.FocusListener" parameters="java.awt.event.FocusEvent" handler="txtDataDiNascitaFocusGained"/>
</Events>
</Component> </Component>
<Component class="javax.swing.JTextField" name="txtSaldoIniziale"> <Component class="javax.swing.JTextField" name="txtSaldoIniziale">
<Events>
<EventHandler event="focusGained" listener="java.awt.event.FocusListener" parameters="java.awt.event.FocusEvent" handler="txtSaldoInizialeFocusGained"/>
</Events>
</Component> </Component>
<Component class="javax.swing.JButton" name="btnDataDiNascita"> <Component class="javax.swing.JButton" name="btnDataDiNascita">
<Properties> <Properties>

View File

@@ -139,14 +139,29 @@
</Properties> </Properties>
</Component> </Component>
<Component class="javax.swing.JTextField" name="txtNome"> <Component class="javax.swing.JTextField" name="txtNome">
<Events>
<EventHandler event="focusGained" listener="java.awt.event.FocusListener" parameters="java.awt.event.FocusEvent" handler="txtNomeFocusGained"/>
</Events>
</Component> </Component>
<Component class="javax.swing.JTextField" name="txtCognome"> <Component class="javax.swing.JTextField" name="txtCognome">
<Events>
<EventHandler event="focusGained" listener="java.awt.event.FocusListener" parameters="java.awt.event.FocusEvent" handler="txtCognomeFocusGained"/>
</Events>
</Component> </Component>
<Component class="javax.swing.JTextField" name="txtCodiceFiscale"> <Component class="javax.swing.JTextField" name="txtCodiceFiscale">
<Events>
<EventHandler event="focusGained" listener="java.awt.event.FocusListener" parameters="java.awt.event.FocusEvent" handler="txtCodiceFiscaleFocusGained"/>
</Events>
</Component> </Component>
<Component class="javax.swing.JTextField" name="txtDataDiNascita"> <Component class="javax.swing.JTextField" name="txtDataDiNascita">
<Events>
<EventHandler event="focusGained" listener="java.awt.event.FocusListener" parameters="java.awt.event.FocusEvent" handler="txtDataDiNascitaFocusGained"/>
</Events>
</Component> </Component>
<Component class="javax.swing.JTextField" name="txtSaldoIniziale"> <Component class="javax.swing.JTextField" name="txtSaldoIniziale">
<Events>
<EventHandler event="focusGained" listener="java.awt.event.FocusListener" parameters="java.awt.event.FocusEvent" handler="txtSaldoInizialeFocusGained"/>
</Events>
</Component> </Component>
<Component class="javax.swing.JButton" name="btnDataDiNascita"> <Component class="javax.swing.JButton" name="btnDataDiNascita">
<Properties> <Properties>

View File

@@ -5,7 +5,11 @@
package guimybank; package guimybank;
import java.awt.Color; import java.awt.Color;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.border.Border; import javax.swing.border.Border;
import javax.swing.border.LineBorder; import javax.swing.border.LineBorder;
@@ -15,7 +19,10 @@ import javax.swing.border.LineBorder;
*/ */
public class GUIMyBank extends javax.swing.JFrame { public class GUIMyBank extends javax.swing.JFrame {
private static final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(GUIMyBank.class.getName()); static Border borderOriginale = UIManager.getBorder("TextField.border");
private static final java.util.logging.Logger logger = java.util.logging.Logger
.getLogger(GUIMyBank.class.getName());
/** /**
* Creates new form GUIMyBank * Creates new form GUIMyBank
@@ -33,6 +40,7 @@ public class GUIMyBank extends javax.swing.JFrame {
* regenerated by the Form Editor. * regenerated by the Form Editor.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() { private void initComponents() {
@@ -64,6 +72,36 @@ public class GUIMyBank extends javax.swing.JFrame {
lblSaldoIniziale.setText("Saldo iniziale"); lblSaldoIniziale.setText("Saldo iniziale");
txtNome.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
txtNomeFocusGained(evt);
}
});
txtCognome.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
txtCognomeFocusGained(evt);
}
});
txtCodiceFiscale.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
txtCodiceFiscaleFocusGained(evt);
}
});
txtDataDiNascita.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
txtDataDiNascitaFocusGained(evt);
}
});
txtSaldoIniziale.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
txtSaldoInizialeFocusGained(evt);
}
});
btnDataDiNascita.setText("[]"); btnDataDiNascita.setText("[]");
btnAprireContoCorrente.setText("Aprire conto corrente"); btnAprireContoCorrente.setText("Aprire conto corrente");
@@ -150,40 +188,75 @@ public class GUIMyBank extends javax.swing.JFrame {
pack(); pack();
}// </editor-fold>//GEN-END:initComponents }// </editor-fold>//GEN-END:initComponents
private void btnAprireContoCorrenteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAprireContoCorrenteActionPerformed private void txtNomeFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNomeFocusGained
txtNome.setBorder(borderOriginale);
}//GEN-LAST:event_txtNomeFocusGained
private void txtCognomeFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCognomeFocusGained
txtCognome.setBorder(borderOriginale);
}//GEN-LAST:event_txtCognomeFocusGained
private void txtCodiceFiscaleFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCodiceFiscaleFocusGained
txtCodiceFiscale.setBorder(borderOriginale);
}//GEN-LAST:event_txtCodiceFiscaleFocusGained
private void txtDataDiNascitaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDataDiNascitaFocusGained
txtDataDiNascita.setBorder(borderOriginale);
}//GEN-LAST:event_txtDataDiNascitaFocusGained
private void txtSaldoInizialeFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtSaldoInizialeFocusGained
txtSaldoIniziale.setBorder(borderOriginale);
}//GEN-LAST:event_txtSaldoInizialeFocusGained
private void btnAprireContoCorrenteActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnAprireContoCorrenteActionPerformed
String nome = txtNome.getText(); String nome = txtNome.getText();
String cognome = txtCognome.getText(); String cognome = txtCognome.getText();
String codiceFiscale = txtCodiceFiscale.getText(); String codiceFiscale = txtCodiceFiscale.getText();
String dataDiNascita = txtDataDiNascita.getText(); // String dataDiNascita = txtDataDiNascita.getText();
String saldoIniziale = txtSaldoIniziale.getText(); String saldoIniziale = txtSaldoIniziale.getText();
boolean nomeEmpty = nome.isEmpty();
if(nome.isEmpty()){ boolean cognomeEmpty = cognome.isEmpty();
JOptionPane.showMessageDialog(null, "Inserire il proprio nome."); boolean codiceFiscaleEmpty = codiceFiscale.isEmpty();
txtNome.setBorder(new LineBorder(Color.red)); // boolean dataDiNascitaEmpty = dataDiNascita.isEmpty();
boolean saldoInizialeEmpty = saldoIniziale.isEmpty();
if (nomeEmpty || cognomeEmpty || codiceFiscaleEmpty /* || dataDiNascitaEmpty */ || saldoInizialeEmpty) {
if (nomeEmpty) {
txtNome.setBorder(new LineBorder(Color.red));
}
if (cognomeEmpty) {
txtCognome.setBorder(new LineBorder(Color.red));
}
if (codiceFiscaleEmpty) {
txtCodiceFiscale.setBorder(new LineBorder(Color.red));
}
/*
* if(dataDiNascitaEmpty){
* txtDataDiNascita.setBorder(new LineBorder(Color.red));
* }
*/
if (saldoInizialeEmpty) {
txtSaldoIniziale.setBorder(new LineBorder(Color.red));
}
JOptionPane.showMessageDialog(null, "Inserire tutti i dati richiesti.");
return; return;
} }
if(cognome.isEmpty()){
JOptionPane.showMessageDialog(null, "Inserire il proprio cognome."); if (isCodiceFiscaleValid(codiceFiscale)) {
txtCognome.setBorder(new LineBorder(Color.red)); JOptionPane.showMessageDialog(null, "Il codice fiscale non è valido.");
return;
}
//TODO: verifica codice fiscale
if(codiceFiscale.isEmpty()){
JOptionPane.showMessageDialog(null, "Inserire il proprio codice fiscale.");
txtCodiceFiscale.setBorder(new LineBorder(Color.red)); txtCodiceFiscale.setBorder(new LineBorder(Color.red));
return; return;
} }
//TODO: gestione data // TODO: gestione data
//if(dataDiNascita.isEmpty()){ // if(dataDiNascita.isEmpty()){
// JOptionPane.showMessageDialog(null, "Inserire la propria data di nascita."); // dataDiNascitaError = true;
// txtDataDiNascita.setBorder(new LineBorder(Color.red)); // }
// return; if (!isSaldoInizialeValid(saldoIniziale)) {
//} JOptionPane.showMessageDialog(null, "Il saldo iniziale non è valido.");
if(saldoIniziale.isEmpty()){
JOptionPane.showMessageDialog(null, "Inserire il proprio saldo iniziale.");
txtSaldoIniziale.setBorder(new LineBorder(Color.red)); txtSaldoIniziale.setBorder(new LineBorder(Color.red));
return; return;
} }
btnVersamento.setVisible(true); btnVersamento.setVisible(true);
btnPrelievo.setVisible(true); btnPrelievo.setVisible(true);
btnListaMovimenti.setVisible(true); btnListaMovimenti.setVisible(true);
@@ -192,17 +265,44 @@ public class GUIMyBank extends javax.swing.JFrame {
txtCodiceFiscale.setEditable(false); txtCodiceFiscale.setEditable(false);
txtDataDiNascita.setEditable(false); txtDataDiNascita.setEditable(false);
txtSaldoIniziale.setEditable(false); txtSaldoIniziale.setEditable(false);
}//GEN-LAST:event_btnAprireContoCorrenteActionPerformed }// GEN-LAST:event_btnAprireContoCorrenteActionPerformed
boolean isCodiceFiscaleValid(String codiceFiscale) {
Pattern pattern = Pattern.compile("[A-Z]{6}[ABCDEHLMPRST]{3}\\d{2}[A-Z]\\d{3}[A-Z]");
return !pattern.matcher(codiceFiscale).find();
}
boolean isSaldoInizialeValid(String saldoIniziale) {
boolean isSaldoInizialeValid = true;
double valoreSaldoIniziale;
try {
valoreSaldoIniziale = Double.parseDouble(saldoIniziale);
if (valoreSaldoIniziale <= 0) {
isSaldoInizialeValid = false;
}
} catch (NumberFormatException _) {
isSaldoInizialeValid = false;
}
return isSaldoInizialeValid;
}
/** /**
* @param args the command line arguments * @param args the command line arguments
*/ */
public static void main(String args[]) { public static void main(String args[]) {
/* Set the Nimbus look and feel */ /* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> // <editor-fold defaultstate="collapsed" desc=" Look and feel setting code
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. // (optional) ">
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html /*
* If Nimbus (introduced in Java SE 6) is not available, stay with the default
* look and feel.
* For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/ */
try { try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
@@ -214,7 +314,7 @@ public class GUIMyBank extends javax.swing.JFrame {
} catch (ReflectiveOperationException | javax.swing.UnsupportedLookAndFeelException ex) { } catch (ReflectiveOperationException | javax.swing.UnsupportedLookAndFeelException ex) {
logger.log(java.util.logging.Level.SEVERE, null, ex); logger.log(java.util.logging.Level.SEVERE, null, ex);
} }
//</editor-fold> // </editor-fold>
/* Create and display the form */ /* Create and display the form */
java.awt.EventQueue.invokeLater(() -> new GUIMyBank().setVisible(true)); java.awt.EventQueue.invokeLater(() -> new GUIMyBank().setVisible(true));