コード例 #1
0
ファイル: Form3.cs プロジェクト: Alderstig/Lab4
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string listName = textBoxListName.Text.ToLower();

            string txt = textBoxLanguages.Text;

            string[] languages = txt.Split(new Char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

            Wordlist newList = new Wordlist(listName, languages);

            newList.Save();

            Close();
        }
コード例 #2
0
        public void New(string[] args)
        {
            if (args.Length > 3)
            {
                string   listName  = args[1];
                string[] languages = args.Skip(2).ToArray();

                Wordlist wordlist = new Wordlist(listName, languages);
                wordlist.Save();
            }
            else
            {
                GlossaryConsole.PrintErrorInstructionMessage();
            }
        }
コード例 #3
0
        public void Remove(string[] args)
        {
            if (args.Length > 3)
            {
                PrintErrorInstructionMessage();
            }

            string listName = args[1];
            int    intLanguage;

            string[] words = args.Skip(3).ToArray();

            try
            {
                Wordlist wordlist = Wordlist.LoadList(listName);

                if (wordlist.Languages.Any(args[2].Contains))
                {
                    intLanguage = Array.IndexOf(wordlist.Languages, args[2]);
                }
                else
                {
                    Console.WriteLine("Language not found!");
                    goto here;
                }

                for (int i = 0; i < args.Length - 3; i++)
                {
                    if (wordlist.Remove(intLanguage, words[i]))
                    {
                        wordlist.Save();
                        Console.WriteLine($"-{words[i]} removed!");
                    }
                    else
                    {
                        Console.WriteLine($"-{words[i]} not dosent exist!");
                    }
                }
                here :;
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("List not found");
                PrintErrorInstructionMessage();
            }
        }
コード例 #4
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Use any of the following parameters:");
                Console.WriteLine($"-lists");
                Console.WriteLine($"-new < listName > < language 1 > < language 2 > .. < langauge n > ");
                Console.WriteLine($"-add < listName > ");
                Console.WriteLine($"-remove < listName > < language > < word 1 > < word 2 > .. < word n > ");
                Console.WriteLine($"-words < listname > < sortByLanguage > ");
                Console.WriteLine($"-count < listName > ");
                Console.WriteLine($"-practice < listName > ");
            }
            else
            {
                foreach (var arg in args)
                {
                    arg.ToLower();
                }

                string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Lab4");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                string s = args[0];

                switch (s)
                {
                case "-lists":
                    Lists = Wordlist.GetLists();

                    if (Lists.Length == 0)
                    {
                        Console.WriteLine("No lists have been added");
                    }
                    else
                    {
                        foreach (string list in Lists)
                        {
                            Console.WriteLine(list);
                        }
                    }

                    break;

                case "-new":
                    string message = "A list must contain a name and at least two languages";

                    if (args.Length <= 3)
                    {
                        Console.WriteLine(message);

                        break;
                    }
                    else
                    {
                        Wordlist newList = new Wordlist(args[1], paramsArrNew(args));
                        newList.Save();

                        goto case "-add";
                    }

                case "-add":
                    Lists = Wordlist.GetLists();

                    if (Lists.Length == 0)
                    {
                        Console.WriteLine("No lists have been added");
                    }
                    else if (!Lists.Contains(args[1]))
                    {
                        Console.WriteLine($"No list with listname '{args[1]}' could be found");
                    }
                    else
                    {
                        Wordlist addtranslations = Wordlist.LoadList(args[1]);

                        int  count = 0;
                        bool loop  = true;

                        Console.WriteLine();
                        Console.WriteLine("Press enter (empty line) to stop input of words");
                        Console.WriteLine();

                        while (loop)
                        {
                            string[] translation = new string[addtranslations.Languages.Length];

                            Console.Write($"Add a new word in '{addtranslations.Languages[0]}': ");
                            string firstWord = Console.ReadLine();
                            if (string.IsNullOrEmpty(firstWord))
                            {
                                loop = false;
                            }
                            else
                            {
                                translation[0] += firstWord;

                                for (int i = 1; i < addtranslations.Languages.Length; i++)
                                {
                                    Console.Write($"Translate '{firstWord}' to '{addtranslations.Languages[i]}': ");
                                    string newWord = Console.ReadLine();
                                    if (string.IsNullOrEmpty(newWord))
                                    {
                                        loop = false;
                                    }
                                    else
                                    {
                                        translation[i] += newWord;
                                    }
                                }

                                addtranslations.Add(translation);
                                translation = null;
                            }

                            count++;
                        }

                        Console.WriteLine();
                        Console.WriteLine($"{count} words was added to '{args[1]}'");
                        Console.WriteLine();

                        addtranslations.Save();
                    }

                    break;

                case "-remove":
                    Lists = Wordlist.GetLists();

                    if (Lists.Length == 0)
                    {
                        Console.WriteLine("No lists have been added");
                    }
                    else if (args.Length < 4)
                    {
                        Console.WriteLine("The 'remove-command' requires a listname, a language and at least one word to remove");
                    }
                    else if (!Lists.Contains(args[1]))
                    {
                        Console.WriteLine($"No list with listname '{args[1]}' could be found");
                    }
                    else
                    {
                        Wordlist removeWord = Wordlist.LoadList(args[1]);

                        if (!removeWord.Languages.Contains(args[2]))
                        {
                            Console.WriteLine($"'{args[1]}' did not contain the language 'args[2]'");
                        }
                        else
                        {
                            int      val   = 0;
                            string[] words = paramsArrRemove(args);

                            foreach (string word in words)
                            {
                                for (int i = 0; i < removeWord.Languages.Length; i++)
                                {
                                    if (removeWord.Languages[i] == args[2])
                                    {
                                        val = i;
                                    }
                                }

                                if (removeWord.Remove(val, word))
                                {
                                    Console.WriteLine($"{word} has been removed");
                                }
                                else
                                {
                                    Console.WriteLine($"{word} has not been removed because it didn't exist in given list");
                                }

                                removeWord.Save();
                            }
                        }
                    }

                    break;

                case "-words":
                    Lists = Wordlist.GetLists();

                    if (Lists.Length == 0)
                    {
                        Console.WriteLine("No lists have been added");
                    }
                    else if (args.Length <= 1)
                    {
                        Console.WriteLine("The 'words-command' requires at least a listname");
                    }
                    else if (!Lists.Contains(args[1]))
                    {
                        Console.WriteLine($"No list with listname '{args[1]}' could be found");
                    }
                    else
                    {
                        Wordlist listWords = Wordlist.LoadList(args[1]);

                        Action <string[]> showTranslations = (translations) =>
                        {
                            foreach (string translation in translations)
                            {
                                Console.Write($"{translation,-20}");
                            }

                            Console.WriteLine();
                        };

                        if (args.Length == 3 && listWords.Languages.Contains(args[2]))
                        {
                            foreach (string language in listWords.Languages)
                            {
                                Console.Write($"{language,-20}");
                            }

                            Console.WriteLine();
                            int num = Array.IndexOf(listWords.Languages, args[2]);
                            listWords.List(num, showTranslations);
                        }
                        else if (args.Length == 2)
                        {
                            foreach (string language in listWords.Languages)
                            {
                                Console.Write($"{language,-20}");
                            }

                            Console.WriteLine();
                            listWords.List(0, showTranslations);
                        }
                        else if (!listWords.Languages.Contains(args[2]))
                        {
                            Console.WriteLine($"'{listWords.Name}' doesn't contain any words in '{args[2]}'");
                        }
                    }

                    break;

                case "-count":
                    Lists = Wordlist.GetLists();

                    if (Lists.Length == 0)
                    {
                        Console.WriteLine("No lists have been added");
                    }
                    else if (args.Length <= 1)
                    {
                        Console.WriteLine("The 'count-command' requires at least a listname");
                    }
                    else if (!Lists.Contains(args[1]))
                    {
                        Console.WriteLine($"No list with listname '{args[1]}' could be found");
                    }
                    else
                    {
                        Wordlist numOfWordsInLanguage = Wordlist.LoadList(args[1]);
                        Console.WriteLine($"The number of words in your list {args[1]} is :{numOfWordsInLanguage.Count()}");
                    }

                    break;

                case "-practice":
                    Lists = Wordlist.GetLists();

                    if (Lists.Length == 0)
                    {
                        Console.WriteLine("No lists have been added");
                    }
                    else if (args.Length <= 1)
                    {
                        Console.WriteLine("The 'practice-command' requires at least a listname");
                    }
                    else if (!Lists.Contains(args[1]))
                    {
                        Console.WriteLine($"No list with listname '{args[1]}' could be found");
                    }
                    else
                    {
                        Wordlist practiceWords = Wordlist.LoadList(args[1]);

                        int correctAnswer  = 0;
                        int wordsPracticed = 0;

                        while (true)
                        {
                            Word     word   = practiceWords.GetWordToPractice();
                            string[] strArr = word.Translations;
                            string   input  = "";

                            Console.WriteLine();
                            Console.Write($"Translate {word.Translations[word.FromLanguage]} to {practiceWords.Languages[word.ToLanguage]}: ");
                            input = Console.ReadLine().ToLower();

                            if (string.IsNullOrEmpty(input) || string.IsNullOrWhiteSpace(input))
                            {
                                break;
                            }

                            if (input == word.Translations[word.ToLanguage])
                            {
                                Console.WriteLine("correct answer");
                                correctAnswer++;
                                wordsPracticed++;
                            }
                            else if (input != word.Translations[word.ToLanguage] && !string.IsNullOrEmpty(input) && !string.IsNullOrWhiteSpace(input))
                            {
                                Console.WriteLine("incorrect answer");
                                wordsPracticed++;
                            }
                        }

                        Console.WriteLine("\nPractice has been ended");
                        Console.WriteLine($"\nYou practiced {wordsPracticed} words in total");
                        Console.WriteLine($"You got {correctAnswer} out of {wordsPracticed} correct answers");
                    }

                    break;

                default:
                    Console.WriteLine("There was something wrong with your command");

                    break;
                }
            }
        }
コード例 #5
0
        public void Add(string[] args)
        {
            if (args.Length == 2)
            {
                string listName = args[1];
                string word     = "";

                try
                {
                    Wordlist wordlist = Wordlist.LoadList(listName);

                    do
                    {
                        Console.WriteLine($"Adding words to wordlist: {listName}.dat");
                        Console.WriteLine("Exit by type empty word!! Your current word will be lost");

                        string[] translation = new string[wordlist.Languages.Length];

                        for (int i = 0; i < wordlist.Languages.Length; i++)
                        {
                            Console.Write($"-Enter the word on {wordlist.Languages[i].ToUpper()}: ");
                            word = Console.ReadLine();

                            if (!word.Equals(""))
                            {
                                if (word.All(char.IsLetter))
                                {
                                    word           = word.ToLower();
                                    translation[i] = word;
                                }
                                else
                                {
                                    throw new Exception("This is not a word.... only use A-Z");
                                }
                            }
                            else
                            {
                                goto start;
                            }
                        }

                        translation = translation.Where(x => !string.IsNullOrEmpty(x)).ToArray();
                        wordlist.Add(translation);
                        Console.WriteLine("\n");

                        wordlist.Save();

                        start :;
                    } while (!word.Equals(""));

                    wordlist.Save();
                }
                catch (FileNotFoundException)
                {
                    Console.WriteLine("List not found");
                    PrintErrorInstructionMessage();
                }
            }
            else
            {
                PrintErrorInstructionMessage();
            }
        }
コード例 #6
0
        private void btn_OkAddNewList_Click(object sender, EventArgs e)
        {
            if (txtBox_NameOfList.Text != "" && cmBox_NumOfLanguages.SelectedItem != null)
            {
                int      numOfLanguages = Convert.ToInt32(cmBox_NumOfLanguages.SelectedItem.ToString()) + 1;
                string[] txtBoxInput    = new string[numOfLanguages];
                string[] languages;
                string   name;
                int      i = 0;

                foreach (TextBox tb in this.Controls.OfType <TextBox>())
                {
                    if (tb.Enabled && tb.Text != "")
                    {
                        txtBoxInput[i] = tb.Text;
                        i++;
                    }
                    else if (tb.Enabled && tb.Text == "")
                    {
                        MessageBox.Show("Error! wrong input!");

                        foreach (TextBox tbx in this.Controls.OfType <TextBox>())
                        {
                            tbx.Text    = "";
                            tbx.Enabled = false;
                        }
                        txtBox_NameOfList.Enabled = true;
                        return;
                    }
                }

                var txtBoxInboxReverse = txtBoxInput.Reverse().ToArray();
                name      = txtBoxInboxReverse[0];
                languages = txtBoxInboxReverse.Skip(1).ToArray();

                var currentLibary = Wordlist.GetLists();

                if (!currentLibary.Contains(name))
                {
                    Wordlist wordlist = new Wordlist(name, languages);

                    wordlist.Save();
                }
                else
                {
                    MessageBox.Show("List already exist!!");
                }

                if (this.AddNewListButtonClicked != null)
                {
                    this.AddNewListButtonClicked(this, EventArgs.Empty);
                }

                foreach (TextBox tb in this.Controls.OfType <TextBox>())
                {
                    tb.Text    = "";
                    tb.Enabled = false;
                }
                txtBox_NameOfList.Enabled = true;
            }
            else
            {
                MessageBox.Show("Error! wrong input!");
                foreach (TextBox tb in this.Controls.OfType <TextBox>())
                {
                    tb.Text    = "";
                    tb.Enabled = false;
                }
                txtBox_NameOfList.Enabled = true;
            }
        }