Using c#: In previous chapters, you have created programs for the Greenville Ido
ID: 3673115 • Letter: U
Question
Using 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.
- The class contains an auto-implemented property that holds a contestant's name
-The class contains fields for a talent code and description. The set accessor for the code assigns a code only if it is valid. Otherwise, it assigns I for Invalid. The talent description is a read-only property that is assigned a value when the code is set.
Modify the GreenvilleRevenue program so that it uses the Contestant class and 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. The program continues to prompt the user until a valid value is entered.
- The expected revenue is calculated and displayed. The revenue is $25 per contestant.
- The program prompts the user for names and talent codes for each contestant entered. Along with the prompt for a talent code, display a list of the valid categories.
- After data entry is complete, the program displays the valid talent categories and then continuously prompts the user for talent codes and displays the names of all contestants in the category. Appropriate messages are displayed if the entered code is not a character or a valid code.
Below is the code from previous chapters (all of the code below still needs to be a part of the new code- just needs to be put into a class):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GreenvilleRevenue1
{
class Program
{
static void Main(string[] args)
{
int contCurr;
int contLast;
Console.WriteLine("Please enter the number of contestants from last years contest");
contLast = cont();
Console.WriteLine(" Please enter the number of contestants from this years contest");
contCurr = cont();
array(contCurr);
comp(contCurr, contLast);
}
public static int cont()
{
int last = 0;
string b;
b= (Console.ReadLine());
int.TryParse(b, out last);
while (last > 30 || last < 0)
{
Console.WriteLine(" You have entered and invalid response, please enter a valid number between 0 and 30.");
b = (Console.ReadLine());
int.TryParse(b, out last);
}
return last;
}
public static void array(int thisyear)
{
string[] contestant = new string[thisyear];
string[] skill = new string[thisyear];
for (int x = 0; x < thisyear; ++x)
{
Console.WriteLine(" Please enter contestant " + (x + 1) + "'s name");
contestant[x] = Console.ReadLine();
bool correct = false;
while (!correct)
{
Console.WriteLine(" Please enter contestant " + (x + 1) + " 's skill, 'S' for sing 'D' for dance 'M' for " +
"musical instrument 'O' for other");
string type = Console.ReadLine().ToUpper();
if (type == "S" || type == "D" || type == "M" || type == "O")
{
skill[x] = type;
correct = true;
}
else {
Console.WriteLine(" Please enter a valid parameter");
}
}
}
talent(skill, contestant);
}
public static void talent(string[] skill, string[] contestant)
{
int dance = 0;
int instrument = 0;
int sing = 0;
int other = 0;
string entry;
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} contestant(s) with other skills", other);
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();
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();
}
}
public static void comp(int contCurr, int contLast)
{
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 * 25));
}
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 * 25));
}
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 * 25));
}
}
}
}
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GreenvilleRevenue1
{
class Contestant
{
public static string[] talentCodes = { "S", "D", "M", "O" };
public static string[] talentDescr = { "Singing", "Dancing", "Musical instrument", "Other" };
private string name;
private string skillCode;
private string skillDescr;
public string getName()
{
return name;
}
public void setName(string cname)
{
name = cname;
}
public string getSkillCode()
{
return skillCode;
}
public void setSkillCode(string scode)
{
bool found = false;
int i;
for(i = 0; i < talentCodes.Length; i++)
{
if (talentCodes[i] == scode)
{
found = true;
break;
}
}
if (found)
{
skillCode = scode;
skillDescr = talentDescr[i];
}
else
skillCode = "I";
}
public string getSkillDescr()
{
return skillDescr;
}
}
class Program
{
static void Main(string[] args)
{
int contCurr;
int contLast;
Console.WriteLine("Please enter the number of contestants from last years contest");
contLast = cont();
Console.WriteLine(" Please enter the number of contestants from this years contest");
contCurr = cont();
array(contCurr);
comp(contCurr, contLast);
}
public static int cont()
{
int last = 0;
string b;
b = (Console.ReadLine());
int.TryParse(b, out last);
while (last > 30 || last < 0)
{
Console.WriteLine(" You have entered and invalid response, please enter a valid number between 0 and 30.");
b = (Console.ReadLine());
int.TryParse(b, out last);
}
return last;
}
public static void array(int thisyear)
{
//string[] contestant = new string[thisyear];
//string[] skill = new string[thisyear];
Contestant[] contestant = new Contestant[thisyear];
string name;
for (int x = 0; x < thisyear; ++x)
{
Console.WriteLine(" Please enter contestant " + (x + 1) + "'s name");
name = Console.ReadLine();
contestant[x] = new Contestant();
contestant[x].setName(name);
bool correct = false;
while (!correct)
{
Console.WriteLine(" Please enter contestant " + (x + 1) + " 's skill, 'S' for sing 'D' for dance 'M' for " +
"musical instrument 'O' for other");
string type = Console.ReadLine().ToUpper();
for (int i = 0; i < Contestant.talentCodes.Length; i++)
{
if (Contestant.talentCodes[i] == type)
{
contestant[x].setSkillCode(type);
correct = true;
}
}
if(!correct) {
Console.WriteLine(" Please enter a valid parameter");
}
}
}
talent(contestant);
}
public static void talent(Contestant[] contestant)
{
int dance = 0;
int instrument = 0;
int sing = 0;
int other = 0;
string entry;
for (int x = 0; x < contestant.Length; ++x)
{
if (contestant[x].getSkillCode() == "O")
{
++other;
}
else if (contestant[x].getSkillCode() == "S")
{
++sing;
}
else if (contestant[x].getSkillCode() == "D")
{
++dance;
}
else if (contestant[x].getSkillCode() == "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} contestant(s) with other skills", other);
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();
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 < contestant.Length; ++x)
{
if (entry == contestant[x].getSkillCode())
Console.WriteLine(" Contestant " + contestant[x].getName() + " skill " + contestant[x].getSkillDescr());
}
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();
}
}
public static void comp(int contCurr, int contLast)
{
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 * 25));
}
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 * 25));
}
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 * 25));
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.