Question
Is there another way to write this code? using System; using System.Globalization; using static System.Console; class GreenvilleRevenue { static void Main() { // Prompt user
Is there another way to write this code?
using System;
using System.Globalization;
using static System.Console;
class GreenvilleRevenue
{
static void Main()
{
// Prompt user for number of contestants
int numContestants;
do
{
Write("Enter the number of contestants (0-30): ");
} while (!int.TryParse(ReadLine(), out numContestants) || numContestants < 0 || numContestants > 30);
// Create array of Contestant objects
Contestant[] contestants = new Contestant[numContestants];
// Prompt user for contestant data and create Contestant objects
for (int i = 0; i < numContestants; i++)
{
Write($" Enter data for contestant #{i + 1}: Name: ");
string name = ReadLine();
// Prompt for age
int age;
do
{
Write("Age: ");
} while (!int.TryParse(ReadLine(), out age) || age < 0);
// Prompt for talent code and store in contestant object
WriteLine("Talent codes are: S Singing D Dancing M Musical instrument O Other");
string code;
while(true){
Write("Talent code: ");
code = ReadLine().ToUpper();
// check valid code enter or not
if(code == "S" || code == "M" ||code == "D" ||code == "O"){
break;
}
else{
Write("select correct option from order ");
}
}
// get talent description given talent code
string desc="";
if (code == "S"){
desc = "Singing";
}
else if(code == "D"){
desc= "Dancing";
}
else if(code== "M"){
desc="Musical instrument";
}
else{
desc = "Others";
}
// compare age and create appropriate contestant object
Contestant contestant;
if (age <= 12)
{
// child contestant object creation
contestant = new ChildContestant(name,code,desc);
}
else if (age >= 13 && age <= 17)
{
// teen contestant object creation
contestant = new TeenContestant(name,code,desc);
}
else
{
// adult contestant object creation
contestant = new AdultContestant(name,code,desc);
}
contestants[i] = contestant;
}
// Calculate and display expected revenue
double revenue = 0;
foreach (Contestant c in contestants)
{
revenue += c.Fee;
}
WriteLine($" Revenue expected this year is {revenue.ToString("C", CultureInfo.GetCultureInfo("en-US"))} ");
// Display all contestants for each talent category
WriteLine("Contestants by talent category:");
WriteLine("Singing:");
foreach (Contestant c in contestants)
{
if (c.TalentCode == "S")
{
WriteLine(c.ToString());
}
}
WriteLine("Dancing:");
foreach (Contestant c in contestants)
{
if (c.TalentCode == "D")
{
WriteLine(c.ToString());
}
}
WriteLine("Musical instrument:");
foreach (Contestant c in contestants)
{
if (c.TalentCode == "M")
{
WriteLine(c.ToString());
}
}
WriteLine("Other:");
foreach (Contestant c in contestants)
{
if (c.TalentCode == "O")
{
WriteLine(c.ToString());
}
}
// Wait for user to acknowledge the results
WriteLine(" Press Enter to terminate...");
ReadLine();
}
}
// Contestant class with Fee property and ToString method and get,set accessors
class Contestant
{
// variables with get,set accessors
public string Name { get; set; }
public string TalentCode { get; set;}
public string TalentDescription { get; set; }
public double Fee { get; set; }
// contestant constructor
public Contestant(string name, string code, string description, double fee)
{
Name = name;
TalentCode = code;
TalentDescription = description;
Fee = fee;
}
public override string ToString()
{
return $"{Name} {TalentCode} Fee ${Fee:F2}";
}
}
// childcontestant Class implementation
class ChildContestant : Contestant
{
public ChildContestant(string name, string code, string description)
: base(name, code, description, 15.00)
{
}
public override string ToString()
{
return $"Child Contestant {base.ToString()}";
}
}
// teencontestant Class implementation
class TeenContestant : Contestant
{
public TeenContestant(string name, string code, string description)
: base(name, code, description, 20.00)
{
}
public override string ToString()
{
return $"Teen Contestant {base.ToString()}";
}
}
// adultcontestant Class implementation
class AdultContestant : Contestant
{
public AdultContestant(string name, string code, string description)
: base(name, code, description, 30.00)
{
}
public override string ToString()
{
return $"Adult Contestant {base.ToString()}";
}
}
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