-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataWriter.java
More file actions
43 lines (33 loc) · 1.18 KB
/
DataWriter.java
File metadata and controls
43 lines (33 loc) · 1.18 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
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class DataWriter extends DataConstants {
public static void saveUsers() {
Users users = Users.getInstance();
ArrayList<User> userList = users.getUsers();
JSONArray jsonUsers = new JSONArray();
//creating all the json objects
for(int i=0; i< userList.size(); i++) {
jsonUsers.add(getUserJSON(userList.get(i)));
}
//Write JSON file
try (FileWriter file = new FileWriter(USER_FILE_NAME)) {
file.write(jsonUsers.toJSONString());
file.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static JSONObject getUserJSON(User user) {
JSONObject userDetails = new JSONObject();
userDetails.put(USER_ID, user.getId().toString());
userDetails.put(USER_USER_NAME, user.getUserName());
userDetails.put(USER_FIRST_NAME, user.getFirstName());
userDetails.put(USER_LAST_NAME, user.getLastName());
userDetails.put(USER_AGE, user.getAge());
userDetails.put(USER_PHONE_NUMBER, user.getPhoneNumber());
return userDetails;
}
}