我是C#新手,对于我的作业,我必须设计一个多项选择的问卷,让学生可以登录并参加考试,查看备忘录等。我不太关心GUI,而是我的类和代码的结构。我将使用StudentController类与GUI交互。这是一个粗略的,但这是我的代码到目前为止:
学生班
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);
}多项选择题
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; }
}试题班
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;
}
}学生控制器
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);
}
}FileManger类只是将对象加载并保存到文件中。如有任何帮助或指导,将不胜感激!
发布于 2018-03-08 20:31:58
让我困惑
学生有答案,但似乎与考试无关。
答案应该是特定于测试的。一个学生要么有多个测试,要么一个测试有一个用户。您可以有一个基类测试,它是原始测试,然后是一个继承自测试的testUser类。
MultipleChoiceQuestion应该有正确的答案,这样才能得分。
考试应该有分数的方法。
对get set方法使用公共属性。
你不需要名单上的一套。他们可以得到然后添加,修改和删除。
你可以像这样做新的,让它远离电脑。
private List<int> answers = new List<int>(); C#有索引器。C#编程指南(使用索引器)
https://codereview.stackexchange.com/questions/189147
复制相似问题