Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Chapter 9 (code below), you created a Contestant class for the Greenville Idol competition. The class includes a contestants name, talent code, and talent

In Chapter 9 (code below), you created a Contestant class for the Greenville Idol competition. The class includes a contestants name, talent code, and talent description.

The competition has become so popular that separate contests with differing entry fees have been established for children, teenagers, and adults. Modify the Contestant class to contain a field that holds the entry fee for each category, and add get and set accessors. Extend the Contestant class to create three subclasses: ChildContestant, TeenContestant, and AdultContestant.

- Child contestants are 12 years old and younger, and their entry fee is $15. - Teen contestants are between 13 and 17 years old, inclusive, and their entry fee is $20. - Adult contestants are 18 years old and older, and their entry fee is $30. In each subclass, set the entry fee field to the correct value, and override the ToString() method to return a string that includes all the contestant data, including the age category and the entry fee.

Modify the GreenvilleRevenue program so that it performs the following tasks: - The program prompts the user for the number of contestants in this years competition, which must be between 0 and 30. The program continues to prompt the user until a valid value is entered. - The program prompts the user for names, ages, and talent codes for the contestants entered. Along with the prompt for a talent code, display a list of valid categories. Based on the age entered for each contestant, create an object of the correct type (adult, teen, or child), and store it in an array of Contestant objects. - After data entry is complete, display the total expected revenue, which is the sum of the entry fees for the contestants. - After data entry is complete, display the valid talent categories and then continuously prompt the user for talent codes, and display all the data for all the contestants in each category. Display an appropriate message if the entered code is not a character or a valid code.

using System;

using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace Wk4Chapter9 { class Chapter9 { static void Main() { int thisYearContestants; int lastYearContestants;

Console.WriteLine("Please enter the number of contestants entered in last year's competition: "); lastYearContestants = GetContestants();

Console.WriteLine("Please enter the number of contestants entered in this year's competition: "); thisYearContestants = GetContestants();

DisplayContestantMessage(thisYearContestants, lastYearContestants); //revenue + contestant message

Contestant[] contestants = new Contestant[thisYearContestants]; FillArrays(thisYearContestants, contestants); PromptTalentCodes(thisYearContestants, contestants); }

static int GetContestants() { int contestants; string input = Console.ReadLine();

while (!Int32.TryParse(input, out contestants) || (contestants < 0 || contestants > 30)) {

Console.WriteLine("Error. Please enter a value between 0 and 30."); input = Console.ReadLine(); } return contestants; } //end GetContestants method

static void DisplayContestantMessage(int thisYearContestants, int lastYearContestants) { if ((lastYearContestants >= 0) && (lastYearContestants <= 30) && (thisYearContestants >= 0) && (thisYearContestants <= 30)) ; Console.WriteLine("The revenue expected for this year's competition : $" + thisYearContestants * 25 + ' '); if (thisYearContestants > lastYearContestants * 2) Console.WriteLine("The competition is more than twice as big this year! "); else if (thisYearContestants > lastYearContestants && thisYearContestants <= (lastYearContestants * 2)) Console.WriteLine("The competition is bigger than ever! "); else if (thisYearContestants < lastYearContestants) Console.WriteLine("A tighter race this year! Come out and cast your vote! "); } // end DisplayContestantMessage

static void FillArrays(int thisYearContestants, Contestant[] contestants) { int x = 0; string name; char talent; while (x < thisYearContestants) { Console.WriteLine("Enter Contestant Name: "); name = Console.ReadLine();

Console.WriteLine("Talent codes are:"); for (int k = 0; k < Contestant.talentCodes.Length; ++k) Console.WriteLine(" {0} {1}", Contestant.talentCodes[k], Contestant.talentStrings[k]); Console.WriteLine("Please enter contestant " + (x+1) + "'s talent code: ");

char.TryParse(Console.ReadLine(), out talent); contestants[x] = new Contestant(); contestants[x].Name = name; contestants[x].TalentCode = talent; ++x; } }// end FillArrays method

static void PromptTalentCodes(int thisYearContestants, Contestant[] contestants) { int x; char QUIT = 'Q'; char input; bool isValid; int pos = 0; bool correct;

Console.WriteLine(" ********************************************************** ");

Console.WriteLine("To see the contestants by talent, please search a valid talent code or " + QUIT + " to quit: "); Console.WriteLine(" The types of talent are:"); for (x = 0; x < Contestant.talentStrings.Length; ++x) Console.WriteLine("{0, -6}{1, -20}", Contestant.talentCodes[x], Contestant.talentStrings[x]); //taken from ch9 sample problem isValid = false; while (!isValid) { if (!char.TryParse(Console.ReadLine(), out input)) // if user enters an invalid code { isValid = false; Console.WriteLine(" Invalid talent code. Please enter a valid talent code or " + QUIT + " to quit: "); } else { if (input == QUIT) // if user enters the Q to quit isValid = true; else { for (int k = 0; k < Contestant.talentCodes.Length; ++k) { if (input == Contestant.talentCodes[k]) // if user enters a valid talent code { isValid = true; } } if (!isValid) //if user enters invalid talent code { Console.WriteLine(input + " is not a valid code. Please enter a talent code or " + QUIT + " to quit: "); } else { Console.WriteLine(" Contestants with talent " + input + "are: "); correct = false; for (x = 0; x < thisYearContestants; ++x) { if (contestants[x].TalentCode == input) { Console.WriteLine(contestants[x].Name); correct = true; } } if (!correct) Console.WriteLine("No contestants entered with talent " + input + "."); isValid = false; Console.Write(" Enter another talent code or " + QUIT + " to quit: "); } } } } } // end PromptTalentCodes method

} //end first class class Contestant { public static char[] talentCodes = { 'S', 'D', 'M', 'O' }; public static string[] talentStrings = {"Singing", "Dancing", "Musical instrument", "Other"}; public string Name { get; set; } private char talentCode; private string talent; public char TalentCode { get { return talentCode; } set { int pos = talentCodes.Length; for (int x = 0; x < talentCodes.Length; ++x) if (value == talentCodes[x]) pos = x; if (pos == talentCodes.Length) { talentCode = 'I'; talent = "Invalid"; } else { talentCode = value; talent = talentStrings[pos]; } }

} public string Talent { get { return talent; } } }

} // end namespace

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

What is a path expression?

Answered: 1 week ago