-
Notifications
You must be signed in to change notification settings - Fork 0
/
schoolsearch.java
219 lines (199 loc) · 9.04 KB
/
schoolsearch.java
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
import java.io.*;
import java.util.*;
public class schoolsearch {
public static void main(String[] args) throws FileNotFoundException{
File studentsFile = new File("students.txt");
if(!studentsFile.exists()){
System.out.println("Cannot find file students.txt");
return;
}
ArrayList<Entry> entries = new ArrayList<Entry>();
getStudentEntries(entries, studentsFile);
getCommands(entries);
}
public static void getCommands(ArrayList<Entry> entries){
Scanner command = new Scanner(System.in);
int quitFlag = 0;
while(quitFlag == 0){
String userCommand = command.nextLine();
String[] tokens = userCommand.split("\\s+");
if(tokens[0].equals("S:") || tokens[0].equals("Student:")){
if(tokens.length == 3){
if(tokens[2].equals("B") || tokens[2].equals("Bus")){
studentBus(entries, tokens[1]);
}
}
if(tokens.length == 2){
student(entries,tokens[1]);
}
}
if(tokens[0].equals("G:") || tokens[0].equals("Grade:")){
if(tokens.length == 3){
if(tokens[2].equals("H") || tokens[2].equals("High")){
printStudentByGradeHiLo(entries, Integer.parseInt(tokens[1]), true);
}else if(tokens[2].equals("L") || tokens[2].equals("Low")){
printStudentByGradeHiLo(entries, Integer.parseInt(tokens[1]), false);
}
}else if(tokens.length == 2){
printStudentsByGrade(entries, Integer.parseInt(tokens[1]));
}
}
if(tokens.length == 2){
if(tokens[0].equals("T:") || tokens[0].equals("Teacher:")){
printStudentsByTeacher(entries, tokens[1]);
}else if(tokens[0].equals("B:") || tokens[0].equals("Bus:")){
printStudentsByBus(entries, Integer.parseInt(tokens[1]));
}else if(tokens[0].equals("A:") || tokens[0].equals("Average:")){
printAverageGPA(entries, Integer.parseInt(tokens[1]));
}
}
if(tokens.length == 1){
if(tokens[0].equals("Q") || tokens[0].equals("Quit")){
quitFlag = 1;
}
if(tokens[0].equals("I") || tokens[0].equals("Info")){
info(entries);
}
}
}
command.close();
}
public static void student(ArrayList<Entry> entries, String lastName){
for(int i = 0; i < entries.size(); i++){
if(entries.get(i).stLastName.equals(lastName.toUpperCase())){
System.out.println("Student Name: " + entries.get(i).stLastName + ", " + entries.get(i).stFirstName
+ ", Grade: " + entries.get(i).grade + ", Classroom: " + entries.get(i).classroom
+ ", Teacher Name: " + entries.get(i).tLastName + ", " + entries.get(i).tFirstName);
}
}
}
public static void studentBus(ArrayList<Entry> entries, String lastName){
for(int i = 0; i < entries.size(); i++){
if(entries.get(i).stLastName.equals(lastName.toUpperCase())){
System.out.println("Student Name: " + entries.get(i).stLastName + ", " + entries.get(i).stFirstName + ", Bus: " + entries.get(i).bus);
}
}
}
public static void printStudentsByTeacher(ArrayList<Entry> entries, String teacherLName){
System.out.println("Students in " + teacherLName + "'s class:");
for(int i = 0; i < entries.size(); i++){
if(entries.get(i).tLastName.equals(teacherLName.toUpperCase())){
System.out.println("\t" + entries.get(i).stLastName + ", " + entries.get(i).stFirstName);
}
}
}
public static void printStudentsByGrade(ArrayList<Entry> entries, int inputGrade){
if(inputGrade > -1 && inputGrade < 7){
System.out.println("Students in grade " + inputGrade + ":");
for(int i = 0; i < entries.size(); i++){
if(entries.get(i).grade == inputGrade){
System.out.println("\t" + entries.get(i).stLastName + ", " + entries.get(i).stFirstName);
}
}
}
}
public static void printStudentByGradeHiLo(ArrayList<Entry> entries, int inputGrade, boolean high){
Entry temp = null;
int studentsInGrade = 0;
for(int i = 0; i < entries.size(); i++){
if(entries.get(i).grade == inputGrade){
studentsInGrade++;
if(temp == null){
temp = entries.get(i);
}else if(high == true){
if(entries.get(i).gpa > temp.gpa){
temp = entries.get(i);
}
}else{
if(entries.get(i).gpa < temp.gpa){
temp = entries.get(i);
}
}
}
}
if(studentsInGrade > 0){
if(high == true){
System.out.println("Student with highest GPA in grade " + inputGrade + ": "
+ temp.stLastName + ", " + temp.stFirstName + ", GPA: " + temp.gpa
+ ", Teacher: " + temp.tLastName + ", " + temp.tFirstName + ", Bus route: " + temp.bus);
}else{
System.out.println("Student with lowest GPA in grade " + inputGrade + ": "
+ temp.stLastName + ", " + temp.stFirstName + ", GPA: " + temp.gpa
+ ", Teacher: " + temp.tLastName + ", " + temp.tFirstName + ", Bus route: " + temp.bus);
}
}else{
System.out.println("There are no students in grade " + inputGrade);
}
}
public static void printStudentsByBus(ArrayList<Entry> entries, int busNum){
System.out.println("Students on bus route " + busNum + ":");
for(int i = 0; i < entries.size(); i++){
if(entries.get(i).bus == busNum){
System.out.println("\tStudent Name: " + entries.get(i).stLastName + ", " + entries.get(i).stFirstName
+ ", grade: " + entries.get(i).grade + ", classroom: " + entries.get(i).classroom);
}
}
}
public static void printAverageGPA(ArrayList<Entry> entries, int targetGrade){
double total = 0.0;
int count = 0;
for(int i = 0; i < entries.size(); i++){
if(entries.get(i).grade == targetGrade){
total += entries.get(i).gpa;
count++;
}
}
System.out.print("For grade " + targetGrade + ", the average GPA is: ");
if(count > 0){
System.out.format("%.2f", total/count);
}else{
System.out.print("0.00");
}
System.out.println();
}
public static void info(ArrayList<Entry> entries){
System.out.println("School information:");
for(int i = 0; i < 7; i++){
int numberOfStudents = studentsInGrade(entries, i);
System.out.println("\tIn grade " + i + ", there are: " + numberOfStudents + " students");
}
}
public static int studentsInGrade(ArrayList<Entry> entries, int grade){
int studentCounter = 0;
for(int i = 0; i < entries.size(); i++){
if(entries.get(i).grade == grade){
studentCounter++;
}
}
return studentCounter;
}
public static void getStudentEntries(ArrayList<Entry> entries, File studentsFile) throws FileNotFoundException{
Scanner students = new Scanner(studentsFile);
String studentLine = "";
while(students.hasNextLine()){
studentLine = students.nextLine();
String[] tokens = studentLine.split(",");
Entry studentEntry = new Entry();
studentEntry.stFirstName = tokens[1];
studentEntry.stLastName = tokens[0];
studentEntry.grade = Integer.parseInt(tokens[2]);
studentEntry.classroom = Integer.parseInt(tokens[3]);
studentEntry.bus = Integer.parseInt(tokens[4]);
studentEntry.gpa = Double.parseDouble(tokens[5]);
studentEntry.tLastName = tokens[6];
studentEntry.tFirstName = tokens[7];
entries.add(studentEntry);
}
students.close();
}
public static void printAllEntries(ArrayList<Entry> entries){
for(int i = 0; i < entries.size(); i++){
System.out.println(entries.get(i).stFirstName + " " +entries.get(i).stLastName);
System.out.println("\tGrade: " + entries.get(i).grade);
System.out.println("\tClassroom: " + entries.get(i).classroom);
System.out.println("\tBus: " + entries.get(i).bus);
System.out.println("\tGPA: " + entries.get(i).gpa);
System.out.println("\tTeacher: " + entries.get(i).tFirstName + " " + entries.get(i).tLastName);
}
}
}