-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.java
More file actions
83 lines (78 loc) · 2.98 KB
/
Database.java
File metadata and controls
83 lines (78 loc) · 2.98 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
import java.util.ArrayList;
import java.io.*;
import java.io.File;
/**
* Team Project
*
* Database.java
*
* @author Huy Vu, Yanxin Yu - CS180 - L22
* @version 28 March 2024
*/
public class Database implements DatabaseInterface {
private String allUserAccountFile; // file name of save file
private ArrayList<UserAccount> allUserAccount;
public Database(String allUserAccountFile) {
this.allUserAccountFile = allUserAccountFile;
this.allUserAccount = new ArrayList<>();
}
public synchronized boolean readAllUserAccount() {
// read from file and make array of account objects
try {
File f = new File(allUserAccountFile);
FileReader fr = new FileReader(f);
BufferedReader bfr = new BufferedReader(fr);
//Skip first line
String firstline = bfr.readLine();
String line = bfr.readLine();
while (line != null) {
String[] element = line.split(";");
String[] userInfo = element[0].split(" ");
Profile profile = new Profile(userInfo[0], userInfo[1], Integer.parseInt(userInfo[2]),
userInfo[3], userInfo[4], userInfo[5], userInfo[6]);
String friendList = element[1].substring(12, element[1].length() - 1);
ArrayList<String> friends = new ArrayList<>();
String[] eachFriend = friendList.split(" ");
for (String username : eachFriend) {
friends.add(username);
}
String blockList = element[2].substring(11, element[2].length() - 1);
ArrayList<String> blockusers = new ArrayList<>();
String[] eachBlockUser = blockList.split(" ");
for (String username : eachBlockUser) {
blockusers.add(username);
}
UserAccount userAccount = new UserAccount(profile);
userAccount.setFriendList(friends);
userAccount.setBlockList(blockusers);
allUserAccount.add(userAccount);
line = bfr.readLine();
}
bfr.close();
} catch (IOException e) {
return false;
}
return true;
}
public synchronized boolean saveAllUserAccount() {
try {
FileOutputStream fos = new FileOutputStream(allUserAccountFile);
PrintWriter pw = new PrintWriter(fos);
pw.println("Database of User Account");
for (int i = 0; i < allUserAccount.size(); i++) {
pw.println(allUserAccount.get(i).toString());
}
pw.flush();
pw.close();
} catch (Exception e) {
return false;
}
return true;
}
public ArrayList<UserAccount> getAllUserAccount() {
return allUserAccount;
}
public void setAllUserAccount(ArrayList<UserAccount> allUserAccount) {
this.allUserAccount = allUserAccount;
}
}