import java.util.ArrayList;
public class ReviewAnalysis
{
/** All user reviews to be included in this analysis */
private Review[] allReviews;
/** Initializes allReviews to contain all the Review objects to be analyzed */
public ReviewAnalysis( int n )
{
allReviews = new Review[n];
}
public double getAverageRating()
{
int sum = 0;
for(int i = 0; i < allReviews.length; i++)
sum += allReviews[i].getRating();
// Make sure you divide with floating point numbers !
return (double)sum /(double) allReviews.length;
}
public ArrayList collectComments()
{
// Create an ArrayList to return
ArrayList result = new ArrayList();
// Go through all reviews
for(int i = 0; i < allReviews.length; i++)
{
// Process allReview[i]
String nextRev = allReviews[i].getComment();
if( nextRev.indexOf("!") >= 0 )
{ // Comment contains an "!"
String formatted = i + "-" + nextRev; // Make a formatted review
// Check if sentence ended properly, if not, add "."
String lastChar = formatted.substring(formatted.length() - 1);
if( ! lastChar.equals("!") && ! lastChar.equals("."))
formatted += ".";
// Insert formatted comment in result
result.add(formatted);
}
}
return result;
}
/* Help method to make question work.... */
public void add( int x, Review r )
{
allReviews[x] = r;
}
}
|