From 48dc167eb1262f077656e39b6be06f2b377b8f1f Mon Sep 17 00:00:00 2001 From: La Programmatrice Verde Date: Thu, 19 Mar 2026 22:37:12 +0100 Subject: [PATCH] Controllo dati --- src/gestioneferie/Dipendente.java | 15 +++- src/gestioneferie/GestioneFerie.java | 107 +++++++++++++++++++++++-- src/gestioneferie/Richiesta_Ferie.java | 16 ++++ 3 files changed, 131 insertions(+), 7 deletions(-) diff --git a/src/gestioneferie/Dipendente.java b/src/gestioneferie/Dipendente.java index bcae528..7c14659 100644 --- a/src/gestioneferie/Dipendente.java +++ b/src/gestioneferie/Dipendente.java @@ -11,5 +11,18 @@ public class Dipendente { this.codiceFiscale = codiceFiscale; this.nome = nome; this.cognome = cognome; - } + } + + public String getCodiceFiscale() { + return codiceFiscale; + } + + public String getNome() { + return nome; + } + + public String getCognome() { + return cognome; + } + } diff --git a/src/gestioneferie/GestioneFerie.java b/src/gestioneferie/GestioneFerie.java index 3df71d4..dd9920c 100644 --- a/src/gestioneferie/GestioneFerie.java +++ b/src/gestioneferie/GestioneFerie.java @@ -13,6 +13,8 @@ import java.util.InputMismatchException; import java.util.Scanner; import java.util.regex.Pattern; +import gestioneferie.Richiesta_Ferie.statiApprovazione; + /** * * @author Verde @@ -55,7 +57,7 @@ public class GestioneFerie { pausa(); break; case 2: - richiediFerie(richieste); + richiediFerie(richieste, dipendenti); System.out.println("Richiesta di ferie aggiunta con successo."); System.out.println("La richiesta è attualmente in attesa di approvazione."); pausa(); @@ -93,8 +95,12 @@ public class GestioneFerie { String nome; String cognome; String codiceFiscale; + boolean error; - System.out.print("Inserire il nome del dipendente: "); + do { + error = false; + + System.out.print("Inserire il nome del dipendente: "); nome = sc.nextLine().trim().toLowerCase(); System.out.print("Inserire il cognome del dipendente: "); @@ -102,6 +108,13 @@ public class GestioneFerie { codiceFiscale = codiceFiscale(); + + if (dipendenteExists(nome, cognome, codiceFiscale, dipendenti)) { + System.out.println("Il dipendente è già stato inserito."); + pausa(); + error = true; + } + } while (error); dipendenti.add(new Dipendente(codiceFiscale, nome, cognome)); } @@ -125,17 +138,99 @@ public class GestioneFerie { return codiceFiscale; } - private static void richiediFerie(ArrayList richieste) { + private static boolean dipendenteExists(String nome, String cognome, String codiceFiscale, + ArrayList dipendenti) { + boolean dipendenteExists = false; + + for (Dipendente dipendente : dipendenti) { + if (dipendente.getNome().equals(nome) && dipendente.getCognome().equals(cognome) && dipendente.getCodiceFiscale().equals(codiceFiscale)) { + dipendenteExists = true; + } + } + return dipendenteExists; + } + + private static boolean codiceFiscaleExists(String codiceFiscale, + ArrayList dipendenti) { + boolean codiceFiscaleExists = false; + + for (Dipendente dipendente : dipendenti) { + if (dipendente.getCodiceFiscale().equals(codiceFiscale)) { + codiceFiscaleExists = true; + } + } + return codiceFiscaleExists; + } + + private static void richiediFerie(ArrayList richieste, ArrayList dipendenti) { Calendar inizio = Calendar.getInstance(); Calendar fine = Calendar.getInstance(); - String codiceFiscale = codiceFiscale(); + String codiceFiscale; + String richiestaExists; + boolean error; - inizio.setTime(data("dell'inizio delle ferie")); - fine.setTime(data("della fine delle ferie")); + do { + error = false; + + codiceFiscale = codiceFiscale(); + + if (!codiceFiscaleExists(codiceFiscale, dipendenti)) { + System.out.println("Il codice fiscale selezionato non è valido."); + pausa(); + error = true; + } else { + inizio.setTime(data("dell'inizio delle ferie")); + fine.setTime(data("della fine delle ferie")); + + if (isPeriodoValido(inizio, fine)) { + System.out.println("Il periodo di ferie selezionato non è valido."); + pausa(); + error = true; + } else { + richiestaExists = richiestaExists(codiceFiscale, inizio, fine, richieste); + + if (!richiestaExists.isEmpty()) { + System.out.println(richiestaExists); + pausa(); + error = true; + } + } + } + } while (error); richieste.add(new Richiesta_Ferie(codiceFiscale, inizio, fine)); } + private static boolean isPeriodoValido(Calendar inizio, Calendar fine) { + return inizio.before(fine) && inizio.after(new Date()) && fine.getTimeInMillis() / (24 * 60 * 60 * 1000) >= 1; // da millisecondi a giorni + } + + private static String richiestaExists(String codiceFiscale, Calendar inizio, Calendar fine, + ArrayList richieste) { + SimpleDateFormat sdf = new SimpleDateFormat(FORMATO_DATA); + + for (Richiesta_Ferie richiesta : richieste) { + if (sdf.format(richiesta.getInizio().getTime()).equals(sdf.format(inizio.getTime())) + && sdf.format(richiesta.getFine().getTime()).equals(sdf.format(fine.getTime()))) { + if (codiceFiscale.equals(richiesta.getCodiceFiscale())) { + switch (richiesta.getStato()) { + case statiApprovazione.RIFIUTATA: + return "Non è possibile rifare una richiesta di ferie per un periodo già rifiutato"; + case statiApprovazione.APPROVATA: + return "Non è possibile rifare una richiesta di ferie per un periodo già approvato"; + case statiApprovazione.IN_ATTESA: + return "Questo dipendente ha già richiesto delle ferie in questo periodo"; + default: + break; + } + } else { + return "Un altro dipendente ha già richiesto delle ferie in questo periodo"; + } + } + } + return ""; + } + private static Date data(String diCheCosa) { SimpleDateFormat sdf = new SimpleDateFormat(FORMATO_DATA); boolean error; diff --git a/src/gestioneferie/Richiesta_Ferie.java b/src/gestioneferie/Richiesta_Ferie.java index ad14722..d730839 100644 --- a/src/gestioneferie/Richiesta_Ferie.java +++ b/src/gestioneferie/Richiesta_Ferie.java @@ -20,4 +20,20 @@ public class Richiesta_Ferie { this.inizio = inizio; this.fine = fine; } + + public String getCodiceFiscale() { + return codiceFiscale; + } + + public Calendar getInizio() { + return inizio; + } + + public Calendar getFine() { + return fine; + } + public statiApprovazione getStato() { + return stato; + } + }