-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserData.java
More file actions
145 lines (112 loc) · 3.99 KB
/
Copy pathUserData.java
File metadata and controls
145 lines (112 loc) · 3.99 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
package dashboard;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;
class UserData {
private String ID;
private String Email;
private String Password;
private String Name;
private String Status;
private static final String DELIMITER = ",";
private static final int USER_LIMIT = 10;
private UserData[] users;
public void create(String dest){
// Data for the CSV file:
String[] headers = {"ID", "UserEmail", "Password", "Name", "Designation"};
String[][] data = {
{"000", "admin@sql", "0000", "Admin", "admin"},
{"001", "eng1@sql", "pass1", "Mr.Eng", "engineer"},
{"002", "eng2@sql", "pass2", "Ms.Eng", "engineer"}
};
createCsvFile(dest, headers, data);
System.out.println("users.csv file created successfully.");
}
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();
}
}
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();
}
//Storing in an array list:
ArrayList<UserData> userList = new ArrayList<>();
while (sc.hasNextLine() && userList.size() < USER_LIMIT) {
String line = sc.nextLine();
// 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 == 5) {
String id = fields[0].trim();
String email = fields[1].trim();
String password = fields[2].trim();
String name = fields[3].trim();
String status = fields[4].trim();
userList.add(new UserData(id, email, password, name, status));
} else {
System.out.println("Error: Missing tokens in the input file. Skipping the current line.");
}
}
users = userList.toArray(new UserData[0]);
System.out.println("\nNo. of users: " + userList.size());
sc.close();
} catch (FileNotFoundException e) {
System.out.println("Error: File not found.");
System.out.println("Creating file...");
create(source);
load(source);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
//Constructors:
public UserData(String ID, String Email, String Password, String Name, String Status) {
this.ID = ID;
this.Email = Email;
this.Password = Password;
this.Name = Name;
this.Status = Status;
}
public UserData[] getUsers() {
return users;
}
public String getEmail() {
return Email;
}
public String getPassword() {
return Password;
}
public String getName() {
return Name;
}
public String getID() {
return ID;
}
public String getStatus() {
return Status;
}
}