using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment
{
//[Serializable()]
class Questions
{
//fields
private string idCategories;
private string idQuestion;
private double mark;
private string question;
private Dictionary<string, string> listAnswer;
//Properties
public string IdCategories
{
get { return idCategories; }
set { idCategories = value; }
}
public string IdQuestion
{
get { return idQuestion; }
set { idQuestion = value; }
}
public string Question
{
get { return question; }
set { question = value; }
}
public double Mark
{
get { return mark; }
set { mark = value; }
}
public Dictionary<string,string> ListAnswer
{
get { return listAnswer; }
set { listAnswer = value; }
}
//Constructor
public Questions(string idCategories,string idQuestion,string question,double mark,Dictionary<string,string> listAnswer)
{
this.idCategories = idCategories;
this.idQuestion = idQuestion;
this.question = question;
this.mark = mark;
this.listAnswer = listAnswer;
}
//ToString
public override string ToString()
{
string strQuestion = "-----------------------------------------------------";
strQuestion += "\r\nID Category: {3}\r\nID Question: {0}\r\nQuestions : {1}\r\nMark : {2}\r\nAnswer : \r\n";
for (int i = 0; i < listAnswer.Count; i++)
{
strQuestion += (i+1) + ". " + listAnswer.ElementAt(i).Key + "\t" + listAnswer[listAnswer.ElementAt(i).Key]+"\r\n";
}
return string.Format(strQuestion,idQuestion,question,mark,idCategories);
}
}
}
------------------------------------------------------------------------------- Questions Management:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment
{
class QuestionsManagement
{
//fields--------------------------------------------------------------------------
List<Questions> listQuestion;
//Properties
public List<Questions> ListQuestion
{
get { return listQuestion; }
set { listQuestion = value; }
}
//Constructor---------------------------------------------------------------------
public QuestionsManagement()
{
listQuestion = new List<Questions>();
}
//Methods-------------------------------------------------------------------------
//Tim theo id------------------------------------
public Questions SearchQuestion(string idQuestion)
{
foreach (Questions q in listQuestion)
{
if (q.IdQuestion.Equals(idQuestion.ToUpper())) return q;
}
return null;
}
//Nhap------------------------
public Questions InputQuestion()
{
//ID danh muc
string idCategory = CheckInputCategory();//goi ham check danh muc
//ID cau hoi
string idQuestion = CheckQuestion();//goi ham check cau hoi
//Nhap cau hoi
Console.Write("Questions : ");
string question = Console.ReadLine();
//Nhap Diem
Console.Write("Mark : ");
double mark = Convert.ToDouble(Console.ReadLine());
//Nhap Dap an
Dictionary<string, string> listAnswer = InputListAnswer();//goi ham nhap dap an
//Co muon luu cau hoi hay khong
Console.Write("Do you want to save Questions? [Y/N] (Other key = N) : ");
string saveQuestion = Console.ReadLine();
if (saveQuestion.ToUpper().Equals("Y"))
{
return new Questions(idCategory, idQuestion, question, mark, listAnswer);
}
else
{
return null;
}
}
//Them----------------------------
public void AddQuestion(Questions q)
{
listQuestion.Add(q);
}
//Sua-------------------------------
public void UpdateQuestion()
{
//UpdateQuestion
Console.Write("Enter ID Question to UPDATE : ");
string idQuestion = Console.ReadLine();
Questions q = SearchQuestion(idQuestion);
if (q != null)
{
//ID danh muc
string newIdCategory = CheckInputCategory();//goi ham check danh muc
//ID cau hoi
string newIdQuestion = CheckQuestion();//goi ham check cau hoi
//Muon sua hay xoa
Console.Write("Do you want to UPDATE or DELETE Question? [U/D] (Other key = D) : ");
string choice = Console.ReadLine();
if (choice.ToUpper().Equals("U"))
{
//Nhap cau hoi
Console.Write("Update Questions : ");
string newQuestion = Console.ReadLine();
//Nhap Diem
Console.Write("Update Mark : ");
double newMark = Convert.ToDouble(Console.ReadLine());
//Nhap Dap an
Console.WriteLine("Update Answer:");
Dictionary<string, string> newListAnswer = InputListAnswer();
//Co muon luu cau hoi hay khong
Console.Write("Do you want to save Questions? [Y/N] (Other key = N) : ");
string saveQuestion = Console.ReadLine();
if (saveQuestion.ToUpper().Equals("Y"))
{
Questions q1 = new Questions(newIdCategory, newIdQuestion, newQuestion, newMark, newListAnswer);
q.IdCategories = q1.IdCategories;
q.IdQuestion = q1.IdQuestion;
q.Mark = q1.Mark;
q.ListAnswer = q1.ListAnswer;
Console.WriteLine("Update Questions successfully!");
}
else
{
Console.WriteLine("Question stay the same follow your choice!");
}
}
else
{
listQuestion.Remove(q);
Console.WriteLine("Question has been REMOVED!");
}
}
else
{
Console.WriteLine("Invalid ID Questions!");
}
}
//In-------------------------------
public void DisplayAllQuestion()
{
foreach (Questions q in listQuestion)
{
Console.WriteLine(q.ToString());
}
}
//Check----------------------------
//Check danh muc
public string CheckInputCategory()
{
string idCategory;
do
{
Console.Write("ID Category : ");
idCategory = Console.ReadLine();
} while (!idCategory.ToUpper().Equals("G001") && !idCategory.ToUpper().Equals("G002") && !idCategory.ToUpper().Equals("G003"));
return idCategory;
}
//Tim so cau hoi trong mot danh muc
public int FindTotalQuestionInCategory(string idCategory)
{
int total = 0;
foreach (Questions q in listQuestion)
{
if (q.IdCategories.Equals(idCategory.ToUpper()))
{
total++;
}
}
return total;
}
//Show danh muc
public void ShowCategory(string idCategory)
{
string nameCategory = "";
int numberQuestionInCategory = FindTotalQuestionInCategory(idCategory);
switch (idCategory.ToUpper())
{
case "G001":
nameCategory = "HTML5";
break;
case "G002":
nameCategory = "JAVASCRIPT";
break;
case "G003":
nameCategory = "OOP";
break;
default:
Console.WriteLine("Category does not exist!");
break;
}
Console.WriteLine("----- " + nameCategory + " -----");
Console.WriteLine("Category " + nameCategory + " have total " + numberQuestionInCategory + " Questions");
Console.Write("Enter ID Question to view (Invalid ID = View All) : ");
string idQuestion = Console.ReadLine();
Questions q = SearchQuestion(idQuestion);
if (q != null && q.IdCategories.Equals(idCategory))
{
Console.WriteLine(q.ToString());
}
else
{
Console.WriteLine("ID Question is not valid!\n");
Console.WriteLine("---------- All Question in " + nameCategory + " Category -----------");
foreach (Questions question in listQuestion)
{
if (question.IdCategories.Equals(idCategory.ToUpper()))
{
Console.WriteLine(question.ToString());
}
}
}
Console.WriteLine();
}
//Check cau hoi
public string CheckQuestion()
{
string idQuestion;
bool isIdQuestion;
do
{
isIdQuestion = false;
Console.Write("ID Questions : ");
idQuestion = Console.ReadLine();
foreach (Questions q in listQuestion)//Kiem tra xem ID cau hoi da ton tai chua
{
if (q.IdQuestion.Equals(idQuestion))
{
Console.WriteLine("ID Questions is already exist, try again !");
isIdQuestion = true;
}
}
} while (isIdQuestion);
return idQuestion;
}
//Nhap list dap an
public Dictionary<string, string> InputListAnswer()
{
Dictionary<string, string> listAnswer = new Dictionary<string, string>();
int indexAnswer = 0;
string addMoreAnswer = "1";
do
{
indexAnswer++;
//Kiem tra xem Answer nhap vao co bi trung hay khong
bool isDuplicateAnswer = false;
do
{
try
{
Console.Write("Answer {0} : ", indexAnswer);
string tempAnswer = Console.ReadLine();
Console.Write("Is Answer correct? [Y/N/D] (Other key = Y) : ");
string choiceAnswer = Console.ReadLine();
if (choiceAnswer.ToUpper().Equals("N"))
{
choiceAnswer = "False";
}
else if (choiceAnswer.ToUpper().Equals("D"))
{
Console.WriteLine("You choose reinput Answer!");
isDuplicateAnswer = true;//De tiep tuc nhap lai cau hoi vua bo qua
continue;
}
else
{
choiceAnswer = "True";
}
listAnswer.Add(tempAnswer, choiceAnswer);
isDuplicateAnswer = false;
}
catch (Exception)
{
Console.WriteLine("Answer is already exist, reinput answer!");
isDuplicateAnswer = true;
}
} while (isDuplicateAnswer);
if (indexAnswer >= 2)//Them dap an
{
Console.Write("Do you want to add more Answer? [Y/N] (Other key = Y) : ");
addMoreAnswer = Console.ReadLine();
}
} while (!addMoreAnswer.ToUpper().Equals("N"));
//Kiem tra xem co dap an dung chua
bool isHaveCorrectAnswer = false;
foreach (string a in listAnswer.Values)
{
if (a.Equals("True")) isHaveCorrectAnswer = true;
}
int indexCorrectAnswer = 100;
if (!isHaveCorrectAnswer)
{
Console.WriteLine("Questions does not have Correct Answer!");
do
{
try
{
Console.Write("Enter Correct Answer (1 to {0}) : ", indexAnswer);
indexCorrectAnswer = Convert.ToInt32(Console.ReadLine()) - 1;
if (indexCorrectAnswer > indexAnswer - 1 || indexCorrectAnswer < 0) throw new Exception();
}
catch (Exception)
{
indexCorrectAnswer = 100;//tranh exception o vong while
Console.WriteLine("Please enter number from 1 to {0}!", indexAnswer);
}
} while (indexCorrectAnswer == 100);
//Gan index dap an dung vao list cac dap an
listAnswer[listAnswer.ElementAt(indexCorrectAnswer).Key] = "True";//listAnswer[listAnswer.Keys.ElementAt(indexCorrectAnswer)] = "True";
}
return listAnswer;
}
}
}
--------------------------------------------------------------------------------------------------- Class Program
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
namespace Assignment
{
class Program
{
static QuestionsManagement qm = new QuestionsManagement();
static void LoadData()
{
Dictionary<string, string> listAnswer = new Dictionary<string, string>();
listAnswer.Add("Answer1", "True");
listAnswer.Add("Answer2", "False");
listAnswer.Add("Answer3", "True");
listAnswer.Add("Answer4", "False");
qm.ListQuestion.Add(new Questions("G001", "1", "What is HTML?", 11, listAnswer));
qm.ListQuestion.Add(new Questions("G001", "2", "What is XML?", 12, listAnswer));
qm.ListQuestion.Add(new Questions("G001", "3", "What is XHTML?", 13, listAnswer));
qm.ListQuestion.Add(new Questions("G002", "4", "What is JAVASCRIPT?", 24, listAnswer));
qm.ListQuestion.Add(new Questions("G002", "5", "What is AJAX?", 25, listAnswer));
qm.ListQuestion.Add(new Questions("G002", "6", "What is JQUERY?", 26, listAnswer));
qm.ListQuestion.Add(new Questions("G003", "7", "What is ABSTRACTION?", 27, listAnswer));
qm.ListQuestion.Add(new Questions("G003", "8", "What is POLYMORPHISM?", 28, listAnswer));
qm.ListQuestion.Add(new Questions("G003", "9", "What is INHERITANCE?", 29, listAnswer));
}
static void Main(string[] args)
{
Console.Write("Do you want to load Data [Y/N] (Other key = N) : ");
string strLoadData = Console.ReadLine();
if (strLoadData.ToUpper().Equals("Y")) LoadData();
string mainChoice;
do
{
//Main menu
Console.WriteLine("============= TEST MANAGEMENT PROGRAM ==============");
Console.WriteLine("1. QUESTION/ANSWER MANAGEMENT");
Console.WriteLine("2. TEST MANAGEMENT");
Console.WriteLine("3. EXIT");
Console.WriteLine("-------------------");
Console.Write("* YOUR CHOICE (Other key = 3): ");
mainChoice = Console.ReadLine();
Console.WriteLine();
//Sub menu
switch (mainChoice)
{
case "1":
string subChoice;
do
{
Console.WriteLine("============ QUESTION/ANSWER MANAGEMENT ============");
Console.WriteLine("1. DISPLAY");
Console.WriteLine("2. UPDATE");
Console.WriteLine("3. ADD");
Console.WriteLine("4. BACK TO MAIN MENU");
Console.WriteLine("-------------------");
Console.Write("* YOUR CHOICE (Other key = 4): ");
subChoice = Console.ReadLine();
Console.WriteLine();
switch (subChoice)
{
case "1":
do
{
Console.WriteLine("----- QUESTION LIST -----");
Console.WriteLine("1. HTML5");
Console.WriteLine("2. JAVASCRIPT");
Console.WriteLine("3. OOP");
Console.WriteLine("4. BACK TO PREVIOUS MENU");
Console.WriteLine("-------------------");
Console.Write("* YOUR CHOICE (Other key = 4): ");
subChoice = Console.ReadLine();
switch (subChoice)
{
case "1":
qm.ShowCategory("G001");
break;
case "2":
qm.ShowCategory("G002");
break;
case "3":
qm.ShowCategory("G003");
break;
default:
break;
}
Console.WriteLine();
} while (subChoice.Equals("1") || subChoice.Equals("2") || subChoice.Equals("3"));
subChoice = "1";//de ve lai menu truoc, neu khong se thoat vi dung lai biet subChoice
break;
case "2":
Console.WriteLine("----- UPDATE QUESTION -----");
qm.UpdateQuestion();
Console.WriteLine();
break;
case "3":
Console.WriteLine("----- ADD QUESTION -----");
Questions q = qm.InputQuestion();
if (q != null)
{
qm.AddQuestion(q);
Console.WriteLine("Question has been Added successfully!");
}
else
{
Console.WriteLine("Question did not Add follow your choose!");
}
Console.WriteLine();
break;
default:
break;
}
} while (subChoice.Equals("1") || subChoice.Equals("2") || subChoice.Equals("3"));
break;
case "2":
Console.WriteLine("============ EXAM MANAGEMENT ============");
Console.WriteLine("1. DISPLAY");
Console.WriteLine("2. CREATE");
Console.WriteLine("3. BACK TO MAIN MENU");
Console.WriteLine("-------------------");
Console.Write("* YOUR CHOICE (Other key = 3): ");
subChoice = Console.ReadLine();
switch (subChoice)
{
case "1":
qm.DisplayAllQuestion();
break;
case "2":
do
{
Console.WriteLine("----- CREATE EXAM -----");
Console.WriteLine("1. EXAM BASED ON NUMBER QUESTIONS");
Console.WriteLine("2. EXAM BASED ON NUMBER TOTAL MARK");
Console.WriteLine("3. BACK TO PREVIOUS MENU");
Console.WriteLine("-------------------");
Console.Write("* YOUR CHOICE (Other key = 3): ");
subChoice = Console.ReadLine();
switch (subChoice)
{
case "1":
//Kiem tra so question nhap vao dung chua
int numberQuestion = 1;
bool isCorrectNumber = true;
Console.WriteLine("1. EXAM BASED ON NUMBER QUESTIONS");
do
{
try
{
Console.Write("Enter number of Question : ");
numberQuestion = Convert.ToInt32(Console.ReadLine());
if (numberQuestion > qm.ListQuestion.Count || numberQuestion < 1)
{
throw new Exception();
}
isCorrectNumber = false;
}
catch (Exception)
{
Console.WriteLine("Invalid number, reinput data!");
isCorrectNumber = true;
}
} while (isCorrectNumber);
//Tao de thi theo so luong cau hoi
List<Questions> listExam = new List<Questions>();
for (int i = 0; i < numberQuestion; i++)
{
listExam.Add(qm.ListQuestion.ElementAt(i));
}
Console.WriteLine("............Created Exam successfully!");
Console.Write("Do you want to save Exam [Y/N] (Other key = N) : ");
string strSave = Console.ReadLine();
if (strSave.ToUpper().Equals("Y"))
{
Console.Write("Enter file name : ");
string fileName = @"D:\"+Console.ReadLine() + ".txt";
//Ghi vao file..................
string fileContent = "";
foreach (Questions q in listExam)
{
fileContent += q.ToString();
}
System.IO.File.WriteAllText(fileName, fileContent);
//Stream fileStream = File.Create(fileName);
//BinaryFormatter bf = new BinaryFormatter();
//bf.Serialize(fileStream, listExam);
//fileStream.Close();
Console.WriteLine("Saving file............");
Console.WriteLine("File save successfully!");
}
else
{
Console.WriteLine("Exam does not save follow your choice!");
}
break;
case "2":
Console.WriteLine("2. EXAM BASED ON NUMBER TOTAL MARK");
Console.WriteLine("..........................UPDATING");
break;
default:
break;
}
} while (subChoice.Equals("1") || subChoice.Equals("2"));
break;
default:
break;
}
Console.WriteLine();
break;
default:
Console.WriteLine("GOODBYE!!!");
break;
}
} while (mainChoice.Equals("1") || mainChoice.Equals("2"));
Console.ReadLine();
}
}
}
No comments:
Post a Comment