-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
64 lines (54 loc) · 2.31 KB
/
User.java
File metadata and controls
64 lines (54 loc) · 2.31 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
import java.util.*;
public class User {
// Storing user details
String username; // The username of the user
String password; // The password of the user (not encrypted in this simple example)
List<Tweet> tweets; // A list to store all the tweets made by the user
Set<String> following; // A set to store the usernames of users that the current user is following
Set<String> followers; // A set to store the usernames of users that follow the current user
// Constructor to initialize a new user with a username and password
public User(String username, String password) {
this.username = username; // Assign the username for the user
this.password = password; // Assign the password for the user
this.tweets = new ArrayList<>(); // Initialize the list to store tweets
this.following = new HashSet<>(); // Initialize the set of users the user is following
this.followers = new HashSet<>(); // Initialize the set of users who are following the user
}
// Method to post a new tweet
public void postTweet(String message) {
Tweet newTweet = new Tweet(username, message);
tweets.add(newTweet);
}
// Method to display all tweets by this user
public void displayTweets() {
// If the user has no tweets, print a message saying so
if (tweets.isEmpty()) {
System.out.println("No tweets found for user @" + username);
} else {
// Loop through all tweets and show each one
for (Tweet tweet : tweets) {
tweet.displayTweet();
}
}
}
// Method to follow another user
public void followUser(String username) {
following.add(username);
}
// Method to add a follower to the current user
public void addFollower(String username) {
followers.add(username);
}
// Method to check if the user is following another user
public boolean isFollowing(String username) {
return following.contains(username);
}
// Method to check if the user is followed by another user
public boolean isFollowedBy(String username) {
return followers.contains(username);
}
// Method for the user to like a tweet
public void likeTweet(Tweet tweet) {
tweet.addLike(username);
}
}