-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
206 lines (177 loc) · 6.39 KB
/
main.java
File metadata and controls
206 lines (177 loc) · 6.39 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import java.util.*;
class Post {
String content;
List<String> comments;
int likes;
int shares;
public Post(String content) {
this.content = content;
this.comments = new ArrayList<>();
this.likes = 0;
this.shares = 0;
}
public void addComment(String comment) {
comments.add(comment);
}
public void like() {
likes++;
}
public void share() {
shares++;
}
}
class User {
String name;
List<User> friends;
List<Post> posts;
public User(String name) {
this.name = name;
this.friends = new ArrayList<>();
this.posts = new ArrayList<>();
}
public void addFriend(User friend) {
friends.add(friend);
friend.friends.add(this);
}
public void removeFriend(User friend) {
friends.remove(friend);
friend.friends.remove(this);
}
public void addPost(String postContent) {
posts.add(new Post(postContent));
}
public void likePost(int postIndex) {
if (postIndex >= 0 && postIndex < posts.size()) {
posts.get(postIndex).like();
}
}
public void sharePost(int postIndex) {
if (postIndex >= 0 && postIndex < posts.size()) {
posts.get(postIndex).share();
}
}
public void subscribe(User user) {
if (!friends.contains(user)) {
friends.add(user);
user.friends.add(this);
}
}
public void commentOnPost(int postIndex, String comment) {
if (postIndex >= 0 && postIndex < posts.size()) {
posts.get(postIndex).addComment(comment);
}
}
public List<User> getMutualFriends(User other) {
List<User> mutualFriends = new ArrayList<>();
for (User friend : friends) {
if (other.friends.contains(friend)) {
mutualFriends.add(friend);
}
}
return mutualFriends;
}
public int getFriendCount() {
return friends.size();
}
}
class SocialNetwork {
Map<String, User> users;
public SocialNetwork() {
users = new HashMap<>();
}
public void addUser(String name) {
if (!users.containsKey(name)) {
users.put(name, new User(name));
}
}
public void removeUser(String name) {
if (users.containsKey(name)) {
User user = users.get(name);
for (User friend : user.friends) {
friend.friends.remove(user);
}
users.remove(name);
}
}
public void addFriendship(String userName1, String userName2) {
if (users.containsKey(userName1) && users.containsKey(userName2)) {
User user1 = users.get(userName1);
User user2 = users.get(userName2);
user1.addFriend(user2);
}
}
public void removeFriendship(String userName1, String userName2) {
if (users.containsKey(userName1) && users.containsKey(userName2)) {
User user1 = users.get(userName1);
User user2 = users.get(userName2);
user1.removeFriend(user2);
}
}
public int getUserCount() {
return users.size();
}
public Map<String, Integer> getFollowersCount() {
Map<String, Integer> followersCount = new HashMap<>();
for (User user : users.values()) {
followersCount.put(user.name, user.getFriendCount());
}
return followersCount;
}
}
public class main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
SocialNetwork socialNetwork = new SocialNetwork();
System.out.println("Welcome to the Social Network Analysis Tool!");
while (true) {
System.out.println("\nSelect an option:");
System.out.println("1. Add User");
System.out.println("2. Remove User");
System.out.println("3. Add Friendship");
System.out.println("4. Remove Friendship");
System.out.println("5. Analyze Followers");
System.out.println("6. Exit");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.println("Enter the name of the user to add:");
String addUser = scanner.nextLine();
socialNetwork.addUser(addUser);
System.out.println("User " + addUser + " added.");
break;
case 2:
System.out.println("Enter the name of the user to remove:");
String removeUser = scanner.nextLine();
socialNetwork.removeUser(removeUser);
System.out.println("User " + removeUser + " removed.");
break;
case 3:
System.out.println("Enter the names of users to form friendship (separated by space):");
String[] addFriendship = scanner.nextLine().split(" ");
socialNetwork.addFriendship(addFriendship[0], addFriendship[1]);
System.out.println("Friendship formed between " + addFriendship[0] + " and " + addFriendship[1] + ".");
break;
case 4:
System.out.println("Enter the names of users to remove friendship (separated by space):");
String[] removeFriendship = scanner.nextLine().split(" ");
socialNetwork.removeFriendship(removeFriendship[0], removeFriendship[1]);
System.out.println("Friendship removed between " + removeFriendship[0] + " and " + removeFriendship[1] + ".");
break;
case 5:
Map<String, Integer> followersCount = socialNetwork.getFollowersCount();
System.out.println("Followers count:");
for (Map.Entry<String, Integer> entry : followersCount.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
break;
case 6:
System.out.println("Exiting...");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice! Please enter a number between 1 and 6.");
}
}
}
}