Beispiel #1
0
        //end of DisplayQuestion
        public void AddQuestion()
        {
            //store temporary strings of user input to then be stored in Question instance
            Utilities.header("Add Question");
            Console.Write("\n\nWhat is the question: ");
            string tmpQuestion = Console.ReadLine();
            Console.Write("\nEnter Choice 1: ");
            string choiceOne = Console.ReadLine();
            Console.Write("\nEnter Choice 2: ");
            string choiceTwo = Console.ReadLine();
            Console.Write("\nEnter Choice 3: ");
            string choiceThree = Console.ReadLine();
            Console.Write("\nEnter Choice 4: ");
            string choiceFour = Console.ReadLine();
            int rightChoice = Utilities.validateInt("\nGood. Now which choice is correct? (1-4):\nEnter correct choice: ");
            Console.Write("\n\nNow leave some feedback for the player to understand the answer!\nEnter Feedback: ");
            string newFeedback = Console.ReadLine();

            //insert temporary strings into parameters to be accepted as a new question in array
            questionBank[questionCount] = new Question(tmpQuestion, choiceOne, choiceTwo, choiceThree, choiceFour, rightChoice, newFeedback);

            //increment QuestionBank so user can add additional questions
            questionCount++;

            Console.WriteLine("\nThis question has been added!\n");
            Utilities.PressAnyKey();
        }
        /// <summary>
        /// This method will read the datafile and populate an array of Questions that is given to it as an argument
        /// </summary>
        /// <param name="QuestionBank">An already-instantiated reference to an array of Questions</param>
        /// <param name="QuestionCount">an integer, passed by reference, that will store the count of how many questions were found in the data file</param>
        public void LoadQuestions(Question[] QuestionBank, ref int QuestionCount)
        {
            // Store a reference to the QuestionBank in this instance's instance variables
            this.QuestionBank = QuestionBank;

            // local variables to store question information
            string q = "";
            string a0 = "";
            string a1 = "";
            string a2 = "";
            string a3 = "";
            int intCorrectAnswer;
            string feedback = "";

            // the line of text
            LineText = "";

            // store the value passed as a parameter within the class's instance variable
            NumofQuestions = QuestionCount;

            try
            {

                // create a new file stream
                input = new FileStream("Data.txt", FileMode.Open, FileAccess.Read);

                // create a new streamreader
                sr = new StreamReader(input);

                while (LineText != null) // keep on going while no null is encountered
                {

                    q = ReadNextLine(); // read the next line
                    if (q == null)

                    {
                        // it was null. Terminate by closing the stream and writer.

                        input.Dispose();
                        sr.Dispose();

                        // end the method.
                        return;

                    }

                    // It wasn't null. Keep on reading the question details line after line.
                    a0 = ReadNextLine();
                    a1 = ReadNextLine();
                    a2 = ReadNextLine();
                    a3 = ReadNextLine();

                    intCorrectAnswer = Convert.ToInt32(ReadNextLine());
                    feedback = ReadNextLine();

                    // Now instantiate the question and provide it with all the values.
                    Question tmpQuestion = new Question(q, a0, a1, a2, a3, intCorrectAnswer, feedback);

                    // put the object into the array
                    QuestionBank[QuestionCount] = tmpQuestion;

                    // increment the question count
                    QuestionCount = QuestionCount + 1;
                }

            }

            catch (IOException)
            {
                // there was a file IO error
                Console.WriteLine("There was an IO error in writing.");
                Console.ReadKey(true);
                System.Environment.Exit(0);
            }
            catch (Exception)
            {
                // there was some other generic exception thrown
                Console.WriteLine("There was an File Read Error. Press a key to exit");
                Console.ReadKey(true);
                System.Environment.Exit(0);
            }
        }