Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

 Microsoft Visual C# 7th edition.  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.

 I am working in CENGAGE MINDTAP. Here is my code, but the TryParse is not working. I am not getting the point for TryParse. thanks

using System; 
using static System.Console;
using System.Globalization;
class GreenvilleRevenue
{
   static void Main()
 {
  int numLastYear;
     int numThisYear;
     double revenue;
     int fee = 25;
     const int MAX_CONTESTANTS = 30;
     string[] names = new string[MAX_CONTESTANTS];
     char[] talents = new char[MAX_CONTESTANTS];
     char[] talentCodes = { 'S', 'D', 'M', 'O' };
     string[] talentCodesStrings = { "Singing", "Dancing", "Musical instrument", "Other" };
     int[] counts = { 0, 0, 0, 0 }; ;
     
numLastYear = GetContestantNumber("last", 0, 30);
numThisYear = GetContestantNumber("this", 0, 30);
WriteLine("Last year's competition had {0} contestants, and this year's has {1} contestants", numLastYear, numThisYear);
 revenue = (numThisYear * fee);
WriteLine("Revenue expected this year is {0}", revenue.ToString("C", CultureInfo.GetCultureInfo("en-US")));
DisplayRelationship(numThisYear, numLastYear);
GetContestantData(numThisYear, names, talents, talentCodes, talentCodesStrings, counts);
 GetLists(numThisYear, talentCodes, talentCodesStrings, names, talents, counts);
}
public static int GetContestantNumber(string when, int min, int max)
{
 int contestants;
 string contestantString;
 WriteLine("Enter number of contestants {0} year >> ", when);
 contestantString = ReadLine();
 while(! int.TryParse(contestantString, out contestants))
{
WriteLine("Invalid");
WriteLine("Reenter the valid constestants number");
contestantString = ReadLine();
}     
return contestants;
}
public static void DisplayRelationship( int numThisYear2, int numLastYear2)
{ if (numThisYear2 > 2 * numLastYear2)
WriteLine("The competition is more than twice as big this year!");
else
 if (numThisYear2 > numLastYear2)
 WriteLine("The competition is bigger than ever!");
 else
 if (numThisYear2 < numLastYear2)
 WriteLine("A tighter race this year! Come out and cast your vote!"); }
 public static void GetContestantData(int numThisYear, string[] names, char[] talents, char[] talentCodes, string[] talentCodesStrings, int[] counts)
{ int x = 0;
bool isValid;
string talentString;
while (x < numThisYear)
{ Write("Enter contestant name >> ");
names[x] = Console.ReadLine();
       
WriteLine("Talent codes are:");
for (int y = 0; y < talentCodes.Length; ++y)
WriteLine(" {0} {1}", talentCodes[y], talentCodesStrings[y]);
Write(" Enter talent code >> ");
talentString = ReadLine();
while(! char.TryParse(talentString, out talents[x]))
{ WriteLine(" Invalid format - entry must be a single character");
WriteLine("That is not a valid code");
Write(" Enter talent code >> ");
talentString = ReadLine();}
talents[x] = GetChar();
isValid = false;
while (!isValid)
{for (int z = 0; z < talentCodes.Length; ++z)
{
 if (talents[x] == talentCodes[z])
{isValid = true;
++counts[z];
}
}
if (!isValid)
{WriteLine("{0} is not a valid code", talents[x]);
Write(" Enter talent code >> ");
talents[x] = GetChar();
}
}
++x;
}   
}
   public static void GetLists(int numThisYear, char[] talentCodes, string[] talentCodesStrings, string[] names, char[] talents, int[] counts)
{int x;
char QUIT = 'Z';
char option;
bool isValid;
int pos = 0;
bool found;
WriteLine("\nThe types of talent are:");
for (x = 0; x < counts.Length; ++x)
WriteLine("{0, -20} {1, 5}", talentCodesStrings[x], counts[x]);
WriteLine("\nEnter a talent type or {0} to quit >> ", QUIT);
option = GetChar();
while (option != QUIT)
{ isValid = false;
for (int z = 0; z < talentCodes.Length; ++z)
{if (option == talentCodes[z])
 { isValid = true;
 pos = z;
}
}
 if (!isValid)
WriteLine("{0} is not a valid code", option);
else
{ WriteLine("\nContestants with talent {0} are:", talentCodesStrings[pos]);
found = false;
for (x = 0; x < numThisYear; ++x)
{ if (talents[x] == option)
{WriteLine(names[x]);
found = true;}
}
if (!found)
Console.WriteLine("No contestants had talent {0}",     talentCodesStrings[pos]);
}
WriteLine("\nEnter a talent type or {0} to quit >> ", QUIT);
 option = GetChar();
}
}
                 
private static char GetChar()
 {char validChar = '0';
bool isValidChar = false;
var charInput = string.Empty;
            
 while (!isValidChar)
{ charInput = Console.ReadLine();
 isValidChar = Char.TryParse(charInput, out validChar);
              
 if (!isValidChar)
 {
WriteLine("Invalid character - Please try again by entering one character!");
               /* WriteLine("Invalid format - entry must be a single character");
 }
                else
WriteLine("That is not a valid code");*/
}
}
return validChar;-
}
}



Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education