From 3f3d52b4c76c220ac3fea6f70534629fc98f91ec Mon Sep 17 00:00:00 2001 From: Kristofer Hoadley Date: Fri, 26 May 2017 16:19:50 -0500 Subject: [PATCH 1/5] Passes userMentionsAnotherUser and tweetTooLong. --- .../java/com/sf/codingcomp/tweet/Tweeter.java | 28 +++++++++++++++++-- .../java/com/sf/codingcomp/tweet/User.java | 8 ++++-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/sf/codingcomp/tweet/Tweeter.java b/src/main/java/com/sf/codingcomp/tweet/Tweeter.java index 05da9c4..f111fc6 100644 --- a/src/main/java/com/sf/codingcomp/tweet/Tweeter.java +++ b/src/main/java/com/sf/codingcomp/tweet/Tweeter.java @@ -1,10 +1,18 @@ package com.sf.codingcomp.tweet; -import java.util.List; +import java.util.*; public class Tweeter { public void tweet(String text, User user) throws TweetTooLongException { + if(text.length()>140) throw new TweetTooLongException(); + Tweet thisTweet = new Tweet(text, user); + Feed tempFeed = user.getFeed(); + List tempTweets = tempFeed.getTweets(); + tempTweets.add(thisTweet); + tempFeed.setTweets(tempTweets); + user.setFeed(tempFeed); + // TODO implement me } @@ -16,8 +24,22 @@ 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){ + 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()); + usernames.add(s.substring(temp, i)); + } + } + } + return usernames; } /** diff --git a/src/main/java/com/sf/codingcomp/tweet/User.java b/src/main/java/com/sf/codingcomp/tweet/User.java index aae4984..5ca0120 100644 --- a/src/main/java/com/sf/codingcomp/tweet/User.java +++ b/src/main/java/com/sf/codingcomp/tweet/User.java @@ -1,9 +1,13 @@ 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 List following = new ArrayList(); public String getUsername() { return username; @@ -22,11 +26,11 @@ public void setFeed(Feed feed) { } public void follow(User user) { - // TODO implement me + following.add(user); } public void unfollow(User user) { - // TODO implement me + following.remove(user); } public String toString() { From c00e448fe5bc6d63622cc81812ca56b554675f7b Mon Sep 17 00:00:00 2001 From: Kristofer Hoadley Date: Fri, 26 May 2017 16:22:59 -0500 Subject: [PATCH 2/5] Added CodeDevils MIT license. --- .../java/com/sf/codingcomp/tweet/Tweeter.java | 14 ++++++++++++++ src/main/java/com/sf/codingcomp/tweet/User.java | 15 +++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/main/java/com/sf/codingcomp/tweet/Tweeter.java b/src/main/java/com/sf/codingcomp/tweet/Tweeter.java index f111fc6..8343562 100644 --- a/src/main/java/com/sf/codingcomp/tweet/Tweeter.java +++ b/src/main/java/com/sf/codingcomp/tweet/Tweeter.java @@ -1,3 +1,17 @@ +/* + * 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.*; diff --git a/src/main/java/com/sf/codingcomp/tweet/User.java b/src/main/java/com/sf/codingcomp/tweet/User.java index 5ca0120..deeca03 100644 --- a/src/main/java/com/sf/codingcomp/tweet/User.java +++ b/src/main/java/com/sf/codingcomp/tweet/User.java @@ -1,3 +1,18 @@ +/* + * 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; From a75f0b7dd4f2511e63e5ec221940d00823522cf6 Mon Sep 17 00:00:00 2001 From: Kristofer Hoadley Date: Mon, 29 May 2017 12:22:04 -0500 Subject: [PATCH 3/5] Tweeter now tracks all users currently active and posts new tweets to mentioned users feed. --- .../java/com/sf/codingcomp/tweet/Tweeter.java | 38 ++++++++++++++++++- .../java/com/sf/codingcomp/tweet/User.java | 4 ++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/sf/codingcomp/tweet/Tweeter.java b/src/main/java/com/sf/codingcomp/tweet/Tweeter.java index 8343562..e391d3b 100644 --- a/src/main/java/com/sf/codingcomp/tweet/Tweeter.java +++ b/src/main/java/com/sf/codingcomp/tweet/Tweeter.java @@ -17,9 +17,11 @@ import java.util.*; public class Tweeter { + private static List users = new ArrayList(); public void tweet(String text, User user) throws TweetTooLongException { if(text.length()>140) throw new TweetTooLongException(); + // add tweet to the users feed Tweet thisTweet = new Tweet(text, user); Feed tempFeed = user.getFeed(); List tempTweets = tempFeed.getTweets(); @@ -27,7 +29,30 @@ public void tweet(String text, User user) throws TweetTooLongException { tempFeed.setTweets(tempTweets); user.setFeed(tempFeed); - // TODO implement me + // 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()); + tempFeed = mentioned.getFeed(); + tempTweets = tempFeed.getTweets(); + tempTweets.add(thisTweet); + tempFeed.setTweets(tempTweets); + mentioned.setFeed(tempFeed); + } + } + } + } + + } + public static void addUser(User user){ + users.add(user); } /** @@ -42,6 +67,9 @@ public List findMentions(User user) { 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)=='@'){ @@ -49,10 +77,16 @@ public List findMentions(User user) { do{ i++; }while(s.charAt(i)!=' ' || i == s.length()); - usernames.add(s.substring(temp, i)); + 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; } diff --git a/src/main/java/com/sf/codingcomp/tweet/User.java b/src/main/java/com/sf/codingcomp/tweet/User.java index deeca03..709a499 100644 --- a/src/main/java/com/sf/codingcomp/tweet/User.java +++ b/src/main/java/com/sf/codingcomp/tweet/User.java @@ -23,6 +23,10 @@ public class User { public String username; public Feed feed = new Feed(); private List following = new ArrayList(); + + public User(){ + Tweeter.addUser(this); + } public String getUsername() { return username; From b54ccdc207c58ddffa703c65f47f73f54efb822e Mon Sep 17 00:00:00 2001 From: Kristofer Hoadley Date: Mon, 5 Jun 2017 14:12:33 -0500 Subject: [PATCH 4/5] mostTweets now passes. Refactored adding a tweet to a users feed. --- .../java/com/sf/codingcomp/tweet/Feed.java | 7 +- .../java/com/sf/codingcomp/tweet/Tweeter.java | 69 +++++++++++++++---- .../java/com/sf/codingcomp/tweet/User.java | 22 +++++- 3 files changed, 79 insertions(+), 19 deletions(-) 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 e391d3b..1e23f11 100644 --- a/src/main/java/com/sf/codingcomp/tweet/Tweeter.java +++ b/src/main/java/com/sf/codingcomp/tweet/Tweeter.java @@ -22,12 +22,8 @@ public class Tweeter { public void tweet(String text, User user) throws TweetTooLongException { if(text.length()>140) throw new TweetTooLongException(); // add tweet to the users feed - Tweet thisTweet = new Tweet(text, user); - Feed tempFeed = user.getFeed(); - List tempTweets = tempFeed.getTweets(); - tempTweets.add(thisTweet); - tempFeed.setTweets(tempTweets); - user.setFeed(tempFeed); + 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++){ @@ -40,17 +36,23 @@ public void tweet(String text, User user) throws TweetTooLongException { for(User mentioned : users){ if(mentioned.getUsername().equals(mention) && !mentioned.getUsername().equals(user.getUsername())){ System.out.println("New tweet at: " + mentioned.getUsername()); - tempFeed = mentioned.getFeed(); - tempTweets = tempFeed.getTweets(); - tempTweets.add(thisTweet); - tempFeed.setTweets(tempTweets); - mentioned.setFeed(tempFeed); + 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); } @@ -99,8 +101,34 @@ public List findMentions(User user) { * @return */ public List findMostPopularHashtags(User user, int howMany) { - // TODO implement me - return null; + List hashtags = new ArrayList(); + + Feed tempFeed = user.getFeed(); + List tempTweets = tempFeed.getTweets(); + for(Tweet tweet: tempTweets){ + String text = tweet.getText(); + 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); + boolean found = false; + for(Hashtag tag : hashtags){ + if(tag.getText().equals(mention)){ + // increment occurences by 1 + tag.setOccurrences(tag.getOccurrences()+1); + break; + } + if(!found) { + hashtags.add(new Hashtag(mention, 1)); + } + } + } + } + } + return hashtags; } /** @@ -124,7 +152,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 709a499..171b385 100644 --- a/src/main/java/com/sf/codingcomp/tweet/User.java +++ b/src/main/java/com/sf/codingcomp/tweet/User.java @@ -20,9 +20,10 @@ 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 int authored = 0; public User(){ Tweeter.addUser(this); @@ -43,6 +44,10 @@ public Feed getFeed() { public void setFeed(Feed feed) { this.feed = feed; } + + public int getAuthored() { + return this.authored; + } public void follow(User user) { following.add(user); @@ -51,6 +56,19 @@ public void follow(User user) { public void unfollow(User user) { 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) { + if(tweet.getAuthor().equals(this)); + this.authored++; + feed.addTweet(tweet); + } public String toString() { return username; From 66fb802b02e895c8cc0327cb819aa56df8fd270d Mon Sep 17 00:00:00 2001 From: Kristofer Hoadley Date: Mon, 5 Jun 2017 14:51:26 -0500 Subject: [PATCH 5/5] Tweeter now passes all tests. Added a list of hashtags to user that tracks occurrances of hashtags as they are added to the users feed. --- .../java/com/sf/codingcomp/tweet/Tweeter.java | 57 ++++++++++--------- .../java/com/sf/codingcomp/tweet/User.java | 30 ++++++++++ 2 files changed, 60 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/sf/codingcomp/tweet/Tweeter.java b/src/main/java/com/sf/codingcomp/tweet/Tweeter.java index 1e23f11..2d3a88e 100644 --- a/src/main/java/com/sf/codingcomp/tweet/Tweeter.java +++ b/src/main/java/com/sf/codingcomp/tweet/Tweeter.java @@ -101,35 +101,30 @@ public List findMentions(User user) { * @return */ public List findMostPopularHashtags(User user, int howMany) { - List hashtags = new ArrayList(); - - Feed tempFeed = user.getFeed(); - List tempTweets = tempFeed.getTweets(); - for(Tweet tweet: tempTweets){ - String text = tweet.getText(); - 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); - boolean found = false; - for(Hashtag tag : hashtags){ - if(tag.getText().equals(mention)){ - // increment occurences by 1 - tag.setOccurrences(tag.getOccurrences()+1); - break; - } - if(!found) { - hashtags.add(new Hashtag(mention, 1)); - } - } + 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; } /** diff --git a/src/main/java/com/sf/codingcomp/tweet/User.java b/src/main/java/com/sf/codingcomp/tweet/User.java index 171b385..431fad4 100644 --- a/src/main/java/com/sf/codingcomp/tweet/User.java +++ b/src/main/java/com/sf/codingcomp/tweet/User.java @@ -23,6 +23,7 @@ public class User { private String username; private Feed feed = new Feed(); private List following = new ArrayList(); + private List hashtags = new ArrayList(); private int authored = 0; public User(){ @@ -65,10 +66,39 @@ public boolean isFollowing(User user) { } 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() { return username;