diff --git a/src/main/java/com/sf/codingcomp/tweet/Feed.java b/src/main/java/com/sf/codingcomp/tweet/Feed.java index 9cb2ce5..bb5fb60 100644 --- a/src/main/java/com/sf/codingcomp/tweet/Feed.java +++ b/src/main/java/com/sf/codingcomp/tweet/Feed.java @@ -4,7 +4,6 @@ import java.util.List; public class Feed { - private List tweets = new ArrayList(); public List getTweets() { @@ -14,5 +13,9 @@ public List getTweets() { public void setTweets(List tweets) { this.tweets = tweets; } - + public void addTweet(Tweet tweet) { + this.tweets.add(tweet); + } + + } diff --git a/src/main/java/com/sf/codingcomp/tweet/Tweeter.java b/src/main/java/com/sf/codingcomp/tweet/Tweeter.java index 05da9c4..2d3a88e 100644 --- a/src/main/java/com/sf/codingcomp/tweet/Tweeter.java +++ b/src/main/java/com/sf/codingcomp/tweet/Tweeter.java @@ -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 users = new ArrayList(); 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); } /** @@ -16,8 +65,31 @@ public void tweet(String text, User user) throws TweetTooLongException { * @return */ public List findMentions(User user) { - // TODO implement me - return null; + List usernames = new ArrayList(); + Feed tempFeed = user.getFeed(); + List 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; } /** @@ -29,9 +101,30 @@ public List findMentions(User user) { * @return */ public List findMostPopularHashtags(User user, int howMany) { - // TODO implement me - return null; + List hashtags = user.getHashtags(); + List returnHashtags = new ArrayList(); + Hashtag tempHashtag = null; + for(int i = 0; i findMostPopularHashtags(User user, int howMany) { * @return */ public List findMostRecentTweets(User user, int howMany) { - // TODO implement me - return null; + List userTweets = user.getFeed().getTweets(); + List tweets = new ArrayList(); + + 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; } /** @@ -54,7 +155,18 @@ public List 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; } } diff --git a/src/main/java/com/sf/codingcomp/tweet/User.java b/src/main/java/com/sf/codingcomp/tweet/User.java index aae4984..431fad4 100644 --- a/src/main/java/com/sf/codingcomp/tweet/User.java +++ b/src/main/java/com/sf/codingcomp/tweet/User.java @@ -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 following = new ArrayList(); + private List hashtags = new ArrayList(); + private int authored = 0; + + public User(){ + Tweeter.addUser(this); + } public String getUsername() { return username; @@ -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 getHashtags(){ + List newHashtags = new ArrayList(); + for(Hashtag hashtag: hashtags){ + newHashtags.add(hashtag); + } + return newHashtags; } public String toString() {