Advertisement
techno-

Movie.java

Oct 4th, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. package Movie;
  2.  
  3. import java.util.NoSuchElementException;
  4.  
  5. public class Movie {
  6.  
  7.     String title;
  8.     private MovieRating lista[];
  9.     private int cont;
  10.     private int max = 100;
  11.  
  12.     /**
  13.      * Creates a new movie with the list of ratings empty .
  14.      *
  15.      * @param title Movie.Movie title .
  16.      */
  17.     public Movie(String title) {
  18.         this.title=title;
  19.         this.lista=new MovieRating[max];
  20.         this.cont = 0;
  21.     }
  22.  
  23.     /**
  24.      * Returns the movie title
  25.      *
  26.      * @return the movie title .
  27.      */
  28.     public String getTitle() {
  29.         return title;
  30.     }
  31.  
  32.     /**
  33.      * Inserts a new movieRating .
  34.      * It is allowed to insert NOT_RATED .
  35.      *
  36.      * @param movieRating Movie.MovieRating to be inserted .
  37.      */
  38.     public void insertRating(MovieRating movieRating) {
  39.         if(cont>=100){
  40.             MovieRating[] temp = new MovieRating[max+1];
  41.             for(int i =0; i< cont; i++){
  42.                 temp[i] = lista[i];
  43.             }
  44.             lista = temp;
  45.             max++;
  46.         }
  47.         lista[cont] = movieRating;
  48.         cont++;
  49.     }
  50.  
  51.     /**
  52.      * Check if this movie has any rating .
  53.      *
  54.      * @return true if and only if there is a valuation other than NOT_RATED .
  55.      */
  56.     public boolean isRated() {
  57.         if(lista[0]==null){
  58.             return false;
  59.         }else return true;
  60.     }
  61.  
  62.     /**
  63.      * Gets the highest rating for this movie .
  64.      *
  65.      * @return maximum rating ; or NOT_RATED if there are no ratings .
  66.      */
  67.     public MovieRating maximumRating() {
  68.         int x;
  69.         MovieRating mejorvaloracion = MovieRating.NOT_RATED;
  70.         for(x=0; lista[x] != null; x++){
  71.             if(mejorvaloracion.getNumericRating() < lista[x].getNumericRating()){
  72.                 mejorvaloracion = lista[x];
  73.             }
  74.         }
  75.         return mejorvaloracion;
  76.     }
  77.  
  78.     /**
  79.      * Calculate the numerical average rating of this movie .
  80.      * NOT_RATED values are not considered .
  81.      *
  82.      * @return Numerical average rating of this movie .
  83.      * @throws java . util . NoSuchElementException if there are no valid ratings .
  84.      */
  85.     public double averageRating() {
  86.         int x;
  87.         int suma = 0;
  88.         int cont2=0;
  89.         for(x = 0; lista[x]!=null; x++){
  90.             if(lista[x] != MovieRating.NOT_RATED) {
  91.                 suma = suma + lista[x].getNumericRating();
  92.                 cont2++;
  93.             }
  94.         }
  95.         if(cont2==0){
  96.             throw new NoSuchElementException("No hay valoraciones");
  97.         }else {
  98.             return (double) suma / cont2;
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement