-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocialNetwork.java
94 lines (76 loc) · 3.65 KB
/
SocialNetwork.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
94
import java.util.*;
@SuppressWarnings("unused")
public class SocialNetwork {
public static void main(String[] args) {
String network = "";
// Start with an empty network; print it out.
System.out.println("Initial Network:");
System.out.println(network);
// Add three users, swofferh, achen47 and hibbahk, in the correct order and with the friends listed above.
network = addUser(network, "swofferh", "gbuck21-haleyc22-macy16-mnats-mxw-nwang913-gumball");
network = addUser(network, "achen47", "ali1726-cartierc-tan7271-yethanli-hzpan8");
network = addUser(network, "hibbahk", "janvi26-jvdyfu");
// Print out the network and the network's size.
System.out.println("Network after adding swofferh, achen47, and hibbahk:");
System.out.println(network);
System.out.println("Network size: " + networkSize(network));
// Remove the user achen47.
network = removeUser(network, "achen47");
// Print out the network and the network's size.
System.out.println("Network after removing achen47:");
System.out.println(network);
System.out.println("Network size: " + networkSize(network));
// Add the user juliak24, with the friends listed above.
network = addUser(network, "juliak24", "qsun05-ldhar-lukel7-parakhm");
// Print out the network and the network's size.
System.out.println("Network after adding juliak24:");
System.out.println(network);
System.out.println("Network size: " + networkSize(network));
// Add the user meravf, with the friends listed above.
network = addUser(network, "meravf", "rkorol-samkoro-sbabu23-shay2022-sshan854-vwang2");
// Print out the network and the network's size.
System.out.println("Network after adding meravf:");
System.out.println(network);
System.out.println("Network size: " + networkSize(network));
// Add the user mxw, with no friends.
network = addUser(network, "mxw", "");
// Print out the network and the network's size.
System.out.println("Network after adding mxw:");
System.out.println(network);
System.out.println("Network size: " + networkSize(network));
}
// Method to add a user to the network
public static String addUser(String network, String user, String friends) {
if (network.isEmpty()) {
return user + ":" + friends + ",";
} else {
return network + user + ":" + friends + ",";
}
}
// Method to count the number of users in the network
public static int networkSize(String network) {
if (network.isEmpty()) {
return 0;
}
return network.split(",").length;
}
// Method to remove a user from the network
public static String removeUser(String network, String user) {
int startIndex = network.indexOf(user + ":");
if (startIndex == -1) {
return network;
}
int endIndex = network.indexOf(",", startIndex);
if (endIndex == -1) {
return network.substring(0, startIndex);
}
String updatedNetwork = network.substring(0, startIndex) + network.substring(endIndex + 1);
// Remove references to the deleted user from other users' friends lists
String userToRemove = user + "-";
String userToRemoveEnd = "-" + user;
while (updatedNetwork.contains(userToRemove) || updatedNetwork.contains(userToRemoveEnd)) {
updatedNetwork = updatedNetwork.replace(userToRemove, "").replace(userToRemoveEnd, "");
}
return updatedNetwork;
}
}