-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
170 lines (158 loc) · 4.03 KB
/
Program.cs
File metadata and controls
170 lines (158 loc) · 4.03 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Student_Records
{
class Program
{
static List<Student> records = new List<Student>();
static void Main(string[] args)
{
ReadInFile();
Console.WriteLine("Welcome to Student Records");
bool keepGoing = true;
while (keepGoing)
{
Console.WriteLine("1 - ");
Console.WriteLine("2 - Find Student by index No.");
Console.WriteLine("3 - Find First Match");
Console.WriteLine("4 - Output All Records");
Console.WriteLine("5 - Find Student by Student No.");
Console.WriteLine("6 - Change the Grade of a student");
Console.WriteLine("9 - Quit");
Console.Write("Select an option: ");
char menuChoice = Console.ReadLine().ToCharArray()[0];
Console.WriteLine();
switch (menuChoice)
{
case '1':
Console.Write("Please Enter FileName: ");
string fileName = Console.ReadLine() + ".csv";
WriteRecordsIntoFile(fileName);
break;
case '2':
FindRecordByIndex();
break;
case '3':
FindFirstMatch();
break;
case '4':
ListedAllRecords();
break;
case '5':
FindRecordByStudentNumber();
break;
case '6':
ChangeGrade();
break;
case '9':
keepGoing = false;
break;
default:
Console.WriteLine("Invalid menu choice");
break;
}
}
}
static void WriteRecordsIntoFile(string fileName)
{
using (StreamWriter writer = new StreamWriter(fileName))
{
foreach (var st in records)
{
writer.WriteLine(Convert.ToString(st.Number) + "," + st.firstName + "," + st.lastName + "," + st.DateOfBirth + "," + st.grade);
}
writer.Flush();
}
}
static void ReadInFile()
{
// Console.Write("Please Enter FileName: ");
// string fileName = Console.ReadLine() + ".csv";
try
{
ReadFileIntoRecordsArray("Student Records.csv");
}
catch (FileNotFoundException)
{
Console.WriteLine("File does not exist");
}
}
static void ReadFileIntoRecordsArray(string fileName)
{
using (StreamReader reader = new StreamReader(fileName))
{
int i = 0;
while (!reader.EndOfStream)
{
//records.Add(reader.ReadLine());
string[] details = reader.ReadLine().Split(',');
records.Add(new Student(Convert.ToInt32(details[0]), details[1], details[2], Convert.ToDateTime(details[3])));
records[i].grade = Convert.ToChar(details[4]);
i++;
}
}
}
static void ChangeGrade()
{
Console.Write("Enter a search string: ");
string searchString = Console.ReadLine();
Console.Write("Enter the new Grade: ");
char grade = Convert.ToChar(Console.ReadLine());
records.Where(r => r.ConvertToString().Contains(searchString)).First().grade = grade;
}
static void FindFirstMatch()
{
Console.Write("Enter a search string: ");
string searchString = Console.ReadLine();
var record = FindFirstMatch(searchString);
Student.Output(record);
}
public static Student FindFirstMatch(string searchString)
{
return records.Where(r => r.ConvertToString().Contains(searchString)).First();
}
static void FindRecordByIndex()
{
Console.Write("Enter Index No. :");
try
{
int index = Convert.ToInt32(Console.ReadLine());
Student.Output(records[index]);
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("Not a valid index");
}
catch (FormatException)
{
Console.WriteLine("Not a number");
}
}
static void ListedAllRecords()
{
foreach (var record in records.OrderBy(l => l.lastName).ThenBy(f => f.firstName))
{
Student.Output(record);
}
}
static void FindRecordByStudentNumber()
{
Console.Write("Enter Student No. :");
int number = Convert.ToInt32(Console.ReadLine());
//Get the number from the user
var record = records.Where(s => s.Number == number).FirstOrDefault();
if (record != null)
{
Student.Output(record);
}
else
{
Console.WriteLine("Invalid Number");
}
}
}
}