Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the GreenvilleRevenue program (code below) created in the previous chapter so that it performs the following tasks : - The program prompts the user

Modify the GreenvilleRevenue program (code below) created in the previous chapter so that it performs the following tasks:

- The program prompts the user for the number of contestants in this years competition; the number must be between 0 and 30. Use exception-handling techniques to ensure a valid value. - The program prompts the user for talent codes. Use exception-handling techniques to ensure a valid code. - After data entry is complete, the program prompts the user for codes so the user can view lists of appropriate contestants. Use exception-handling techniques for the code verification.

CODE BELOW *****

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

namespace GreenvilleRevenue { class Chapter10 { static void Main() { int thisYearContestants;

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

Contestant[] contestants = new Contestant[thisYearContestants];

FillArrays(thisYearContestants, contestants);

double totalrevenue = 0; foreach (Contestant c in contestants) { totalrevenue += c.EntryFee; //Display contestants Console.WriteLine(c); } Console.WriteLine(" ********************************************************** "); Console.WriteLine(" Total Revenue: $" + totalrevenue);

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 FillArrays(int thisYearContestants, Contestant[] contestants) { int x = 0; string name; int age; char talent; while (x < thisYearContestants) { Console.WriteLine(" Enter Contestant Name: "); name = Console.ReadLine(); Console.WriteLine("Enter Contestant age: "); int.TryParse(Console.ReadLine(), out age); 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); if (age <= 12) { contestants[x] = new ChildContestant(); contestants[x].Name = name; contestants[x].TalentCode = talent; ++x; } else if (age > 12 && age <= 17) { contestants[x] = new TeenContestant(); contestants[x].Name = name; contestants[x].TalentCode = talent; ++x; } else { contestants[x] = new AdultContestant(); 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; 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; private int entryFee;

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; } }

public int EntryFee { get { return entryFee; } set { entryFee = value; } } } // end contestant superclass

class ChildContestant : Contestant { public ChildContestant() { EntryFee = 15; } public override string ToString() { return " Name: " + Name + " Talent Code: " + TalentCode + " Talent Description: " + Talent + " Entry Fee: " + EntryFee + " Age Category: Child"; } } // end ChildContestant subclass class TeenContestant : Contestant { public TeenContestant() { EntryFee = 20; } public override string ToString() { return " Name: " + Name + " Talent Code: " + TalentCode + " Talent Description: " + Talent + " Entry Fee: " + EntryFee + " Age Category: Teen"; } } // end TeenContestant subclass class AdultContestant : Contestant { public AdultContestant() { EntryFee = 30; } public override string ToString() { return " Name: " + Name + " Talent Code: " + TalentCode + " Talent Description: " + Talent + " Entry Fee: " + EntryFee + " Age Category: Adult"; } } // end AdultContestant subclass } // end GreenvilleRevenue

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

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

More Books

Students also viewed these Databases questions

Question

What is a chemical property?

Answered: 1 week ago