Skip to content

question.cs #2

@tranlongcbvn112-sketch

Description

@tranlongcbvn112-sketch

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

public abstract class Question
{
public string QuestionText { get; set; }
public abstract bool CheckAnswer(string answer);
public abstract string GetCorrectAnswer();

// Hàm hiển thị giao diện
public abstract void RenderToUI(Label lbl, RadioButton[] options, TextBox txt);

}

// MULTIPLE CHOICE
public class MultipleChoiceQuestion : Question
{
public string[] Options { get; set; }
public int CorrectOption { get; set; }

public override bool CheckAnswer(string answer)
{
    return Options[CorrectOption].Trim().Equals(answer.Trim(), StringComparison.OrdinalIgnoreCase);
}

public override string GetCorrectAnswer()
{
    return (CorrectOption >= 0 && CorrectOption < Options.Length)
        ? Options[CorrectOption]
        : "(Invalid correct option)";
}

public override void RenderToUI(Label lbl, RadioButton[] options, TextBox txt)
{
    lbl.Text = QuestionText;
    for (int i = 0; i < options.Length; i++)
    {
        if (i < Options.Length)
        {
            options[i].Visible = true;
            options[i].Text = Options[i];
            options[i].Checked = false;
        }
        else
        {
            options[i].Visible = false;
        }
    }
    txt.Visible = false;
}

}

// OPEN ENDED
public class OpenEndedQuestion : Question
{
public List AcceptedAnswers { get; set; }

public override bool CheckAnswer(string answer)
{
    return AcceptedAnswers.Any(a => a.Equals(answer, StringComparison.OrdinalIgnoreCase));
}

public override string GetCorrectAnswer()
{
    return string.Join(", ", AcceptedAnswers);
}

public override void RenderToUI(Label lbl, RadioButton[] options, TextBox txt)
{
    lbl.Text = QuestionText;
    foreach (var opt in options) opt.Visible = false;
    txt.Visible = true;
    txt.Text = "";
}

}

// TRUE / FALSE
public class TrueFalseQuestion : Question
{
public bool IsTrue { get; set; }

public override bool CheckAnswer(string answer)
{
    return (IsTrue && answer.Equals("True", StringComparison.OrdinalIgnoreCase)) ||
           (!IsTrue && answer.Equals("False", StringComparison.OrdinalIgnoreCase));
}

public override string GetCorrectAnswer()
{
    return IsTrue ? "True" : "False";
}

public override void RenderToUI(Label lbl, RadioButton[] options, TextBox txt)
{
    lbl.Text = QuestionText;
    options[0].Visible = true; options[0].Text = "True"; options[0].Checked = false;
    options[1].Visible = true; options[1].Text = "False"; options[1].Checked = false;
    options[2].Visible = false; options[3].Visible = false;
    txt.Visible = false;
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions