Repository navigation
Expand file tree
/
Copy pathStudentManagementSystem.cs
More file actions
100 lines (81 loc) · 2.54 KB
/
Copy pathStudentManagementSystem.cs
File metadata and controls
100 lines (81 loc) · 2.54 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
namespace KashifRaza
{
class Students
{
public int id { get; set; }
public string name { get; set; }
public double gpa { get; set; }
}
internal class Program
{
static List<Students> students = new List<Students>();
static string f = "students.json";
static void Main(string[] args)
{
load();
while (true)
{
Console.WriteLine("\n1. Add 2. View 3. Search 4. Save 5. Exit");
Console.Write("Choose: ");
int c = int.Parse(Console.ReadLine());
if (c == 1)
{
Students s = new Students();
Console.Write("ID: ");
s.id = int.Parse(Console.ReadLine());
Console.Write("Name: ");
s.name = Console.ReadLine();
Console.Write("GPA: ");
s.gpa = double.Parse(Console.ReadLine());
students.Add(s);
}
if (c == 2)
{
students.ForEach(student =>
Console.WriteLine($"{student.id} - {student.name} - {student.gpa}"));
}
if (c == 3)
{
Console.Write("ID: ");
int id = int.Parse(Console.ReadLine());
var student = students.FirstOrDefault(x => x.id == id);
Console.WriteLine(student != null
? student.name
: "not found");
}
if (c == 4)
{
save();
Console.WriteLine("Saved successfully.");
}
if (c == 5)
{
save();
break;
}
}
}
static void load()
{
if (File.Exists(f))
{
string json = File.ReadAllText(f);
students = JsonSerializer.Deserialize<List<Students>>(json)
?? new List<Students>();
}
}
static void save()
{
string json = JsonSerializer.Serialize(
students,
new JsonSerializerOptions { WriteIndented = true }
);
File.WriteAllText(f, json);
}
}
}