Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace UnitTestingClasses
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class Startup
- {
- public static void Main()
- {
- Inbox personsInbox = new Inbox();
- Person person = new Person("Ivan", personsInbox);
- Website website = new Website(@"https://www.softuni.bg");
- website.SubscribeToNews(person);
- News firstNews = new News("first");
- website.PostNews(firstNews);
- News secondNews = new News("second");
- website.PostNews(secondNews);
- List<News> notificationNews = person.GetNewsFromNotifications();
- string titles = string.Join(Environment.NewLine, notificationNews.Select(n => n.Title));
- Console.WriteLine(titles);
- }
- }
- public class Inbox
- {
- private List<News> news;
- public Inbox()
- {
- this.news = new List<News>();
- }
- public List<News> News
- {
- get
- {
- return this.news.ToList();
- }
- }
- public void AddNewsNotification(News news)
- {
- this.news.Add(news);
- }
- }
- public class Person
- {
- private string name;
- private Inbox inbox;
- public Person(string name, Inbox inbox)
- {
- this.name = name;
- this.inbox = inbox;
- }
- public void NewsPostedEventHandler(object sender, WebsiteEventArgs websiteEventArgs)
- {
- News news = websiteEventArgs.News;
- this.inbox.AddNewsNotification(news);
- }
- public List<News> GetNewsFromNotifications()
- {
- return this.inbox.News;
- }
- }
- public delegate void NewsPostedEventHandler(object sender, WebsiteEventArgs websiteEventArgs);
- public class Website
- {
- private event NewsPostedEventHandler NewsPosted;
- public Website(string url)
- {
- this.Url = url;
- }
- public string Url { get; set; }
- protected virtual void OnNewsPosted(WebsiteEventArgs websiteEventArgs)
- {
- this.NewsPosted?.Invoke(this, websiteEventArgs);
- }
- public void PostNews(News news)
- {
- WebsiteEventArgs websiteEventArgs = new WebsiteEventArgs(news);
- this.OnNewsPosted(websiteEventArgs);
- }
- public void SubscribeToNews(Person person)
- {
- this.NewsPosted += person.NewsPostedEventHandler;
- }
- }
- public class WebsiteEventArgs : EventArgs
- {
- public WebsiteEventArgs(News news)
- {
- this.News = news;
- }
- public News News { get; set; }
- }
- public class News
- {
- private string title;
- public News(string title)
- {
- this.Title = title;
- }
- public string Title
- {
- get
- {
- return this.title;
- }
- private set
- {
- this.title = value;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement