Пример #1
0
        static void Main(string[] args)
        {
            IHasScore equalChecker = null;
            Random    rnd          = new Random();

            IHasScore[] hasScores = new IHasScore[10];

            for (int i = 0; i < 10; i++)
            {
                if (i % 2 == 0)
                {
                    hasScores[i] = new Player(DefPlayerName(rnd.Next(3)),
                                              rnd.Next(6));
                }
                else if (i % 2 != 0)
                {
                    hasScores[i] = new Hotel(
                        DefHotelName(rnd.Next(3)), DefDescription(rnd.Next(3)),
                        rnd.Next(1, 11), rnd.Next(3), rnd.Next(6));
                }
            }

            foreach (IHasScore item in hasScores)
            {
                Console.WriteLine($"{item}");
                Console.WriteLine();
                Console.WriteLine($"Is {equalChecker} the same as {item}? " +
                                  $"{item.Equals(equalChecker)}");

                equalChecker = item;

                Console.WriteLine();
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            IHasScore previousScore = null;

            IHasScore[] scoreArray = new IHasScore[10];
            scoreArray[0] = new Player("Boi1", 6);
            scoreArray[1] = new Hotel("Hotel1", "hotel1, man", 8, 3);
            scoreArray[2] = new Player("Boi2", 3);
            scoreArray[3] = new Hotel("´Hotel2", "hotel2, man", 41, 5);
            scoreArray[4] = new Player("Boi3", 4);
            scoreArray[5] = new Hotel("Hotel3", "hotel3, man", 35, 4);
            scoreArray[6] = new Player("Boi4", 25);
            scoreArray[7] = new Hotel("Hotel4", "hotel4, man", 14, 3);
            scoreArray[8] = new Player("Boi5", 5);
            scoreArray[9] = new Hotel("Hotel5", "hotel5, man", 7, 2);

            foreach (IHasScore s in scoreArray)
            {
                Console.WriteLine($"{s}'s score is {s.Score}");
                if (previousScore != null)
                {
                    Console.WriteLine($"Equals to last? Answer: " +
                                      $"{s.Equals(previousScore)}");
                }
                previousScore = s;
            }
        }
Пример #3
0
 /// <summary>
 /// This method returns true if another instance of type IHasScore
 /// contains the same score as the current instance. This method is
 /// required because Player implements <c>IEquatable</c> (via
 /// <see cref="IHasScore"/>).
 /// </summary>
 /// <param name="other">
 /// An instance of a class that implements the IHasScore interface.
 /// </param>
 /// <returns>True if both instances have the same score.</returns>
 public bool Equals(IHasScore other)
 {
     if (other == null)
     {
         return(false);
     }
     return(Score == other.Score);
 }
Пример #4
0
 /// <summary>
 /// This method is required by the <see cref="IComparable{T}"/>
 /// interface, so that players can be compared.
 /// </summary>
 /// <param name="other">
 /// The player to which the current player is to be compared with.
 /// </param>
 /// <returns>
 /// -1 if this player comes before the other.
 ///  0 if players have a similar score.
 ///  1 if this player comes after the other.
 ///  </returns>
 public int CompareTo(IHasScore other)
 {
     if (other == null)
     {
         return(-1);
     }
     return(other.Score - Score);
 }
Пример #5
0
        public bool Equals([AllowNull] IHasScore other)
        {
            if (other == null || other.Score != Score)
            {
                return(false);
            }

            return(true);
        }
Пример #6
0
 public bool Equals(IHasScore other)
 {
     if (other.Score == Score)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #7
0
        /// <summary>
        /// The program starts here and the tests are also done here.
        /// </summary>
        /// <param name="args">Not used.</param>
        public static void Main(string[] args)
        {
            // This local variable will contain the previous object when we
            // cycle thorugh the array (initially it's null)
            IHasScore previous = null;

            // Declare and create an array with space for 10 IHasScore objects
            IHasScore[] objectsThatHaveScore = new IHasScore[10];

            // Create 10 IHasScore objects (Player's and Hotel's) using the
            // syntax for initializing objects with properties
            objectsThatHaveScore[0] = new Player()
            {
                Name = "Ze", Score = 5
            };

            objectsThatHaveScore[1] = new Hotel()
            {
                Name             = "Four Seasonz", Score = 5,
                Description      = "By the sea, near the river",
                NumberOfBedrooms = 50, NumberOfFloors = 8
            };

            objectsThatHaveScore[2] = new Player()
            {
                Name = "Paula", Score = 100
            };

            objectsThatHaveScore[3] = new Hotel()
            {
                Name             = "Holiday Innz",
                Score            = 2,
                Description      = "In the middle of the action",
                NumberOfBedrooms = 80,
                NumberOfFloors   = 12
            };

            objectsThatHaveScore[4] = new Player()
            {
                Name = "André", Score = 55
            };

            objectsThatHaveScore[5] = new Hotel()
            {
                Name             = "Ibiz",
                Score            = 5,
                Description      = "A low cost place",
                NumberOfBedrooms = 70,
                NumberOfFloors   = 10
            };

            objectsThatHaveScore[6] = new Player()
            {
                Name = "Pedro", Score = 99
            };

            objectsThatHaveScore[7] = new Hotel()
            {
                Name             = "Sanaz Hotel",
                Score            = 4,
                Description      = "Almost perfect, but not quite",
                NumberOfBedrooms = 120,
                NumberOfFloors   = 15
            };

            objectsThatHaveScore[8] = new Player()
            {
                Name = "Joana", Score = 25
            };

            objectsThatHaveScore[9] = new Hotel()
            {
                Name             = "Radizzon",
                Score            = 3,
                Description      = "For people who travel for work",
                NumberOfBedrooms = 40,
                NumberOfFloors   = 6
            };

            // Go through the array
            foreach (IHasScore aThingWithAScore in objectsThatHaveScore)
            {
                // Show info, namely if current object is "equal" to the
                // previous one
                Console.WriteLine(
                    "Current object (a {0}) has a score of {1} and "
                    + "is {2} the previous",
                    aThingWithAScore.GetType().Name,
                    aThingWithAScore.Score,
                    aThingWithAScore.Equals(previous) ?
                    "equal to" : "different from");

                // Make current object the previous one in the next iteration
                previous = aThingWithAScore;
            }
        }