Esempio n. 1
0
        static void AddAnswer()
        {
            // Database initialize.
            if (DatabaseManager.IsOpen())
            {
                DatabaseManager.RestartDatabase(@"answers.db");
            }
            else
            {
                DatabaseManager.OpenDatabase(@"answers.db");
            }

            // Input
            Console.WriteLine("Enter the setcode you want to add answers.");
            Console.Write("SET-CODE>");
            string setCode = Console.ReadLine();

            if (!setCode.Contains("-"))
            {
                SwitchToConsole();
            }

            // Split set code.
            string set  = setCode.Split('-')[0];
            string code = setCode.Split('-')[1];

            DatabaseManager.CreateAnswerTable(setCode); // Create answer table.

            // Add answers process.
            if (DatabaseManager.IsTableExists(setCode))
            {
                Console.WriteLine("Enter the input and output of the problem in the form of A~&~B.\nIf you want to exit, please enter #END#.");

                // IO Values Input part.
                Dictionary <string, string> answers = new Dictionary <string, string>();
                while (true)
                {
                    string IO = Console.ReadLine();

                    if (string.IsNullOrEmpty(IO))
                    {
                        continue;
                    }

                    if (IO.ToUpper() == "#END#")
                    {
                        break;
                    }

                    if (!IO.Contains("~&~"))
                    {
                        continue;
                    }

                    string[] temp = IO.Split(new string[] { "~&~" }, StringSplitOptions.None);
                    answers.Add(temp[0], temp[1]); // Input/Output values add to Dictionary.
                    continue;
                }

                // Show the check value.
                foreach (var pair in answers)
                {
                    Console.WriteLine($"[CHECK]Input : ({pair.Key}) -> Output : ({pair.Value})");
                }

                // Synchronize IO values to database.
                Console.WriteLine("Please make sure the I/O values are correct.");
                while (true)
                {
                    Console.Write("Would you like to add these?(Y/N)>");
                    string agree = Console.ReadLine().ToLower();
                    if (agree == "y" || agree == "yes") // If you agree to this.
                    {
                        int count = 0;
                        foreach (var pair in answers)
                        {
                            Console.Write($"[ADD]Input : [{pair.Key}] -> Output : [{pair.Value}]... ");
                            if (DatabaseManager.AddAnswer(setCode, pair.Key, pair.Value)) // Add
                            {
                                Console.WriteLine("OK.");
                            }
                            else
                            {
                                Console.WriteLine("FAILURE.");
                            }
                            count++;
                        }
                        Console.WriteLine($"Done! Total number of answers is {count}."); // Done
                        break;
                    }
                    else if (agree == "n" || agree == "no")
                    {
                        SwitchToConsole();
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            else
            {
                Console.WriteLine("Answer set does not exist.");
            }

            Console.WriteLine();
            SwitchToConsole();
        }