diff --git a/Program.cs b/Program.cs index 19b970e..04e3ea4 100644 --- a/Program.cs +++ b/Program.cs @@ -7,7 +7,7 @@ class Program { const int DIMENSIONE_RUBRICA = 100; int scelta = 0; string nome = ""; - string numeroditelefono, tipologia; + string numeroditelefono; bool check = false; bool check2 = false; bool check3 = false; @@ -18,7 +18,7 @@ class Program { string[] nomi = new string[DIMENSIONE_RUBRICA]; string[] numeriditelefono = new string[DIMENSIONE_RUBRICA]; Voce singolavoce = null; - Voce[] vocidellarubrica = new Voce[DIMENSIONE_RUBRICA]; + Voce[] voci = new Voce[DIMENSIONE_RUBRICA]; Rubrica rubrica = null; do { Console.WriteLine("1)Aggiungi nuova voce in rubrica"); @@ -38,13 +38,14 @@ class Program { Console.WriteLine("Inserisci il nome della voce che andrà nella rubrica telefonica"); nome = Console.ReadLine().ToLower(); nomi[contatore] = nome; - tipologia = "nome"; - check2 = VerificaCorrettezza(nome, true); check3 = VerificaDoppione(nome, nomi, contatore); + if (contatore == 0) { + + } if (nome.Length > 40) { Console.WriteLine("il nome non può essere lungo più di 40 caratteri"); } - if (check2 == true) { + if (VerificaCorrettezza(nome, true)) { Console.WriteLine("il nome non può contenere dei numeri"); } if (check3 == true) { @@ -58,7 +59,6 @@ class Program { numeroditelefono = Console.ReadLine(); numeroditelefono = numeroditelefono.ToLower(); numeriditelefono[contatore] = numeroditelefono; - tipologia = "numero di telefono"; check2 = VerificaCorrettezza(numeroditelefono, false); check3 = VerificaDoppione(numeroditelefono, numeriditelefono, contatore); if (numeroditelefono.Length > 20) { @@ -73,8 +73,8 @@ class Program { } while (numeroditelefono.Length > 20 || check2 == true || check3 == true); singolavoce = new Voce(nome, numeroditelefono); - vocidellarubrica[contatore] = singolavoce; - rubrica = new Rubrica(vocidellarubrica); + voci[contatore] = singolavoce; + rubrica = new Rubrica(voci); contatore++; check = true; } @@ -89,7 +89,6 @@ class Program { Console.WriteLine("Inserisci il nome esatto che stai cercando"); nomeesatto = Console.ReadLine(); nomeesatto = nomeesatto.ToLower(); - tipologia = "nome"; check2 = VerificaCorrettezza(nome, true); if (nomeesatto.Length > 40) { Console.WriteLine("il nome non può essere lungo più di 40 caratteri"); @@ -113,7 +112,6 @@ class Program { Console.WriteLine("Inserisci il nome approsimato che stai cercando"); nomeapprossimato = Console.ReadLine(); nomeapprossimato = nomeapprossimato.ToLower(); - tipologia = "nome"; check2 = VerificaCorrettezza(nome, true); if (nomeapprossimato.Length > 40) { Console.WriteLine("il nome non può essere lungo più di 40 caratteri"); @@ -147,16 +145,15 @@ class Program { static bool VerificaDoppione(string p_stringa, string[] p_stringhe, int p_contatore) { bool check = false; if (p_contatore != 0) { - for (int i = 0; i < p_contatore; i++) { - if (p_stringhe[i] == p_stringa) { - check = true; - break; - } - } + for (int i = 0; i < p_contatore && !(p_stringhe[i] == p_stringa); i++) ; } return check; } static bool VerificaCorrettezza(string p_stringa, bool p_tipologia) { + /* + tipologia=true -> Controllo validità nome + tipologia=false -> Controllo validità numero di telefono + */ bool check; const string NOME = "[1-9]+"; const string TELEFONO = "^(?:\\(?\\+?\\d{1,3}\\)?|\\(?00\\d{1,3}\\)?)?[\\s-]?\\d{3}[\\s-]\\d{3}[\\s-]\\d{4}$"; @@ -169,4 +166,25 @@ class Program { } return check; } + + static bool Controllo(string p_stringa, bool p_tipologia, int p_contatore) { + bool ritorno = true; + if (p_stringa.Length > (p_tipologia ? 40 : 20)) { + Console.WriteLine($"Il {(p_tipologia ? "nome" : "numero di telefono")} non può essere lungo più di {(p_tipologia ? "40" : "20")} caratteri"); + ritorno = false; + } + if (VerificaCorrettezza(p_stringa, p_tipologia)) { + Console.WriteLine(p_tipologia ? "Il nome non può contenere numeri" : "Numero di telefono non valido"); + ritorno = false; + } + if (p_contatore != 0) { + if (check3 == true) { + Console.WriteLine("Questo nome esiste già nelle rubrica"); + } + } + + + return ritorno; + + } } diff --git a/Voce.cs b/Voce.cs index 23e3ed0..feb8af8 100644 --- a/Voce.cs +++ b/Voce.cs @@ -1,28 +1,23 @@ namespace Rubrica_Miglioria; class Voce { -string nome; - string numeroditelefono; + string nome; + string numeroditelefono; - public Voce(string p_nome, string p_numeroditelefono) - { - this.nome = p_nome; - this.numeroditelefono = p_numeroditelefono; - } - public void SetNome(string p_nome) - { - this.nome = p_nome; - } - public void SetNumeroditelefono(string p_numeroditelefono) - { - this.numeroditelefono = p_numeroditelefono; - } - public string GetNome() - { - return this.nome; - } - public string Getnumeroditelefono() - { - return this.numeroditelefono; - } + public Voce(string p_nome, string p_numeroditelefono) { + this.nome = p_nome; + this.numeroditelefono = p_numeroditelefono; + } + public void SetNome(string p_nome) { + this.nome = p_nome; + } + public void SetNumeroditelefono(string p_numeroditelefono) { + this.numeroditelefono = p_numeroditelefono; + } + public string GetNome() { + return this.nome; + } + public string Getnumeroditelefono() { + return this.numeroditelefono; + } } \ No newline at end of file