Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
курсовая ООП / kursovaya_oop444.doc
Скачиваний:
36
Добавлен:
22.05.2015
Размер:
355.84 Кб
Скачать

4 Uml-диаграмма «Прецедентов» решаемой задачи

На данном этапе разработки мы определяем систему, не зависящую от условий эксплуатации, а именно разрабатываем диаграмму прецедентов. На рисунке 1 изображена Uml-диаграмма «Прецедентов» нашей системы.

Рисунок 1 - Uml-диаграмма «Прецедентов»

5 Uml-диаграмма «Классов» решаемой задачи

Данный этап включает в себя разработку структуры классов решаемой задачи.

Рисунок 2.1 - Uml-диаграмма класса Type

Рисунок 2.2 - Uml-диаграмма Main

Рисунок 2.3 - Uml-диаграмма Login

Рисунок 2.4 - Uml-диаграмма Report

6 Текст программы на языке программирования c#

На данном этапе необходимо реализовать и подключить классы к проекту. Результат работы приложения представлен на рисунках. Вопросы к тесту представлены в приложении А.

Рисунок 3 – Окно Login

Рисунок 4 – Главное окно программы

Рисунок 5 - Отчет

Рисунок 6 - Подробный отчет

Код программы на Языке программирования C# представлен в листингах 1-4.

Листинг 1- Type

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml;

using System.IO;

namespace tester

{

public class Type

{

/// <summary>

/// Имя файла с вопросами

/// </summary>

public static string ContentFile = "\\content.qdb";

/// <summary>

/// Номер текущего вопроса

/// </summary>

public static int Index;

/// <summary>

/// Имя тестируемого

/// </summary>

public static string FirstName;

/// <summary>

/// Фамилия тестируемого

/// </summary>

public static string SecondName;

/// <summary>

/// Папка с отчетами

/// </summary>

public static string ReportDir = "\\reports";

/// <summary>

/// Запись в файл (текстовый)

/// </summary>

/// <param name="File">Имя файла</param>

/// <param name="Line">Строка</param>

public static void WriteFile(string File, string Line)

{

System.IO.StreamWriter sw;

using (sw = new System.IO.StreamWriter(File, true, Encoding.UTF8))

{ sw.WriteLine(Line); }

sw.Dispose();

}

/// <summary>

/// Случайные индексы

/// </summary>

/// <param name="Count">Размер</param>

/// <returns>Возвращает массив перемешанных чисел (от 1) заданного размера</returns>

public int[] Randomize(int Count)

{

int[] d = new int[Count];

bool b = false;

Random r = new Random();

int f = -1;

for (int i = 0; i < Count; i++)

{

do

{

f = r.Next(Count + 1);

if (Array.IndexOf(d, f) < 0)

{ d[i] = f; b = true; }

}

while (b == false);

b = false;

}

return d;

}

/// <summary>

/// Количество вопросов

/// </summary>

public int GetCount

{

get

{

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(ContentFile);

XmlNodeList list = xmlDoc.GetElementsByTagName("question");

return list.Count;

}

}

/// <summary>

/// Чтение вопроса по номеру

/// </summary>

/// <param name="Index">Номер вопроса</param>

public void ReadItem(int Index)

{

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(ContentFile);

XmlNodeList list = xmlDoc.GetElementsByTagName("question");

for (int i = 0; i < list.Count; i++)

{

XmlElement id = (XmlElement)xmlDoc.GetElementsByTagName("question")[i];

XmlElement text = (XmlElement)xmlDoc.GetElementsByTagName("question_text")[i];

XmlElement ans_1 = (XmlElement)xmlDoc.GetElementsByTagName("answer_1")[i];

XmlElement ans_2 = (XmlElement)xmlDoc.GetElementsByTagName("answer_2")[i];

XmlElement ans_3 = (XmlElement)xmlDoc.GetElementsByTagName("answer_3")[i];

XmlElement v = (XmlElement)xmlDoc.GetElementsByTagName("answer_V")[i];

if (id.GetAttribute("id") == (Index).ToString())

{

Question = text.InnerText;

Answer_1 = ans_1.InnerText;

Answer_2 = ans_2.InnerText;

Answer_3 = ans_3.InnerText;

Answer_V = int.Parse(v.InnerText);

}

}

}

public string Question;

public string Answer_1;

public string Answer_2;

public string Answer_3;

public int Answer_V;

}

}

Листинг 2 – Login

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace tester

{

public partial class frmLogin : Form

{

public frmLogin()

{

InitializeComponent();

this.Text = "Test";

this.StartPosition = FormStartPosition.CenterScreen;

this.lblF.Text = "Фамилия";

this.lblN.Text = "Имя";

this.btnExit.Text = "Закрыть";

this.btnOk.Text = "Вход";

}

private void btnExit_Click(object sender, EventArgs e)

{

Application.Exit();

}

private void btnOk_Click(object sender, EventArgs e)

{

//Запись имени и фамилии

Type.FirstName = this.txtF.Text.Trim();

Type.SecondName = this.txtN.Text.Trim();

if (Type.FirstName != "" & Type.SecondName != "")

{

//открытие основной формы программы

this.ShowInTaskbar = false;

frmMain f = new frmMain();

f.ShowDialog();

Application.Exit();

}

}

private void btnExit_Click_1(object sender, EventArgs e)

{

Application.Exit();

}

}

}

Листинг 3 – Main

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace tester

{

public partial class frmMain : Form

{

Type c = new Type();

int[] RandomIndex;

int[] Results;

public frmMain()

{

InitializeComponent();

this.Text = "Тестируется " + Type.FirstName + " " + Type.SecondName;

this.StartPosition = FormStartPosition.CenterScreen;

Start();

button2.Visible = false;

}

public void Start()

{

pictureBox1.Visible = false;

pictureBox2.Visible = false;

radioButton1.Image = pictureBox1.Image;

radioButton2.Image = pictureBox1.Image;

radioButton3.Image = pictureBox1.Image;

radioButton1.Text = "";

radioButton2.Text = "";

radioButton3.Text = "";

radioButton4.Visible = false;

radioButton4.Checked = true;

textBox1.Enabled = false;

textBox2.Enabled = false;

textBox3.Enabled = false;

txtQ.ReadOnly = true;

Type.ContentFile = Application.StartupPath + Type.ContentFile;

if (System.IO.File.Exists(Type.ContentFile) == false)

{

MessageBox.Show("Не найден файл с вопросами.\n Программа будет закрыта.");

Application.Exit();

}

else

{

Type.Index = 0;

RandomIndex = c.Randomize(c.GetCount);

Results = new int[c.GetCount];

NextQuesion();

}

}

public void Checked(RadioButton r)

{

if (r.Checked == true) { r.Image = pictureBox2.Image; }

else { r.Image = pictureBox1.Image; }

}

private void radioButton1_CheckedChanged(object sender, EventArgs e)

{

Checked(radioButton1);

}

private void radioButton2_CheckedChanged(object sender, EventArgs e)

{

Checked(radioButton2);

}

private void radioButton3_CheckedChanged(object sender, EventArgs e)

{

Checked(radioButton3);

}

public void NextQuesion()

{

c.ReadItem(RandomIndex[Type.Index]);

txtQ.Text = c.Question;

textBox1.Text = c.Answer_1;

textBox2.Text = c.Answer_2;

textBox3.Text = c.Answer_3;

}

public void Report()

{

int h = Type.Index;

RadioButton[] r = { radioButton4, radioButton1, radioButton2, radioButton3 };

for (int i = 0; i < r.Length; i++)

{ if (r[i].Checked == true) { Results[RandomIndex[h] - 1] = i; } }

}

private void button1_Click(object sender, EventArgs e)

{

if (Type.Index < c.GetCount)

{

Report();

NextQuesion();

radioButton4.Checked = true; Type.Index++;

}

else

{

frmReport f = new frmReport(Results);

f.ShowDialog();

Application.Exit();

}

}

}

}

Листинг 4 – Report

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace tester

{

public partial class frmReport : Form

{

Type c = new Type();

int[] a = new int[0];

public frmReport(int[] ans)

{

InitializeComponent();

a = ans;

Type.ReportDir = Application.StartupPath + "\\reports";

if (System.IO.Directory.Exists(Type.ReportDir) == false)

{ System.IO.Directory.CreateDirectory(Type.ReportDir); }

this.StartPosition = FormStartPosition.CenterScreen;

this.FormBorderStyle = FormBorderStyle.FixedDialog;

this.MinimizeBox = false;

button2.Text = "Подробный отчет";

button1.Text = "Закрыть";

label3.Text = "Тестировался " + Type.FirstName + " " + Type.SecondName;

label1.Text = "Всего вопросов: " + ans.Length.ToString();

int t = 0;

for (int i = 0; i < ans.Length; i++)

{

c.ReadItem(i + 1);

if (c.Answer_V == ans[i]) { t++; }

}

label2.Text = "Верных ответов: " + t.ToString();

Type.ReportDir += "\\" + DateTime.Now.Day.ToString() + "." + DateTime.Now.Month.ToString() + "." + DateTime.Now.Year.ToString();

Type.ReportDir += ";" + DateTime.Now.Hour.ToString() + "." + DateTime.Now.Minute.ToString() + "." + DateTime.Now.Second.ToString();

Type.ReportDir += " " + Type.FirstName + " " + Type.SecondName + ".txt";

Rep();

}

public void Rep()

{

Type c = new Type();

Type.WriteFile(Type.ReportDir, "Тестировался " + Type.FirstName + " " + Type.SecondName);

Type.WriteFile(Type.ReportDir, "Подробные резельтаты:");

int t = 0;

for (int i = 0; i < c.GetCount; i++)

{

c.ReadItem(i + 1);

Type.WriteFile(Type.ReportDir, "Вопрос №" + (i + 1).ToString() + " " + c.Question);

if

(a[i] == c.Answer_V)

{ Type.WriteFile(Type.ReportDir, "Ответ верен"); t++; }

else { Type.WriteFile(Type.ReportDir, "Ответ неверен"); }

}

Type.WriteFile(Type.ReportDir, "Итого: " + t.ToString() + " верных ответов из " + a.Length.ToString());

}

private void button2_Click(object sender, EventArgs e)

{

System.Diagnostics.Process p = new System.Diagnostics.Process();

p.StartInfo.FileName = Type.ReportDir;

p.Start();

}

}

}