Skip to main content
edited tags; edited title
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Multiple Choice Questionnaire Code Structure-choice questionnaire

Source Link

Multiple Choice Questionnaire Code Structure

I'm new to C# and for my assignment I have to design a multiple choice questionnaire where the student can login and take the test, view the memo, etc. I'm not very concerned about the GUI, but rather the structure of my classes and code. I will be using the StudentController class to interact with the GUI. It's a rough but here's my code so far:

Student class

public class Student : User
{
    private int finalMark;
    private List<int> answers;

    public Student()
    {
        answers = new List<int>();
    }

    /// <summary>
    /// Gets the final mark for the student
    /// </summary>
    public int GetFinalMark() => finalMark;

    /// <summary>
    /// Sets the final mark for the student
    /// </summary>
    public void SetFinalMark(int value) => finalMark = value;

    /// <summary>
    /// Fetches the answer the student chose for the indexed question
    /// </summary>
    /// <param name="index">The index of the question to retrieve the student's answer from</param>
    /// <returns>The index of the answer the user chose</returns>
    public int GetAnswer(int index) => answers[index];

    /// <summary>
    /// Sets the answer at the index's position
    /// </summary>
    /// <param name="index">The index to set the answer at</param>
    /// <param name="answer">The answer to set</param>
    public void SetAnswer(int index, int answer) => answers[index] = answer;

    /// <summary>
    /// Adds an answer to the student's list of answered questions
    /// </summary>
    /// <param name="answer">The answer to add to the list of answered questions</param>
    public void AddAnswer(int answer) => answers.Add(answer);
}

Multiple choice question

public class MultipleChoiceQuestion
{
    //  The question that is stated in the multiple choice question
    private string question;

    //  The list of possible answers to the question
    private string[] choices;

    //  The index of the correct answer, ie. choices[answer]
    private int answer;

    public MultipleChoiceQuestion()
    {
        choices = new string[4];
    }

    /// <summary>
    /// Gets and sets the question
    /// </summary>
    public string Question { get => question; set => question = value; }

    /// <summary>
    /// Gets the indexed question as a string
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public string GetChoices(int index) => choices[index];

    /// <summary>
    /// Sets the choice at the current index, eg. choice[0] = "Static void"
    /// </summary>
    /// <param name="index">The index of the choice</param>
    /// <param name="value">The choice to set</param>
    public void SetChoice(int index, string value) => choices[index] = value;

    /// <summary>
    /// Gets and sets the correct answer as an index
    /// </summary>
    public int Answer { get => answer; set => answer = value; }
}

Test Class

    public class Test
{
    private List<MultipleChoiceQuestion> question;

    public Test()
    {
        question = new List<MultipleChoiceQuestion>(); ;
    }

    /// <summary>
    /// Returns the question text at the given index of questions
    /// </summary>
    /// <param name="index">The index of the question to retrieve</param>
    /// <returns>A string of the question at the current index</returns>
    public string GetQuestionText(int index) => question[index].Question;

    
    public void SetQuestion(int index, string value) => question[index].SetChoice(index, value);

    /// <summary>
    /// Adds a new question to the list
    /// </summary>
    /// <param name="newQuestion">The question to add to the list</param>
    public void AddQuestion(MultipleChoiceQuestion newQuestion) => question.Add(newQuestion);

    /// <summary>
    /// Retries one of the choices for the question. Eg. choice A, choice B, choice C...
    /// </summary>
    /// <param name="choice">The index of the choice, eg. choice 2</param>
    /// <param name="index">The index of the question</param>
    /// <returns></returns>
    public string GetChoice(int choice, int index) => question[index].GetChoices(choice);

    /// <summary>
    /// Retrieves the index of the answer to the question
    /// </summary>
    /// <param name="questionIndex">The index of the answer</param>
    /// <returns></returns>
    public int GetAnswerIndex(int questionIndex) => question[questionIndex].Answer;

    /// <summary>
    /// Gets how many questions there are in the test
    /// </summary>
    /// <returns>The amount of questions in the test</returns>
    public int GetSize()
    {
        return question.Count;
    }
}

Student Controller

    public class StudentController
{
    private List<Student> students;
    private FileManager fileManager;
    private Test test;
    
    public StudentController()
    {
        students = new List<Student>();
        fileManager = new FileManager();
        test = new Test();
        Init();
    }

    private void Init()
    {
        students = fileManager.LoadStudents();
        test = fileManager.LoadTest();
    }

    /// <summary>
    /// Verifies that the login details match what is in the system
    /// </summary>
    /// <param name="username">The username of the student</param>
    /// <param name="password">The password of the student</param>
    /// <returns>True if the credentials match that on the system</returns>
    public bool CheckLogin(int username, string password)
    {
        bool credentialsMatch = false;
        foreach (Student student in students)
        {
            if (student.ID == username && student.Password == password)
            {
                credentialsMatch = true;
                break;
            }
        }
        return credentialsMatch;
    }

    /// <summary>
    /// Fetches the index of the student in a list
    /// </summary>
    /// <param name="ID">The ID of the student</param>
    /// <returns>The student's ID if found, else returns -1</returns>
    public int GetStudentIndex(int ID)
    {
        int id = -1;
        foreach (Student student in students)
        {
            if (student.ID == ID)
            {
                id = ID;
                break;
            }
        }
        return id;
    }

    /// <summary>
    /// Sets the final mark of the student
    /// </summary>
    /// <param name="studentIndex">The index of the student</param>
    /// <param name="mark">The final mark of the student</param>
    public void SetFinalMark(int studentIndex, int mark) => students[studentIndex].SetFinalMark(mark);

    /// <summary>
    /// Sets the answer of the student at the current index
    /// </summary>
    /// <param name="studentIndex">The index of the student</param>
    /// <param name="questionNumber">The question number</param>
    /// <param name="answer">The answer for the student</param>
    public void SetStudentAnswer(int studentIndex, int questionNumber, int answer) => students[studentIndex].SetAnswer(questionNumber, answer);

    /// <summary>
    /// Adds a new answer to the list of the student's answers
    /// </summary>
    /// <param name="studentIndex">The index of the student</param>
    /// <param name="answer">The answer to add</param>
    public void AddStudentAnswer(int studentIndex, int answer) => students[answer].AddAnswer(answer);

    /// <summary>
    /// Gets the text for the question
    /// </summary>
    /// <param name="questionNumber">The question number</param>
    /// <returns>A string of the question's text</returns>
    public string GetQuestionText(int questionNumber) => test.GetQuestionText(questionNumber);

    /// <summary>
    /// Fetches the choice of the question at the index
    /// </summary>
    /// <param name="choice">The index of the choice</param>
    /// <param name="questionNumber">The question number</param>
    /// <returns></returns>
    public string GetQuestionChoice(int choice, int questionNumber) => test.GetChoice(choice, questionNumber);


    /// <summary>
    /// Gets the answer to the question
    /// </summary>
    /// <param name="questionNumber">The question number</param>
    /// <returns>The answer to the question</returns>
    public int GetAnswer(int questionNumber)
    {
        return test.GetAnswerIndex(questionNumber);
    }

    /// <summary>
    /// Saves the students to file
    /// </summary>
    public void SaveStudents()
    {
        fileManager.SaveStudents(students);
    }
}

The FileManger class just loads and saves objects to a file. Any help or guidance would be appreciated!