//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();
        }
Beispiel #2
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);
                    }
                }
            }
        }