Skip to content
This repository was archived by the owner on Jan 16, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/main/java/com/sf/codingcomp/tweet/Feed.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;

public class Feed {

private List<Tweet> tweets = new ArrayList<Tweet>();

public List<Tweet> getTweets() {
Expand All @@ -14,5 +13,9 @@ public List<Tweet> getTweets() {
public void setTweets(List<Tweet> tweets) {
this.tweets = tweets;
}

public void addTweet(Tweet tweet) {
this.tweets.add(tweet);
}


}
132 changes: 122 additions & 10 deletions src/main/java/com/sf/codingcomp/tweet/Tweeter.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,60 @@
/*
* Copyright (c) 2016 ASU CodeDevils
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without
limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.sf.codingcomp.tweet;

import java.util.List;
import java.util.*;

public class Tweeter {
private static List<User> users = new ArrayList<User>();

public void tweet(String text, User user) throws TweetTooLongException {
// TODO implement me
if(text.length()>140) throw new TweetTooLongException();
// add tweet to the users feed
Tweet tweet = new Tweet(text, user);
user.addTweet(tweet);

// add tweet to the feed of all those mentioned in the tweet (@username)
for(int i = 0; i < text.length(); i++){
if(text.charAt(i)=='@'){
int temp = i+1;
do{
i++;
}while(text.charAt(i)!=' ' || i == text.length());
String mention = text.substring(temp, i);
for(User mentioned : users){
if(mentioned.getUsername().equals(mention) && !mentioned.getUsername().equals(user.getUsername())){
System.out.println("New tweet at: " + mentioned.getUsername());
mentioned.addTweet(tweet);
break;
}
}
}
}

// add tweet to the feed of all those being followed by this user
for(User aUser : users){
if(aUser.isFollowing(user)){
aUser.addTweet(tweet);
break;
}
}

}

public static void addUser(User user){
users.add(user);
}

/**
Expand All @@ -16,8 +65,31 @@ public void tweet(String text, User user) throws TweetTooLongException {
* @return
*/
public List<String> findMentions(User user) {
// TODO implement me
return null;
List<String> usernames = new ArrayList<String>();
Feed tempFeed = user.getFeed();
List<Tweet> tempTweets = tempFeed.getTweets();
for(Tweet tweet:tempTweets){
// uncomment below to not count mentions in feed by other authors
/*if(!tweet.getAuthor().equals(user.getUsername()))
continue;*/
String s = tweet.getText();
for(int i = 0; i < s.length(); i++){
if(s.charAt(i)=='@'){
int temp = i;
do{
i++;
}while(s.charAt(i)!=' ' || i == s.length());
String mention = s.substring(temp, i);
//System.out.println(mention);
if(!usernames.contains(mention) && !mention.equals("@" + user.getUsername())) {
usernames.add(mention);
//System.out.println(usernames.toString());
}
}
}
}
Collections.sort(usernames);
return usernames;
}

/**
Expand All @@ -29,9 +101,30 @@ public List<String> findMentions(User user) {
* @return
*/
public List<Hashtag> findMostPopularHashtags(User user, int howMany) {
// TODO implement me
return null;
List<Hashtag> hashtags = user.getHashtags();
List<Hashtag> returnHashtags = new ArrayList<Hashtag>();
Hashtag tempHashtag = null;
for(int i = 0; i<howMany; i++){
if(hashtags.isEmpty())
break;
tempHashtag = null;
for(Hashtag hashtag: hashtags) {
if(tempHashtag == null) {
tempHashtag = hashtag;
continue;
}
if(tempHashtag.getOccurrences() < hashtag.getOccurrences()) {
tempHashtag = hashtag;

}
}
returnHashtags.add(tempHashtag);
hashtags.remove(tempHashtag);
}

return returnHashtags;
}


/**
* This method finds the most recent tweets authored by the user.
Expand All @@ -42,8 +135,16 @@ public List<Hashtag> findMostPopularHashtags(User user, int howMany) {
* @return
*/
public List<Tweet> findMostRecentTweets(User user, int howMany) {
// TODO implement me
return null;
List<Tweet> userTweets = user.getFeed().getTweets();
List<Tweet> tweets = new ArrayList<Tweet>();

for(int i = userTweets.size()-1; i >=0; i--){
if(tweets.size()>= howMany)
break;
if(userTweets.get(i).getAuthor()==user)
tweets.add(userTweets.get(i));
}
return tweets;
}

/**
Expand All @@ -54,7 +155,18 @@ public List<Tweet> findMostRecentTweets(User user, int howMany) {
* @return
*/
public User findMostActiveFollower(User user) {
// TODO implement me
return null;
User mostActive = null;
for(User following: users){
if(following.isFollowing(user)){
if(mostActive == null) {
mostActive = following;
continue;
}
if(mostActive.getAuthored() < following.getAuthored())
mostActive = following;

}
}
return mostActive;
}
}
79 changes: 75 additions & 4 deletions src/main/java/com/sf/codingcomp/tweet/User.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
/*
* Copyright (c) 2016 ASU CodeDevils
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without
limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.sf.codingcomp.tweet;

import java.util.ArrayList;
import java.util.List;

public class User {

public String username;
public Feed feed = new Feed();
private String username;
private Feed feed = new Feed();
private List<User> following = new ArrayList<User>();
private List<Hashtag> hashtags = new ArrayList<Hashtag>();
private int authored = 0;

public User(){
Tweeter.addUser(this);
}

public String getUsername() {
return username;
Expand All @@ -20,13 +45,59 @@ public Feed getFeed() {
public void setFeed(Feed feed) {
this.feed = feed;
}

public int getAuthored() {
return this.authored;
}

public void follow(User user) {
// TODO implement me
following.add(user);
}

public void unfollow(User user) {
// TODO implement me
following.remove(user);
}

public boolean isFollowing(User user) {
for(User follow: following)
if(user.equals(follow))
return true;
return false;
}

public void addTweet(Tweet tweet) {
String text = tweet.getText();
for(int i = 0; i < text.length(); i++){
if(text.charAt(i)=='#'){
int temp = i;
do{
i++;
}while(i < text.length() && text.charAt(i)!=' ');
String substring = text.substring(temp, i);
this.addHashtag(substring);
}
}
if(tweet.getAuthor().equals(this));
this.authored++;
feed.addTweet(tweet);
}

public void addHashtag(String text) {
for(Hashtag hashtag: hashtags) {
if(hashtag.getText().equals(text)) {
hashtag.setOccurrences(hashtag.getOccurrences()+1);
return;
}
}
hashtags.add(new Hashtag(text, 1));
}

public List<Hashtag> getHashtags(){
List<Hashtag> newHashtags = new ArrayList<Hashtag>();
for(Hashtag hashtag: hashtags){
newHashtags.add(hashtag);
}
return newHashtags;
}

public String toString() {
Expand Down