186 lines
7.6 KiB
C#
186 lines
7.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 rubricaNonVuota = false;
|
|
bool error;
|
|
int contatore = 0;
|
|
string ricerca;
|
|
string[] nomi = new string[DIMENSIONE_RUBRICA];
|
|
string[] numeriditelefono = new string[DIMENSIONE_RUBRICA];
|
|
Voce singolavoce = null;
|
|
Voce[] voci = new Voce[DIMENSIONE_RUBRICA];
|
|
Rubrica rubrica = new Rubrica(DIMENSIONE_RUBRICA);
|
|
do {
|
|
Console.WriteLine("1)Aggiungi nuova voce in rubrica");
|
|
Console.WriteLine("2)Ricerca esatta per nome");
|
|
Console.WriteLine("3)Ricerca approssimata per nome");
|
|
Console.WriteLine("4)Stampa completa rubrica");
|
|
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 {
|
|
string? message;
|
|
error = true;
|
|
Console.WriteLine("Inserisci il nome della voce che andrà nella rubrica telefonica");
|
|
nome = Console.ReadLine().ToLower();
|
|
|
|
message = Controllo(nome, true, contatore, rubrica);
|
|
|
|
if (message != null) {
|
|
Console.WriteLine(message);
|
|
}
|
|
else {
|
|
error = false;
|
|
}
|
|
|
|
}
|
|
while (error);
|
|
do {
|
|
string? message;
|
|
error = true;
|
|
Console.WriteLine("Inserisci il numero di telefono della voce che andrà nella rubrica telefonica");
|
|
numeroditelefono = Console.ReadLine();
|
|
|
|
message = Controllo(numeroditelefono, false, contatore, rubrica);
|
|
|
|
if (message != null) {
|
|
Console.WriteLine(message);
|
|
}
|
|
else {
|
|
error = false;
|
|
}
|
|
}
|
|
while (error);
|
|
|
|
rubrica.AddVocidellarubrica(new Voce(nome, numeroditelefono), contatore);
|
|
contatore++;
|
|
rubricaNonVuota = true;
|
|
}
|
|
break;
|
|
case 2:
|
|
if (rubricaNonVuota == false) {
|
|
Console.WriteLine("Devi prima aggiungere una voce in rubrica");
|
|
}
|
|
else {
|
|
do {
|
|
string? message;
|
|
error = true;
|
|
Console.WriteLine("Inserisci il nome esatto che stai cercando");
|
|
ricerca = Console.ReadLine().ToLower();
|
|
|
|
message = Controllo(ricerca, true, 0, rubrica); //lo zero serve per saltare il controllo per verificare se il nome esiste già o meno
|
|
|
|
if (message != null) {
|
|
Console.WriteLine(message);
|
|
}
|
|
else {
|
|
error = false;
|
|
}
|
|
}
|
|
while (error);
|
|
|
|
Console.WriteLine(rubrica.Ricerca(ricerca, true, contatore));
|
|
}
|
|
break;
|
|
case 3:
|
|
if (rubricaNonVuota == false) {
|
|
Console.WriteLine("Devi prima aggiungere una voce in rubrica");
|
|
}
|
|
else {
|
|
do {
|
|
string? message;
|
|
error = true;
|
|
Console.WriteLine("Inserisci il nome approssimato che stai cercando");
|
|
ricerca = Console.ReadLine().ToLower();
|
|
|
|
message = Controllo(ricerca, true, 0, rubrica); //lo zero serve per saltare il controllo per verificare se il nome esiste già o meno
|
|
|
|
if (message != null) {
|
|
Console.WriteLine(message);
|
|
}
|
|
else {
|
|
error = false;
|
|
}
|
|
}
|
|
while (error);
|
|
|
|
Console.WriteLine(rubrica.Ricerca(ricerca, false, contatore));
|
|
}
|
|
break;
|
|
case 4:
|
|
if (rubricaNonVuota == 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 effettuata con successo");
|
|
}
|
|
|
|
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 string? Controllo(string p_stringa, bool p_tipologia, int p_contatore, Rubrica p_rubrica) {
|
|
/*
|
|
tipologia=true -> Controllo validità nome
|
|
tipologia=false -> Controllo validità numero di telefono
|
|
*/
|
|
string? ritorno = null;
|
|
const int LUNGHEZZA_NOME = 40;
|
|
const int LUNGHEZZA_TELEFONO = 20;
|
|
if (p_stringa.Length > (p_tipologia ? LUNGHEZZA_NOME : LUNGHEZZA_TELEFONO)) {
|
|
ritorno = $"Il {(p_tipologia ? "nome" : "numero di telefono")} non può essere lungo più di {(p_tipologia ? LUNGHEZZA_NOME : LUNGHEZZA_TELEFONO)} caratteri";
|
|
}
|
|
if (VerificaCorrettezza(p_stringa, p_tipologia)) {
|
|
ritorno = p_tipologia ? "Il nome non può contenere numeri" : "Numero di telefono non valido";
|
|
}
|
|
if (p_contatore != 0) {
|
|
bool exit = false;
|
|
for (int i = 0; i < p_contatore && !exit; i++) {
|
|
exit = p_tipologia ? p_rubrica.GetVocidellarubrica()[i].GetNome().Contains(p_stringa) : p_rubrica.GetVocidellarubrica()[i].Getnumeroditelefono().Contains(p_stringa);
|
|
if (exit) {
|
|
ritorno = $"Questo {(p_tipologia ? "nome" : "numero di telefono")} esiste già nella rubrica.";
|
|
}
|
|
}
|
|
|
|
}
|
|
return ritorno;
|
|
}
|
|
}
|