-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
127 lines (108 loc) · 3.14 KB
/
User.java
File metadata and controls
127 lines (108 loc) · 3.14 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
import java.util.ArrayList;
import java.util.List;
public class User implements Component, Observer, Subject{
private String userID;
private List<Observer> followers;
private List<User> following;
private List<String> postedMessages;
private List<String> newsFeed;
private UserGroup parentGroup;
private long creationTime;
private long lastUpdateTime;
// user constructor
public User(String userID){
long start = System.currentTimeMillis();
this.userID = userID;
this.followers = new ArrayList<>();
this.following = new ArrayList<>();
this.postedMessages = new ArrayList<>();
this.newsFeed = new ArrayList<>();
this.creationTime = System.currentTimeMillis() - start;
}
// get creation time
public long getCreationTime(){
return this.creationTime;
}
// get last update time
public long getLastUpdateTime(){
return this.lastUpdateTime;
}
// get parent (group) of user
public UserGroup getParent(){
return this.parentGroup;
}
// set the group of the user
public void setParent(UserGroup group){
this.parentGroup = group;
}
// follow another user
public void follow(User user){
following.add(user);
user.addObserver(this);
}
// post a message on the feed
public void postMessage(String message){
postedMessages.add(message);
update(message);
notifyObservers(message);
}
// get the followins of this user
public List<User> getFollowing(){
return this.following;
}
// get the posted messages of the user
public List<String> getPostedMessages(){
return this.postedMessages;
}
// get the news feed of the user
public List<String> getNewsFeed(){
return this.newsFeed;
}
// accept a visitor for data
public void acceptVisitor(Visitor visitor){
visitor.visit(this);
}
// get id of user
@Override
public String getID(){
return this.userID;
}
// add user
@Override
public void addUser(Component user) throws UnsupportedOperationException{
throw new UnsupportedOperationException("Cannot add a user to a user");
}
// remove user
@Override
public void removeUser(Component user) throws UnsupportedOperationException{
throw new UnsupportedOperationException("Cannot remove a user from a user");
}
// print user
@Override
public String toString(){
return getID();
}
// add observer
@Override
public void addObserver(Observer observer){
if(observer instanceof User){
this.followers.add((User) observer);
}
else{
this.followers.add((UserView) observer);
}
}
// notify observers of post
@Override
public void notifyObservers(String tweet){
for(Observer observer : followers){
observer.update(tweet);
}
}
// update observers
@Override
public void update(String tweet){
this.newsFeed.add(tweet);
this.lastUpdateTime = System.currentTimeMillis();
}
}