Question

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 System.Console;
class GreenvilleRevenue
{
static void Main()
{
const int MIN_CONTESTANTS = 0;
const int MAX_CONTESTANTS = 30;
int num;
int revenue = 0;
const char QUIT = 'Z';
char option = ' ';;
Contestant[] contestants = new Contestant[MAX_CONTESTANTS];
num = getContestantNumber(MIN_CONTESTANTS, MAX_CONTESTANTS);
revenue = getContestantData(num, contestants, revenue);
WriteLine("\n\nRevenue expected this year is {0}", revenue.ToString("C"));
while(option != QUIT)
option = getLists(num, contestants);   
}
private static int getContestantNumber(int min, int max)
{
string entryString;
int num = max + 1;
Write("Enter number of contestants >> ");
entryString = ReadLine();
while(num < min || num > max)
{
if(!int.TryParse(entryString, out num))
{
WriteLine("Format invalid");
num = max + 1;
Write("Enter number of contestants >> ");
entryString = ReadLine();
}
else
{
try
{
if(num < min || num > max)
throw(new ArgumentException());
}
catch(ArgumentException e)
{
WriteLine(e.Message);
WriteLine("Number must be between {0} and {1}", min, max);
num = max + 1;
Write("Enter number of contestants >> ");
entryString = ReadLine();
}
}
}
return num;
}
private static int getContestantData(int num, Contestant[] contestants, int revenue)
{
const int ADULTAGE = 17;
const int TEENAGE = 12;
int x = 0;
string name;
char talent;
int age;
int pos;
while(x < num)
{
Write("Enter contestant name >> ");
name = ReadLine();
WriteLine("Talent codes are:");
for(int y = 0; y < Contestant.talentCodes.Length; ++y)
WriteLine(" {0} {1}", Contestant.talentCodes[y], Contestant.talentStrings[y]);
Write(" Enter talent code >> ");
char.TryParse(ReadLine(), out talent);
try
{
validateCode(talent, out pos);
}
catch(ArgumentException e)
{
WriteLine(e.Message);
WriteLine("{0} is not a valid code. Assigned as Invalid.", talent);
}
Write(" Enter contestant's age >> ");
int.TryParse(ReadLine(), out age);
if(age > ADULTAGE)
contestants[x] = new AdultContestant();
else
if(age > TEENAGE)
contestants[x] = new TeenContestant();
else
contestants[x] = new ChildContestant();
contestants[x].Name = name;
contestants[x].TalentCode = talent;
revenue += contestants[x].Fee;
++x;
}
return revenue;
}
private static char getLists(int num, Contestant[] contestants)
{
int x;
char QUIT = 'Z';
char option = ' ';
bool isValid;
int pos = 0;
bool found;
WriteLine("\nThe types of talent are:");
for(x = 0; x < Contestant.talentStrings.Length; ++x)
WriteLine("{0, -6}{1, -20}", Contestant.talentCodes[x], Contestant.talentStrings[x]);
Write("\nEnter a talent type or {0} to quit >> ", QUIT);
isValid = false;
while(!isValid)
{
if(!char.TryParse(ReadLine(), out option))
{
isValid = false;
  
WriteLine("Invalid format - entry must be a single character");
Write("\nEnter a talent type or {0} to quit >> ", QUIT);
}
else
{
if(option == QUIT)
isValid = true;
else
{
try
{
validateCode(option, out pos);
isValid = true;
}
catch(ArgumentException e)
{
WriteLine(e.Message);
WriteLine("{0} is not a valid code", option);
Write("\nEnter a talent type or {0} to quit >> ", QUIT);
isValid = false;
}
}
if(isValid && option != QUIT)
{

WriteLine("\nContestants with talent {0} are:", Contestant.talentStrings[pos]);
found = false;
for(x = 0; x < num; ++x)
{
if(contestants[x].TalentCode == option)
{
WriteLine(contestants[x].ToString());
found = true;
}
}
if(!found)
{
WriteLine("No contestants had talent {0}", Contestant.talentStrings[pos]);
isValid = false;
Write("\nEnter a talent type or {0} to quit >> ", QUIT);
}
}
}
}
return option;
}
public static void validateCode(char option, out int pos)
{
bool isValid = false;
pos = Contestant.talentCodes.Length - 1;
for(int z = 0; z < Contestant.talentCodes.Length; ++z)
{
if(option == Contestant.talentCodes[z])
{
isValid = true;
pos = z;
}
}
if(!isValid)
throw(new ArgumentException());
}
}

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 fee;
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;
if(pos == talentCodes.Length)
{
talentCode = 'I';
talent = "Invalid";
}
else
{
talentCode = value;
talent = talentStrings[pos];
}
}   

}
public string Talent
{
get
{
return talent;
}
}
public int Fee
{
get
{
return fee;
}
set
{
fee = value;
}
}
}
class AdultContestant : Contestant
{
public int ADULT_FEE = 30;
public AdultContestant()
{
Fee = ADULT_FEE;
}
public override string ToString()
{
return("Adult Contestant " + Name + " " + TalentCode + " Fee " + Fee.ToString("C"));
}
}   
class TeenContestant : Contestant
{
public int TEEN_FEE = 20;
public TeenContestant()
{
Fee = TEEN_FEE;
}
public override string ToString()
{
return("Teen Contestant " + Name + " " + TalentCode + " Fee " + Fee.ToString("C"));
}
}
class ChildContestant : Contestant
{
public int CHILD_FEE = 15;
public ChildContestant()
{
Fee = CHILD_FEE;
}
public override string ToString()
{
return("Child Contestant " + Name + " " + TalentCode + " Fee " + Fee.ToString("C"));
}
}

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

/*
* File Name: TeenContestant.cs
* NameSpace: GreenvilleRevenueApp
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GreenvilleRevenueApp
{
class TeenContestant : Contestant
{
public int TEEN_FEE = 20;
public TeenContestant()
{
Fee = TEEN_FEE;
}
public override string ToString()
{
return ("Teen Contestant " + Name + " " + TalentCode + " Fee " + Fee.ToString("C"));
}
}
}

/*
* File Name: GreenvilleRevenue.cs
* NameSpace: GreenvilleRevenueApp
*/
using System;
using static System.Console;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace GreenvilleRevenueApp
{

class GreenvilleRevenue
{
static void Main()
{
const int MIN_CONTESTANTS = 0;
const int MAX_CONTESTANTS = 30;
int num;
int revenue = 0;
const char QUIT = 'Z';
char option = ' '; ;
Contestant[] contestants = new Contestant[MAX_CONTESTANTS];
  
num = getContestantNumber(MIN_CONTESTANTS, MAX_CONTESTANTS);
int []loadRes = loadContestants(num, contestants, revenue);
int startPos = loadRes[0];
revenue = loadRes[1];
WriteLine("\n\nRevenue expected this year is {0} after loading data from file", revenue.ToString("C"));

revenue = getContestantData(num, startPos,contestants, revenue);
WriteLine("\n\nRevenue expected this year is {0}", revenue.ToString("C"));
while (option != QUIT)
option = getLists(num, contestants);

saveContestants(num, contestants);
WriteLine("\nEnter any key to continue");
ReadKey();
}
private static int getContestantNumber(int min, int max)
{
string entryString;
int num = max + 1;
Write("\nEnter number of contestants >> ");
entryString = ReadLine();
while (num < min || num > max)
{
if (!int.TryParse(entryString, out num))
{
WriteLine("Format invalid");
num = max + 1;
Write("\nEnter number of contestants >> ");
entryString = ReadLine();
}
else
{
try
{
if (num < min || num > max)
throw (new ArgumentException());
}
catch (ArgumentException e)
{
WriteLine(e.Message);
WriteLine("Number must be between {0} and {1}", min, max);
num = max + 1;
Write("\nEnter number of contestants >> ");
entryString = ReadLine();
}
}
}
return num;
}
private static int getContestantData(int num, int startPos,Contestant[] contestants, int revenue)
{
const int ADULTAGE = 17;
const int TEENAGE = 12;
int x = startPos;
string name;
char talent;
int age;
int pos;
while (x < num)
{
Write("\nEnter contestant name >> ");
name = ReadLine();
WriteLine("Talent codes are:");
for (int y = 0; y < Contestant.talentCodes.Length; ++y)
WriteLine(" {0} {1}", Contestant.talentCodes[y], Contestant.talentStrings[y]);
Write(" Enter talent code >> ");
char.TryParse(ReadLine(), out talent);
try
{
validateCode(talent, out pos);
}
catch (ArgumentException e)
{
WriteLine(e.Message);
WriteLine("{0} is not a valid code. Assigned as Invalid.", talent);
}
Write(" Enter contestant's age >> ");
int.TryParse(ReadLine(), out age);
if (age > ADULTAGE)
contestants[x] = new AdultContestant();
else
if (age > TEENAGE)
contestants[x] = new TeenContestant();
else
contestants[x] = new ChildContestant();
contestants[x].Name = name;
contestants[x].TalentCode = talent;
revenue += contestants[x].Fee;
++x;
}
return revenue;
}
private static char getLists(int num, Contestant[] contestants)
{
int x;
char QUIT = 'Z';
char option = ' ';
bool isValid;
int pos = 0;
bool found;
WriteLine("\nThe types of talent are:");
for (x = 0; x < Contestant.talentStrings.Length; ++x)
WriteLine("{0, -6}{1, -20}", Contestant.talentCodes[x], Contestant.talentStrings[x]);
Write("\nEnter a talent type or {0} to quit >> ", QUIT);
isValid = false;
while (!isValid)
{
if (!char.TryParse(ReadLine(), out option))
{
isValid = false;

WriteLine("Invalid format - entry must be a single character");
Write("\nEnter a talent type or {0} to quit >> ", QUIT);
}
else
{
if (option == QUIT)
isValid = true;
else
{
try
{
validateCode(option, out pos);
isValid = true;
}
catch (ArgumentException e)
{
WriteLine(e.Message);
WriteLine("{0} is not a valid code", option);
Write("\nEnter a talent type or {0} to quit >> ", QUIT);
isValid = false;
}
}
if (isValid && option != QUIT)
{

WriteLine("\nContestants with talent {0} are:", Contestant.talentStrings[pos]);
found = false;
for (x = 0; x < num; ++x)
{
if (contestants[x].TalentCode == option)
{
WriteLine("\n"+contestants[x].ToString());
found = true;
}
}
if (!found)
{
WriteLine("\n" + "No contestants had talent {0}", Contestant.talentStrings[pos]);
isValid = false;
Write("\nEnter a talent type or {0} to quit >> ", QUIT);
}
}
}
}
return option;
}
public static void validateCode(char option, out int pos)
{
bool isValid = false;
pos = Contestant.talentCodes.Length - 1;
for (int z = 0; z < Contestant.talentCodes.Length; ++z)
{
if (option == Contestant.talentCodes[z])
{
isValid = true;
pos = z;
}
}
if (!isValid)
throw (new ArgumentException());
}

private static int[] loadContestants(int num, Contestant[] contestants, int revenue)
{
int x = 0;
StreamReader sr = null;
int[] res = new int[2];
try
{
sr = new StreamReader("Greenville.ser");
string line = sr.ReadLine();
int pos;
int fee;
string name;
char talent;
  
while (line != null && x < num)
{
string[] data = line.Trim().Split(',');
if(data.Length == 3)
{
name = data[0];
char.TryParse(data[1], out talent);
try
{
validateCode(talent, out pos);
}
catch (ArgumentException e)
{
WriteLine(e.Message);
WriteLine("{0} is not a valid code. Assigned as Invalid.", talent);
}
int.TryParse(data[2], out fee);
if (fee == 30)
contestants[x] = new AdultContestant();
else if(fee == 30)
{
contestants[x] = new TeenContestant();
}
else
{
contestants[x] = new ChildContestant();
}

contestants[x].Name = name;
contestants[x].TalentCode = talent;
revenue += contestants[x].Fee;
x++;
}
else
{
WriteLine("\nLine: " + line + " has invalid number of fields");
}
line = sr.ReadLine();
}
WriteLine("\nLoaded " + x + " Contestants from file : Greenville.ser");
sr.Close();
}
catch(Exception e)
{
Console.WriteLine("\nError occured while reding file: Greenville.ser" );
if(sr != null)
{
sr.Close();
}
  
}
res[0] = x;
res[1] = revenue;
return res;
  
}
private static void saveContestants(int num, Contestant[] contestants)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter("Greenville.ser");
for(int i =0;i<num;i++)
{
sw.WriteLine(String.Format("{0:},{1:},{2:}",contestants[i].Name,contestants[i].TalentCode,contestants[i].Fee));
Console.WriteLine("Writing data: "+String.Format("{0:},{1:},{2:}", contestants[i].Name, contestants[i].TalentCode, contestants[i].Fee));
}
sw.Close();
}
catch (Exception e)
{
Console.WriteLine("\nError occured while writing to file : Greenville.ser \nerror log: " + e.Message);
if(sw != null)
{
sw.Close();
}
  
}

}
}
}

/*
* File Name: Contestant.cs
* NameSpace: GreenvilleRevenueApp
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GreenvilleRevenueApp
{
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 fee;
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;
if (pos == talentCodes.Length)
{
talentCode = 'I';
talent = "Invalid";
}
else
{
talentCode = value;
talent = talentStrings[pos];
}
}

}
public string Talent
{
get
{
return talent;
}
}
public int Fee
{
get
{
return fee;
}
set
{
fee = value;
}
}
}
}


/*
* File Name: ChildContestant.cs
* NameSpace: GreenvilleRevenueApp
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GreenvilleRevenueApp
{
class ChildContestant : Contestant
{
public int CHILD_FEE = 15;
public ChildContestant()
{
Fee = CHILD_FEE;
}
public override string ToString()
{
return ("Child Contestant " + Name + " " + TalentCode + " Fee " + Fee.ToString("C"));
}
}
}

/*
* File Name: AdultContestant.cs
* NameSpace: GreenvilleRevenueApp
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GreenvilleRevenueApp
{
class AdultContestant : Contestant
{
public int ADULT_FEE = 30;
public AdultContestant()
{
Fee = ADULT_FEE;
}
public override string ToString()
{
return ("Adult Contestant " + Name + " " + TalentCode + " Fee " + Fee.ToString("C"));
}
}
}

Add a comment
Know the answer?
Add Answer to:
In Chapter 11, you created the most recent version of the GreenvilleRevenue program, which prompts the...
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
  • 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 4 of your book, you created an interactive application named MarshallsRevenue that prompts a...

    In Chapter 4 of your book, you created an interactive application named MarshallsRevenue that prompts a user for the number of interior and exterior murals scheduled to be painted during a month and computes the expected revenue for each type of mural. The program also prompts the user for the month number and modifies the pricing based on requirements listed in Chapter 4. Now, modify the program so that the user must enter a month value from 1 through 12....

  • 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...

  • The following file has syntax and/or logical errors. In each case, determine the problem, and fix...

    The following file has syntax and/or logical errors. In each case, determine the problem, and fix the program. using System; using static System.Console; using System.IO; class DebugFourteen1 { static void Main() { string fileName; string directory; string path; string files; int x; Write("Enter a directory: "); directory = ReadLine(); if(Directory.Exists(Directory)) { files = Directory.GetFiles(directory); if(files.Length = 0) WriteLine("There are no files in " + directory); else { WriteLine(directory + " contains the following files"); for(x = 0; x <= files.Length;...

  • So i need help with this program, This program is supposed to ask the user to...

    So i need help with this program, This program is supposed to ask the user to input their weight on earth so I would put "140" then it would ask "enter planet name, Min, Max or all" and if the user typed in all it would display all the planets and your weight on them. Right now Im suck on the part where im required to do a switch statement with a linear search. Below this im going to put...

  • In Chapter 5, you wrote a HomeSales application for three salespeople: Danielle, Edward, and Francis. Now,...

    In Chapter 5, you wrote a HomeSales application for three salespeople: Danielle, Edward, and Francis. Now, using the code you wrote in Chapter 5, Programming Exercise 5, modify the program to use arrays to store the salesperson names, allowed initials, and accumulated sales values. 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...

  • In Chapter 5, you wrote a HomeSales application for three salespeople: Danielle, Edward, and Francis. Now,...

    In Chapter 5, you wrote a HomeSales application for three salespeople: Danielle, Edward, and Francis. Now, using the code you wrote in Chapter 5, Programming Exercise 5, modify the program to use arrays to store the salesperson names, allowed initials, and accumulated sales values. 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...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • Write a program in Java that prompts a user for Name and id number. and then...

    Write a program in Java that prompts a user for Name and id number. and then the program outputs students GPA MAIN import java.util.StringTokenizer; import javax.swing.JOptionPane; public class Main {    public static void main(String[] args) {                       String thedata = JOptionPane.showInputDialog(null, "Please type in Student Name. ", "Student OOP Program", JOptionPane.INFORMATION_MESSAGE);        String name = thedata;        Student pupil = new Student(name);                   //add code here       ...

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