Beispiel #1
0
        public void BackSummary_WhenGivenAnEmptyList_ReturnsAnEmptyString()
        {
            var    flashcards = new List <Flashcard>();
            var    lesson     = new Lesson();
            string result     = lesson.BackSummary(flashcards);

            Assert.That(result, Is.EqualTo(string.Empty));
        }
Beispiel #2
0
        public void FrontSummary_WhenGivenAListWithTwoFlashcardsTheFirstWhoseFrontSideIsRedAndWhoseSecondCardHasAFrontSideWithBlue_ShouldReturnRedFollowedByANewLineFollowedByBlue()
        {
            var flashcards = new List <Flashcard>();

            flashcards.Add(new Flashcard("a", "red"));
            flashcards.Add(new Flashcard("b", "blue"));
            var    lesson = new Lesson();
            string result = lesson.BackSummary(flashcards);

            Assert.That(result, Is.EqualTo("red\nblue\n"));
        }
Beispiel #3
0
        private static void StartCommandLineLoop(IList <Flashcard> flashcards,
                                                 bool fromDatabase)
        {
            while (true)
            {
                WriteLine("Enter 'a' to show both front and back of each card.");
                WriteLine("Enter 'f' to show the front of each card.");
                WriteLine("Enter 'b' to show the back of each card.");
                WriteLine("Enter 'add' to add a flashcard.");
                WriteLine("Enter 'delete' to delete a flashcard.");
                WriteLine("Enter 'save' to save all flashcards");
                WriteLine("Enter 'x' to exit the application.");
                PrintPrompt();
                string userInput = ReadLine();
                Lesson lesson    = new Lesson();

                switch (userInput)
                {
                case "a":
                    WriteLine("Printing Lesson summary:");
                    WriteLine(lesson.LessonSummary(flashcards));
                    break;

                case "f":
                    WriteLine("Print only fronts of each card:");
                    WriteLine(lesson.FrontSummary(flashcards));
                    break;

                case "b":
                    WriteLine("Print only backs of each card:");
                    WriteLine(lesson.BackSummary(flashcards));
                    break;

                case "add":
                    WriteLine("Adding a flashcard...");
                    WriteLine("Enter the front side:");
                    PrintPrompt();
                    string fSide = ReadLine();
                    WriteLine("You entered the following for the front side: (" +
                              fSide + ")");
                    WriteLine("Enter the back side:");
                    PrintPrompt();
                    string bSide = ReadLine();
                    WriteLine("You entered the following for the back side: (" +
                              bSide + ")");
                    var addition = new Flashcard(fSide, bSide);
                    flashcards.Add(addition);
                    if (fromDatabase)
                    {
                        new DatabaseBridge().AddFlashcard(addition);

                        // We reload the flashcards from the database
                        // so that we have the ID that the
                        // flashcard we just added has. It is the
                        // database that sets the ID.
                        flashcards = LoadFromDatabase();
                        // If we didn't do this, the IDs would
                        // not be unique within this application.
                        // but they would be unique in the database.
                    }
                    else
                    {
                        // Set the ID to a unique ID.
                        int maxID = 0;
                        foreach (var f in flashcards)
                        {
                            if (f.ID > maxID)
                            {
                                maxID = f.ID;
                            }
                        }
                        addition.ID = maxID + 1;
                    }

                    WriteLine("Done adding flashcard.");
                    break;

                case "save":
                    if (fromDatabase)
                    {
                        WriteLine("Already saved.");
                    }
                    else
                    {
                        // Let the user choose the file name.
                        WriteLine("Enter a name for a file to save to:");
                        PrintPrompt();
                        string fileName = ReadLine();

                        WriteLine("Saving flashcards to file called '" + fileName + "'");
                        File.WriteAllText(fileName, lesson.TabSeparatedValues(flashcards));
                        WriteLine("Done writing to file named " + fileName);
                    }
                    break;

                case "delete":
                    foreach (Flashcard f in flashcards)
                    {
                        WriteLine($"ID: {f.ID}\t\t{f.ShowFront()}\t{f.ShowBack()}");
                    }
                    WriteLine("Enter the ID of the card you want to delete, or enter 'c' to cancel");
                    PrintPrompt();
                    string userDeleteResponse = ReadLine();

                    if (userDeleteResponse == "c")
                    {
                        WriteLine("Deletion cancelled. " +
                                  "Nothing has been deleted.\n");
                    }
                    else
                    {
                        foreach (Flashcard f in flashcards)
                        {
                            if (f.ID.ToString() == userDeleteResponse)
                            {
                                if (fromDatabase)
                                {
                                    new DatabaseBridge().DeleteFlashcard(f);
                                }
                                flashcards.Remove(f);
                                WriteLine($"Flashcard with ID: {userDeleteResponse} succesfully deleted.");
                                break;
                            }
                        }
                        if (fromDatabase)
                        {
                            // Reload flashcards from database.
                            flashcards = LoadFromDatabase();
                        }
                    }
                    break;

                case "x":
                    WriteLine("Exiting...");
                    System.Environment.Exit(0);
                    break;

                default:
                    WriteLine("Unrecognized input: (" + userInput + ")");
                    break;
                }
            }
        }