-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
149 lines (125 loc) · 5.22 KB
/
User.java
File metadata and controls
149 lines (125 loc) · 5.22 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
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class User implements UserInterface {
private String userID;
//private User user;
private UserControlPanel controlPanel;
//private Map<String, User> existingUsers;
private List<String> tweets;
private List<String> followers;
private Map<String, User> usersFollowed;
private String timeCreated;
private String lastUpdateTime;
//Constructor of user to initialize all the variables before the first method is called
public User() {
controlPanel = new UserControlPanel();
//existingUsers = Admin.getAdmin().getUsers();
tweets = new ArrayList<>();
usersFollowed = new HashMap<>();
followers = new ArrayList<>();
}
//this method opens the user control panel after recieving the username and the user object
public void openUser(String nodeName, User user) {
controlPanel.initializeUI(nodeName, user, getCreationTime());
this.userID = nodeName;
}
//save the username to this user object
public void setID(String ID) {
this.userID = ID;
}
//retrieve the username
public String getID() {
return this.userID;
}
//follow a user returns a boolean to ensure that the user exists
public boolean followUser(String ID) {
boolean isUser = false;
if ((Admin.getAdmin().getUsers().containsKey(ID) == true) && (!(ID.equals(getID())))) { //checks with admin to see if user exists
isUser = true;
Admin.getAdmin().getUsers().get(ID).addUserAsFollower(getID()); //enters the user that is being followed user object to add themselves as their follower
updateFollowingList(ID, Admin.getAdmin().getUsers().get(ID)); //updates the users following list (not needed in this program but in case it will in the future)
}
return isUser;
}
//adds user as a follower
public void addUserAsFollower(String ID) {
followers.add(ID);
}
//retrieve followers
public List<String> getFollowers() {
return followers;
}
//posts a tweet
public void postTweet(String tweet) {
lastUpdateTime();
String time = getLastUpdateTime(); //gets the user creation time
Admin.getAdmin().setLastUserToUpdate(getID()); //updates the admin with the user who last updated the tree (tweeted)
tweets.add(tweet);
controlPanel.updatePostsPanel(getID(), tweet); //updates the users post panel so that they can see their own tweet
Admin.getAdmin().addToTotalMessages();//increments total messages
if (positivityCheck(tweet) == true) { //checks if the message is positive
Admin.getAdmin().addToPositivity(); //increments total positive messages
}
else {
Admin.getAdmin().calculatePositvityPercentage(); // if not positive simply recalculate the positivity percentatge
}
for (int i = 0; i < followers.size() - 1; i++) { //iterates through the users followers so their newsfeed is updated
String element = followers.get(i);
Admin.getAdmin().getUsers().get(element).controlPanel.updatePostsPanel(getID(), tweet);
Admin.getAdmin().getUsers().get(element).setLastUpdateTime(time); //sets the last time the tree was updated
}
}
//update following list
public void updateFollowingList(String userFollowedID, User userFollowedObject) {
usersFollowed.put(userFollowedID, userFollowedObject);
}
//update news feed
public void updateNewsFeed(String follower, String message) {
this.controlPanel.updatePostsPanel(follower, message);
}
//checks if tweet is positive
public boolean positivityCheck(String tweet) {
boolean isPositive = false;
for (String word : Admin.getAdmin().getListPositiveWords()) { //retrieves the list of positive words
if (tweet.contains(word) == true) { //compares the string to the list of positive words
isPositive = true;
}
}
return isPositive;
}
//sets and formats the user creation time
public void setCreationTime(long time) {
Instant instant = Instant.ofEpochMilli(time);
LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h:mm a");
String formattedTime = dateTime.format(formatter);
this.timeCreated = formattedTime;
}
//retrieves user creation time
public String getCreationTime() {
return this.timeCreated;
}
//formats the last time the tree was updated
public void lastUpdateTime() {
Instant instant = Instant.ofEpochMilli(System.currentTimeMillis());
LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h:mm a");
String formattedTime = dateTime.format(formatter);
this.lastUpdateTime = formattedTime;
System.out.println("Last update at: " + this.lastUpdateTime);
}
//sets the last time the tree was updated
public void setLastUpdateTime(String time) {
this.lastUpdateTime = time;
}
//retrieves the last time the tree was updated
public String getLastUpdateTime() {
return this.lastUpdateTime;
}
}