Repository navigation
Expand file tree
/
Copy pathGradeBook.java
More file actions
258 lines (232 loc) · 9.1 KB
/
Copy pathGradeBook.java
File metadata and controls
258 lines (232 loc) · 9.1 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import java.util.*;
import java.util.Map;
public class GradeBook{
public static Scanner sc = new Scanner(System.in);
public static Map<String, String> DBTeachers = new HashMap<String, String>();
public static String get_S(String text) {
System.out.print(text);
return sc.nextLine();
}
public static void Login() {
DBTeachers.put("Windows", "window");
// DBStudents.add(new Quartet<>("Aomine", "Daiki", "230107101", 21));
// DBStudents.add(new Quartet<>("Kise", "Ryota", "230107102", 24));
// DBStudents.add(new Quartet<>("Akashi", "Seijuro", "230107103", 25));
while(true) {
String username = get_S("Enter username: ");
String password = get_S("Enter password: ");
if (DBTeachers.containsKey(username) && DBTeachers.get(username).equals(password)) {
System.out.println("You have successfully logged in to your account");
return;
}
System.out.println("The name or password is not correct");
}
}
public static boolean Check_Student_Name(String name) {
return name.matches("[A-Z][a-z]+");
}
public static boolean Contain_Student_ID(String ID) {
for(Quartet<String, String, String, Integer> Q : DBStudents) {
if (Q.ID().equals(ID)) {
return true;
}
}
return false;
}
public static boolean Check_Student_ID(String ID) {
return ID.matches("[0-9]{9}");
}
public static boolean Check_Number(String number, Integer L, Integer R) {
if(number.isEmpty() || !number.matches("[0-9]+")) return false;
int x = Integer.parseInt(number);
return (L <= x && x <= R);
}
public static class Quartet<A, B, C, D> {
private final A Name;
private final B Surname;
private final C ID;
public D Score;
public Quartet(A Name, B Surname, C ID, D Score) {
this.Name = Name;
this.Surname = Surname;
this.ID = ID;
this.Score = Score;
}
public C ID() {return ID;}
public D Score() {return Score;}
}
public static int[] grades = new int[]{90, 80, 70, 60, 0};
public static int[] sum = new int[5];
public static char[] alp = new char[]{'A', 'B', 'C', 'D', 'F'};
public static List<Quartet<String, String, String, Integer>> DBStudents = new ArrayList<>();
public static int Highest_score, Lowest_score;
public static double Average_score;
public static void Update_results() {
if (DBStudents.isEmpty()) return;
sum = new int[5];
Highest_score = -1; Lowest_score = 1000; Average_score = -1;
for(Quartet<String,String,String,Integer> Q : DBStudents){
Highest_score = Math.max(Highest_score, Q.Score());
Lowest_score = Math.min(Lowest_score, Q.Score());
Average_score += Q.Score();
for(int j = 0; j < 5; ++j) {
if (grades[j] <= Q.Score()) {
sum[j] += 1;
break;
}
}
}
Average_score /= (double)DBStudents.size();
}
public static void AddStudent(boolean type){
String student_name = get_S("\nEnter name: ");
while(!Check_Student_Name(student_name)) {
System.out.println("Student's name is incorrect, please write correctly");
student_name = get_S("Enter name: ");
}
String student_surname = get_S("Enter surname: ");
while(!Check_Student_Name(student_surname)) {
System.out.println("Student's surname is incorrect, please write correctly");
student_surname = get_S("Enter surname: ");
}
String student_ID = get_S("Enter ID: ");
while(!Check_Student_ID(student_ID) || Contain_Student_ID(student_ID)) {
System.out.println("Student's ID is incorrect, please write correctly");
student_ID = get_S("Enter ID: ");
}
String student_score = get_S("Enter Score: ");
while(!Check_Number(student_score, 0, 100)) {
System.out.println("Student's Score is incorrect, please write correctly");
student_score = get_S("Enter Score: ");
}
DBStudents.add(new Quartet<>(student_name, student_surname, student_ID, Integer.parseInt(student_score)));
Update_results();
System.out.println("Student " + (type ? "added" : "modified") + " successfully");
}
public static void Remove_Student(String ID) {
for(int i = 0; i < DBStudents.size(); ++i) {
if (DBStudents.get(i).ID().equals(ID)) {
DBStudents.remove(i);
return;
}
}
}
public static void MangeRecords(){
if (DBStudents.isEmpty()) {
System.out.println("You can't manage change the data because there are no students");
return;
}
GenerateReports();
DeleteStudent(false);
AddStudent(false);
}
public static void CalulateGrades() {
Update_results();
System.out.println("\nGrades calculated" + "\n" + "Grade Details added");
}
public static void ViewStatistics() {
System.out.println("\nTotal Students: " + DBStudents.size());
System.out.println("Highest Students: " + Highest_score);
System.out.println("Average Score: " + String.format("%(.2f", Average_score));
System.out.println("Lowest Students: " + Lowest_score);
for(int i = 0; i < 5; ++i) {
System.out.println(alp[i] + ": " + sum[i]);
}
}
public static void DeleteStudent(boolean need) {
if (DBStudents.isEmpty()) {
System.out.println("You can't manage change the data because there are no students");
return;
}
if (need) {
GenerateReports();
}
String choosed = get_S("Enter number or ID: ");
while(!Check_Number(choosed, 1, DBStudents.size()) && !Contain_Student_ID(choosed)) {
System.out.println("You are entered incorrect number");
choosed = get_S("Enter number or ID: ");
}
if (Check_Number(choosed, 1, DBStudents.size())) {
choosed = DBStudents.get(Integer.parseInt(choosed)-1).ID();
}
Remove_Student(choosed);
if (need) {
System.out.println("Student deleted successfully.");
}
}
public static void output(String s, Integer n) {
System.out.print(s);
for(int i = 1; i <= n - (int)s.length(); ++i) {
System.out.print(" ");
}
}
public static void GenerateReports() {
if (DBStudents.isEmpty()) {
System.out.println("There are no students on the list");
return;
}
int i = 1;
System.out.println("The are list of students");
output("№", 3); output("Name", 10); output("Surname", 10); output("ID", 15); output("Score\n",0);
for(Quartet<String, String, String, Integer> Q : DBStudents){
System.out.print(i++ + ": ");
output(Q.Name, 10); output(Q.Surname, 10); output(Q.ID, 15); output(Q.Score.toString()+"\n", 0);
}
}
public static void Enter() {
System.out.print("\nEnter anything to continue: ");
sc.nextLine();
}
public static void Menu() {
while(true) {
System.out.println("\nMain Menu");
System.out.println("1. Add student");
System.out.println("2. Manage Records");
System.out.println("3. Calculate Grades");
System.out.println("4. View Statistics");
System.out.println("5. Generate Reports");
System.out.println("6. Delete Student");
System.out.println("7. Logout and exit\n");
System.out.print("Enter your choice: ");
String s = sc.nextLine();
if (!Check_Number(s, 1, 7)) {
System.out.println("You entered the number incorrectly");
} else {
int c = Integer.parseInt(s);
switch(c){
case 1:
AddStudent(true);
Enter();
break;
case 2:
MangeRecords();
Enter();
break;
case 3:
CalulateGrades();
Enter();
break;
case 4:
ViewStatistics();
Enter();
break;
case 5:
GenerateReports();
Enter();
break;
case 6:
DeleteStudent(true);
Enter();
break;
case 7:
System.out.println("You are successfully log outed");
System.exit(0);
}
}
}
}
public static void main(String[] args) {
Login();
Menu();
}
}