This program simply creates an array of movies with a star rating for each one. My problem is, I want to have code that adds the contents of the array if the movie is the same. So for my example I was using Frozen Ground as the movie. In my code it is rated 3 stars and 4 stars as well. How would I sum those numbers together without hardcoding.
I guess I am asking, how would I access the 3 of the first Frozen Ground rating and the 4 of the second, and then sum them together without using ratings[1] or ratings[3] as the access variables
public class MovieRating
{
private String name; // Name of the movie
private int stars; // A number from 1-5
public MovieRating()
{
name = "Unknown";
stars = 0;
}
public MovieRating(String movieName, int numStars)
{
name = movieName;
stars = numStars;
}
public int getNumStars()
{
return stars;
}
public String getMovieName()
{
return name;
}
// Main method
public static void main(String[] args)
{
// Create an array with 4 movie ratings
MovieRating[] ratings = new MovieRating[4];
// Some hypothetical ratings. Note that we have to create a new MovieRating
// object for each array entry.
ratings[0] = new MovieRating("Big Miracle", 3);
ratings[1] = new MovieRating("Frozen Ground", 3);
ratings[2] = new MovieRating("Into The Wild", 5);
ratings[3] = new MovieRating("Frozen Ground", 4);
//COMPUTE COMMON SUM
// RATING FOR "Frozen Ground" -
String s = "Frozen Ground";
double average = 0;
for (int i = 0; i < ratings.length; i++)
{
if (ratings[i].getMovieName().equals(s))
{
int sum = ratings[i].getNumStars();
//System.out.println(sum);
}
//System.out.println(ratings[i].getNumStars());
}
}
}