예제 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("[Create and Display the First object]");
            // Create a medal object
            Medal m1 = new Medal("Horace Gwynne", "Boxing", "Gold", 2012, true);

            Console.WriteLine("\n-----------------------------------------------\n");

            // Display the object
            Console.WriteLine(m1);

            Console.WriteLine("\n-----------------------------------------------\n");

            // Print only the name of the medal holder
            Console.WriteLine("\n[Print only name of the object]");
            Console.WriteLine(m1.Name);

            Console.WriteLine("\n-----------------------------------------------\n");

            // Assign(Modify) the first object and Display
            Console.WriteLine("\n[Assign(Modify) the first object and Display]");
            m1 = new Medal("Michael Phelps", "Swimming", "Gold", 2012, false);
            Console.WriteLine(m1);

            Console.WriteLine("\n-----------------------------------------------\n");

            Console.WriteLine("[Create a list of medalist objects]");
            // Create a list to store the medal objects
            List <Medal> medals = new List <Medal>();

            medals.Add(new Medal("Horace Gwynne", "Boxing", "Gold", 2012, true));
            medals.Add(new Medal("Michael Phelps", "Swimming", "Gold", 2012, false));
            medals.Add(new Medal("Ryan Cochrane", "Swimming", "Silver", 2012, false));
            medals.Add(new Medal("Adam van Koeverden", "Canoeing", "Silver", 2012, false));
            medals.Add(new Medal("Rosie MacLennan", "Gymnastics", "Gold", 2012, false));
            medals.Add(new Medal("Christine Girard", "Weightlifting", "Bronze", 2012, false));
            medals.Add(new Medal("Charles Hamelin", "Short Track", "Gold", 2014, true));
            medals.Add(new Medal("Alexandre Bilodeau", "Freestyle skiing", "Gold", 2012, true));
            medals.Add(new Medal("Jennifer Jones", "Curling", "Gold", 2014, false));
            medals.Add(new Medal("Charle Cournoyer", "Short Track", "Bronze", 2014, false));
            medals.Add(new Medal("Mark McMorris", "Snowboarding", "Bronze", 2014, false));
            medals.Add(new Medal("Sidney Crosby ", "Ice Hockey", "Gold", 2014, false));
            medals.Add(new Medal("Brad Jacobs", "Curling", "Gold", 2014, false));
            medals.Add(new Medal("Ryan Fry", "Curling", "Gold", 2014, false));
            medals.Add(new Medal("Antoine Valois-Fortier", "Judo", "Bronze", 2012, false));
            medals.Add(new Medal("Brent Hayden", "Swimming", "Bronze", 2012, false));

            Console.WriteLine("\n===================================================\n");

            #region Question 1
            // Prints a numbered list of all athletes 16 medals
            Console.WriteLine("[Q1. Prints a numbered list of all athletes 16 medals]");

            int iQ1 = 1;

            foreach (Medal m in medals)
            {
                Console.WriteLine("{0}. " + m, iQ1);

                iQ1++;
            }
            #endregion

            Console.WriteLine("\n-----------------------------------------------\n");

            #region Question 2
            // Prints a numbered list of 16 athlete's enames
            Console.WriteLine("[Q2. Prints a numbered list of 16 athlete's enames]");
            int iQ2 = 1;

            foreach (Medal m in medals)
            {
                Console.WriteLine("{0}. " + m.Name, iQ2);

                iQ2++;
            }
            #endregion

            Console.WriteLine("\n-----------------------------------------------\n");

            #region Question 3
            // Prints a numbered list of 9 athletes who got gold medals
            Console.WriteLine("[Q3. Prints a numbered list of 9 athletes who got gold medals]");

            int iQ3 = 1;

            foreach (Medal m in medals)
            {
                if (m.Color == "Gold")
                {
                    Console.WriteLine("{0}. " + m.Name + " - " + m.Color, iQ3);

                    iQ3++;
                }
            }
            #endregion

            Console.WriteLine("\n-----------------------------------------------\n");

            #region Question 4
            // Prints a numbered list of 9 athletes with medals in 2012
            Console.WriteLine("[Q4. Prints a numbered list of 9 medals in 2012]");
            int iQ4 = 1;

            foreach (Medal m in medals)
            {
                if (m.Year == 2012)
                {
                    Console.WriteLine("{0}. " + m.Year + " - " + m.Name + " - " + m.Color, iQ4);

                    iQ4++;
                }
            }
            #endregion

            Console.WriteLine("\n-----------------------------------------------\n");

            #region Question 5

            // Prints a numbered athletes list of 3 world record medals
            Console.WriteLine("[Q5. Prints a numbered athletes list of 3 world record medals]");
            int iQ5 = 1;

            foreach (Medal m in medals)
            {
                if (m.IsRecord == true)
                {
                    Console.WriteLine("{0}. " + m.Name + " - {1}", iQ5, (m.IsRecord)? "World Record" : "");

                    iQ5++;
                }
            }
            #endregion
        }
예제 #2
0
        static void Main(string[] args)
        {
            //create a medal object
            Medal m1 = new Medal("Horace Gwynne", "Boxing", "Gold", 2012, true);

            //print the object
            Console.WriteLine(m1);
            //print only the name of the medal holder
            Console.WriteLine(m1.Name);

            //create another object
            Medal m2 = new Medal("Michael Phelps", "Swimming", "Gold", 2012, false);

            //print the updated m2
            Console.WriteLine(m2);



            //create a list to store the medal objects
            List <Medal> medals = new List <Medal>()
            {
                m1, m2
            };

            medals.Add(new Medal("Ryan Cochrane", "Swimming", "Silver", 2012, false));
            medals.Add(new Medal("Adam van Koeverden", "Canoeing", "Silver", 2012, false));
            medals.Add(new Medal("Rosie MacLennan", "Gymnastics", "Gold", 2012, false));
            medals.Add(new Medal("Christine Girard", "Weightlifting", "Bronze", 2012, false));
            medals.Add(new Medal("Charles Hamelin", "Short Track", "Gold", 2014, true));
            medals.Add(new Medal("Alexandre Bilodeau", "Freestyle skiing", "Gold", 2012, true));
            medals.Add(new Medal("Jennifer Jones", "Curling", "Gold", 2014, false));
            medals.Add(new Medal("Charle Cournoyer", "Short Track", "Bronze", 2014, false));
            medals.Add(new Medal("Mark McMorris", "Snowboarding", "Bronze", 2014, false));
            medals.Add(new Medal("Sidney Crosby ", "Ice Hockey", "Gold", 2014, false));
            medals.Add(new Medal("Brad Jacobs", "Curling", "Gold", 2014, false));
            medals.Add(new Medal("Ryan Fry", "Curling", "Gold", 2014, false));
            medals.Add(new Medal("Antoine Valois-Fortier", "Judo", "Bronze", 2012, false));
            medals.Add(new Medal("Brent Hayden", "Swimming", "Bronze", 2012, false));

            //prints a numbered list of 16 medals.
            Console.WriteLine("\n\nAll 16 medals");
            int cont = 1;

            foreach (Medal item in medals)
            {
                Console.WriteLine($"{cont} - {item}");
                cont++;
            }

            //prints a numbered list of 16 names
            Console.WriteLine("\n\nAll 16 names");
            cont = 1;
            foreach (Medal item in medals)
            {
                Console.WriteLine($"{cont} - {item.Name}");
                cont++;
            }

            //prints a numbered list of 9 gold medals
            Console.WriteLine("\n\nAll 9 gold medals");
            cont = 1;
            foreach (Medal item in medals)
            {
                if (item.Color == "Gold")
                {
                    Console.WriteLine($"{cont} - {item}");
                }
                cont++;
            }

            //prints a numbered list of 9 medals in 2012
            Console.WriteLine("\n\nAll 9 medals in 2012");
            cont = 1;
            foreach (Medal item in medals)
            {
                if (item.Year == 2012)
                {
                    Console.WriteLine($"{cont} - {item}");
                }
                cont++;
            }

            //prints a numbered list of 4 gold medals in 2012
            Console.WriteLine("\n\nAll 4 gold medals in 2012");
            cont = 1;
            foreach (Medal item in medals)
            {
                if (item.Color == "Gold" && item.Year == 2012)
                {
                    Console.WriteLine($"{cont} - {item}");
                }
                cont++;
            }

            //prints a numbered list of 3 world record medals
            Console.WriteLine("\n\nAll 3 records");
            foreach (Medal item in medals)
            {
                if (item.IsRecord == true)
                {
                    Console.WriteLine($"{cont} - {item}");
                }
                cont++;
            }

            //saving all the medal to file Medals.txt
            Console.WriteLine("\n\nSaving to file");
            TextWriter writer = new System.IO.StreamWriter("Medals.txt");

            foreach (Medal item in medals)
            {
                writer.WriteLine(item);
            }

            writer.Close();

            //reading from the file and putting on the screen:
            Console.WriteLine("\n\nReading from file");
            TextReader reader = new StreamReader("Medals.txt");
            string     input  = "";
            int        lineNo = 1;

            while (input != null)
            {
                input = reader.ReadLine();
                if (input != null && input.Length != 0)
                {
                    Console.WriteLine($"{lineNo++} - {input}");
                }
            }
            reader.Close();
        }
예제 #3
0
        static void Main(string[] args)
        {
            //create a medal object
            Medal m1 = new Medal("Horace Gwynne", "Boxing", "Gold", 2012, true);

            //print the object
            Console.WriteLine(m1);
            //print only the name of the medal holder
            Console.WriteLine(m1.Name);

            //assign a new object to m1
            m1 = new Medal("Michael Phelps", "Swimming", "Gold", 2012, false);
            //print the updated m1
            Console.WriteLine(m1);

            //create a list to store the medal objects
            List <Medal> medals = new List <Medal>();

            medals.Add(new Medal("Horace Gwynne", "Boxing", "Gold", 2012, true));
            medals.Add(new Medal("Michael Phelps", "Swimming", "Gold", 2012, false));
            medals.Add(new Medal("Ryan Cochrane", "Swimming", "Silver", 2012, false));
            medals.Add(new Medal("Adam van Koeverden", "Canoeing", "Silver", 2012, false));
            medals.Add(new Medal("Rosie MacLennan", "Gymnastics", "Gold", 2012, false));
            medals.Add(new Medal("Christine Girard", "Weightlifting", "Bronze", 2012, false));
            medals.Add(new Medal("Charles Hamelin", "Short Track", "Gold", 2014, true));
            medals.Add(new Medal("Alexandre Bilodeau", "Freestyle skiing", "Gold", 2012, true));
            medals.Add(new Medal("Jennifer Jones", "Curling", "Gold", 2014, false));
            medals.Add(new Medal("Charle Cournoyer", "Short Track", "Bronze", 2014, false));
            medals.Add(new Medal("Mark McMorris", "Snowboarding", "Bronze", 2014, false));
            medals.Add(new Medal("Sidney Crosby ", "Ice Hockey", "Gold", 2014, false));
            medals.Add(new Medal("Brad Jacobs", "Curling", "Gold", 2014, false));
            medals.Add(new Medal("Ryan Fry", "Curling", "Gold", 2014, false));
            medals.Add(new Medal("Antoine Valois-Fortier", "Judo", "Bronze", 2012, false));
            medals.Add(new Medal("Brent Hayden", "Swimming", "Bronze", 2012, false));

            // [for] and [foreach] can be used in all below question (Just Chosen one)
            Console.WriteLine("\n----- Q1. Prints a numbered list of all athletes 16 medals -----");
            for (int i = 0; i < medals.Count; i++)
            {
                Console.WriteLine(medals[i]);
            }

            // [for] and [foreach] can be used in all below question (Just Chosen one)
            Console.WriteLine("\n----- Q2. Prints a numbered list of 16 athlete's names -----");
            foreach (Medal x in medals)
            {
                Console.WriteLine(x.Name);
            }

            // [for] and [foreach] can be used in all below question (Just Chosen one)
            Console.WriteLine("\n----- Q3. Prints a numbered list of 9 athletes who got gold medals -----");
            for (int i = 0; i < medals.Count; i++)
            {
                if (medals[i].Color == "Gold")
                {
                    Console.WriteLine(medals[i]);
                }
            }

            // [for] and [foreach] can be used in all below question (Just Chosen one)
            Console.WriteLine("\n----- Q4. Prints a numbered list of 9 athletes with medals in 2012 -----");
            foreach (Medal x in medals)
            {
                if (x.Year == 2012)
                {
                    Console.WriteLine(x);
                }
            }

            // [for] and [foreach] can be used in all below question (Just Chosen one)
            Console.WriteLine("\n----- Q5. Prints a numbered athletes list of 3 world record medals -----");
            for (int i = 0; i < medals.Count; i++)
            {
                if (medals[i].IsRecord == true)
                {
                    Console.WriteLine(medals[i]);
                }
            }
        }