Advertisement
NelloRizzo

News - Business

Jan 18th, 2018
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace AssaBusiness
  8. {
  9.     /*
  10.      * Cosa è per noi una News?
  11.      *
  12.      * Una [news] è costituita da un [titolo], un [testo]
  13.      * un [autore] e una [data].
  14.      *
  15.      * +----------------------+
  16.      * | News                 |
  17.      * +------------------+---+
  18.      * | Title            | A |
  19.      * | Content          | A |
  20.      * | Author           | A |
  21.      * | PublicationDate  | D |
  22.      * +------------------+---+
  23.      *
  24.      * */
  25.     public class News
  26.     {
  27.         // JAVA:
  28.         //private int id;
  29.         //public int getId() { return id; }
  30.         //public void setId(int value) { id = value; }
  31.         // C#:
  32.         //private int id;
  33.         //public int Id
  34.         //{
  35.         //    get { return id; }
  36.         //    set { id = value; }
  37.         //}
  38.         // C# evoluto!
  39.         // ID, Title, Content, Author e PublicationDate sono PROPERTIES: PROPRIETA'
  40.         public int Id { get; set; }
  41.         public string Title { get; set; }
  42.         public string Content { get; set; }
  43.         public string Author { get; set; }
  44.         public DateTime PublicationDate { get; set; }
  45.     }
  46.  
  47.     /*
  48.      * Cosa dobbiamo fare con una News?
  49.      * (Questo è il processo di business da implementare!!!)
  50.      * Il nostro processo di business deve:
  51.      * gestire un [elenco di News], ovvero
  52.      * di <aggiungere>, <modificare>, <eliminare> e
  53.      * <visualizzare> le news.
  54.      *
  55.      * */
  56.     public class Business
  57.     {
  58.         // elenco di news
  59.         private static List<News> newsList;
  60.         private List<News> NewsList
  61.         {
  62.             get
  63.             {
  64.                 if (newsList == null) newsList = new List<News>();
  65.                 return newsList;
  66.             }
  67.         }
  68.  
  69.         /// <summary>
  70.         /// Costruttore della classe.
  71.         /// </summary>
  72.         /// <remarks>
  73.         /// Il costruttore è un metodo che viene richiamato quando viene
  74.         /// effettuata una chiamata a <code>new Business()</code>.
  75.         /// Serve per inizializzare le variabili interne alla classe in maniera
  76.         /// da poterle correttamente utilizzare durante il ciclo di vita della
  77.         /// classe stessa.
  78.         /// </remarks>
  79.         public Business()
  80.         {
  81.             //Console.WriteLine("Sto eseguendo il costruttore della classe Business");
  82.         }
  83.  
  84.         public void CreateNews(News n)
  85.         {
  86.             // produco un ID univoco per la news che vado ad inserire
  87.             // mi leggo le news
  88.             List<News> news = ReadNews();
  89.             // inizializzo la variabile che conterrà il mio id
  90.             // se non ce ne sono essa varrà 1 per la prima news
  91.             int id = 1;
  92.             // se ci sono news
  93.             if (news.Count > 0)
  94.                 // leggo l'id più alto e lo aumento di 1
  95.                 id = news.Max(i => i.Id) + 1;
  96.             // assegno l'id alla news da inserire tramite il business layer
  97.             n.Id = id;
  98.             NewsList.Add(n);
  99.         }
  100.         public List<News> ReadNews()
  101.         {
  102.             return NewsList;
  103.         }
  104.         public News ReadNews(int id)
  105.         {
  106.             return NewsList.FirstOrDefault(n => n.Id == id);
  107.         }
  108.         public void UpdateNews(News n)
  109.         {
  110.             // ricerco la notizia nella lista
  111.             News old = ReadNews(n.Id);
  112.             if (old != null)
  113.             {
  114.                 // la modifico con i nuovi valori
  115.                 // passati come parametro
  116.                 DeleteNews(n.Id);
  117.                 NewsList.Add(n);
  118.             }
  119.         }
  120.         public void DeleteNews(int id)
  121.         {
  122.             // ricerco la notizia nella lista
  123.             News old = ReadNews(id);
  124.             if (old != null)
  125.             {
  126.                 // la cancello dalla lista
  127.                 NewsList.Remove(old);
  128.             }
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement