import java.util.ArrayList;
public class
ReviewAnalysis
{
/** All user reviews to be included in
this analysis */
private Review[]
allReviews;
/**Initializes allReviews
to contains all the Review objects to be analyzed.*/
//Note: This constructor was changed a
little from question to pass the data
//into the object. It won't impact the solution.
public ReviewAnalysis(Review[] reviews)
{
allReviews
= reviews;
}
/**Returns a double representing the
average rating of all Review objects to
* be analyzed, as described in part A
* Precondition: allReviews
contains at least one Review.
*
No element of allReviews is null.
*/
public double getAverageRating()
{
//to be implemented in part A
int total = 0;
for (int i=0; i<allReviews.length; i++)
{
total += allReviews[i].getRating();
}
return (double)total/allReviews.length;
}
/** Returns an ArrayList
of String objects containing formatted versiosn
of
*
selected user comments, as described in part B.
* Precondition: allReviews
contains at least one Review.
*
No element of allReviews is null.
* Postcondition: allReviews
is unchanged.
*/
public ArrayList<String>
collectComments()
{
//to be implemented in part B
ArrayList<String> al = new ArrayList<String>();
for (int i=0; i<allReviews.length; i++)
{
if (allReviews[i].getComment().contains("!"))
{
String
toAdd = i + "-" + allReviews[i].getComment();
int lasti = toAdd.length()-1;
String
last = toAdd.substring(lasti, lasti+1);
if (!last.equals(".") && !last.equals("!"))
{
toAdd = toAdd + ".";
}
al.add(toAdd);
}
}
return al;
}
}
|