Question: Make OP_14_Events(41); with c# namespace Humans { public delegate void Invitation(Human Z); public class Human { private string Name { get; set; } private System.DateTime
Make OP_14_Events(41); with c#
namespace Humans { public delegate void Invitation(Human Z); public class Human { private string Name { get; set; } private System.DateTime BirthDate { get; } private System.Random R; public Human(string _Name, System.DateTime _BirthDate, System.Random _R) { Name = _Name; BirthDate = _BirthDate; R = _R; } private float AgeInDays { get { return (float)(System.DateTime.Today - BirthDate).TotalDays; } } private float Age { get { return AgeInDays / (float)365.2425; } } public bool Birthday { get { return BirthDate.Month == System.DateTime.Today.Month && BirthDate.Day == System.DateTime.Today.Day; } } public void TellAboutYourself() { System.Console.WriteLine($"Hi! I'm {Age:0.00} years ({AgeInDays} days) old {Name}."); } public void CompareTo(Human X) { System.Console.WriteLine($"I'm {(Age > X.Age ? "older" : "younger")} than {X.Name} by {System.Math.Abs(Age - X.Age):0.0} years."); } private event Invitation GuestList; // ! public void Invite(Human X) { GuestList += new Invitation(X.GoToVisit); } private void GoToVisit(Human X) { switch (R.Next() % 3) { // or any other logic case 0 : System.Console.WriteLine($"{Name} is going to buy a gift for {X.Name}"); break; case 1 : System.Console.WriteLine($"{Name} is going to make a gift for {X.Name}"); break; case 2 : System.Console.WriteLine($"This time {Name} doesn't want to wish {X.Name} a happy birthday..."); break; } } public void Party() { if (R.NextDouble() > 0.3) { // or any other logic System.Console.WriteLine($"Hi, it's {Name}. Today I have a party, visit me!"); GuestList?.Invoke(this); } // if (GuestList != null) GuestList(this); else System.Console.WriteLine($"{Name} doesn't want to have a party this year..."); } } public class Program { public static void Main(string[] args) { Human John = new Human("John", new System.DateTime(1998, 10, 24), new System.Random()); Human Mary = new Human("Mary", new System.DateTime(2000, 04, 14), new System.Random()); Human Andy = new Human("Andy", new System.DateTime(1950, 05, 14), new System.Random()); Human Mike = new Human("Mike", new System.DateTime(1985, System.DateTime.Today.Month, System.DateTime.Today.Day), new System.Random()); // John.TellAboutYourself(); John.CompareTo(Mary); Mary.TellAboutYourself(); Mary.CompareTo(John); Human[] Humans = { John, Mary, Andy, Mike, new Human("Lora", new System.DateTime(1999, System.DateTime.Today.Month, System.DateTime.Today.Day), new System.Random()) }; John.Invite(Mary); // John.GuestList += new Invitation(Mary.GoToVisit); // if public Mary.Invite(John); Mike.Invite(John); foreach (Human X in Humans) if (X != Andy) Andy.Invite(X); // Andy.Invite(Andy); foreach (Human X in Humans) /* if (X.Birthday) */ X.Party(); // John.Party(); Mary.Party(); // ... }} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
