Exemple #1
0
        public static void ShowAllPosts()
        {
            List <Post> myPosts = Postfile.GetPosts();

            myPosts.Sort();
            Postutils.PrintAllPosts(myPosts);
            Console.WriteLine("");
            Console.WriteLine("Would you like to return to the menu? 1 for yes.");
            int userChoice = int.Parse(Console.ReadLine());

            if (userChoice == 1)
            {
                Console.Clear();
                Menu();
            }
        }
Exemple #2
0
        public static List <Post> DeletePosts()
        {
            List <Post> myPosts = new List <Post>();

            StreamReader inFile = null;

            //try catch error handling
            try
            {
                inFile = new StreamReader("posts.txt");
            }
            catch (FileNotFoundException e) //create an exception variable that we expect to be the error for the catch
            {
                Console.WriteLine("Something went wrong... returning a blank list 0}", e);
                return(myPosts);
            }
            string line = inFile.ReadLine();     //reading in the data from posts.txt and putting it into variable line

            while (line != null)                 //runs as long as there is still data in the text file
            {
                string[] temp = line.Split("#"); //read data in and split it into temp string array
                //DateTime dateTime = DateTime.Parse(temp[2]);
                myPosts.Add(new Post()
                {
                    id = int.Parse(temp[0]), text = temp[1], dateTime = DateTime.Parse(temp[2])
                });
                line = inFile.ReadLine(); //update read
            }

            Console.WriteLine("What is the ID of the post you would like to delete?");
            Postutils.PrintAllPostsDelete(myPosts);
            int deleteId = int.Parse(Console.ReadLine());

            myPosts.RemoveAt(deleteId - 1); //-1 so we delete the index of the post the user wants and not the index that is one less in the list
            inFile.Close();
            SaveAllPosts(myPosts);
            return(myPosts);
        }