We are creating an application that allows customers to buy movie tickets. I am having issues with utilizing the age verification to then create a case switch that takes the Rating and shows them movies they would only be allowed to purchase.
For the ratings, we used an ENUM.
// an enum for all the ratings that a movie can have
public enum Rating
{
G,
PG,
PG13,
R,
NC17
}
And we are reading the movies from a file utilizing the following code:
List<Movie> movies = new List<Movie>(); //Creating a new list for movies
string input; //String to store input from user
string Filepath = @"C:\\Users\\jackpirtle\\source\\repos\\HW4PirtleCinema\\movie (1).txt"; //Creating string of filepath to read the movie.txt from my file explorer
StreamReader sr = new StreamReader(Filepath); //Creates a new streamreader that connects to the filepath we just created
// Begin creating the list of movies and the main program to gather user input.
do
{
do
{
Console.WriteLine("\nHere are a list of the movies showing at Starlight Cinema.\nEnter a corresponding number to select a movie.\n"); //Displays the listing of movies
if (File.Exists(Filepath)) //Only runs if the FilPath exists
{
while (!sr.EndOfStream) //Keeps Running untdil its the end of the file
{
string movieName = sr.ReadLine();
Rating rating;
switch (sr.ReadLine()) //Switch case statement to determine the ratings, and spit out the ratings from the enum that we created earlier
{
case "G":
rating = Rating.G;
break;
case "PG":
rating = Rating.PG;
break;
case "PG-13":
rating = Rating.PG13;
break;
case "R":
rating = Rating.R;
break;
case "NC-17":
rating = Rating.NC17;
break;
default:
rating = Rating.G;
break;
}
int screen = Convert.ToInt32(sr.ReadLine()); //Initializes variable screen as integer and coverts it to in as the streamReader
double price = Convert.ToDouble(sr.ReadLine()); //Initializes variabl eprice as double and converts it to double as streamReader
List<string> movieTimes = new List<string>(); //Creates a new list of movieTImes
movieTimes.Add(sr.ReadLine()); //Adds whatever is on the file to this list line by line
movieTimes.Add(sr.ReadLine());
movieTimes.Add(sr.ReadLine());
Movie movie = new Movie(movieName, rating, screen, price, movieTimes); //Calling movies given the Name, rating, screen, price and times
movies.Add(movie); //Adding the movies to Movie
}
for (int i = 1; i < movies.Count; i ) //Creating a for loop to display the movies onto the console
{
Console.WriteLine("" i "" "." movies[i].movie " |Rating: " movies[i].rating.ToString() " |Price: $" movies[i].price "|\n");
}
}
else
{
Console.WriteLine("\n------------------- Order Movie Tickets -------------------");
Console.WriteLine("\nThe file of movies can't be found."); //If the file doesn't exist, display an error message
}
userInput = ValidEntry(1, movies.Count); //Calls valid entry function
bool movieConfirmed = ConfirmSelection(movies[userInput].movie " - " movies[userInput].rating); //Initializes movieconfirmed as boolean to coorelate to the ConfirmSelection method
Console.Clear();
Console.WriteLine("\n------------------- Order Movie Tickets -------------------");
Console.WriteLine("\nYou have selected " movies[userInput].movie ". Please select a time below to view your selection!"); //Displays a message to user sayign what movie they selected, and asing what time they want to watch
} while (!confirm); //Code continues until it is !confirm
If there is a simple way to use a switch case to hide rated R movies from children under the age of 13, and so forth, then I would greatly appreciate any help. Been working on this for a while and have been stuck. Any feedback is welcome!
I tried to use a case switch and also a simple if else statement but am not sure how to hide certain elements of the file utilizing the ENUM variables.
CodePudding user response:
Is this what you're after?
Rating rating = sr.ReadLine() switch
{
"PG" => Rating.PG,
"PG-13" => Rating.PG13,
"R" => Rating.R,
"NC-17" => Rating.NC17,
_ => Rating.G,
};
CodePudding user response:
First find a suitable place for the user to enter the age
Console.Write("Please enter your age:");
int age = int.Parse(Console.ReadLine());
Then modify the level:
switch (sr.ReadLine()) //Switch case statement to determine the ratings, and spit out the ratings from the enum that we created earlier
{
case "G":
rating = Rating.G;
break;
case "PG":
rating = Rating.PG;
break;
case "PG-13":
if (age > 13)
{
rating = Rating.PG13;
}
else {
rating = Rating.G;
}
break;
case "R":
if (age > 13)
{
rating = Rating.R;
}
else {
rating = Rating.G;
}
break;
case "NC-17":
if (age > 17)
{
rating = Rating.NC17;
}
else {
rating = Rating.G;
}
break;
default:
rating = Rating.G;
break;
}
Another method: Divide users into three grades according to the entered age, and the three grades correspond to three ranges;
Console.Write("Please enter your age:");
int age = int.Parse(Console.ReadLine());
string Flag = "";
if (age < 13 && age > 0)
{
Flag = "A";
}
else if (age >= 17)
{
Flag = "B";
}
else if (age >= 13 && age < 17)
{
Flag = "C";
}
else
{
Flag = "error";
}
switch (Flag)
{
case "A":
//G PG
break;
case "B":
//G PG PG13 R NC17
break;
case "C":
//G PG PG13 R
break;
default:
//
break;
}