public List <PostsInfo> Posts = new List <PostsInfo>(); // Creates a list from PostsInfo class public void AddPostData() { PostsInfo postsUser = new PostsInfo(); // Creates an instance of PostsInfo Console.Clear(); Console.WriteLine("\nPost Creation:\n"); Console.WriteLine("\nName..."); string savedPosterName = Console.ReadLine(); Console.WriteLine($"\nTitle..."); string savedPostTitle = Console.ReadLine(); Console.WriteLine($"\nPost..."); string savedPost = Console.ReadLine(); while (true) // Will run until the Console.ReadLine() has no char (empty string) and breaks the while loop { string extraText = Console.ReadLine(); if (extraText == "") { break; } savedPost = savedPost + "\n" + extraText; } Console.Clear(); Console.WriteLine($"\nPost Added. Returning to Main Menu.\n"); // Adds all the data into the postUser variable postsUser.DateOfPost = DateTime.Now; postsUser.PosterName = savedPosterName; postsUser.PosterTitle = savedPostTitle; postsUser.PosterComment = savedPost; Posts.Add(postsUser); // Adds the data above (postUser - variable) into the list persons var jsonData = PostsClass.ConvertToJson(Posts); // Converts the Posts data into JSON and then writes it to a file below this code WritePostData(jsonData); }
static void Main(string[] args) { PostsClass postsInfo = new PostsClass(); // Creates an instance of AddToFile // Checker for the PostsData.json file to make sure the program does not crash if the file does not exist or if the file is empty. FileInfo FileChecker = new FileInfo("PostsData.json"); if (!File.Exists("PostsData.json")) { FileStream ff = FileChecker.Create(); // Creates a file called "PostsData.json" if it does not exist and then exits so that the file is not locked. ff.Close(); } long length = FileChecker.Length; if (length > 10) // Makes sure the file has content in it before adding it to Posts { postsInfo.Posts = postsInfo.ReadJsonFromFile(); // Adds the data from JSON file into postsInfo.posts (list) } // End of File Checker bool menuChoice = true; Console.Clear(); while (menuChoice) { Console.WriteLine(@" ______ _ | _ (_) | | | |_ __ _ _ __ _ _ | | | | |/ _` | '__| | | | | |/ /| | (_| | | | |_| | |___/ |_|\__,_|_| \__, | __/ | |___/ "); Console.WriteLine(""); Console.WriteLine("1. Create a new post"); Console.WriteLine("2. Show all posts"); Console.WriteLine("3. Search post"); Console.WriteLine("4. Exit"); char buttonPressed; buttonPressed = Console.ReadKey(true).KeyChar; switch (buttonPressed) { case '1': postsInfo.AddPostData(); // Method break; case '2': postsInfo.OutputPersonData(); // Method break; case '3': postsInfo.OutputSearchParameter(); // Method break; case '4': Console.WriteLine("\nGoodbye\n"); // Exits the program menuChoice = false; break; default: Console.Clear(); Console.WriteLine("\nIncorrect: Please choose a number between 1-4.\n"); break; } } }