Question
In C# I am asked once again to modify my code with the following: In Chapter 7, you modified the GreenvilleRevenue program to include a
In C# I am asked once again to modify my code with the following:
In Chapter 7, you modified the GreenvilleRevenue program to include a number of methods. Now modify every data entry statement to use a TryParse() method to ensure that each piece of data is the correct type. Any invalid user entries should generate an appropriate message, and the user should be required to reenter the data.
My code is as follows:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace Week_3_Ques_2 { class Program { static void Main(string[] args) { int entFee = 25; int contLast; int contCurr; int contMin = 0; int contMax = 30; int other = 0; int dance = 0; int instrument = 0; int sing = 0;
string message = ("No. of contestant's in last year's competition? Enter a number from 0 to 30."); contLast = getNoOfContestants(message, contMin, contMax); message = (" No. of contestants in this year's competition? Enter a number from 0 to 30.");
contCurr = getNoOfContestants(message, contMin, contMax); string[] contestant = new String[contCurr]; string[] skill = new String[contCurr]; getContestantsInfo(contestant, skill); for (int x = 0; x < skill.Length; ++x) { if (skill[x] == "O") { ++other; } else if (skill[x] == "S") { ++sing; } else if (skill[x] == "D") { ++dance; } else if (skill[x] == "M") { ++instrument; } } Console.Clear(); Console.WriteLine("There are:"); Console.WriteLine("{0} dancers", dance); Console.WriteLine("{0} singers", sing); Console.WriteLine("{0} musicians", instrument); Console.WriteLine("{0} people with other skills", other); displayContsByTalent(contestant, skill);
Console.Clear(); displayCompInfo(contCurr, contLast, entFee); } //- A method that gets and returns a valid number of contestants and is called twice- once for last year's number of contestants and one for this year's value static int getNoOfContestants(string message, int contMin, int contMax) { Console.WriteLine("No. of contestant's in last year's competition? Enter a number from 0 to 30."); string input = Console.ReadLine(); int conts = Convert.ToInt32(input); while (conts < contMin || conts > contMax) { Console.WriteLine(" You have entered and invalid response, please enter a valid number."); conts = Convert.ToInt32(Console.ReadLine()); } return conts; } static string getSkill(int contNum) { bool correct = false; string type = ""; while (!correct) { Console.WriteLine(" Please enter contestant " + contNum + " 's skill, 'S' for sing 'D' for dance 'M' for " + "musical instrument 'O' for other"); type = Console.ReadLine().ToUpper(); if (type == "S" || type == "D" || type == "M" || type == "O") { correct = true; } else { Console.WriteLine("Please enter a valid parameter."); } } return type; }
//A method that continuously prompts for talent codes and displays contestants with the corresponding talent until a sentinel value is entered static void displayContsByTalent(string[] contestant, string[] skill) { Console.WriteLine(" Please enter a skill code, 'S' 'D' 'M' 'O', to see a list of contestants with that skill or enter '!' to exit."); string entry = Console.ReadLine().ToUpper(); while (entry != "!") { if (entry != "S" && entry != "D" && entry != "M" && entry != "O") { Console.WriteLine(" Please try again: Enter a VALID skill code, 'S' 'D' 'M' 'O', to see a list of contestants with that skill or '!' to exit."); entry = Console.ReadLine().ToUpper(); if (entry == "!") break; } for (int x = 0; x < skill.Length; ++x) { if (entry == skill[x]) Console.WriteLine(" Contestant " + contestant[x] + " skill " + skill[x]); } Console.WriteLine(" Please enter a skill code, 'S' 'D' 'M' 'O', to see a list of contestants with that skill or enter '!' to exit"); entry = Console.ReadLine().ToUpper(); }
} //A method that fills the array of competitors and their talent codes static void getContestantsInfo(string[] contestant, string[] skill) { for (int x = 0; x < contestant.Length; ++x) { Console.WriteLine(" Please enter contestant no. " + (x + 1) + "'s name"); contestant[x] = Console.ReadLine(); skill[x] = getSkill(x + 1); } }
//-A method that accepts the number of contestants this year and last year, and displays one of the three messages that describes the relationship between the two contestant values static void displayCompInfo(int contCurr, int contLast, int entFee) { if (contCurr > contLast * 2) { Console.WriteLine("The competition is more than twice as big this year! "); Console.WriteLine(" The revenue expected for this year's competition is {0:C}", (contCurr * entFee)); } else if (contCurr > contLast && contCurr <= (contLast * 2)) { Console.WriteLine("The competition is bigger than ever! "); Console.WriteLine(" The revenue expected for this year's competition is {0:C}", (contCurr * entFee)); } else if (contCurr < contLast) { Console.WriteLine("A tighter race this year! Come out and cast your vote! "); Console.WriteLine(" The revenue expected for this year's competition is {0:C}", (contCurr * entFee)); Console.ReadLine(); }
} } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started