I have a method that looks like
public double calculator(Iterable<Integer> userRating)
I'm trying to implement logic that removes the highest and lowest values and calculate the average. The end result should look like (removing the mins -2, and maxs 15)
public void ProperlyCalculates() {
List<Integer> ratings = new ArrayList<Integer>();
ratings.add(-2);
ratings.add(-2);
ratings.add( 3);
ratings.add( 7);
ratings.add( 8);
ratings.add( 9);
ratings.add(15);
ratings.add(15);
ratings.add(15);
double rating = rater.getMovieRating(ratings);
assertEquals(6.75D, rating, 0.0000001D);
I'm confusing myself trying to create an iterator that I can loop through and add in my logic. So far I have tried.
public double getMovieRating(Iterable<Integer> userRating) {
Iterator ratings = userRating.iterator();
int min = Integer.MIN_VALUE;
int max = Integer.MAX_VALUE;
while (ratings.hasNext()) {
Integer val = ratings.next();
if (val > max){ max = val;}
if (val < min) {min = val;}
}
}
But Integer val = ratings.next()
throws an incompatible type
error. How can I can loop through all of the values in the iterator? Is this a case where I would need a private inner class?