Question
Case Problem 8-1 In Chapter 7, you modified the GreenvilleRevenue program to include a number of methods. Now, using your code from Chapter 7 Case
Case Problem 8-1
In Chapter 7, you modified the GreenvilleRevenue program to include a number of methods.
Now, using your code from Chapter 7 Case Study 1, modify your program so every data entry statement uses a TryParse() method to ensure that each piece of data is the correct type.
Any invalid user entries should generate an appropriate message that contains the word Invalid, and the user should be required to reenter the data.
An example of the program is shown below:
Enter number of contestants last year >> 1 Enter number of contestants this year >> 2 Last year's competition had 1 contestants, and this year's has 2 contestants Revenue expected this year is $50.00 The competition is bigger than ever! Enter contestant name >> Matt Talent codes are: S Singing D Dancing M Musical instrument O Other Enter talent code >> Sarah Invalid format - entry must be a single character That is not a valid code Enter talent code >> R That is not a valid code Enter talent code >> J That is not a valid code Enter talent code >> S Enter contestant name >> Sarah Talent codes are: S Singing D Dancing M Musical instrument O Other Enter talent code >> D The types of talent are: Singing 1 Dancing 1 Musical instrument 0 Other 0 Enter a talent type or Z to quit >> Z
In order to prepend the $ to currency values, the program will need to use the CultureInfo.GetCultureInfo method. In order to do this, include the statement using System.Globalization; at the top of your program and format the output statements as follows: WriteLine("This is an example: {0}", value.ToString("C", CultureInfo.GetCultureInfo("en-US")));
The given Code:
using System;
using static System.Console;
using System.Globalization;
class GreenvilleRevenue
{
static void Main()
{
int contestantThisYear = 0;
int contestantLastYear = 0;
string input;
// get contestant number of this year
WriteLine("Enter the number of contestants for this year:");
input = ReadLine();
while (!Int32.TryParse(input, out contestantThisYear) || contestantThisYear < 0)
{
WriteLine("Invalid input. Enter a positive integer value:");
input = ReadLine();
}
// get contestant number of last year
WriteLine("Enter the number of contestants for last year:");
input = ReadLine();
while (!Int32.TryParse(input, out contestantLastYear) || contestantLastYear < 0)
{
WriteLine("Invalid input. Enter a positive integer value:");
input = ReadLine();
}
// display relationship
DisplayRelationship(contestantThisYear, contestantLastYear);
// get contestants' data
string[] names = new string[contestantThisYear];
char[] talents = new char[contestantThisYear];
char[] talentCodes = new char[contestantThisYear];
string[] talentCodesStrings = new string[] { "Vocal", "Dance", "Instrument", "Theater" };
int[] counts = new int[4];
GetContestantData(contestantThisYear, names, talents, talentCodes, talentCodesStrings, counts);
// get lists
GetLists(contestantThisYear, talentCodes, talentCodesStrings, names, talents, counts);
}
public static int GetContestantNumber(string when, int min, int max)
{
// Write your GetContestantNumber() here.
}
public static void DisplayRelationship(int numThisYear, int numLastYear)
{
// Write your DisplayRelationship() here.
WriteLine("This years contest has " + (numThisYear < numLastYear ? "fewer" :
numThisYear == numLastYear ? "the same" : "more") +
" contestants than last years.");
}
public static void GetContestantData(int numThisYear, string[] names, char[] talents, char[] talentCodes, string[] talentCodesStrings, int[] counts)
{
for (int i = 0; i < numThisYear; i++)
{
WriteLine("Enter the name of contestant " + (i + 1) + ":");
names[i] = ReadLine();
WriteLine("Enter the talent code (V, D, I, T) of contestant " + (i + 1) + ":");
string talentInput = ReadLine();
while (!char.TryParse(talentInput, out talents[i]) || !Array.Exists(talentCodesStrings, element => element[0] == talents[i]))
{
WriteLine("Invalid input. Enter a single character among V
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