You are on page 1of 5

import java.util.

*;

import com.teamtreehouse.Treet;
import com.teamtreehouse.Treets;

public class Example {

public static void main(String[] args) {


Treet[] treets = Treets.load();
System.out.printf("There are %d treets. %n",
treets.length);

Set<String> allHashTags = new HashSet<String>();


Set<String> allMentions = new TreeSet<String>();
for (Treet treet : treets) {
allHashTags.addAll(treet.getHashTags());
allMentions.addAll(treet.getMentions());
}

System.out.printf("Hash tags: %s %n", allHashTags);


System.out.printf("Mensions: %s %n", allMentions);

Map<String, Integer> hashTagCounts = new HashMap<String, Integer>();


for (Treet treet : treets){
for (String hashTag : treet.getHashTags()){
Integer count = hashTagCounts.get(hashTag);
if(count == null){
count = 0 ;
}
count++;
hashTagCounts.put(hashTag,count);
}
}
System.out.printf("Hash tag counts: %s %n", hashTagCounts);

Map<String, List<Treet>> treetsByAuthor = new HashMap<String, List<Treet>>();


for (Treet treet : treets) {
List<Treet> authoredTreets = treetsByAuthor.get(treet.getAuthor());
if(authoredTreets == null){
authoredTreets = new ArrayList<Treet>();
treetsByAuthor.put(treet.getAuthor(), authoredTreets);
}
authoredTreets.add(treet);
}

System.out.printf("Treets by author: %s %n", treetsByAuthor);


System.out.printf("Treets by nickrp: %s %n", treetsByAuthor.get("nickrp"));

}
}

package com.teamtreehouse;

import java.io.Serializable;
import java.*;

public class Treet implements Comparable<Treet>, Serializable {


private static final long serialVersionUID = 7146681148113043748L;
private String mAuthor;
private String mDescription;
private Date mCreationDate;

public Treet(String author, String description, Date creationDate) {


mAuthor = author;
mDescription = description;
mCreationDate = creationDate;
}

@Override
public String toString() {
return String.format("Treet:

\"%s\" by %s on %s",

mDescription, mAuthor, mCreationDate);


}

@Override
public int compareTo(Treet other) {
if (equals(other)) {
return 0;
}
int dateCmp = mCreationDate.compareTo(other.mCreationDate);
if (dateCmp == 0) {
return mDescription.compareTo(other.mDescription);
}
return dateCmp;
}

public String getAuthor() {


return mAuthor;
}

public String getDescription() {


return mDescription;
}

public Date getCreationDate() {


return mCreationDate;
}

public List<String> getWords() {


String[] words = mDescription.toLowerCase().split("[^\\w#@']+");
return Arrays.asList(words);

public List<String> getHashTags(){


return getWordsPrefixedWith("#");
}
public List<String> getMentions(){
return getWordsPrefixedWith("@");
}

private List<String> getWordsPrefixedWith(String prefix){


List<String> results = new ArrayList<String>();
for(String word : getWords())
if(word.startsWith(prefix))
results.add(word);

return results;
}}

package com.teamtreehouse;

import java.io.*;

public class Treets {


public static void save(Treet[] treets) {
try (
FileOutputStream fos = new FileOutputStream("treets.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
) {
oos.writeObject(treets);
} catch(IOException ioe) {
System.out.println("Problem saving Treets");
ioe.printStackTrace();
}
}

public static Treet[] load() {


Treet[] treets = new Treet[0];
try (
FileInputStream fis = new FileInputStream("treets.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
) {
treets = (Treet[]) ois.readObject();
} catch(IOException ioe) {
System.out.println("Error reading file");
ioe.printStackTrace();
} catch(ClassNotFoundException cnfe) {
System.out.println("Error loading treets");
cnfe.printStackTrace();
}
return treets;
}
}