-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStudentList.java
More file actions
204 lines (138 loc) · 6.29 KB
/
StudentList.java
File metadata and controls
204 lines (138 loc) · 6.29 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
// Completed Task 1 : Update code style for better consistency.
// Completed Task 2 : Fixed early terminates for passing wrong number of argument.
// Completed Task 3 : Makes improvement in variable names.
// Completed Task 4 : Refactors Duplicates File Read and write logic into method.
// Completed Task 5 : Replace String Literals with Constant.java Class.
// Completed Task 6 : Remove Temporary Variable.
// Completed Task 7 : Elimates 'Done' and adds better response for search operation.
// Completed Task 8 : Simplfies Counter Logic.
// Completed Task 9 : Adds handling for invalid arguments (Already handled in Task 2)
// Completed Task 10 : Add comments and more naming improvement
// ---------------------END -----------------------------------//
import java.io.*;
import java.text.*;
import java.util.*;
public class StudentList {
static BufferedReader bufferedReader;
static String lines;
static String [] studentNames;
// This method will create object of BufferReader and load lines in studentNames String Array. It is used to avoid repeatative calling.
public static void getReader(){
try {
bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("students.txt")));
lines = bufferedReader.readLine();
studentNames = lines.split(",");
}
catch (FileNotFoundException e) {
System.out.println(Constants.FileIOExMessage);
}
catch (IOException e) {
System.out.println(Constants.IOExMessage);
}
}
// Main Method
public static void main(String[] args) {
getReader();
// Check for invalid arguments
// Check if user pass no argument
if (args.length == 0) {
System.out.println(Constants.ZeroArgument);
}
// Check if user pass more than 1 argument
else if (args.length > 1) {
System.out.println(Constants.MultipleArgument);
}
// Check if user pass 1 argument but that is incorrect (Ex: java StudentList Akif)
else if (!args[0].startsWith("@") && !args[0].startsWith("+") && args[0].length() > 1) {
System.out.println(Constants.WrongArgument);
}
//Check if user pass 'a' to see the list of students.
else if (args[0].equals("a")) {
System.out.println(Constants.DataLoadingProgress);
try {
for (String name : studentNames) {
if(!name.startsWith("("))
System.out.println(name);
}
} catch (Exception e) {
System.out.println(Constants.DataLoadingFail);
}
System.out.println(Constants.DataLoadSucessMsg);
}
//Check if user pass 'r' to see a random student.
else if (args[0].equals("r")) {
System.out.println(Constants.DataLoadingProgress);
System.out.println("Random Students...");
try {
Random random = new Random();
int indexOfRandomStudent = random.nextInt(studentNames.length-1);
if(studentNames[indexOfRandomStudent].startsWith("("))
indexOfRandomStudent--;
System.out.println(studentNames[indexOfRandomStudent]);
} catch (Exception e) {
System.out.println(Constants.DataLoadingFail);
}
System.out.println(Constants.DataLoadSucessMsg);
}
//Check if user pass '+' to add a student name
else if (args[0].contains("+")) {
try {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("students.txt", true));
String newStudentName = args[0].substring(1);
Date todaysDate = new Date();
String formatedDateString = Constants.formatedDateString;
DateFormat dateFormat = new SimpleDateFormat(formatedDateString);
String currentDateTime = dateFormat.format(todaysDate);
bufferedWriter.write(newStudentName + ",(Added on " + currentDateTime+ "),");
System.out.println("New Data Added.");
bufferedWriter.close();
} catch (Exception e) {
System.out.println("Could not add new data");
}
}
//Check if user pass '@' to check if a student present in the list
else if (args[0].contains("@")) {
System.out.println(Constants.DataLoadingProgress);
try {
//Take the substring with @ symbol. (Ex : Making from @Akif to Akif)
String studentName = args[0].substring(1);
for (int idx = 0; idx < studentNames.length; idx++) {
if (studentNames[idx].startsWith(studentName)) {
System.out.println(Constants.FoundYesMessage);
System.out.println(Constants.DataLoadSucessMsg);
return;
}
}
System.out.println(Constants.FoundNoMessage);
} catch (Exception e) {
System.out.println(Constants.DataLoadingFail);
}
}
// Count the number of words and characters in the file.
else if (args[0].contains("c")) {
System.out.println(Constants.DataLoadingProgress);
try {
int totalChars = 0;
int totalWords = 0;
for (String student : studentNames) {
if (!student.startsWith("(")) {
// Check if a name contains 2-3 words splited by space like "Mehedi Hasan Tarik".
for (String word : student.split(" ")){
totalWords++;
totalChars+=word.length();
}
}
}
System.out.println("Word(s) found " + totalWords);
System.out.println("Characters(s) found " + totalChars);
}
catch (Exception e) {
System.out.println(Constants.DataLoadingFail);
}
System.out.println(Constants.DataLoadSucessMsg);
}
else {
System.out.println(Constants.WrongArgument);
}
}
}