Question

Case Problem 11 - 1: Modify the GreenvilleRevenue program created in the previous chapter so that...

Case Problem 11 - 1:

Modify the GreenvilleRevenue program created in the previous chapter so that it performs the following tasks:

The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. Use exception-handling techniques to ensure a valid value and display the error message: Number must be between 0 and 30

The program prompts the user for talent codes. 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.

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 and display the following message: x is not a valid code

where x was the invalid code entered into the console

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:---                                                      Date:--29/03/2019

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;
Console.WriteLine(c);
}
Console.WriteLine("\n ********************************************************** \n");
Console.WriteLine("\nTotal Revenue: $" + totalrevenue);


PromptTalentCodes(thisYearContestants, contestants);
}


static int GetContestants()
{
int contestants = -1;
string input = Console.ReadLine();
bool valid = false;
while (!valid)
{
try
{
if (!Int32.TryParse(input, out contestants) || (contestants < 0 || contestants > 30))
{
throw new Exception("Error. Please enter a value between 0 and 30.");
}
else
{
valid = true;
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
input = Console.ReadLine();
}
}
return contestants;
}

static void FillArrays(int thisYearContestants, Contestant[] contestants)
{
int x = 0;
string name;
int age;
char talent;
while (x < thisYearContestants)
{
Console.WriteLine("\nEnter 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;
}
}
}


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

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

Console.WriteLine("To see the contestants by talent, please search a valid talent code or " + QUIT + " to quit: ");
Console.WriteLine("\nThe 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))
{
isValid = false;
Console.WriteLine("\nInvalid talent code. Please enter a valid talent code or " + QUIT + " to quit: ");
}
else
{
if (input == QUIT)
isValid = true;
else
{
for (int k = 0; k < Contestant.talentCodes.Length; ++k)
{
if (input == Contestant.talentCodes[k])
{
isValid = true;
}
}
try
{
if (!isValid)
{
throw new Exception(input + " is not a valid code.\n Please enter a talent code or " + QUIT + " to quit: ");
}
else
{
Console.WriteLine("\nContestants 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("\nEnter another talent code or " + QUIT + " to quit: ");
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
}


}


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;
try
{
if (pos == talentCodes.Length)
{
throw new Exception("Invalid");
}
else
{
talentCode = value;
talent = talentStrings[pos];
}
}
catch(Exception e)
{
talentCode = 'I';
talent = e.Message;
}
}

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

public int EntryFee
{
get
{
return entryFee;
}
set
{
entryFee = value;
}

}
}

  
  
class ChildContestant : Contestant
{
public ChildContestant()
{
EntryFee = 15;
}
public override string ToString()
{
return "\n Name: " + Name + "\n Talent Code: " + TalentCode +
"\n Talent Description: " + Talent + "\nEntry Fee: " + EntryFee +
"\nAge Category: Child";
}
}   
  
class TeenContestant : Contestant
{
public TeenContestant()
{
EntryFee = 20;
}
public override string ToString()
{
return "\n Name: " + Name + "\n Talent Code: " + TalentCode +
"\n Talent Description: " + Talent + "\nEntry Fee: " + EntryFee +
"\nAge Category: Teen";
}
}
  
  
class AdultContestant : Contestant
{
public AdultContestant()
{
EntryFee = 30;
}
public override string ToString()
{
return "\n Name: " + Name + "\n Talent Code: " + TalentCode +
"\n Talent Description: " + Talent + "\nEntry Fee: " + EntryFee +
"\nAge Category: Adult";
}
}
  

Add a comment
Know the answer?
Add Answer to:
Case Problem 11 - 1: Modify the GreenvilleRevenue program created in the previous chapter so that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Case Program 10 – 1: Part 1: Previously, you created a Contestant class for the Greenville Idol c...

    Case Program 10 – 1: Part 1: Previously, you created a Contestant class for the Greenville Idol competition. The class includes a contestant’s 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 Fee that holds the entry fee for each category, and add get and set accessors. Extend the Contestant class to create three...

  • Use a C# In previous chapters, you have created programs for the Greenville Idol competition. Now...

    Use a C# In previous chapters, you have created programs for the Greenville Idol competition. Now create a Contestant class with the following characteristics: The Contestant class contains public static arrays that hold talent codes and descriptions. Recall that the talent categories are Singing, Dancing, Musical instrument, and Other. Name these fields talentCodes and talentStrings respectively. The class contains an auto-implemented property Name that holds a contestant’s name. The class contains fields for a talent code (talentCode) and description (talent)....

  • In Chapter 1, you created two programs to display the motto for the Greenville Idol competition...

    In Chapter 1, you created two programs to display the motto for the Greenville Idol competition that is held each summer during the Greenville County Fair. Now write a program named GreenvilleRevenue that prompts a user for the number of contestants entered in last year’s competition and in this year’s competition. Display all the input data. Compute and display the revenue expected for this year’s competition if each contestant pays a $25 entrance fee. Also display a statement that indicates...

  • In Chapter 11, you created the most recent version of the GreenvilleRevenue program, which prompts the...

    In Chapter 11, you created the most recent version of the GreenvilleRevenue program, which prompts the user for contestant data for this year’s Greenville Idol competition. Now, save all the entered data to a Greenville.ser file that is closed when data entry is complete and then reopened and read in, allowing the user to view lists of contestants with requested talent types. The program should output the name of the contestant, the talent, and the fee. using System; using static...

  • Be sure to include a message to the user explaining the purpose of the program before...

    Be sure to include a message to the user explaining the purpose of the program before any other printing to the screen. Be sure to include information about which document codes are valid. Within the main method, please complete the following tasks in java program: Create a loop that allows the user to continue to enter two-character document designations until a sentinel value is entered (ex. “XX”). If the user enters a valid document code, please echo that value back...

  • JAVA 2. (8 points) Write a code segment which prompts the user to enter the price...

    JAVA 2. (8 points) Write a code segment which prompts the user to enter the price of a tour and receives input from the user. A valid tour price is between $52.95 and $259.95, inclusive. Your program should continue to repeat until a valid tour price has been entered. If the user enters an invalid price, display a message describing why the input is invalid. You may assume that the user only enters a real number. The user will not...

  • Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program...

    Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program named GetIDAndAge that continually prompts the user for an ID number and an age until a terminal 0 is entered for both. If the ID and age are both valid, display the message ID and Age OK. Throw a DataEntryException if the ID is not in the range of valid ID numbers (0 through 999), or if the age is not in the range...

  • Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anythi...

    Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anything. No code outside of a function! Median List Traversal and Exception Handling Create a menu-driven program that will accept a collection of non-negative integers from the keyboard, calculate the mean and median values and display those values on the screen. Your menu should have 6 options 50% below 50% above Add a number to the list/array...

  • I am required to use the try - catch block to validate the input as the...

    I am required to use the try - catch block to validate the input as the test data uses a word "Thirty" and not numbers. The program needs to validate that input, throw an exception and then display the error message. If I don't use the try - catch method I end up with program crashing. My issue is that I can't get the try - catch portion to work. Can you please help? I have attached what I have...

  • Weather Program Create an application to interacts with a web service in order to obtain data....

    Weather Program Create an application to interacts with a web service in order to obtain data. Program must prompt the user for their city or zip code and request weather forecast data from OpenWeatherMap (https://openweathermap.org). Program must display the weather information in a READABLE format to the user. Requirements: Create a header for your program just as you have in the past. Create a Python Application which asks the user for their zip code or city. Use the zip code...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT