数据以固定的格式存放在文本文件中。
(1)分析项目中有哪些类(或对象)参与程序。
对象选择的标准:
初步找到的:试卷、试题、计算机、考生、答案、题干、选项…
筛选后:
(2)分析项目中类或对象之间的关系。
分析结果:
设计答案类
设计试题类
设计试卷类
设计边界类
代码下载
Answer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TeachDemo
{
///
/// 答案类
///
[Serializable]
public class Answer
{
//因为后面我们要直接使用这些属性值,所以必须有初始值,因为默认值是null
public string RightAnaswer { get; set; } = string.Empty;
public string SeletedAnswer { get; set; } = string.Empty;
public string AnaswerAnalysis { get; set; } = string.Empty;
}
}
Questions.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TeachDemo
{
///
/// 试题类
///
[Serializable ]
public class Question
{
public Question()
{
QAnswer = new Answer();//因为外面是直接使用这个对象,不需要创建,所以我们在这个地方必须直接实例化
}
public int QuestionId { get; set; }
public string Title { get; set; }
public string OptionA { get; set; }
public string OptionB { get; set; }
public string OptionC { get; set; }
public string OptionD { get; set; }
public Answer QAnswer { get; set; }//答案(对象属性)
}
}
Paper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace TeachDemo
{
///
/// 试卷类
///
public class Paper
{
public Paper()
{
this.questions = new List<Question>();
}
///
/// 试题集合对象(因为一张试卷包括若干试题)
///
private List<Question> questions;
public List<Question> Questions
{
get { return this.questions; }
}
/////
///// 抽取全部试题
/////
//public void ExtractQuestions()
//{
// FileStream fs = new FileStream("questions.txt", FileMode.Open);
// StreamReader sr = new StreamReader(fs, Encoding.Default);
// string content = sr.ReadToEnd();//一次性读取全部内容
// string[] questionArray = content.Split(Convert.ToChar("&"));//将字符串分隔为试题对象数组
// string[] question = null;//用来保存一道试题
// foreach (string item in questionArray)
// {
// //将一道试题字符串分隔后变成数组信息
// question = item.Trim().Split(Convert.ToChar("\r"));
// this.questions.Add(new Question
// {
// Title = question[0].Trim(),
// OptionA = question[1].Trim(),
// OptionB = question[2].Trim(),
// OptionC = question[3].Trim(),
// OptionD = question[4].Trim(),
// QAnswer = new Answer { RightAnaswer = question[5].Trim() }
// });
// }
// sr.Close();
// fs.Close();
// SavePaper();
//}
//private void SavePaper()
//{
// FileStream fs = new FileStream("questions.obj", FileMode.Create);
// BinaryFormatter bf = new BinaryFormatter();
// bf.Serialize(fs, this.questions);//将当前文本文件中读取的数据对象,保存为集合对象,并以序列化方式存在
// fs.Close();
//}
///
/// 通过反序列化的方式读取试题
///
public void ExtractQuestions()
{
FileStream fs = new FileStream("questions.obj", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
this.questions = (List<Question>)bf.Deserialize(fs);
fs.Close();
}
///
/// 提交试卷
///
///
public int SubmitPaper()
{
int score = 0;
foreach (Question item in this.questions)
{
if (item.QAnswer.SeletedAnswer == string.Empty) continue;
if (item.QAnswer.RightAnaswer.Equals(item.QAnswer.SeletedAnswer))
score += 5;
}
return score;
}
}
}
FrmMain.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TeachDemo
{
public partial class FrmMain : Form
{
//试卷对象(如果我们后续扩展,可以包含若干试卷,这个地方应该设计成集合)
private Paper paper = new Paper();
//。。。。。。
private int questionIndex = 0;//当前试题的序号
public FrmMain()
{
InitializeComponent();
}
//抽取试题
private void btnStart_Click(object sender, EventArgs e)
{
paper.ExtractQuestions();
//隐藏抽取试题面板和按钮
this.panelPaper.Visible = false;
this.btnStart.Visible = false;
//显示第一题
ShowQuestion();
}
private void ShowQuestion()
{
this.lblTitle.Text = paper.Questions[this.questionIndex].Title;
this.lblA.Text = paper.Questions[this.questionIndex].OptionA;
this.lblB.Text = paper.Questions[this.questionIndex].OptionB;
this.lblC.Text = paper.Questions[this.questionIndex].OptionC;
this.lblD.Text = paper.Questions[this.questionIndex].OptionD;
}
//上一题
private void btnPre_Click(object sender, EventArgs e)
{
if (questionIndex == 0) return;
else
{
SaveAnswer();
this.questionIndex--;
ShowQuestion();
ResetAnswer();
}
}
//下一题
private void btnNext_Click(object sender, EventArgs e)
{
if (questionIndex == paper.Questions .Count -1) return;
else
{
SaveAnswer();
this.questionIndex++;
ShowQuestion();
ResetAnswer();
}
}
private void SaveAnswer()
{
string answer = string.Empty;
if (this.ckbA.Checked)
answer += "A";
if (this.ckbB.Checked)
answer += "B";
if (this.ckbC.Checked)
answer += "C";
if (this.ckbD.Checked)
answer += "D";
//找到当前试题对象,保存当前用户所选答案
paper.Questions[questionIndex].QAnswer.SeletedAnswer = answer;
}
//重置答案(在上一题、下一题选择中,如果试题已经选过答案,则显示以前选择的答案)
private void ResetAnswer()
{
this.ckbA.Checked = paper.Questions[this.questionIndex].QAnswer.SeletedAnswer.Contains("A");
this.ckbB.Checked = paper.Questions[this.questionIndex].QAnswer.SeletedAnswer.Contains("B");
this.ckbC.Checked = paper.Questions[this.questionIndex].QAnswer.SeletedAnswer.Contains("C");
this.ckbD.Checked = paper.Questions[this.questionIndex].QAnswer.SeletedAnswer.Contains("D");
}
//提交试卷
private void btnSubmit_Click(object sender, EventArgs e)
{
SaveAnswer();//保存最后一次用户所选答案
//计算分数
int score = this.paper.SubmitPaper();
//显示面板和当前成绩
this.panelPaper.Visible = true;
this.lblInfo.Text = $"您当前成绩为:{score}分!";
}
//关闭窗体
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}