Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Movie;
- import java.util.NoSuchElementException;
- public class Movie {
- String title;
- private MovieRating lista[];
- private int cont;
- private int max = 100;
- /**
- * Creates a new movie with the list of ratings empty .
- *
- * @param title Movie.Movie title .
- */
- public Movie(String title) {
- this.title=title;
- this.lista=new MovieRating[max];
- this.cont = 0;
- }
- /**
- * Returns the movie title
- *
- * @return the movie title .
- */
- public String getTitle() {
- return title;
- }
- /**
- * Inserts a new movieRating .
- * It is allowed to insert NOT_RATED .
- *
- * @param movieRating Movie.MovieRating to be inserted .
- */
- public void insertRating(MovieRating movieRating) {
- if(cont>=100){
- MovieRating[] temp = new MovieRating[max+1];
- for(int i =0; i< cont; i++){
- temp[i] = lista[i];
- }
- lista = temp;
- max++;
- }
- lista[cont] = movieRating;
- cont++;
- }
- /**
- * Check if this movie has any rating .
- *
- * @return true if and only if there is a valuation other than NOT_RATED .
- */
- public boolean isRated() {
- if(lista[0]==null){
- return false;
- }else return true;
- }
- /**
- * Gets the highest rating for this movie .
- *
- * @return maximum rating ; or NOT_RATED if there are no ratings .
- */
- public MovieRating maximumRating() {
- int x;
- MovieRating mejorvaloracion = MovieRating.NOT_RATED;
- for(x=0; lista[x] != null; x++){
- if(mejorvaloracion.getNumericRating() < lista[x].getNumericRating()){
- mejorvaloracion = lista[x];
- }
- }
- return mejorvaloracion;
- }
- /**
- * Calculate the numerical average rating of this movie .
- * NOT_RATED values are not considered .
- *
- * @return Numerical average rating of this movie .
- * @throws java . util . NoSuchElementException if there are no valid ratings .
- */
- public double averageRating() {
- int x;
- int suma = 0;
- int cont2=0;
- for(x = 0; lista[x]!=null; x++){
- if(lista[x] != MovieRating.NOT_RATED) {
- suma = suma + lista[x].getNumericRating();
- cont2++;
- }
- }
- if(cont2==0){
- throw new NoSuchElementException("No hay valoraciones");
- }else {
- return (double) suma / cont2;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement