-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertNewQuestions.cs
More file actions
79 lines (73 loc) · 2.85 KB
/
ConvertNewQuestions.cs
File metadata and controls
79 lines (73 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// ReSharper disable ConvertToUsingDeclaration
namespace Az900_QuizApp;
public class ConvertNewQuestions
{
public static void Convert(string questionFile, string answerFile, int startAt)
{
ReadQuestions(questionFile, startAt);
ReadAnswers(answerFile, startAt);
}
private static void ReadAnswers(string file, int questionIndex)
{
var questionId = questionIndex;
try
{
// Open the file for reading using a StreamReader
using (var reader = new StreamReader(file))
{
// Read and display lines from the file until the end is reached
while (reader.ReadLine() is { } line)
{
var answer = line.Substring(4, 1);
Console.WriteLine(
$"{{\"question_id\": {questionId++}, \"correct_option\": \"{answer}\"}},");
}
}
}
catch (FileNotFoundException)
{
Console.WriteLine($"Error: File not found at {file}");
}
catch (IOException ex)
{
Console.WriteLine($"An error occurred while reading the file: {ex.Message}");
}
}
private static void ReadQuestions(string questionFile, int questionIndex)
{
var questionId = questionIndex;
try
{
// Open the file for reading using a StreamReader
using (var reader = new StreamReader(questionFile))
{
// Read and display lines from the file until the end is reached
while (reader.ReadLine() is { } line)
{
var question = line[4..];
var validLine = reader.ReadLine();
var questionOption1 = validLine?[3..];
var questionOption2 = reader.ReadLine()?[3..];
var questionOption3 = reader.ReadLine()?[3..];
var questionOption4 = reader.ReadLine()?[3..];
if (validLine != null && validLine.StartsWith("A") == false)
{
Console.WriteLine($"Error on question: \n{question}\nOption 1 did not start with an A");
Console.ReadKey();
return;
}
Console.WriteLine(
$"{{\n\"question_id\": {questionId++},\n\"question\": \"{question}\",\n\"options\": [\n\"{questionOption1}\",\n\"{questionOption2}\",\n\"{questionOption3}\",\n\"{questionOption4}\"\n]\n}},");
}
}
}
catch (FileNotFoundException)
{
Console.WriteLine($"Error: File not found at {questionFile}");
}
catch (IOException ex)
{
Console.WriteLine($"An error occurred while reading the file: {ex.Message}");
}
}
}