Skip to content

Instantly share code, notes, and snippets.

Created March 3, 2013 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5078759 to your computer and use it in GitHub Desktop.
Save anonymous/5078759 to your computer and use it in GitHub Desktop.
package Networking.Intro;
import java.io.*;
import java.net.*;
import java.util.*;
public class LikePostRatio {
public static void main(String[] args){
try{
HashMap<String, ForumUser> users = new HashMap<String, ForumUser>();
List<ForumUser> finalout = new ArrayList<ForumUser>();
URL likeUrl = new URL("http://cnaude.freeshell.org/bukkit-likes.txt");
URLConnection c = likeUrl.openConnection();
InputStream in = c.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String likeLine = null;
URL msgUrl = new URL("http://cnaude.freeshell.org/bukkit-messages.txt");
URLConnection msgc = msgUrl.openConnection();
InputStream msgin = msgc.getInputStream();
BufferedReader msgbr = new BufferedReader(new InputStreamReader(msgin));
String msgline = null;
while((likeLine = br.readLine()) != null){
String[] splitted = likeLine.split(" ");
if("0".equalsIgnoreCase(splitted[1])) continue;
if(! splitted[1].isEmpty())
users.put(splitted[2], new ForumUser(splitted[2]+(splitted.length==4? splitted[3]:""), Double.parseDouble(splitted[1])));
}
while((msgline = msgbr.readLine()) != null){
String[] splitted = msgline.split(" ");
if("0".equalsIgnoreCase(splitted[1])) continue;
if(users.containsKey(splitted[2]+(splitted.length==4? splitted[3]:""))){
if(users.get(splitted[2]+(splitted.length==4? splitted[3]:"")).getLikes() > 0){
ForumUser f = users.get(splitted[2]+(splitted.length==4? splitted[3]:"")).setMessages(Double.parseDouble(splitted[1]));
if(f.getMessages() > 0)
users.put(splitted[2]+(splitted.length==4? splitted[3]:""), f);
}
}
}
/* for(ForumUser f : users.values()){
finalout.add(f);
} */
finalout.addAll(users.values());
Iterator<ForumUser> it = finalout.iterator();
while(it.hasNext()){
ForumUser f = it.next();
if(f.postLike()==0) it.remove();
}
Collections.sort(finalout);
System.out.println("Post:Like ratios:");
for(ForumUser f : finalout){
System.out.println(f.toString());
}
}catch (MalformedURLException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}
public class ForumUser implements Comparable {
private String name;
private double messages;
private double likes;
public static int place = 1;
public ForumUser(String name, double likes){
this.name = name;
this.likes = likes;
}
@Override
public int compareTo(Object o){
if(o instanceof ForumUser){
ForumUser user = (ForumUser)o;
double us = postLike();
double other = user.postLike();
if(us > other)
return 1;
else if(us == other)
return 0;
else if(other > us)
return - 1;
}
return 0;
}
public double getMessages(){
return messages;
}
public ForumUser setMessages(double messages){
this.messages = messages;
return this;
}
public double getLikes(){
return likes;
}
public ForumUser setLikes(double likes){
this.likes = likes;
return this;
}
public double postLike(){
return (getMessages() / getLikes());
}
@Override
public String toString(){
String s = place + ": " + postLike() + " " + name;
place++;
return s;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment