Question: In my C# code I'm trying to add exception handling, so if the user types in something other than *, **, ***, ****, or *****
In my C# code I'm trying to add exception handling, so if the user types in something other than *, **, ***, ****, or ***** for a rating it will repeat until the user types in valid entry.
// This program read from a json file to retrive the year and title of some movies // prompting the user to rate them and saving the data back to the json file.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Text.Json; using System.Text.Json.Serialization;
namespace ConsoleAssignment4 { class Program { static void Main(string[] args) { multipleJson(); }
static void multipleJson() { int MoviesListCount = 0; string jsonDirectory = "JSON"; List
// go through the list one by one change the score if (theMovies != null && theMovies.Count > 0) { MoviesListCount = theMovies.Count;
for (int i = 0; i < MoviesListCount; i++) { Console.WriteLine(theMovies[i].title + " (" + theMovies[i].year + ")"); Console.WriteLine("Please provide rating for - " + theMovies[i].title + ": "); string userRating = Console.ReadLine(); theMovies[i].rating = userRating; } }
// print the movies with the rating Console.WriteLine("the rating for the movies are"); foreach (var aMovies in theMovies) { Console.WriteLine(aMovies.title + " (" + aMovies.year + ") - " + aMovies.rating); try { string jsonData = JsonSerializer.Serialize(aMovies); File.WriteAllText(aMovies.jsonFileName, jsonData); } catch (Exception ex) { Console.WriteLine("Could not write " + aMovies.id + " to file."); }
}
Console.ReadLine();
} static List
} return MoviesList;
}
}
class Movies { public string id { get; set; } public string title { get; set; } public string year { get; set; } public string runtime { get; set; } public string genre { get; set; } public string rating { get; set; } public string jsonFileName { get; set; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
