import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.io.UnsupportedEncodingException;

import java.util.Iterator;

import java.util.List;

 

import com.CsvFileReader;

import com.CsvFileWriter;

import com.Student;

 

// int foo = Integer.parseInt("1234"); da stringa ad intero

 

public class Default {

 

        public static String Name_csv ="utenti.csv";

        public static String fileNameCountID ="countID.txt";

 

        // SCELTE MENU

        // 0 Menu principale - 1 Registrati - 2 Login - 3 Logout - 4 Modifica dati

        // 5 Ricerca - 6 Esci

       

        static int id_menu=0; // Mi indica in che menų/sottomenų sono

       

        static int RowCl=20; // Di quante righe nuove crea ad ogni CLEAR_CONSOLE()

        public static String NextIDUser; // L'ID del nuovo utente     

        public static List<Student> List_students; // Lista degli utenti registrati

               

        public static String fileNamefullpath; // Path del file Csv

        public static String fileCountIDfullpath; // Path del file txt che mi memorizza l'ID del prossimo utente

       

        public static void main(String[] args) {

 

                fileNamefullpath=UTILITY.GET_PATH_CSV(Name_csv);

                fileCountIDfullpath=UTILITY.GET_PATH_CSV(fileNameCountID);

                                                      

 

                try {

                        BufferedReader in = new BufferedReader(new FileReader(fileCountIDfullpath));

                        String line;

                        while((line = in.readLine()) != null)

                        {

                               NextIDUser= line;

                               break;

                        }

                        in.close();

                } catch (IOException e) {

                        // TODO Auto-generated catch block

                        e.printStackTrace();

                }

 

               

                List_students = CsvFileReader.GET_LIST_USER(fileNamefullpath);

 

                System.out.println("Benvenuto su FaceDoor, siamo giā in ... " + List_students.size() + "!");

                System.lineSeparator();

                       

                UTILITY.PREMI_UN_TASTO_PER_MENU_PRINCIPALE();         

                ESEGUI_MENU();

       

        }

       

 

        /**

        //      * Funzione che setta la variabile globale id_menu

         * e mi "sposta" virtualmente nel menu indicato come

         * argomento 

         * @param Int x Numero del menu

         */

        public static void SET_MENU_ID(int x){

                id_menu=x;

        }

               

        private static void GESTISCI_SCELTA_MENU_PRINCIPALE() {

 

                // 1 Registrati - 2 Login - 3 Logout - 4 Modifica dati

                // 5 Ricerca - 6 Esci

 

                int selectedOption=UTILITY.LEGGI_INT_USER_LINE();

 

                System.out.println("Hai scelto " + selectedOption);

               

                if (selectedOption >= 1  && selectedOption <= 6){

                    UTILITY.CLEAR_CONSOLE();

                        if (UTILITY.ERRORE_SCELTA_UTENTE(selectedOption) == false) {

                               SET_MENU_ID(selectedOption);

                        } else {

                               UTILITY.RITORNA_MENU_CON_MESSAGGIO_ERRORE(1);

                        }      

                        ESEGUI_MENU();

                }else

                {

                        UTILITY.RITORNA_MENU_CON_MESSAGGIO_ERRORE(2);

                }

                                      

        }

       

        private static void GESTISCI_SCELTA_MENU_REGISTRATI() {

 

                // 1 Registrati - 2 Torna indietro

 

                try

                {

                        int selectedOption=UTILITY.LEGGI_INT_USER_LINE();

                        UTILITY.CLEAR_CONSOLE();

                        switch (selectedOption) {

 

                        // Registrati

                        case 1:

                               GESTISCI_REGISTRAZIONE.GET_INPUT();

                               break;

                       

                        // Torna indietro

                        case 2:

                               SET_MENU_ID(0);

                               ESEGUI_MENU();

                               break;

 

                        default:

                               UTILITY.RITORNA_MENU_CON_MESSAGGIO_ERRORE(2);

                               break;

                               }

               

                }catch (Exception ioe) {

                        UTILITY.RITORNA_MENU_CON_MESSAGGIO_ERRORE(2);

                }

 

        }

       

        private static void GESTISCI_SCELTA_MENU_LOGIN() {

 

                // 1 Login - 2 Torna indietro

 

                try {

 

                        int selectedOption=UTILITY.LEGGI_INT_USER_LINE();

                        UTILITY.CLEAR_CONSOLE();

                       

                        switch (selectedOption) {

                       

                        // Login

                        case 1:

                               GESTISCI_LOGIN.GET_INPUT();

                               break;

                       

                        // Torna indietro

                        case 2:

                               SET_MENU_ID(0);

                               ESEGUI_MENU();

                               break;

 

                        default:

                               UTILITY.RITORNA_MENU_CON_MESSAGGIO_ERRORE(2);

                               break;

                               }

               

                }catch (Exception ioe) {

                        UTILITY.RITORNA_MENU_CON_MESSAGGIO_ERRORE(2);

                }

 

        }

 

        private static void GESTISCI_SCELTA_MENU_LOGOUT() {

 

                // 1 logout - 2 Torna indietro

 

                try {

 

                        int selectedOption=UTILITY.LEGGI_INT_USER_LINE();

                        UTILITY.CLEAR_CONSOLE();

                       

                        switch (selectedOption) {

                       

                        // Login

                        case 1:

                               GESTISCI_LOGIN.EFFETTUA_LOG_OUT();

                               break;

                       

                        // Torna indietro

                        case 2:

                               SET_MENU_ID(0);

                               ESEGUI_MENU();

                               break;

 

                        default:

                               UTILITY.RITORNA_MENU_CON_MESSAGGIO_ERRORE(2);

                               break;

                               }

               

                }catch (Exception ioe) {

                        UTILITY.RITORNA_MENU_CON_MESSAGGIO_ERRORE(2);

                }

 

        }

 

        private static void GESTISCI_SCELTA_MENU_MODIFICA_DATI() {

 

                // 1) nome 2) cognome 3) email

                // 4) password 5) Descrizione 6) Cv 7) Foto 8) Torna al menu principale

 

               

                try {

 

                        int selectedOption=UTILITY.LEGGI_INT_USER_LINE();

                        UTILITY.CLEAR_CONSOLE();

                       

                        switch (selectedOption) {

                       

                        // 1) nome 2) cognome 3) email

                        // 4) password 5) Descrizione 6) Cv 7) Foto 8) Profilo

                        case 1:

                        case 2:

                        case 3:

                        case 4:

                        case 5:

                        case 6:

                        case 7:

                        case 8:

                               GESTISCI_REGISTRAZIONE.MODIFICA_DATI(selectedOption);

                               break;

 

                               // Cancella utente

                               case 9:

                                      

                                       String Profilo="";

                                       Boolean IsValid_Profilo=false;

                                       do{

                                               Profilo = UTILITY.LEGGI_USER_LINE("Sei sicuro di voler cancellare il tuo utente? [Y][N]");

                                               if ((Profilo.equals("Y")==true) || (Profilo.equals("N") ==true)) IsValid_Profilo=true;

                                               if (IsValid_Profilo == false)System.out.println("Input non corretto!");

                                       }while(IsValid_Profilo==false);

 

                                       if (Profilo.equals("Y")==true) GESTISCI_REGISTRAZIONE.CANCELLA_UTENTE();

                                       else ESEGUI_MENU();

                               break;

                                                              

                        // Torna indietro

                        case 10:

                               SET_MENU_ID(0);

                               ESEGUI_MENU();

                               break;

 

                        default:

                               UTILITY.RITORNA_MENU_CON_MESSAGGIO_ERRORE(2);

                               break;

                               }

               

                }catch (Exception ioe) {

                        UTILITY.RITORNA_MENU_CON_MESSAGGIO_ERRORE(2);

                }

 

        }

       

        private static void GESTISCI_SCELTA_MENU_RICERCA() {

 

                // 1 Cerca nome, cognome - 2 Torna indietro

 

                try {

 

                        int selectedOption=UTILITY.LEGGI_INT_USER_LINE();

                        UTILITY.CLEAR_CONSOLE();

                       

                        switch (selectedOption) {

                       

                        // Ricerca

                        case 1:

                               GESTISCI_RICERCA.GET_INPUT();

                               break;

                       

                        // Torna indietro

                        case 2:

                               SET_MENU_ID(0);

                               ESEGUI_MENU();

                               break;

 

                        default:

                               UTILITY.RITORNA_MENU_CON_MESSAGGIO_ERRORE(2);

                               break;

                               }

               

                }catch (Exception ioe) {

                        UTILITY.RITORNA_MENU_CON_MESSAGGIO_ERRORE(2);

                }

 

        }

 

        private static void GESTISCI_SCELTA_MENU_ESCI() {

 

                // 1 Esci - 2 Torna indietro

               

               

                try {

 

                        int selectedOption=UTILITY.LEGGI_INT_USER_LINE();

                        UTILITY.CLEAR_CONSOLE();

                       

                        switch (selectedOption) {

                       

                        // Esci

                        case 1:

                               System.exit(1);

                               break;

                       

                        // Torna indietro

                        case 2:

                               SET_MENU_ID(0);

                               ESEGUI_MENU();

                               break;

 

                        default:

                               UTILITY.RITORNA_MENU_CON_MESSAGGIO_ERRORE(2);

                               break;

                               }

               

                }catch (Exception ioe) {

                        UTILITY.RITORNA_MENU_CON_MESSAGGIO_ERRORE(2);

                }

 

        }

       

       

        /**

         * Funzione che stampa il menu in base al

         * parametro globale id_menu

         *

         */

        public static void ESEGUI_MENU(){

               

                UTILITY.CLEAR_CONSOLE();

                       

                // 0 Menu principale - 1 Registrati - 2 Login - 3 Logout -

                // 4 Modifica dati 5 Ricerca - 6 Esci

               

                        switch (id_menu) {

 

                        case 0: {

                               STAMPA_MENU.STAMPA_MENU_PRINCIPALE();

                               GESTISCI_SCELTA_MENU_PRINCIPALE();

                               break;

                        }

 

                        case 1: {

                               STAMPA_MENU.STAMPA_MENU_REGISTRATI();

                               GESTISCI_SCELTA_MENU_REGISTRATI();

                               break;

                        }

                       

                        case 2: {

                               STAMPA_MENU.STAMPA_MENU_LOGIN();

                               GESTISCI_SCELTA_MENU_LOGIN();

                               break;

                        }

                       

                        case 3: {

                               STAMPA_MENU.STAMPA_MENU_LOGOUT();

                               GESTISCI_SCELTA_MENU_LOGOUT();

                               break;

                        }

                       

                        case 4: {

                               STAMPA_MENU.STAMPA_MENU_MODIFICA_DATI();

                               GESTISCI_SCELTA_MENU_MODIFICA_DATI();

                               break;

                        }

                       

                        case 5: {

                               STAMPA_MENU.STAMPA_MENU_RICERCA();

                               GESTISCI_SCELTA_MENU_RICERCA();

                               break;

                        }

                       

                        case 6: {

                               STAMPA_MENU.STAMPA_MENU_ESCI();

                               GESTISCI_SCELTA_MENU_ESCI();

                               break;

                        }

                       

                        }              

                }

 

        /**

         * Controlla l'userID passato come argomento sia unico

         *

         * <p>

         *

         * @param  String UserID

         * @return Boolean

         */

        public static boolean IS_USER_VALID(String UserID){

 

                //Print the new student list

        for (Student student : List_students) {

                        if (student.Get_UserID().equals(UserID)==true)return false;

                }

       

        return true;

               

        }

       

        /**

         * Controlla se IDutente e password corrispondo e ritorna l'ID dell'utente

         * che si logga

         * <p>

         *

         * @param  String UserIDNome

         * @param  String Password

         * @return Int l'ID del utente

         */

        public static int RETURN_ID_LOGIN_VALID(String UserID, String Password){

 

                //Print the new student list

        for (Student student : List_students) {

                        if (student.Get_UserID().equals(UserID)==true)

                               if (student.Get_Password().equals(Password)==true)

                               return student.Get_ID();

                }

       

        return -1;

               

        }

       

        /**

         * Classe che gestisce le informazione dell'utente

         * una volta che effettua il login

         *

    */

        public static class UTENTE {

 

                static int ID_utente = 0;

                static String Nome_utente = "";

 

                public static void SET_ID(int ID_user) {

                        UTENTE.ID_utente = ID_user;

                }

 

                public static int GET_ID() {

                        return UTENTE.ID_utente;

                }

 

        }

 

        /**

         * Classe che gestisce che menu stampare a video

         *

    */

        public static class STAMPA_MENU {

 

                // 0 Menu principale - 1 Registrati - 2 Login - 3 Logout -

                // 4 Modifica dati 5 Ricerca - 6 Esci

               

                public static void STAMPA_FRASE_SCEGLI(){

                        System.out.println("Scegli cosa fare: ");             

                }

               

                public static void STAMPA_MENU_PRINCIPALE() {

 

                        System.out.println("--- MENU PRINCIPALE ---");

                        System.lineSeparator();

 

                        if (UTENTE.GET_ID() == 0) {

 

                               System.out.println("1) Registrati");

                               System.lineSeparator();

 

                               System.out.println("2) Login");

                               System.lineSeparator();

 

                        } else {

 

                               System.out.println("3) Logout");

                               System.lineSeparator();

 

                               System.out.println("4) Edita i tuoi dati");

                               System.lineSeparator();

                        }

 

                        System.out.println("5) Ricerca");

                        System.lineSeparator();

 

                        System.out.println("6) Esci ");

                        System.lineSeparator();

 

                        STAMPA_FRASE_SCEGLI();

 

                }

       

                public static void STAMPA_MENU_REGISTRATI() {

 

                        System.out.println("--- MENU REGISTRAZIONE ---");

                        System.lineSeparator();

 

                        System.out.println("1) Registrati");

                        System.lineSeparator();

 

                        System.out.println("2) Torna al menu principale");

                        System.lineSeparator();

                       

                        STAMPA_FRASE_SCEGLI();

                       

                }

       

                public static void STAMPA_MENU_LOGIN() {

 

                        System.out.println("--- MENU LOGIN ---");

                        System.lineSeparator();

 

                        System.out.println("1) Effettua il login");

                        System.lineSeparator();

 

                        System.out.println("2) Torna al menu principale");

                        System.lineSeparator();

                       

                        STAMPA_FRASE_SCEGLI();

                       

                }

       

                public static void STAMPA_MENU_LOGOUT() {

 

                        System.out.println("--- MENU LOGOUT ---");

                        System.lineSeparator();

 

                        System.out.println("1) Effettua il loguot");

                        System.lineSeparator();

 

                        System.out.println("2) Torna al menu principale");

                        System.lineSeparator();

                       

                        STAMPA_FRASE_SCEGLI();

                       

                }

                       

                public static void STAMPA_MENU_MODIFICA_DATI() {

 

                        // 1) nome 2) cognome 3) email

                        // 4) password 5) Descrizione 6) Cv 7) Foto 8)Profilo

                       

                        System.out.println("--- MENU MODIFICA I TUOI DATI ---");

                        System.lineSeparator();

 

                        System.out.println("1) Cambia il nome");

                        System.lineSeparator();

                       

                        System.out.println("2) Cambia il cognome");

                        System.lineSeparator();

                       

                        System.out.println("3) Cambia la mail");

                        System.lineSeparator();

                       

                        System.out.println("4) Cambia la password");

                        System.lineSeparator();

                       

                        System.out.println("5) Cambia la descrizione");

                        System.lineSeparator();

 

                        System.out.println("6) Cambia il tuo Cv");

                        System.lineSeparator();

                       

                        System.out.println("7) Cambia la tua foto");

                        System.lineSeparator();

                       

                        System.out.println("8) Cambia il profilo");

                        System.lineSeparator();

                       

                        System.out.println("9) Cancella il tuo profilo");

                        System.lineSeparator();

                                                                      

                        System.out.println("10) Torna al menu principale");

                        System.lineSeparator();

                       

                        STAMPA_FRASE_SCEGLI();

                       

                }

 

                public static void STAMPA_MENU_RICERCA() {

 

                        System.out.println("--- MENU RICERCA ---");

                        System.lineSeparator();

 

                        System.out.println("1) Effettua la ricerca per nome e cognome");

                        System.lineSeparator();

 

                        System.out.println("2) Torna al menu principale");

                        System.lineSeparator();

                       

                        STAMPA_FRASE_SCEGLI();

                       

                }

               

                public static void STAMPA_MENU_ESCI() {

 

                        System.out.println("--- MENU ESCI DA FACEDOOR ---");

                        System.lineSeparator();

 

                        System.out.println("1) Esci");

                        System.lineSeparator();

 

                        System.out.println("2) Torna al menu principale");

                        System.lineSeparator();

                       

                        STAMPA_FRASE_SCEGLI();

                       

                }

               

        }

       

        /**

         * Classe dove ci sono procedure varie che possono servire in

         * vari progetti

         *

    */

        public static class UTILITY {

 

                /**

                 * Ritorna il path di un file passato come argomento (se il file esiste)

                 * <p>

                 *

                 * @param  Nome del file

                 * @return Path del file

                 */

                public static String GET_PATH_CSV(String Name_CSV){

               

                       

                       

                          File f = null;

                      String path = "-1";

                      boolean bool = false;

                     

                      try{

                         // create new files

                         f = new File(Name_CSV);

                        

                         // returns true if the file exists

                         bool = f.exists();

                        

                         // if file exists

                         if(bool)

                         {

                            // get absolute path

                            path = f.getAbsolutePath();

                           

                         }

                      }catch(Exception e){

                         // if any error occurs

                          return("-1");

                          //return(e.getMessage());

                          

                      }

 

                      return path;

                       

                       

                }

                       

                public static String LEGGI_USER_LINE(String FraseInput){

                       

                        /**

                         * Stampa a video la stringa passata come argomento e ritorna la risposta dell'utente ( alla presumibile domanda posta)

                         * <p>

                         *

                         * @param  String Stinga da stampare

                         * @return String Stringa che ha digitato l'utente

                         */

                       

                        System.out.println(FraseInput);

                       

                        BufferedReader br;

                        String UserInput="";

 

                        br = new BufferedReader(new InputStreamReader(System.in));

 

                        try {

                               UserInput = br.readLine();

                        }

                        catch (Exception ioe) {

                               System.out.println("IO error trying to read your input." + ioe);

                               System.exit(1);

                        }

                       

                        if (UserInput.isEmpty()==true) {

                               System.out.println("Nessun input in ingresso, " + FraseInput );

                               LEGGI_USER_LINE(FraseInput);

                        }

                        return UserInput;

                }

 

                /**

                 * Stampa a video la stringa passata come argomento e ritorna la risposta dell'utente sotto forma di intero ( alla presumibile domanda posta)

                 * <p>

                 * Visualizza un errore e chiede un numero finche non si inserisce un dato corretto

                 *

                 *

                 * @param  String Stinga da stampare

                 * @return Int che ha digitato l'utente

                 */

                public static int LEGGI_INT_USER_LINE(){

 

                                              

                        int OUT=-1;

                        boolean IsANumber=false;

                        String UserInput="";

                        do{

                               BufferedReader br;

                               br = new BufferedReader(new InputStreamReader(System.in));

                               try {

                                       UserInput = br.readLine();

                               } catch (IOException e) {

                                       e.printStackTrace();

                               }

                               IsANumber=UTILITY.IS_NUMERIC(UserInput);

                               if (IsANumber == false)System.out.println("Per favore inserisci un numero");

                        }while(IsANumber==false);

                        OUT= Integer.parseInt(UserInput);

                       

                        return OUT;

                }

               

                /**

                 * Stampa a video la stringa "Premi un tasto per ritornare al menų principale"

                 * ed aspetta che l'utente prema un tasto, NON ritorna niente

                 */

                public static void PREMI_UN_TASTO_PER_MENU_PRINCIPALE(){

               

                       

                       

                        System.out.println("Premi un tasto per ritornare al menų principale");

                        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

                       

                        try

                        {

                               br.readLine();

                        }

                        catch (IOException e)

                        {

                               e.printStackTrace();

                        }

               

                       

                }

               

                /**

                 * "pulisce" lo schermo semplicemente stampando

                 * una serie di nuove righe.

                 */

                public static void CLEAR_CONSOLE() {

                       

                       

                       

                       

                        for (int i =0; i <RowCl ; i++){

                               System.out.println("");

                        }

                }

               

                public void STAMPA_LINEA_SEPARATRICE(){

                        System.lineSeparator();

                        System.out.println("_____________________________________________________________________");

                        System.lineSeparator();

                }

       

                /**

                 * Funzione che mi ritorna in base al menu la possibilita di accedere

                 * ad una specifica opzione

                 *

                 * @param  Menu scelto dall'utente

                 * @return Boolean che mi dice se l'utente ha l'accesso a una particolare opzione

                 *

                 */

                public static boolean ERRORE_SCELTA_UTENTE(int N_menuScelto) {

                       

                       

                       

                       

                        // 1 Registrati - 2 Login - 3 Logout - 4 Modifica dati

                        // 5 Ricerca - 6 Esci

                       

                        if (UTENTE.GET_ID() == 0) // Utente non loggato

                        {

                               if ((id_menu==0)&&(N_menuScelto==3)) return true; // Logout ( NON PUOI ACCEDERE SE NN LOGGATO)

                               if ((id_menu==0)&&(N_menuScelto==4)) return true; // Modifica dati ( NON PUOI ACCEDERE SE NN LOGGATO)

                              

                        }else // Utente loggato

                        {

                               if ((id_menu==0)&&(N_menuScelto==1)) return true; // Registrati ( NON PUOI ACCEDERE SE LOGGATO)

                               if ((id_menu==0)&&(N_menuScelto==2)) return true; // Login ( NON PUOI ACCEDERE SE LOGGATO)

                        }

 

                        return false;

 

                }

 

                /**

                 * Funzione che mi stampa a video in base all numero di errore la stringa appropriata

                 *

                 * @param  Int Numero di errore

                 *

                 */

                public static void RITORNA_MENU_CON_MESSAGGIO_ERRORE(int N_errore) {

                       

                       

                       

                        switch (N_errore) {

 

                        case 1: {

                               System.out.println("Non puoi accedere a questa sezione.Registrati ed effettua il login .... ");

                               break;

                        }

 

                        case 2: {

                               System.out.println("C'č stato un errore imprevisto .... ");

                               break;

                        }

                        }

 

                        UTILITY.PREMI_UN_TASTO_PER_MENU_PRINCIPALE();

                        SET_MENU_ID(0);

                        ESEGUI_MENU();

                         

                }

               

                /**

                 * Funzione che mi controlla che la stringa immessa sia un numero

                 *

                 * @param  Int Numero di errore

                 * @return Boolean

                 */

                public static boolean IS_NUMERIC(String str

                { 

                       

                       

                       

                  try 

                  { 

                    double d = Double.parseDouble(str); 

                  } 

                  catch(NumberFormatException nfe

                  { 

                    return false

                  } 

                  return true

                }

               

                /**

                 * Funzione che mi controlla che la stringa immessa sia una mail valida

                 *

                 * @param  Stringa formato mail

                 * @return Boolean

                 */

                public static boolean IS_VALID_EMAIL_ADRESS(String email) {

                       

                       

                       

                   String ePattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";

                   java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);

                   java.util.regex.Matcher m = p.matcher(email);

                   return m.matches();

            }

                 

                /**

                 * Funzione che mi aggiorna (incrementando di uno) un numero passato come argomento nel file TXT specificato

                 *

                 * @param  Path del file txt da aggiornare

                 * @param  Int Numero da prima di essere aggiornato e da salvare

                 * @return Int il numero salvato

                 */

                public static String AGGIONA_NUMERO_UTENTI_REGISTRATI(String Path, String Numero){

                       

                       

                       

                        int x = Integer.parseInt(Numero);

                        x=x+1;

                       

                        PrintWriter writer=null;

                        try {

                               writer = new PrintWriter(Path, "UTF-8");

                        } catch (FileNotFoundException | UnsupportedEncodingException e) {

                               // TODO Auto-generated catch block

                               e.printStackTrace();

                        }

                        writer.println(x);

                        writer.close();

                       

                       

                        return Integer.toString(x);

                       

                }

       

       

        }

       

        /**

     *

     * Classe che gestisce inserimento, modifica e cancellazione di un utente.

     */

        public static class GESTISCI_REGISTRAZIONE{

               

                /**

             * Tramite console vengono richieste le informazioni necessarie

             * per Registare il profilo, che viene salvato sul file cvs

            */

                public static void GET_INPUT(){

               

                       

                       

                        //Per poter creare un nuovo profilo, l’utente deve registrarsi a FACEDOOR inserendo Foto del

                        //proprio Viso, Nome, Cognome, UserID, Email valida e Password, Breve descrizione del proprio

                        //profilo e Curriculum.

                       

                        String[] ARR_studente1= new String [10];

               

                ARR_studente1[0] = NextIDUser;

                ARR_studente1[1] = UTILITY.LEGGI_USER_LINE("Inserisci il tuo nome");

                ARR_studente1[2] = UTILITY.LEGGI_USER_LINE("Inserisci il tuo cognome");

               

                String UserID="";

                        Boolean IsValid_UserID=false;

                        do{

                               UserID=  UTILITY.LEGGI_USER_LINE("Inserisci il tuo User ID");

                               IsValid_UserID=IS_USER_VALID(UserID);

                               if (IsValid_UserID == false)System.out.println("Username giā scelto, per favore scegli un altro UserID");

                        }while(IsValid_UserID==false);

                ARR_studente1[3] = UserID;

               

                String Email="";

                        Boolean IsValid_EMail=false;

                        do{

                               Email=  UTILITY.LEGGI_USER_LINE("Inserisci la tua e-mail");

                               IsValid_EMail=UTILITY.IS_VALID_EMAIL_ADRESS(Email);

                               if (IsValid_EMail == false)System.out.println("Formato email non corretto !");

                        }while(IsValid_EMail==false);

                ARR_studente1[4] = Email;

                               

                ARR_studente1[5] = UTILITY.LEGGI_USER_LINE("Inserisci il tua password");

                ARR_studente1[6] = UTILITY.LEGGI_USER_LINE("Inserisci il una breve descrizione");

                ARR_studente1[7] = UTILITY.LEGGI_USER_LINE("Inserisci l'URL del tuo CV");

                ARR_studente1[8] = UTILITY.LEGGI_USER_LINE("Inserisci l'URL della tua foto");

               

                String Profilo="";

                        Boolean IsValid_Profilo=false;

                        do{

                               Profilo = UTILITY.LEGGI_USER_LINE("Scegli profilo pubblico (0) o profilo privato (1)");

                               if ((Profilo.equals("0")==true) || (Profilo.equals("1") ==true)) IsValid_Profilo=true;

                               if (IsValid_Profilo == false)System.out.println("Input non corretto!");

                        }while(IsValid_Profilo==false);

                ARR_studente1[9] = Profilo;

 

               

                Student NuovoUtente= new Student(ARR_studente1);

                        List_students.add(NuovoUtente);

                        CsvFileWriter.UPLOAD_Csv(fileNamefullpath, List_students);

 

                        NextIDUser=UTILITY.AGGIONA_NUMERO_UTENTI_REGISTRATI(fileCountIDfullpath,NextIDUser);

                       

                        System.out.println("");

                        System.lineSeparator();

                        System.out.println("Grazie per esserti registato a Facedoor");

                        System.lineSeparator();

 

                        UTILITY.PREMI_UN_TASTO_PER_MENU_PRINCIPALE();

                        SET_MENU_ID(0);

                        ESEGUI_MENU();

                       

                }

       

                /**

             * Tramite console vengono richieste le informazioni necessarie

             * per Modificare il profilo, che viene di nuovo salvato sul file cvs

             * tramite un UPLOAD

            */

                public static void MODIFICA_DATI(int menu){

 

                       

 

                       

                        // 1) nome 2) cognome 3) email

                        // 4) password 5) Descrizione 6) Cv 7) Foto 8)Profilo

                       

                        String x="";

                       

                        for (Student student : List_students) {

                              

                               if (UTENTE.ID_utente==student.Get_ID()){

                                      

                                       switch (menu) {

 

                                       // nome

                                       case 1:

                                                x = UTILITY.LEGGI_USER_LINE("Inserisci il tuo nuovo nome ('" + student.Get_Nome() + "' quello vecchio )");   

                                               student.Set_Nome(x);

                                               break;

                                      

                                       // Cognome

                                       case 2:

                                                x = UTILITY.LEGGI_USER_LINE("Inserisci il tuo nuovo Cognome ('" + student.Get_Cognome() + "' quello vecchio )");        

                                               student.Set_Cognome(x);

                                               break;

                                              

                                       // email

                                       case 3:

                                              

                                               String Email="";

                                               Boolean IsValid_EMail=false;

                                               do{

                                                       Email= UTILITY.LEGGI_USER_LINE("Inserisci la tua nuova Email ('" + student.Get_email() + "' quella vecchia )");

                                                       IsValid_EMail=UTILITY.IS_VALID_EMAIL_ADRESS(Email);

                                                       if (IsValid_EMail == false)System.out.println("Formato email non corretto !");

                                               }while(IsValid_EMail==false);

                                               student.Set_email(Email);

                                               break;

                                      

                                       // password

                                       case 4:

                                                x = UTILITY.LEGGI_USER_LINE("Inserisci la tua nuova password ('" + student.Get_Password() + "' quella vecchia )");       

                                               student.Set_Password(x);

                                               break;

                                      

                                       // Descrizione

                                       case 5:

                                       x = UTILITY.LEGGI_USER_LINE("Inserisci la tua nuova descrizione ('" + student.Get_Descrizione() + "' quella vecchia )");    

                                       student.Set_Descrizione(x);

                                       break;

                                      

                                       // Cv

                                       case 6:

                                       x = UTILITY.LEGGI_USER_LINE("Inserisci il tuo nuovo cv ('" + student.Get_CV() + "' quella vecchia )");    

                                       student.Set_CV(x);

                                       break;

 

                                       // Foto

                                       case 7:

                                       x = UTILITY.LEGGI_USER_LINE("Inserisci la tua nuova foto ('" + student.Get_Foto() + "' quella vecchia )");

                                       student.Set_Foto(x);

                                       break;

                                      

                                       // Profilo

                                       case 8:

                                        String Profilo="";

                                               Boolean IsValid_Profilo=false;

                                               do{

                                                       Profilo = UTILITY.LEGGI_USER_LINE("Inserisci il tuo profilo pubblico [0] o profilo privato [1] ('" + student.Get_Profilo() + "' quello vecchio )");

                                                       if ((Profilo.equals("0")==true) || (Profilo.equals("1") ==true)) IsValid_Profilo=true;

                                                       if (IsValid_Profilo == false)System.out.println("Input non corretto!");

                                               }while(IsValid_Profilo==false);

                                               student.Set_Profilo(Profilo); 

                                       break;

                                                              

                               }

                       

                        }

 

                        CsvFileWriter.UPLOAD_Csv(fileNamefullpath, List_students);

 

                        System.out.println("");

                        System.lineSeparator();

                        System.out.println("Modifica effettuata con successo!");

                        System.lineSeparator();

 

                        UTILITY.PREMI_UN_TASTO_PER_MENU_PRINCIPALE();

                        ESEGUI_MENU();

                       

                }

       

               

        }      

       

                /**

                 * Cancellazione del profilo e aggiornamento del file csv

                 *

            */

                public static void CANCELLA_UTENTE(){

                        for (Iterator<Student> iter = List_students.listIterator(); iter.hasNext(); ) {

                            Student a = iter.next();

                            if (UTENTE.ID_utente==a.Get_ID()) {

                                iter.remove();

                            }

                        }

 

                        CsvFileWriter.UPLOAD_Csv(fileNamefullpath, List_students);

 

                        System.out.println("");

                        System.lineSeparator();

                        System.out.println("Cancellazione utente effettuata con successo!");

                        System.lineSeparator();

 

                        UTENTE.SET_ID(0);

                        SET_MENU_ID(0);

                       

                        UTILITY.PREMI_UN_TASTO_PER_MENU_PRINCIPALE();

                        ESEGUI_MENU();

                       

                }

}

       

        public static class GESTISCI_LOGIN{

               

 

                /**

                 * Classe che gestisce il login che quindi setta

                 * o resetta (mettendo a zero) l'ID della classe

                 * UTENTE

                 *

            */

               

                public static void GET_INPUT(){

 

 

                        /**

                         * Funzione che chiede UserID e password tramite console

                         * se queste corrispondono ad un utente registrato settano

                         * l'ID della classe UTENTE con l'ID dell'utente

                         *

                    */

                       

                        String UserID=  UTILITY.LEGGI_USER_LINE("Inserisci la tua UserID");

                        String Password=  UTILITY.LEGGI_USER_LINE("Inserisci la tua Password");

 

                        int IDUtente=RETURN_ID_LOGIN_VALID(UserID,Password);

                       

                        if (IDUtente!=-1){

                              

                               System.lineSeparator();

                               System.out.println("Login avvenuto con successo");

                               System.lineSeparator();

                                                      

                               UTENTE.SET_ID(IDUtente);

                              

                               UTILITY.PREMI_UN_TASTO_PER_MENU_PRINCIPALE();         

                               SET_MENU_ID(0);

                               ESEGUI_MENU();

                        }

                        else{

                               System.out.println("UserID o Password errati!");

                               UTILITY.PREMI_UN_TASTO_PER_MENU_PRINCIPALE();         

                               ESEGUI_MENU();

                        }

                       

                       

                       

                }

               

                public static void EFFETTUA_LOG_OUT(){

                       

                        /**

                         * Classe che gestisce il logout che quindi resetta (mettendo a zero) l'ID della classe

                         * UTENTE e scrive sulla console il messaggio di logout avvenuto con successo

                         *

                    */

                       

                        System.lineSeparator();

                        System.out.println("Logout avvenuto con successo");

                        System.lineSeparator();

                                              

                        UTENTE.SET_ID(0);

                       

                        UTILITY.PREMI_UN_TASTO_PER_MENU_PRINCIPALE();         

                        SET_MENU_ID(0);

                        ESEGUI_MENU();

                       

                }

               

        }

       

        public static class GESTISCI_RICERCA{

 

        /**

         * <p>In questa funzione di ricerca si richiede tramite console

         * il nome e cognome. Poi attraverso la classe StringSimilarity

         * si confronta con la lista degli utenti di facedoor(List_students). Se la

         * percentuale di similitudine č maggiore del 70% stampo i vari risultati.</p>

         *

         * <p>Per ogni utente trovato, se pubblico, viene stampato il suo ID numerico,

         * che permette nella domanda successiva (Quale profilo si vuole vedere?)

         * di selezionare il profilo da visualizzare</p>

         *

         * <p>Una volta visualizzato un profilo si ritorna al menu principale</p>

         *

         */

       

                public static void GET_INPUT(){

 

                        String Nome =  UTILITY.LEGGI_USER_LINE("Inserisci il nome");

                        String Cognome =  UTILITY.LEGGI_USER_LINE("Inserisci il cognome");

                       

                        int i = 0;

 

                        for (Student student : List_students) {

                              

                               String Row_Nome =student.Get_Nome();

                               String Row_Cognome= student.Get_Cognome();

 

                               boolean IsStringMatch=false;

                              

                               // Controllo se le due stringhe sono simili

                               double NomeSimile= StringSimilarity.similarity(Nome,Row_Nome);

                               if (NomeSimile>0.70){

                                       IsStringMatch=true;

                               }

 

                               double CognomeSimile= StringSimilarity.similarity(Cognome,Row_Cognome);

                               if (CognomeSimile>0.70){

                                       IsStringMatch=true;

                               }

 

                               // VISUALIZZA LE PERCENTUALI MATCH

                               //System.out.println(Nome + " e " +  Row_Nome  + " sono simili al " + NomeSimile + "%");

                               //System.out.println(Cognome + " e " +  Row_Cognome  + " sono simili al " + CognomeSimile + "%");

 

                              

                               // Se si, stampo i risultati (con il numeretto della scelta se pubblico, senza scelta

                               // se č privato)

                              

                               if (IsStringMatch==true){

 

                                       if (i==0){

                                               System.out.println("I risultati della ricerca sono:");

                                               i++;

                                       }

                                      

                                       // Scegli profilo pubblico (0) o profilo privato (1)

                                       String Line="";

                                       if (student.Get_Profilo().equals("0")){ // Profilo pubblico

                                               Line=student.Get_ID() + ") " + student.Get_Nome() + " " + student.Get_Cognome();

                                                                                             

                                       }else{

                                               Line= student.Get_Nome() + " " + student.Get_Cognome() + " (profilo privato!)";                                           

                                       }

                                      

                                       System.out.println(Line);

                                      

                               }

 

 

                        }

 

                        if (i==0){

                               System.out.println("Spiacente! La persona ricercata non č presente nei nostri archivi. Riprova fra qualche settimana. Puoi ricercare un nuovo profilo o creare il tuo profilo personale registrandoti a FACEDOOR nell'apposito menu");

                        }

                        else

                        {

                              

                               String Scelta = UTILITY.LEGGI_USER_LINE("Scegli che profilo visualizzare, oppure premi una qualsiasi lettera per tornare al menu di ricerca.");;

                              

                               if (UTILITY.IS_NUMERIC(Scelta)==true){

                                       for (Student student : List_students) {

                                               int Int_Scelta = Integer.parseInt(Scelta);

                                               if (student.Get_ID()==(int)Int_Scelta){

                                                      

                                                       if (student.Get_Profilo().equals("0")==true)

                                                       {

                                                               System.out.println(student.RITORNA_PROFILO());

                                                       }else{

                                                               System.out.println("Biricchino! stai cercando di accedere ad un profilo privato!");

                                                       }

                                               }

                                       }

                               }else{

                                       ESEGUI_MENU();

                               }

                        }

                       

                        UTILITY.PREMI_UN_TASTO_PER_MENU_PRINCIPALE();

                        ESEGUI_MENU();

                       

                }

 

       

        }

 

 

}