//This method allows the user to enter new information and write a new post to the file. public static void AddPosts(List <Posts> posts) { Console.Clear(); List <Posts> bigAlPosts = PostFile.GetPosts(); //Show the User Current Posts Console.WriteLine("Here are the current posts:"); bigAlPosts.Sort(Posts.CompareByID); PrintAllPosts(bigAlPosts); //Prompt the user to enter new information Console.WriteLine("\nEnter the ID of the new post"); int newPostID = int.Parse(Console.ReadLine()); Console.WriteLine("Enter what you would like to say in the new post"); string newPost = Console.ReadLine(); //Write to the last line of the file bigAlPosts.Add(new Posts() { ID = newPostID, Post = newPost, Time = DateTime.Now }); //Show the user the updated posts Console.WriteLine("Here are the updated posts:\n"); bigAlPosts.Sort(Posts.CompareByDate); PrintAllPosts(bigAlPosts); //Save to the file PostFile.SaveAllPosts(bigAlPosts); Console.WriteLine("\n\nPress Enter to return to main Menu..."); Console.ReadLine(); Console.Clear(); }
//Delete Post method public static void DeletePost(List <Post> posts) { Post[] myPosts = new Post[100]; PostFile postFile = new PostFile(); //variable declared int removedPost = 0; PostFile.GetPosts(); Console.WriteLine("Which post would you like removed from the file? (give a number) "); PrintAllPosts(posts); //get user input removedPost = int.Parse(Console.ReadLine()); //remove post int foundIndex = posts.FindIndex(x => x.PostID == removedPost); posts.RemoveAt(foundIndex); StreamWriter outFile = new StreamWriter("posts.txt"); //update file foreach (Post post in posts) { outFile.WriteLine(post.ToFile()); } Console.WriteLine("Your post was removed from the file! "); //show updated file to user foreach (Post post in posts) { Console.WriteLine(post.ToString()); } outFile.Close(); }
static void Main(string[] args) { List <Post> bigAlPosts = PostFile.GetPosts(); bigAlPosts.Sort(); //Displays main menu after getting the user menu choice DisplayMainMenu(); string menuChoice = GetMenuChoice(); //while loop for menu choice while (menuChoice != "4") { if (menuChoice == "1") { Console.WriteLine("\nYou chose to show all posts! "); bigAlPosts.Sort(); PostUtils.PrintAllPosts(bigAlPosts); } else if (menuChoice == "2") { Console.WriteLine("\nYou chose to add a post! "); PostUtils.AddPost(bigAlPosts); } else { Console.WriteLine("\nYou chose to delete a post by ID! "); PostUtils.DeletePost(bigAlPosts); } DisplayMainMenu(); menuChoice = GetMenuChoice(); } Console.WriteLine("Goodbye! "); Console.ReadKey();
//This method takes the ID from the user and deletes that post public static void DeletePost(List <Posts> posts) { Console.Clear(); List <Posts> bigAlPosts = PostFile.GetPosts(); Console.WriteLine("Here are the posts:\n\n"); bigAlPosts.Sort(Posts.CompareByID); PostUtil.PrintAllPosts(bigAlPosts); Console.WriteLine("\nEnter the ID of the post you would like to delete."); int searchVal = int.Parse(Console.ReadLine()); //search for ID by user entered number and delets that post Posts tempPosts = new Posts(); foreach (Posts x in bigAlPosts) { if (x.ID == (searchVal)) { tempPosts = x; } } bigAlPosts.Remove(tempPosts); //Save Posts to the file PostFile.SaveAllPosts(bigAlPosts); //print Updated posts to console Console.WriteLine("\nHere are the updated posts:\n"); bigAlPosts.Sort(Posts.CompareByDate); PrintAllPosts(bigAlPosts); Console.WriteLine("\n\nPress Enter to return to main Menu..."); Console.ReadLine(); Console.Clear(); }
public static void Menu() { Console.Clear(); int menuChoice = 0; while (menuChoice != 4) { //Presents menu options Console.WriteLine("Enter '1' to show all posts."); Console.WriteLine("Enter '2' to add a post."); Console.WriteLine("Enter '3' to delete a post."); Console.WriteLine("Enter '4' to exit."); try { //Tries to parse the input and check if it is within the boundaries menuChoice = int.Parse(Console.ReadLine()); if (menuChoice < 1 || menuChoice > 4) { throw new Exception("Please enter a valid input."); } } catch (Exception e) { //Presents error message Console.WriteLine(e.Message); Console.WriteLine("Press any key to return to the Menu."); Console.ReadKey(); Menu(); } finally { //If the input goes through, it is then routed //If the input was '1', the current posts are displayed if (menuChoice == 1) { Console.Clear(); List <Posts> BigAlsPosts = PostFile.GetPosts(); BigAlsPosts.Sort(); foreach (Posts post in BigAlsPosts) { Console.WriteLine("Post ID " + post.ID + " " + post.Text + " " + post.Timestamp); } Console.WriteLine("Press any key to return to the Menu."); Console.ReadKey(); Menu(); } //If the input was '2', the user is prompted to add their new post else if (menuChoice == 2) { Console.Clear(); List <Posts> BigAlsPosts = PostFile.GetPosts(); //If the list comes back from the file empty. if (BigAlsPosts.Count == 0) { Console.WriteLine("Enter your post."); //the ID is set to '1' automatically. Posts newPost = new Posts() { ID = 1, Text = Console.ReadLine(), Timestamp = DateTime.Now.ToString() }; Console.WriteLine("Post ID " + newPost.ID + " " + newPost.Text + " " + newPost.Timestamp); PostFile.SavePost(newPost); } //If there are already items in the list. else { //This list compiles the postIDs into one list so that the code can reference it easier. List <int> postIDs = new List <int>(); //The loop runs through the BigAlsPosts list and adds the postIDs to the postIDs list. foreach (Posts post in BigAlsPosts) { postIDs.Add(post.ID); } //Sets the maximum number assigned as a post ID as the lastPost int lastPost = postIDs.Max(); //This adds 1 to the most recent post ID, and sets it as the current postID int currentPost = lastPost + 1; Console.WriteLine("Enter your post."); Posts newPost = new Posts() { ID = currentPost, Text = Console.ReadLine(), Timestamp = DateTime.Now.ToString() }; Console.WriteLine("Post ID " + newPost.ID + " " + newPost.Text + " " + newPost.Timestamp); BigAlsPosts.Sort(); PostFile.SavePost(newPost); } Console.WriteLine("Press any key to return to the Menu."); Console.ReadKey(); Menu(); } //If the input was '3', the user is prompted to delete a post. else if (menuChoice == 3) { Console.Clear(); List <Posts> BigAlsPosts = PostFile.GetPosts(); //If the list comes back from the file empty. if (BigAlsPosts.Count == 0) { Console.WriteLine("There are no posts to delete."); } else { BigAlsPosts.Sort(); List <int> postIDs = new List <int>(); //Adds the postIDs to the postID list and displays them to the user foreach (Posts post in BigAlsPosts) { Console.WriteLine("Post ID " + post.ID + " " + post.Text + " " + post.Timestamp); postIDs.Add(post.ID); } while (true) { Console.WriteLine("Enter the ID of the post you wish to delete."); try { //Tries to parse the input, sets the maximum ID as the lastPost and the minimum as the firstPost int deleteID = int.Parse(Console.ReadLine()); int lastPost = postIDs.Max(); int firstPost = postIDs.Min(); //If the input is outside the boundaries, throws an error message if (deleteID < firstPost || deleteID > lastPost) { throw new Exception("Please enter a valid ID."); } //If the input goes through else { //Removes the post corresponding to the ID input BigAlsPosts.Remove(new Posts() { ID = deleteID, Text = " ", Timestamp = " " }); //Displays the updated posts foreach (Posts post in BigAlsPosts) { Console.WriteLine("Post ID " + post.ID + " " + post.Text + " " + post.Timestamp); } BigAlsPosts.Sort(); PostFile.Save(BigAlsPosts); Console.WriteLine("Press any key to return to the Menu."); Console.ReadKey(); break; } } //Throws an error message if the input was in incorrect format catch (Exception e) { Console.WriteLine(e.Message); continue; } } } } //Exits the code else if (menuChoice == 4) { Console.Clear(); System.Environment.Exit(0); } } } }
static void Main(string[] args) { int menuInput = 0; while (menuInput != 4) { Console.WriteLine("Press 1 to Show All Posts.\nPress 2 to Add a Post.\nPress 3 to delete a Post.\nPress 4 to Exit."); try { menuInput = int.Parse(Console.ReadLine()); if (menuInput < 1 || menuInput > 4) { throw new Exception("Not a valid menu choice"); } } catch (Exception e) { Console.WriteLine(e.Message); } finally { //Directs to user to see all if (menuInput == 1) { menuInput = 0; List <Posts> bigAlPosts = PostFile.GetPosts();//Reading books in Console.Clear(); Console.WriteLine("Here are the posts Sorted by Timestamp:\n"); bigAlPosts.Sort(Posts.CompareByDate); PostUtil.PrintAllPosts(bigAlPosts); Console.WriteLine("\nPress enter to return to main menu..."); Console.ReadLine(); Console.Clear(); } //Directs the user to add a post else if (menuInput == 2) { Console.WriteLine("here is where you add a post"); menuInput = 0; List <Posts> bigAlPosts = PostFile.GetPosts(); PostUtil.AddPosts(bigAlPosts); } //Directs the user to delete a post else if (menuInput == 3) { Console.WriteLine("here is where you delete a post"); menuInput = 0; List <Posts> bigAlPosts = PostFile.GetPosts(); PostUtil.DeletePost(bigAlPosts); } } } }