191 lines
8.6 KiB
C#
191 lines
8.6 KiB
C#
using System.Text.RegularExpressions;
|
|
namespace Rubrica_Miglioria;
|
|
|
|
class Program {
|
|
static void Main(string[] args) {
|
|
Console.Clear();
|
|
const int DIMENSIONE_RUBRICA = 100;
|
|
int scelta = 0;
|
|
string nome = "";
|
|
string numeroditelefono;
|
|
bool check = false;
|
|
bool check2 = false;
|
|
bool check3 = false;
|
|
int contatore = 0;
|
|
string nomeesatto;
|
|
string nomeapprossimato;
|
|
string messaggio;
|
|
string[] nomi = new string[DIMENSIONE_RUBRICA];
|
|
string[] numeriditelefono = new string[DIMENSIONE_RUBRICA];
|
|
Voce singolavoce = null;
|
|
Voce[] voci = new Voce[DIMENSIONE_RUBRICA];
|
|
Rubrica rubrica = null;
|
|
do {
|
|
Console.WriteLine("1)Aggiungi nuova voce in rubrica");
|
|
Console.WriteLine("2)Ricerca esatta per nome");
|
|
Console.WriteLine("3)Ricerca approsimata per nome");
|
|
Console.WriteLine("4)Stampa completa rubrice");
|
|
Console.WriteLine("5)Esci");
|
|
scelta = Convert.ToInt32(Console.ReadLine());
|
|
switch (scelta) {
|
|
case 1:
|
|
if (contatore > DIMENSIONE_RUBRICA - 1) {
|
|
Console.WriteLine($"Limite di {DIMENSIONE_RUBRICA} voci nella rubrica raggiunta");
|
|
}
|
|
else {
|
|
do {
|
|
check2 = false;
|
|
Console.WriteLine("Inserisci il nome della voce che andrà nella rubrica telefonica");
|
|
nome = Console.ReadLine().ToLower();
|
|
nomi[contatore] = nome;
|
|
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 (VerificaCorrettezza(nome, true)) {
|
|
Console.WriteLine("il nome non può contenere dei numeri");
|
|
}
|
|
if (check3 == true) {
|
|
Console.WriteLine("Questo nome esiste già nelle rubrica");
|
|
}
|
|
}
|
|
while (nome.Length > 40 || check2 == true || check3 == true);
|
|
do {
|
|
check2 = false;
|
|
Console.WriteLine("Inserisci il numero di telefono della voce che andrà nella rubrica telefonica");
|
|
numeroditelefono = Console.ReadLine();
|
|
numeroditelefono = numeroditelefono.ToLower();
|
|
numeriditelefono[contatore] = numeroditelefono;
|
|
check2 = VerificaCorrettezza(numeroditelefono, false);
|
|
check3 = VerificaDoppione(numeroditelefono, numeriditelefono, contatore);
|
|
if (numeroditelefono.Length > 20) {
|
|
Console.WriteLine("il numero di telefono non può essere lungo più di 20 caratteri");
|
|
}
|
|
if (check2 == true) {
|
|
Console.WriteLine("il numero di telefono deve contenere solo numeri");
|
|
}
|
|
if (check3 == true) {
|
|
Console.WriteLine("Questo numero di telefono esiste già nelle rubrica");
|
|
}
|
|
}
|
|
while (numeroditelefono.Length > 20 || check2 == true || check3 == true);
|
|
singolavoce = new Voce(nome, numeroditelefono);
|
|
voci[contatore] = singolavoce;
|
|
rubrica = new Rubrica(voci);
|
|
contatore++;
|
|
check = true;
|
|
}
|
|
break;
|
|
case 2:
|
|
if (check == false) {
|
|
Console.WriteLine("devi prima aggiungere una voce in rubrica");
|
|
}
|
|
else {
|
|
do {
|
|
check2 = false;
|
|
Console.WriteLine("Inserisci il nome esatto che stai cercando");
|
|
nomeesatto = Console.ReadLine();
|
|
nomeesatto = nomeesatto.ToLower();
|
|
check2 = VerificaCorrettezza(nome, true);
|
|
if (nomeesatto.Length > 40) {
|
|
Console.WriteLine("il nome non può essere lungo più di 40 caratteri");
|
|
}
|
|
if (check2 == true) {
|
|
Console.WriteLine("il nome non può contenere dei numeri");
|
|
}
|
|
}
|
|
while (nomeesatto.Length > 40 || check2 == true);
|
|
messaggio = rubrica.EsistenzaNomeEsatto(nomeesatto, contatore);
|
|
Console.WriteLine(messaggio);
|
|
}
|
|
break;
|
|
case 3:
|
|
if (check == false) {
|
|
Console.WriteLine("devi prima aggiungere una voce in rubrica");
|
|
}
|
|
else {
|
|
do {
|
|
check2 = false;
|
|
Console.WriteLine("Inserisci il nome approsimato che stai cercando");
|
|
nomeapprossimato = Console.ReadLine();
|
|
nomeapprossimato = nomeapprossimato.ToLower();
|
|
check2 = VerificaCorrettezza(nome, true);
|
|
if (nomeapprossimato.Length > 40) {
|
|
Console.WriteLine("il nome non può essere lungo più di 40 caratteri");
|
|
}
|
|
if (check2 == true) {
|
|
Console.WriteLine("il nome non può contenere dei numeri");
|
|
}
|
|
}
|
|
while (nomeapprossimato.Length > 40 || check2 == true);
|
|
rubrica.EsistenzaNomeApprosimato(nomeapprossimato, contatore);
|
|
}
|
|
break;
|
|
case 4:
|
|
if (check == false) {
|
|
Console.WriteLine("devi prima aggiungere una voce in rubrica");
|
|
}
|
|
else {
|
|
rubrica.StampaRubricaCompleta(contatore);
|
|
}
|
|
break;
|
|
case 5:
|
|
break;
|
|
default:
|
|
Console.WriteLine("Questa scelta non esiste");
|
|
break;
|
|
}
|
|
}
|
|
while (scelta != 5);
|
|
Console.WriteLine("Chiusura effettuta con successo");
|
|
}
|
|
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 && !(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}$";
|
|
|
|
if (p_tipologia) {
|
|
check = Regex.IsMatch(p_stringa, NOME);
|
|
}
|
|
else {
|
|
check = Regex.IsMatch(p_stringa, TELEFONO);
|
|
}
|
|
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;
|
|
|
|
}
|
|
}
|