From a332114a307813139651f9b61d78b0666d77239c Mon Sep 17 00:00:00 2001 From: IlarioXXI <163922523+IlarioXXI@users.noreply.github.com> Date: Mon, 24 Jun 2024 15:43:32 +0200 Subject: [PATCH] first commit --- ToDoList/Entities/Attivita.cs | 9 ++ ToDoList/Interfaces/IAttivitaRepository.cs | 9 ++ ToDoList/Interfaces/ICrud.cs | 48 +++++++++ ToDoList/Program.cs | 65 ++++++++++++ ToDoList/Repository/AttivitaRepository.cs | 110 +++++++++++++++++++++ ToDoList/ToDoList.csproj | 10 ++ 6 files changed, 251 insertions(+) create mode 100644 ToDoList/Entities/Attivita.cs create mode 100644 ToDoList/Interfaces/IAttivitaRepository.cs create mode 100644 ToDoList/Interfaces/ICrud.cs create mode 100644 ToDoList/Program.cs create mode 100644 ToDoList/Repository/AttivitaRepository.cs create mode 100644 ToDoList/ToDoList.csproj diff --git a/ToDoList/Entities/Attivita.cs b/ToDoList/Entities/Attivita.cs new file mode 100644 index 0000000..2c84cfe --- /dev/null +++ b/ToDoList/Entities/Attivita.cs @@ -0,0 +1,9 @@ +namespace ToDoList.Entities +{ + public class Attivita + { + public int? Key { get; } + public string? NomeAttivita { get; set; } + public bool IsCompleted { get; set; } + } +} diff --git a/ToDoList/Interfaces/IAttivitaRepository.cs b/ToDoList/Interfaces/IAttivitaRepository.cs new file mode 100644 index 0000000..514b5c1 --- /dev/null +++ b/ToDoList/Interfaces/IAttivitaRepository.cs @@ -0,0 +1,9 @@ +using ToDoList.Entities; + +namespace ToDoList.Interfaces +{ + public interface IAttivitaRepository : ICrud + { + void Print(string message); + } +} diff --git a/ToDoList/Interfaces/ICrud.cs b/ToDoList/Interfaces/ICrud.cs new file mode 100644 index 0000000..444408a --- /dev/null +++ b/ToDoList/Interfaces/ICrud.cs @@ -0,0 +1,48 @@ +namespace ToDoList.Interfaces +{ + public interface ICrud + { + TEntity Entity { get; } + Dictionary EntityDictionary { get; set; } + /// + /// Questo metodo aggiunge a un dictionary un'attività con la possibilità di avere la chiave che incrementa di uno ogni volta che aggiungiamo una nuova attività. Al termine stampa l'attività aggiunta. + /// + /// Attivita entity = l'attività da aggiungere al dictionary + /// Se la chiave dell'attività da aggiungere esiste già lancia un'eccezione. + void Create(TEntity entity); + /// + /// + /// + /// Attività entity = La nuova attività aggiornata. + /// key = la chiave dell'attività da aggiornare. + /// /// Se non trova la chiave lancia l'eccezione + void Update(TEntity entity,TKey key); + /// + /// Questo metodo aggiunge a un dictionary un'attività con la possibilità di avere la chiave che incrementa di uno ogni volta che aggiungiamo una nuova attività. Al termine stampa l'attività aggiunta. + /// + /// Attivita entity = l'attività da aggiungere al dictionary + /// Se la chiave dell'attività da aggiungere esiste già lancia un'eccezione. + bool Delete(TKey key); + + /// + /// Questo metodo aggiunge gli elementi del dictionary di partenza in una lista che poi ritorna all'utente. + /// + /// Se la lista è vuota lancia un'eccezione. + void GetAll(); + /// + /// Cambia lo stato dell'attività da non svolta a svolta. + /// + /// + void ChangeStatus(TKey key); + /// + /// Metodo che stampa la lista delle attività completate. + /// + void GetCompletedActivitiesList(); + /// + /// Metodo che stampa la lista delle attività da completare. + /// + void GetNotCompleteddActivitiesList(); + + + } +} diff --git a/ToDoList/Program.cs b/ToDoList/Program.cs new file mode 100644 index 0000000..99d9ce0 --- /dev/null +++ b/ToDoList/Program.cs @@ -0,0 +1,65 @@ +using ToDoList.Entities; +using ToDoList.Interfaces; +using ToDoList.Repository; + +namespace ToDoList +{ + internal class Program + { + internal static IAttivitaRepository attivitaRepository = new AttivitaRepository(); + static void Main(string[] args) + { + try + { + Console.WriteLine("Inserisci 1 per inserire una nuova attività."); + Console.WriteLine("Inserisci 2 per osservare tutte le attività."); + Console.WriteLine("Inserisci 3 per rimuovere un'attività."); + Console.WriteLine("Inserisci 4 per cambiare lo stato dell'attività."); + Console.WriteLine("Inserisci 5 per osservare la lista delle attività completate."); + Console.WriteLine("Inserisci 6 per osservare la lista delle attività da completare."); + Console.WriteLine("Inserisci 7 per chiudere l'applicazione."); + + bool isRunning = true; + while (isRunning) + { + Console.WriteLine("Inserire un numero per scegliere la funzionalità da svolgere."); + var inputMenu = Console.ReadLine(); + switch (inputMenu) + { + case "1": + Console.WriteLine("Inserire l'attività da aggiungere."); + attivitaRepository.Create(new Attivita() { NomeAttivita = Console.ReadLine(),IsCompleted=false }); + break; + case "2": + attivitaRepository.GetAll(); + break; + case "3": + Console.WriteLine("Inserisci l'id dell'attività da eliminare."); + attivitaRepository.Delete(int.Parse(Console.ReadLine())); + break; + case "4": + Console.WriteLine("Inserisci l'id dell'attività completata."); + attivitaRepository.ChangeStatus(int.Parse(Console.ReadLine())); + break; + case "5": + attivitaRepository.GetCompletedActivitiesList(); + break; + case "6": + attivitaRepository.GetNotCompleteddActivitiesList(); + break; + case "7": + Environment.Exit(0); + break; + default: + attivitaRepository.Print("Inserire un numero compreso tra 1 e 4."); + break; + } + } + } + catch (Exception ex) + { + attivitaRepository.Print(ex.Message); + } + } + } +} diff --git a/ToDoList/Repository/AttivitaRepository.cs b/ToDoList/Repository/AttivitaRepository.cs new file mode 100644 index 0000000..4d43680 --- /dev/null +++ b/ToDoList/Repository/AttivitaRepository.cs @@ -0,0 +1,110 @@ +using ToDoList.Entities; +using ToDoList.Interfaces; + +namespace ToDoList.Repository +{ + public class AttivitaRepository : IAttivitaRepository + { + public Attivita? Entity { get; set; } + public Dictionary EntityDictionary { get; set; } = new Dictionary(); + + public void ChangeStatus(int key) + { + var oldEntity = EntityDictionary.FirstOrDefault(d => d.Key == key); + if (!EntityDictionary.TryGetValue(key, out var entityForCheck)) + { + throw new ArgumentException($"Non sono state trovate attività da modificare con chiave : {key}\n"); + } + oldEntity.Value.IsCompleted = true; + if(oldEntity.Value.IsCompleted) + { + Print($"La seguente attività è stata completata: {oldEntity.Value.NomeAttivita}\n"); + } + } + public void Print(string message) => Console.WriteLine(message); + #region Metodi CRUD + public void Create(Attivita entity) + { + if (EntityDictionary.Any(a => a.Key == entity.Key)) + { + throw new ArgumentException($"L'attività {entity.NomeAttivita} è stata già aggiunta.\n"); + } + var keysList = new List(); + if (EntityDictionary.Keys.Count == 0) EntityDictionary.Add(0, null); + foreach (var k in EntityDictionary.Keys) + { + keysList.Add(k); + } + var lastKey = keysList.Max(); + var filters = EntityDictionary.Where(x => x.Value != null).Select(x => x.Value).ToList(); + bool exit = true; + foreach (var entityForCheck in filters) + { + if (filters.Last().NomeAttivita.ToLower().Equals(entity.NomeAttivita.ToLower())) + { + Console.WriteLine("Non puoi inserire la stessa attività due volte di fila.\n"); + exit = false; + break; + } + } + if (exit) + { + EntityDictionary.Add(lastKey + 1, entity); + Console.WriteLine("Attività aggiunta correttamente.\n"); + } + } + public bool Delete(int key) + { + var isRemoved = EntityDictionary.Remove(key); + if (!isRemoved) + { + throw new ArgumentException($"L'attività che stai rimuovendo con indice {key} non è in elenco"); + } + return isRemoved; + } + public void GetAll() + { + Print("Le attività presenti sono:"); + foreach (var entity in EntityDictionary) + { + if (entity.Value == null) continue; + + Console.Write($"{entity.Key}. {entity.Value.NomeAttivita} = "); + if (entity.Value.IsCompleted) { Console.WriteLine("Attività svolta"); } + else { Console.WriteLine("Attività da svolgere"); } + } + if (EntityDictionary.Keys.Count == 0) Print("Non sono ancora presenti attività"); + + } + public void Update(Attivita entity, int key) + { + var oldEntity = EntityDictionary.FirstOrDefault(d => d.Key == key); + if (!EntityDictionary.TryGetValue(key, out var entityForCheck)) + { + throw new ArgumentException($"Non sono state trovate attività da modificare con chiave : {key}\n"); + } + EntityDictionary.Remove(oldEntity.Key); + EntityDictionary.Add(oldEntity.Key, entity); + Print($"La nuova attività modificata è {entity.NomeAttivita} invece di {oldEntity.Value.NomeAttivita}"); + } + public void GetCompletedActivitiesList() + { + if (EntityDictionary.Count == 0) Print("La tua lista di attività completata è vuota, inserisci nuove attività. \n"); + foreach (var entity in EntityDictionary) + { + if(entity.Value == null) { continue; } + if (entity.Value.IsCompleted) Console.WriteLine($"{entity.Key}. {entity.Value.NomeAttivita}"); + } + } + public void GetNotCompleteddActivitiesList() + { + if (EntityDictionary.Count == 0) Print("Hai completato tutte le tue attività. \n"); + foreach (var entity in EntityDictionary) + { + if (entity.Value == null) { continue; } + if (!entity.Value.IsCompleted) Console.WriteLine($"{entity.Key}. {entity.Value.NomeAttivita}"); + } + } + #endregion + } +} diff --git a/ToDoList/ToDoList.csproj b/ToDoList/ToDoList.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/ToDoList/ToDoList.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + +