-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
165 lines (161 loc) · 4.98 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StudentPick
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
public static class runtime
{
public static string datafileloc = "data.txt";
public static string fulldataraw;
public static List<Student> allstudents = new List<Student>();
public static bool AppDataIsLatest(string aad)
{
return String.Equals(fulldataraw, aad);
}
public static string LoadData()
{
if (!File.Exists(datafileloc))
{
fulldataraw = "";
} else
{
fulldataraw = File.ReadAllText(datafileloc);
}
if (AppDataIsOld(fulldataraw) || fulldataraw.Equals(""))
{
if (!fulldataraw.Equals(""))
{
MessageBox.Show("StudentPick has changed to a new App Data format. After you press OK, your data will be migrated to this new format", "Appdata upgrade", MessageBoxButtons.OK, MessageBoxIcon.Information);
} else
{
MessageBox.Show("Hello! Welcome to StudentPick. After you press OK, you will be asked to choose a class name. After you answer that initial prompt, you can add more classes and students at your discretion", "Welcome", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
OldAppdataUpgrade o = new OldAppdataUpgrade();
if (fulldataraw.Equals(""))
{
o.Text = "Set a class name";
}
DialogResult d = o.ShowDialog();
string nname = o.name;
//MessageBox.Show(fulldataraw);
foreach (string sss in fulldataraw.Replace('\r','\n').Split('\n'))
{
if (sss.Equals(""))
{
continue;//Skip empty names
}
string[] ssp = sss.Split(';');
Student student = new Student(ssp[0].Trim().Replace(" ", ""));
student.ClassName = nname;
allstudents.Add(student);
}
fulldataraw = "";
//Write with new data
SaveData();
return nname;
}
else
{
foreach (string line in fulldataraw.Split('\n'))
{
if (line.Replace(" ", "").Replace("\r", "").Equals(""))
{
continue;
}
allstudents.Add(Student.FromDataString(line));
}
}
return "";
}
public static void SaveData()
{
fulldataraw = "";
foreach (Student s in allstudents)
{
fulldataraw += s.OutToDataFile() + "\n";
}
File.WriteAllText(datafileloc, fulldataraw);
}
public static bool AppDataIsOld(string data)
{
if (data.Split('\n')[0].Split(';').Length < 3)
{
return true;
}
return false;
}
public static int BoolToInt(bool value)
{
if (value)
{
return 1;
} else
{
return 0;
}
}
public static bool IntToBool(int value)
{
if (value == 0)
{
return false;
} else
{
return true;
}
}
}
public class Student
{
public string Name { get; set; }
public bool ishidden = false;
public string ClassName { get; set; }
public Student (string name)
{
Name = name;
}
public string OutToDataFile()
{
return $"{Name};{runtime.BoolToInt(ishidden).ToString()};{ClassName}";
}
public void Hide()
{
ishidden = true;
}
public void Show()
{
ishidden = false;
}
public static Student FromDataString(string s)
{
string[] split = s.Split(';');
Student z = new Student(split[0]);
z.ishidden = runtime.IntToBool(int.Parse(split[1]));
try
{
z.ClassName = split[2];
} catch
{
//Do absolutely nothing
}
return z;
}
}
}