示例#1
0
    private static int getContestantData(int num, Contestant[] contestants, int revenue)
    {
        const int ADULTAGE = 17;
        const int TEENAGE  = 12;
        int       x        = 0;
        string    name;
        char      talent;
        int       age;
        int       pos;

        //HINT (7) - do seventh
        //HINT: int pos; here
        while (x < num)
        {
            Write("Enter contestant name >> ");
            name = ReadLine();
            WriteLine("Talent codes are:");
            for (int y = 0; y < Contestant.talentCodes.Length; ++y)
            {
                WriteLine("  {0}   {1}", Contestant.talentCodes[y], Contestant.talentStrings[y]);
            }
            Write("       Enter talent code >> ");
            char.TryParse(ReadLine(), out talent);
            //From the instructions:
            //Use exception-handling techniques to ensure a valid code and update the displayed
            //message to the following message:
            //x is not a valid talent code. Assigned as Invalid.
            //where x was the invalid code entered into the console.

            //HINT (8) - Use a Try/Catch block here
            //HINT: In the Try block call a new validateCode method, passing in the talent variable and an Out Integer
            //variable for the position
            //HINT: the Catch block should be:
            //WriteLine("{0} is not a valid talent code. Assigned as Invalid.", talent);

            Write("       Enter contestant's age >> ");
            int.TryParse(ReadLine(), out age);
            if (age > ADULTAGE)
            {
                contestants[x] = new AdultContestant();
            }
            else
            if (age > TEENAGE)
            {
                contestants[x] = new TeenContestant();
            }
            else
            {
                contestants[x] = new ChildContestant();
            }
            contestants[x].Name       = name;
            contestants[x].TalentCode = talent;
            revenue += contestants[x].Fee;
            ++x;
        }
        return(revenue);
    }
示例#2
0
    private static int getContestantData(int num, Contestant[] contestants, int revenue)
    {
        const int ADULTAGE = 17;
        const int TEENAGE  = 12;
        int       x        = 0;
        string    name;
        char      talent;
        int       age;
        int       pos;

        StreamWriter sw = new StreamWriter("Contestant.txt");

        while (x < num)
        {
            Console.Write("Enter contestant name >> ");
            name = Console.ReadLine();
            Console.WriteLine("Talent codes are:");
            for (int y = 0; y < Contestant.talentCodes.Length; ++y)
            {
                Console.WriteLine("  {0}   {1}", Contestant.talentCodes[y], Contestant.talentStrings[y]);
            }
            Console.Write("       Enter talent code >> ");
            char.TryParse(Console.ReadLine(), out talent);
            try
            {
                validateCode(talent, out pos);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine("{0} is not a valid code. Assigned as Invalid.", talent);
            }
            Console.Write("       Enter contestant's age >> ");
            int.TryParse(Console.ReadLine(), out age);
            if (age > ADULTAGE)
            {
                contestants[x] = new AdultContestant();
            }
            else
            if (age > TEENAGE)
            {
                contestants[x] = new TeenContestant();
            }
            else
            {
                contestants[x] = new ChildContestant();
            }
            contestants[x].Name       = name;
            contestants[x].TalentCode = talent;
            revenue += contestants[x].Fee;
            ++x;
            sw.WriteLine(talent + "," + age + "," + name);
            sw.Flush();
            sw.Close();
        }

        return(revenue);
    }