-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudentList.java
93 lines (88 loc) · 3.1 KB
/
StudentList.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
import java.io.*;
import java.text.*;
import java.util.*;
public class StudentList {
public static void main(String[] args) {
// checking Arguments.
if (args.length != 1) {
System.out.println(constants.INVALID_MESSAGE);
} else if (args[0].equals(constants.SHOW_ALL)) {
System.out.println(constants.LOADED_DATA);
try {
BufferedReader bufferreader = readfile(); // read elements of file.
String studentnames[] = bufferreader.readLine().split(constants.COMMA);
for (String name : studentnames) {
System.out.println(name.trim());
}
bufferreader.close();
} catch (Exception e) {
}
System.out.println(constants.DATA_LOADED);
} // finding and printing random names from file.
else if (args[0].equals(constants.RANDOM_NAME)) {
System.out.println(constants.LOADED_DATA);
try {
BufferedReader bufferreader = readfile();
String studentnames[] = bufferreader.readLine().split(constants.COMMA);
int randomindex = new Random().nextInt(studentnames.length); // Random object created
System.out.println(studentnames[randomindex].trim());
bufferreader.close();
} catch (Exception e) {
}
System.out.println(constants.DATA_LOADED);
} // added new elemrnt into the file.
else if (args[0].contains(constants.PLUS)) {
System.out.println(constants.LOADED_DATA);
try {
BufferedWriter bufferwriter = writefile();
String name = args[0].substring(1);
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat(constants.DATE);
String formatdate = dateFormat.format(date);
bufferwriter.write(constants.COMMA + name + constants.LAST_UPDATE_MESSAGE + formatdate);
bufferwriter.close();
} catch (Exception e) {
}
System.out.println(constants.DATA_LOADED);
} // find out the specific word from file.
else if (args[0].contains(constants.FIND)) {
System.out.println(constants.LOADED_DATA);
try {
BufferedReader bufferreader = readfile();
String studentnames[] = bufferreader.readLine().split(constants.COMMA);
String name = args[0].substring(1);
int countNames = 0;
for (int index = 0; index < studentnames.length; index++) {
if (studentnames[index].trim().equals(name)) {
countNames = countNames + 1;
}
}
bufferreader.close();
} catch (Exception e) {
}
System.out.println(constants.DATA_LOADED);
} // counting the words.
else if (args[0].contains(constants.COUNT)) {
System.out.println(constants.LOADED_DATA);
try {
BufferedReader bufferreader = readfile();
char names[] = bufferreader.readLine().toCharArray();
System.out.println(names.length + constants.FOUND_MESSAGE);
bufferreader.close();
} catch (Exception e) {
}
System.out.println(constants.DATA_LOADED);
} else {
System.out.println(constants.INVALID_MESSAGE);
}
}
private static BufferedWriter writefile() throws IOException {
return new BufferedWriter(
new FileWriter(constants.FILE_NAME, true));
}
private static BufferedReader readfile() throws FileNotFoundException {
return new BufferedReader(
new InputStreamReader(
new FileInputStream(constants.FILE_NAME)));
}
}