From f8601985d81e45c7d0296e053a5bea973ad04f5e Mon Sep 17 00:00:00 2001 From: La Programmatrice Verde Date: Fri, 20 Mar 2026 18:15:30 +0100 Subject: [PATCH] Approva richieste --- src/gestioneferie/Dipendente.java | 14 ++- src/gestioneferie/GestioneFerie.java | 161 ++++++++++++++++++++----- src/gestioneferie/Richiesta_Ferie.java | 16 +++ 3 files changed, 158 insertions(+), 33 deletions(-) diff --git a/src/gestioneferie/Dipendente.java b/src/gestioneferie/Dipendente.java index 7de20a5..a9ab115 100644 --- a/src/gestioneferie/Dipendente.java +++ b/src/gestioneferie/Dipendente.java @@ -4,7 +4,7 @@ public class Dipendente { private String codiceFiscale; private String nome; private String cognome; - private int giorniFerieResidui = 0; + private int giorniFerieResidui = GIORNI_FERIE_TOTALI; public static final int GIORNI_FERIE_TOTALI = 35; public Dipendente(String codiceFiscale, String nome, String cognome) { @@ -25,9 +25,11 @@ public class Dipendente { return cognome; } - public void setGiorniFerieResidui(int giorniFerieResidui) { - this.giorniFerieResidui = giorniFerieResidui; - } - - + public void updateGiorniFerieResidui(int giorniFerie) { + this.giorniFerieResidui -= giorniFerie; + } + + public int getGiorniFerieResidui() { + return giorniFerieResidui; + } } diff --git a/src/gestioneferie/GestioneFerie.java b/src/gestioneferie/GestioneFerie.java index e842a37..88f78bf 100644 --- a/src/gestioneferie/GestioneFerie.java +++ b/src/gestioneferie/GestioneFerie.java @@ -10,6 +10,8 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.InputMismatchException; +import java.util.LinkedList; +import java.util.List; import java.util.Scanner; import java.util.regex.Pattern; @@ -63,7 +65,7 @@ public class GestioneFerie { pausa(); break; case 3: - + approvaRichiesteFerie(dipendenti, richieste); pausa(); break; case 4: @@ -101,19 +103,18 @@ public class GestioneFerie { error = false; System.out.print("Inserire il nome del dipendente: "); - nome = sc.nextLine().trim().toLowerCase(); + nome = sc.nextLine().trim().toLowerCase(); - System.out.print("Inserire il cognome del dipendente: "); - cognome = sc.nextLine().trim().toLowerCase(); + System.out.print("Inserire il cognome del dipendente: "); + cognome = sc.nextLine().trim().toLowerCase(); - codiceFiscale = codiceFiscale(); + codiceFiscale = codiceFiscale(); - - if (dipendenteExists(nome, cognome, codiceFiscale, dipendenti)) { - System.out.println("Il dipendente è già stato inserito."); - pausa(); - error = true; - } + 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)); } @@ -138,12 +139,13 @@ public class GestioneFerie { return codiceFiscale; } - private static boolean dipendenteExists(String nome, String cognome, String codiceFiscale, + 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)) { + if (dipendente.getNome().equals(nome) && dipendente.getCognome().equals(cognome) + && dipendente.getCodiceFiscale().equals(codiceFiscale)) { dipendenteExists = true; } } @@ -168,6 +170,7 @@ public class GestioneFerie { String codiceFiscale; String richiestaExists; boolean error; + int giorniDiFerie; do { error = false; @@ -187,21 +190,28 @@ public class GestioneFerie { pausa(); error = true; } else { - richiestaExists = richiestaExists(codiceFiscale, inizio, fine, richieste); - - if (!richiestaExists.isEmpty()) { - System.out.println(richiestaExists); + giorniDiFerie = (int) (fine.getTimeInMillis() / (24 * 60 * 60 * 1000) + - inizio.getTimeInMillis() / (24 * 60 * 60 * 1000)); + if (getDipendente(codiceFiscale, dipendenti).getGiorniFerieResidui() - giorniDiFerie < 0) { + System.out.println("Il periodo di ferie supera la durata di feire massima per dipendente di " + Dipendente.GIORNI_FERIE_TOTALI + "."); pausa(); error = true; + } else { + richiestaExists = richiestaExists(codiceFiscale, inizio, fine, richieste); + + if (!richiestaExists.isEmpty()) { + System.out.println(richiestaExists); + pausa(); + error = true; + } else { + getDipendente(codiceFiscale, dipendenti).updateGiorniFerieResidui(giorniDiFerie); + } } } } } while (error); richieste.add(new Richiesta_Ferie(codiceFiscale, inizio, fine)); - -getDipendente(codiceFiscale, dipendenti).setGiorniFerieResidui((int) (fine.getTimeInMillis() / (24 * 60 * 60 * 1000) - - inizio.getTimeInMillis() / (24 * 60 * 60 * 1000))); } private static Dipendente getDipendente(String codiceFiscale, ArrayList dipendenti) { @@ -213,18 +223,46 @@ getDipendente(codiceFiscale, dipendenti).setGiorniFerieResidui((int) (fine.getTi return null; } - private static Dipendente getDipendente(String nome, String cognome, String codiceFiscale, ArrayList dipendenti) { - for (Dipendente dipendente : dipendenti) { - if (dipendente.getNome().equals(nome) && dipendente.getCognome().equals(cognome) && dipendente.getCodiceFiscale().equals(codiceFiscale)) { - return dipendente; + private static Dipendente getDipendente(ArrayList dipendenti) { + String nome; + String cognome; + String codiceFiscale; + boolean error; + + do { + error = false; + + System.out.print("Inserire il nome del dipendente: "); + nome = sc.nextLine().trim().toLowerCase(); + + System.out.print("Inserire il cognome del dipendente: "); + cognome = sc.nextLine().trim().toLowerCase(); + + codiceFiscale = codiceFiscale(); + + if (!dipendenteExists(nome, cognome, codiceFiscale, dipendenti)) { + System.out.println("Il dipendente inserito non esiste."); + pausa(); + error = true; + } else { + for (Dipendente dipendente : dipendenti) { + if (dipendente.getNome().equals(nome) && dipendente.getCognome().equals(cognome) + && dipendente.getCodiceFiscale().equals(codiceFiscale)) { + return dipendente; + } + } } - } + } while (error); return null; } private static boolean isPeriodoValido(Calendar inizio, Calendar fine) { - return inizio.before(fine) && inizio.after(new Date()) && fine.getTimeInMillis() / (24 * 60 * 60 * 1000) >= 1 && (int) (fine.getTimeInMillis() / (24 * 60 * 60 * 1000) - - inizio.getTimeInMillis() / (24 * 60 * 60 * 1000)) <= Dipendente.GIORNI_FERIE_TOTALI; // da millisecondi a giorni + return inizio.before(fine) && inizio.after(new Date()) && fine.getTimeInMillis() / (24 * 60 * 60 * 1000) >= 1 + && (int) (fine.getTimeInMillis() / (24 * 60 * 60 * 1000) + - inizio.getTimeInMillis() / (24 * 60 * 60 * 1000)) <= Dipendente.GIORNI_FERIE_TOTALI; // da + // millisecondi + // a + // giorni } private static String richiestaExists(String codiceFiscale, Calendar inizio, Calendar fine, @@ -273,4 +311,73 @@ getDipendente(codiceFiscale, dipendenti).setGiorniFerieResidui((int) (fine.getTi return data; } + + private static LinkedList getRichiesteFerie(String codiceFiscale, + ArrayList richieste) { + LinkedList ferieRichieste = new LinkedList<>(); + for (Richiesta_Ferie richiesta : richieste) { + if (richiesta.getCodiceFiscale().equals(codiceFiscale)) { + ferieRichieste.add(richiesta); + } + } + return ferieRichieste; + } + + private static void approvaRichiesteFerie(ArrayList dipendenti, ArrayList richieste) { + Dipendente daApprovare; + LinkedList ferieRichieste; + + daApprovare = getDipendente(dipendenti); + ferieRichieste = getRichiesteFerie(daApprovare.getCodiceFiscale(), richieste); + + if (ferieRichieste.isEmpty()) { + System.out.println("Questo dipendente non ha ancora richiesto nessun periodo di ferie."); + } else { + System.out.println("Richieste:"); + for (Richiesta_Ferie richiesta : ferieRichieste) { + System.out.println(richiesta.toString()); + if (richiesta.getStato().equals(statiApprovazione.IN_ATTESA)) { + approvaRichiestaFerie(richiesta); + } + } + } + } + + private static void approvaRichiestaFerie(Richiesta_Ferie richiesta) { + int scelta = -1; + do { + System.out.println("Che operazione eseguire?"); + System.out.println("1. Approvare"); + System.out.println("2. Ignorare"); + System.out.println("3. Rifiutare"); + System.out.print("Scelta: "); + System.out.print("Opzione: "); + + try { + scelta = sc.nextInt(); + sc.nextLine(); + + switch (scelta) { + case 1: + richiesta.approva(); + System.out.println("Richiesta approvata."); + break; + case 2: + System.out.println("Richiesta ignorata."); + break; + case 3: + richiesta.rifiuta(); + System.out.println("Richiesta rifiutata."); + break; + default: + System.out.println(ERRORE_GENERICO); + pausa(); + break; + } + } catch (InputMismatchException _) { + System.out.println(ERRORE_GENERICO); + pausa(); + } + } while (scelta != 1 && scelta != 2 && scelta != 3); + } } diff --git a/src/gestioneferie/Richiesta_Ferie.java b/src/gestioneferie/Richiesta_Ferie.java index 568c47d..522b077 100644 --- a/src/gestioneferie/Richiesta_Ferie.java +++ b/src/gestioneferie/Richiesta_Ferie.java @@ -1,5 +1,6 @@ package gestioneferie; +import java.text.SimpleDateFormat; import java.util.Calendar; public class Richiesta_Ferie { @@ -42,4 +43,19 @@ public class Richiesta_Ferie { return giorniFerie; } + public void approva() { + stato = statiApprovazione.APPROVATA; + } + + public void rifiuta() { + stato = statiApprovazione.RIFIUTATA; + } + + @Override + public String toString() { + SimpleDateFormat sdf = new SimpleDateFormat(GestioneFerie.FORMATO_DATA); + return "Codice fiscale: " + codiceFiscale + "\nInizio ferie: " + sdf.format(inizio.getTime()) + "\nFine ferie: " + + sdf.format(fine.getTime()) + "\nTotale giorni di ferie: " + giorniFerie + "\nStato approvazione: " + + stato; + } }