static void Main(string[] args) { Human[] people_array = { new Botan("Bob"), new Student("Rick"), new Student("Joe"), new Girl("Pam"), new PrettyGirl("Jill"), new SmartGirl("Sandy") }; for (int i = 0; i < 6; i++) { var first = people_array[UniqueRandom.Instance.Next(people_array.Length)]; var second = people_array[UniqueRandom.Instance.Next(people_array.Length)]; var couple = CoupleAttribute.Couple(first, second); Console.WriteLine($" {first.GetType().Name} {first.Name} + {second.GetType().Name} {second.Name} " + couple.GetType().Name); try { Console.WriteLine(couple.Name + " " + couple.GetType().Name); } catch (Exception e) { Console.WriteLine(e.Message); } Program.KeyListener(); } }
public static IHasName Couple(Human first, Human second) { CoupleAttribute firstAttr = getAttr(first, second); CoupleAttribute secondAttr = getAttr(second, first); bool firstLike = DoYouLikeHim(firstAttr.Probability); bool secondLike = DoYouLikeHim(secondAttr.Probability); string name = "Default"; if (firstLike && secondLike) { var method = first.GetType().GetMethods()[1]; name = (string)method.Invoke(first, null); } else { throw new Exception($"No love 1 = {firstLike} 2 = {secondLike}"); } Type type = Type.GetType("Lab6." + firstAttr.ChildType, true); object obj = Activator.CreateInstance(type, name); return((IHasName)obj); }
public static Name Couple(Human first, Human second) { CoupleAttribute firstAttr = GetAttribute(first, second); CoupleAttribute secondAttr = GetAttribute(second, first); bool rightward = hasAffection(firstAttr.Probability); bool leftward = hasAffection(secondAttr.Probability); string name = "Default"; if (rightward && leftward) { var method = first.GetType().GetMethods()[1]; name = (string)method.Invoke(first, null); } else { throw new System.Exception("No mutual affection."); } Type type = Type.GetType("Lab6." + firstAttr.ChildType, true); object obj = Activator.CreateInstance(type, name); return((Name)obj); }