427 lines
16 KiB
Java
427 lines
16 KiB
Java
/*
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
|
|
*/
|
|
package gestioneferie;
|
|
|
|
import java.util.Date;
|
|
import java.text.ParseException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.ArrayList;
|
|
import java.util.Calendar;
|
|
import java.util.InputMismatchException;
|
|
import java.util.LinkedList;
|
|
import java.util.Scanner;
|
|
import java.util.regex.Pattern;
|
|
|
|
import gestioneferie.Richiesta_Ferie.statiApprovazione;
|
|
|
|
/**
|
|
*
|
|
* @author Verde
|
|
*/
|
|
public class GestioneFerie {
|
|
|
|
/**
|
|
* @param args the command line arguments
|
|
*/
|
|
|
|
static Scanner sc = new Scanner(System.in);
|
|
static final String ERRORE_GENERICO = "Errore: opzione non valida.";
|
|
static final String ERRORE_DIPENDENTI_VUOTO = "È necessario inserire almeno un dipendente prima di proseguire.";
|
|
static final String ERRORE_RICHIESTE_VUOTO = "Aggiungere almeno una richiesta prima di proseguire.";
|
|
static final String FORMATO_DATA = "dd/MM/yyyy";
|
|
|
|
public static void main(String[] args) {
|
|
int scelta = -1;
|
|
ArrayList<Dipendente> dipendenti = new ArrayList<>();
|
|
ArrayList<Richiesta_Ferie> richieste = new ArrayList<>();
|
|
|
|
do {
|
|
System.out.println("Scegliere un'opzione:");
|
|
System.out.println("1. Aggiungi dipendente");
|
|
System.out.println("2. Richiedi ferie");
|
|
System.out.println("3. Approva ferie");
|
|
System.out.println("4. Visualizza ferie residue");
|
|
System.out.println("5. Visualizza dipendenti in ferie in un giorno");
|
|
System.out.println("0. Esci");
|
|
System.out.print("Opzione: ");
|
|
|
|
try {
|
|
scelta = sc.nextInt();
|
|
sc.nextLine();
|
|
|
|
switch (scelta) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
aggiungiDipendente(dipendenti);
|
|
System.out.println("Dipendente aggiunto con successo.");
|
|
pausa();
|
|
break;
|
|
case 2:
|
|
if (dipendenti.isEmpty()) {
|
|
System.out.println(ERRORE_DIPENDENTI_VUOTO);
|
|
} else {
|
|
richiediFerie(richieste, dipendenti);
|
|
System.out.println("Richiesta di ferie aggiunta con successo.");
|
|
System.out.println("La richiesta è attualmente in attesa di approvazione.");
|
|
}
|
|
pausa();
|
|
break;
|
|
case 3:
|
|
if (richieste.isEmpty()) {
|
|
System.out.println(ERRORE_RICHIESTE_VUOTO);
|
|
} else {
|
|
approvaRichiesteFerie(dipendenti, richieste);
|
|
}
|
|
pausa();
|
|
break;
|
|
case 4:
|
|
if (dipendenti.isEmpty()) {
|
|
System.out.println(ERRORE_DIPENDENTI_VUOTO);
|
|
} else {
|
|
System.out.println(
|
|
"Giorni di ferie residui: " + getDipendente(dipendenti).getGiorniFerieResidui());
|
|
}
|
|
pausa();
|
|
break;
|
|
case 5:
|
|
if (dipendenti.isEmpty()) {
|
|
System.out.println(ERRORE_DIPENDENTI_VUOTO);
|
|
} else {
|
|
if (richieste.isEmpty()) {
|
|
System.out.println(ERRORE_RICHIESTE_VUOTO);
|
|
} else {
|
|
mostraDipendentiInFerie(
|
|
data("per cui visualizzare i dipendenti in ferie"), dipendenti, richieste);
|
|
}
|
|
}
|
|
|
|
pausa();
|
|
break;
|
|
default:
|
|
System.out.println(ERRORE_GENERICO);
|
|
pausa();
|
|
break;
|
|
}
|
|
} catch (InputMismatchException _) {
|
|
System.out.println(ERRORE_GENERICO);
|
|
pausa();
|
|
}
|
|
} while (scelta != 0);
|
|
}
|
|
|
|
public static void pausa() {
|
|
System.out.println("Premere un tasto per continuare. . .");
|
|
sc.nextLine();
|
|
}
|
|
|
|
private static void aggiungiDipendente(ArrayList<Dipendente> 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 è già stato inserito.");
|
|
pausa();
|
|
error = true;
|
|
}
|
|
} while (error);
|
|
dipendenti.add(new Dipendente(codiceFiscale, nome, cognome));
|
|
}
|
|
|
|
private static String codiceFiscale() {
|
|
String codiceFiscale;
|
|
Pattern pattern = Pattern.compile("[A-Z]{6}[ABCDEHLMPRST]{3}\\d{2}[A-Z]\\d{3}[A-Z]");
|
|
boolean error;
|
|
|
|
do {
|
|
error = false;
|
|
System.out.print("Inserire il codice fiscale del dipendente: ");
|
|
codiceFiscale = sc.nextLine();
|
|
|
|
if (pattern.matcher(codiceFiscale).find()) {
|
|
System.out.print(ERRORE_GENERICO);
|
|
pausa();
|
|
error = true;
|
|
}
|
|
} while (error);
|
|
|
|
return codiceFiscale;
|
|
}
|
|
|
|
private static boolean dipendenteExists(String nome, String cognome, String codiceFiscale,
|
|
ArrayList<Dipendente> 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<Dipendente> dipendenti) {
|
|
boolean codiceFiscaleExists = false;
|
|
|
|
for (Dipendente dipendente : dipendenti) {
|
|
if (dipendente.getCodiceFiscale().equals(codiceFiscale)) {
|
|
codiceFiscaleExists = true;
|
|
}
|
|
}
|
|
return codiceFiscaleExists;
|
|
}
|
|
|
|
private static void richiediFerie(ArrayList<Richiesta_Ferie> richieste, ArrayList<Dipendente> dipendenti) {
|
|
Calendar inizio = Calendar.getInstance();
|
|
Calendar fine = Calendar.getInstance();
|
|
String codiceFiscale;
|
|
String richiestaExists;
|
|
boolean error;
|
|
int giorniDiFerie;
|
|
|
|
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 {
|
|
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));
|
|
}
|
|
|
|
private static Dipendente getDipendente(String codiceFiscale, ArrayList<Dipendente> dipendenti) {
|
|
for (Dipendente dipendente : dipendenti) {
|
|
if (dipendente.getCodiceFiscale().equals(codiceFiscale)) {
|
|
return dipendente;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private static Dipendente getDipendente(ArrayList<Dipendente> 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) {
|
|
Calendar oggi = Calendar.getInstance();
|
|
oggi.setTime(new Date());
|
|
return inizio.before(fine) && inizio.after(oggi) && fine.getTimeInMillis() / (24 * 60 * 60 * 1000) >= 1
|
|
&& (int) (fine.getTimeInMillis() / (24 * 60 * 60 * 1000)
|
|
- inizio.getTimeInMillis() / (24 * 60 * 60 * 1000)) <= Dipendente.GIORNI_FERIE_TOTALI;
|
|
}
|
|
|
|
private static String richiestaExists(String codiceFiscale, Calendar inizio, Calendar fine,
|
|
ArrayList<Richiesta_Ferie> 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;
|
|
Date data = null;
|
|
sdf.setLenient(false);
|
|
|
|
do {
|
|
error = false;
|
|
System.out.print("Inserire la data " + diCheCosa + " nel formato " + FORMATO_DATA + ": ");
|
|
try {
|
|
data = sdf.parse(sc.nextLine());
|
|
} catch (ParseException _) {
|
|
System.out.println("Errore: data non valida.");
|
|
pausa();
|
|
error = true;
|
|
}
|
|
} while (error);
|
|
|
|
return data;
|
|
}
|
|
|
|
private static LinkedList<Richiesta_Ferie> getRichiesteFerie(String codiceFiscale,
|
|
ArrayList<Richiesta_Ferie> richieste) {
|
|
LinkedList<Richiesta_Ferie> ferieRichieste = new LinkedList<>();
|
|
for (Richiesta_Ferie richiesta : richieste) {
|
|
if (richiesta.getCodiceFiscale().equals(codiceFiscale)) {
|
|
ferieRichieste.add(richiesta);
|
|
}
|
|
}
|
|
return ferieRichieste;
|
|
}
|
|
|
|
private static void approvaRichiesteFerie(ArrayList<Dipendente> dipendenti, ArrayList<Richiesta_Ferie> richieste) {
|
|
Dipendente daApprovare;
|
|
LinkedList<Richiesta_Ferie> 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);
|
|
}
|
|
|
|
private static void mostraDipendentiInFerie(Date data, ArrayList<Dipendente> dipendenti,
|
|
ArrayList<Richiesta_Ferie> richieste) {
|
|
SimpleDateFormat sdf = new SimpleDateFormat(FORMATO_DATA);
|
|
System.out.println("Dipendeti in ferie in data " + sdf.format(data) + ": ");
|
|
boolean dipendentiInFerieInData = false;
|
|
|
|
for (Richiesta_Ferie richiesta : richieste) {
|
|
if (richiesta.getStato().equals(statiApprovazione.APPROVATA) && richiesta.getInizio().before(data)
|
|
&& richiesta.getFine().after(data)) {
|
|
dipendentiInFerieInData = true;
|
|
System.out.println(getDipendente(richiesta.getCodiceFiscale(), dipendenti));
|
|
}
|
|
}
|
|
|
|
if (!dipendentiInFerieInData) {
|
|
System.out.println("Nessuno.");
|
|
}
|
|
}
|
|
}
|