Logica spostata
This commit is contained in:
69
src/registrazionepalestra/Logica.java
Normal file
69
src/registrazionepalestra/Logica.java
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package registrazionepalestra;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class Logica {
|
||||||
|
private Logica() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isEmailValid(String email) {
|
||||||
|
Pattern pattern = Pattern.compile("[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}");
|
||||||
|
return pattern.matcher(email).find();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void salvaSuFile(String nome, String cognome, String codiceFiscale, String email, String password,
|
||||||
|
int intSesso, String citta, String corsi, String note) throws IOException {
|
||||||
|
String sesso;
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
switch (intSesso) {
|
||||||
|
case 0:
|
||||||
|
sesso = "M";
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
sesso = "F";
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
sesso = "NB";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
sesso = "";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.append("Nome: ");
|
||||||
|
sb.append(nome);
|
||||||
|
sb.append("\nCognome: ");
|
||||||
|
sb.append(cognome);
|
||||||
|
sb.append("\nCodice fiscale: ");
|
||||||
|
sb.append(codiceFiscale);
|
||||||
|
sb.append("\nEmail: ");
|
||||||
|
sb.append(email);
|
||||||
|
sb.append("\nPassword: ");
|
||||||
|
sb.append(password);
|
||||||
|
sb.append("\nSesso: ");
|
||||||
|
sb.append(sesso);
|
||||||
|
sb.append("\nCittà: ");
|
||||||
|
sb.append(citta);
|
||||||
|
sb.append("\nCorsi: ");
|
||||||
|
sb.append(corsi);
|
||||||
|
sb.append("\nNote: ");
|
||||||
|
sb.append(note);
|
||||||
|
sb.append("\n--FINE REGISTRAZIONE SOPRA--");
|
||||||
|
|
||||||
|
try (BufferedWriter bw = new BufferedWriter(
|
||||||
|
new FileWriter("./src/registrazionepalestra/registrazioni.txt", true))) {
|
||||||
|
bw.write(sb.toString());
|
||||||
|
} catch (IOException _) {
|
||||||
|
throw new IOException("Errore nel salvataggio della registrazione.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,10 +8,7 @@ import javax.swing.BorderFactory;
|
|||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
import javax.swing.border.LineBorder;
|
import javax.swing.border.LineBorder;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -353,7 +350,7 @@ public class RegistrazionePalestra extends javax.swing.JFrame {
|
|||||||
|
|
||||||
private void btnChiudiActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnChiudiActionPerformed
|
private void btnChiudiActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnChiudiActionPerformed
|
||||||
|
|
||||||
if (isRegistrazioneNonSalvata()) {
|
if (isRegistrazioneDaSalvare()) {
|
||||||
switch (JOptionPane.showConfirmDialog(null, "Salvare?", "Esci", JOptionPane.YES_NO_OPTION)) {
|
switch (JOptionPane.showConfirmDialog(null, "Salvare?", "Esci", JOptionPane.YES_NO_OPTION)) {
|
||||||
case 0:
|
case 0:
|
||||||
btnRegistraActionPerformed(evt);
|
btnRegistraActionPerformed(evt);
|
||||||
@@ -369,7 +366,7 @@ public class RegistrazionePalestra extends javax.swing.JFrame {
|
|||||||
}
|
}
|
||||||
}// GEN-LAST:event_btnChiudiActionPerformed
|
}// GEN-LAST:event_btnChiudiActionPerformed
|
||||||
|
|
||||||
private boolean isRegistrazioneNonSalvata() {
|
private boolean isRegistrazioneDaSalvare() {
|
||||||
return !(txtNome.getText().isEmpty() &&
|
return !(txtNome.getText().isEmpty() &&
|
||||||
txtCognome.getText().isEmpty() &&
|
txtCognome.getText().isEmpty() &&
|
||||||
txtCodiceFiscale.getText().isEmpty() &&
|
txtCodiceFiscale.getText().isEmpty() &&
|
||||||
@@ -445,13 +442,13 @@ public class RegistrazionePalestra extends javax.swing.JFrame {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isCodiceFiscaleValid(codiceFiscale)) {
|
if (!Logica.isCodiceFiscaleValid(codiceFiscale)) {
|
||||||
JOptionPane.showMessageDialog(null, "Il codice fiscale non è valido.");
|
JOptionPane.showMessageDialog(null, "Il codice fiscale non è valido.");
|
||||||
txtCodiceFiscale.setBorder(new LineBorder(Color.red));
|
txtCodiceFiscale.setBorder(new LineBorder(Color.red));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isEmailValid(email)) {
|
if (!Logica.isEmailValid(email)) {
|
||||||
JOptionPane.showMessageDialog(null, "L'indirizzo email non è valido.");
|
JOptionPane.showMessageDialog(null, "L'indirizzo email non è valido.");
|
||||||
txtEmail.setBorder(new LineBorder(Color.red));
|
txtEmail.setBorder(new LineBorder(Color.red));
|
||||||
return;
|
return;
|
||||||
@@ -465,16 +462,6 @@ public class RegistrazionePalestra extends javax.swing.JFrame {
|
|||||||
return !(chkYoga.isSelected() || chkPilates.isSelected() || chkFitness.isSelected());
|
return !(chkYoga.isSelected() || chkPilates.isSelected() || chkFitness.isSelected());
|
||||||
}
|
}
|
||||||
|
|
||||||
private 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();
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isEmailValid(String email) {
|
|
||||||
Pattern pattern = Pattern.compile("[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}");
|
|
||||||
return pattern.matcher(email).find();
|
|
||||||
}
|
|
||||||
|
|
||||||
private int getSelectedSesso() {
|
private int getSelectedSesso() {
|
||||||
int sessoSelected = -1;
|
int sessoSelected = -1;
|
||||||
|
|
||||||
@@ -516,50 +503,15 @@ public class RegistrazionePalestra extends javax.swing.JFrame {
|
|||||||
String codiceFiscale = txtCodiceFiscale.getText().trim().toUpperCase();
|
String codiceFiscale = txtCodiceFiscale.getText().trim().toUpperCase();
|
||||||
String email = txtEmail.getText().trim();
|
String email = txtEmail.getText().trim();
|
||||||
String password = new String(txtPassword.getPassword());
|
String password = new String(txtPassword.getPassword());
|
||||||
String sesso;
|
int sesso = getSelectedSesso();
|
||||||
|
String citta = (String) cmbCitta.getSelectedItem();
|
||||||
|
String corsi = getSelectedCorsi().replace("\n", ", ").replaceFirst(", ", "");
|
||||||
String note = txtaNote.getText();
|
String note = txtaNote.getText();
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
|
|
||||||
switch (getSelectedSesso()) {
|
try {
|
||||||
case 0:
|
Logica.salvaSuFile(nome, cognome, codiceFiscale, email, password, sesso, citta, corsi, note);
|
||||||
sesso = "M";
|
} catch (IOException e) {
|
||||||
break;
|
JOptionPane.showMessageDialog(null, e.getMessage());
|
||||||
case 1:
|
|
||||||
sesso = "F";
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
sesso = "NB";
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
sesso = "";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
sb.append("Nome: ");
|
|
||||||
sb.append(nome);
|
|
||||||
sb.append("\nCognome: ");
|
|
||||||
sb.append(cognome);
|
|
||||||
sb.append("\nCodice fiscale: ");
|
|
||||||
sb.append(codiceFiscale);
|
|
||||||
sb.append("\nEmail: ");
|
|
||||||
sb.append(email);
|
|
||||||
sb.append("\nPassword: ");
|
|
||||||
sb.append(password);
|
|
||||||
sb.append("\nSesso: ");
|
|
||||||
sb.append(sesso);
|
|
||||||
sb.append("\nCittà: ");
|
|
||||||
sb.append(cmbCitta.getSelectedItem());
|
|
||||||
sb.append("\nCorsi: ");
|
|
||||||
sb.append(getSelectedCorsi().replace("\n", ", ").replaceFirst(", ", ""));
|
|
||||||
sb.append("\nNote: ");
|
|
||||||
sb.append(note);
|
|
||||||
sb.append("\n--FINE REGISTRAZIONE SOPRA--");
|
|
||||||
|
|
||||||
try (BufferedWriter bw = new BufferedWriter(
|
|
||||||
new FileWriter("./src/registrazionepalestra/registrazioni.txt", true))) {
|
|
||||||
bw.write(sb.toString());
|
|
||||||
} catch (IOException _) {
|
|
||||||
JOptionPane.showMessageDialog(null, "Errore nel salvataggio della registrazione.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user