-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorData.java
More file actions
152 lines (123 loc) · 4.95 KB
/
Copy pathErrorData.java
File metadata and controls
152 lines (123 loc) · 4.95 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
package dashboard;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class ErrorData {
//variables to store the data read from the file:
private String errorName;
private int serverID;
private String status;
private String lastUpdateTime;
private static final String DELIMITER = ",";
private static final int ERROR_LIMIT = 10;
private ErrorData[] errors;
//This method writes the file if called:
public void create(String dest) {
// Data for the CSV file, separated to make it easier to notice and understand:
String[] headers = {"ErrorName", "ServerID", "Status", "LastUpdateTime"};
String[][] data = {
{"Database Connection", "1", "In Progress", "30-10-2023 08:15"},
{"Disk Space Full", "2", "Automatic", "30-10-2023 08:16"},
{"CPU Overload", "1", "Not Opened", "30-10-2023 08:17"},
{"Network Disruption", "3", "Fixed", "30-10-2023 08:18"}
};
createCsvFile(dest, headers, data);
System.out.println("errors.csv file created successfully.");
}
//This method will call the method to create the file:
private static void createCsvFile(String fileName, String[] headers, String[][] data) {
try (FileWriter writer = new FileWriter(fileName)) {
// Write headers to the CSV file
for (String header : headers) {
writer.append(header).append(",");
}
writer.append("\n");
// Write data to the CSV file
for (String[] row : data) {
for (String value : row) {
writer.append(value).append(",");
}
writer.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
//This reads the data from file to the variables:
public void load(String source) {
File sourceFile = new File(source);
try (Scanner sc = new Scanner(sourceFile)) {
sc.useDelimiter(DELIMITER);
// Skip the header line
if (sc.hasNextLine()) {
sc.nextLine();
}
ArrayList<ErrorData> errorList = new ArrayList<>();
while (sc.hasNextLine() && errorList.size() < ERROR_LIMIT) {
String line = sc.nextLine();
// System.out.println("Line read: " + line); // Debug print
// Split the line into individual fields using ","
String[] fields = line.split(DELIMITER);
// Check if the line has the expected number of fields
if (fields.length == 4) {
String errorName = fields[0].trim();
int serverID = Integer.parseInt(fields[1].trim());
String status = fields[2].trim();
String lastUpdateTime = fields[3].trim();
errorList.add(new ErrorData(errorName, serverID, status, lastUpdateTime));
} else {
System.out.println("Error: Missing tokens in the input file. Skipping the current line.");
}
}
errors = errorList.toArray(new ErrorData[0]);
System.out.println("\nNo. of errors: " + errorList.size());
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
e.printStackTrace();
}
}
//Constructor:
public ErrorData(String errorName, int serverID, String status, String lastUpdateTime) {
this.errorName = errorName;
this.serverID = serverID;
this.status = status;
this.lastUpdateTime = lastUpdateTime;
}
//These are all constructors:
public ErrorData() {
}
public ErrorData[] getErrors() {
return errors;
}
public String getErrorName() {
return errorName;
}
public int getServerID() {
return serverID;
}
public String getStatus() {
return status;
}
public String getLastUpdateTime() {
return lastUpdateTime;
}
//This method creates a well-spaced table in the console:
public static void displayTable(ErrorData[] errors) {
System.out.printf("%-20s%-15s%-15s%-20s\n", "ErrorName", "ServerID", "Status", "LastUpdateTime");
for (ErrorData error : errors) {
System.out.printf("%-20s%-15s%-15s%-20s\n",
error.getErrorName(), error.getServerID(), error.getStatus(), error.getLastUpdateTime());
}
}
//Options for sorting:
public static void sortByLastUpdateTime(ErrorData[] errors) {
Arrays.sort(errors, Comparator.comparing(ErrorData::getLastUpdateTime));
}
public static void sortByStatus(ErrorData[] errors) {
Arrays.sort(errors, Comparator.comparing(ErrorData::getStatus));
}
}