コード例 #1
0
 //Search feature.  Could have avoided difficultly by adding "Invalid" to the talents array, but where is the fun in that.
 private static void SearchContestants(char inputChar, Contestant[] contestant)
 {
     int count = 0;
     char invalid = INVALID.ToCharArray()[0];
     for (int x = 0; x < talents.Length; ++x)
     {
         if (inputChar == talents[x].ToCharArray()[0] || inputChar == invalid)
         {
             count = 1;
             for (int y = 0; y < contestant.Length; ++y)
             {
                 if (inputChar == contestant[y].Talent)
                 {
                     Console.WriteLine("{0,4}{1,-32}{2}", count + ": ", contestant[y].Name, contestant[y].TalentDescription);
                     ++count;
                 }
             }
             break; //Prevents the loop from cycling once a result is found.
         }
     }
     if (count == 1)
     {
         Console.WriteLine("No Results found for talent code: {0}.", inputChar);
     }
 }
コード例 #2
0
            static void Main(string[] args)
            {
                int thisYears, lastYears;
                string inputString;
                char outputTalent, controlChar;
                bool quitCompare, reenterCompare;
                Console.Write("Enter the number of contestants in this years competition >> ");  //Next section of code gets number of contestants.
                inputString = Console.ReadLine();
                while (!int.TryParse(inputString, out thisYears) || !ValidateGroup(thisYears))  //Validation made easy thanks to TryParse.
                {
                    Console.Write("Please enter a valid integer between {0} and {1} >> ", MIN, MAX);
                    inputString = Console.ReadLine();
                }
                Console.Write("Enter the number of contestants in last years competition >> ");
                inputString = Console.ReadLine();
                while (!int.TryParse(inputString, out lastYears) || !ValidateGroup(lastYears))
                {
                    Console.Write("Please enter a valid integer between {0} and {1} >> ", MIN, MAX);
                    inputString = Console.ReadLine();
                }

                Console.WriteLine(GetComparison(thisYears, lastYears));  //Generates comparision message.  GetComparision() does all the work.

                Console.WriteLine("\nIf a {0} entrance fee is charged, then the competition should generate {1} in revenue.  ", ENTRANCE_FEE.ToString("C"), GetRevenue(thisYears).ToString("C")); //Revenue statement.

                Contestant[] contestant = new Contestant[thisYears]; //This section of code gets the names and talents, and delivers them to Contestant class.
                for (int x = 0; x < thisYears; ++x)
                {
                    Console.Write("\nEnter the name of contestant number {0} >> ", x + 1);
                    contestant[x] = new Contestant();  //A new object for each new contestant.
                    contestant[x].Name = Console.ReadLine();
                    do
                    {
                        controlChar = '\0'; //Resetting controlChar for each run of the loop.
                        Console.Write("Enter {0}'s talent code >> ", contestant[x].Name);
                        while (!char.TryParse(Console.ReadLine(), out outputTalent))
                        {
                            Console.Write("Please enter a single character >> ");
                        }
                        if (!Contestant.ValidateCode(outputTalent))  //If the talent code is invalid, lets let the user know before proceeding.
                        {
                            Console.WriteLine(Contestant.GetValidationMsg());
                            Console.Write("\nType {0} to re-enter or press enter to continue >> ", REENTER);
                            char.TryParse(Console.ReadLine(), out controlChar);
                        }
                        reenterCompare = controlChar == REENTER || controlChar == Char.ToLower(REENTER);
                        if (!reenterCompare)
                        {
                            contestant[x].Talent = outputTalent;
                        }
                    } while (reenterCompare); //Loop allows for corrections to the talent code.
                }

                do  //Search contestants by way of SearchContestants().
                {
                    Console.Write("\nEnter a talent code to see users from that category or press {0} to quit >> ", QUIT);
                    while (!char.TryParse(Console.ReadLine(), out controlChar))
                    {
                        Console.Write("\nPlease enter a single character >> ");
                    }
                    quitCompare = controlChar == QUIT || controlChar == Char.ToLower(QUIT);
                    if (!Contestant.ValidateCode(controlChar) && controlChar != INVALID.ToCharArray()[0] && !quitCompare) //All these conditions must be met for the error msg to show.
                    {
                        Console.WriteLine("\n" + Contestant.GetValidationMsg());
                        continue;
                    }
                    else
                    {
                        SearchContestants(controlChar, contestant);
                    }
                } while (!quitCompare);
            }