Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. /**
  2. * Created by Adrijana on 7/21/2017.
  3. */
  4.  
  5. import sun.reflect.generics.tree.Tree;
  6.  
  7. import java.util.*;
  8. import java.util.stream.Collectors;
  9.  
  10. public class MoviesTest {
  11. public static void main(String[] args) {
  12. Scanner scanner = new Scanner(System.in);
  13. MoviesList moviesList = new MoviesList();
  14. int n = scanner.nextInt();
  15. scanner.nextLine();
  16. for (int i = 0; i < n; ++i) {
  17. String title = scanner.nextLine();
  18. int x = scanner.nextInt();
  19. int[] ratings = new int[x];
  20. for (int j = 0; j < x; ++j) {
  21. ratings[j] = scanner.nextInt();
  22. }
  23. scanner.nextLine();
  24. moviesList.addMovie(title, ratings);
  25. }
  26. scanner.close();
  27. List<Movie> movies = moviesList.top10ByAvgRating();
  28. System.out.println("=== TOP 10 BY AVERAGE RATING ===");
  29. for (Movie movie : movies) {
  30. System.out.println(movie);
  31. }
  32. movies = moviesList.top10ByRatingCoef();
  33. System.out.println("=== TOP 10 BY RATING COEFFICIENT ===");
  34. for (Movie movie : movies) {
  35. System.out.println(movie);
  36. }
  37. }
  38. }
  39.  
  40. // vashiot kod ovde
  41. class Movie implements Comparable<Movie> {
  42. public String title;
  43. public int[] ratings;
  44. public float average;
  45. public float coefWithoutMax;
  46.  
  47. public Movie(String title, int[] ratings) {
  48. this.title = title;
  49. this.ratings = ratings;
  50. int sum = 0;
  51. for (int i = 0; i < ratings.length; ++i) {
  52. sum += ratings[i];
  53. }
  54. this.average = ((float) sum / (float) ratings.length);
  55. this.coefWithoutMax = (this.average * ratings.length);
  56.  
  57. }
  58.  
  59. @Override
  60. public String toString() {
  61. return String.format("%s (%.2f) of %d ratings", this.title, this.average, this.ratings.length);
  62. }
  63.  
  64. @Override
  65. public boolean equals(Object o) {
  66. if (this == o) return true;
  67. if (o == null || getClass() != o.getClass()) return false;
  68.  
  69. Movie movie = (Movie) o;
  70.  
  71. if (Float.compare(movie.average, average) != 0) return false;
  72. if (Float.compare(movie.coefWithoutMax, coefWithoutMax) != 0) return false;
  73. if (title != null ? !title.equals(movie.title) : movie.title != null) return false;
  74. return Arrays.equals(ratings, movie.ratings);
  75. }
  76.  
  77. @Override
  78. public int compareTo(Movie m) {
  79. if (Float.compare(this.average, m.average) != 0) {
  80. return Float.compare(this.average, m.average);
  81. }
  82.  
  83. if(Float.compare(this.coefWithoutMax, m.coefWithoutMax) != 0){
  84. return Float.compare(this.coefWithoutMax, m.coefWithoutMax);
  85. }
  86.  
  87. return this.title.compareTo(m.title);
  88.  
  89. }
  90. }
  91.  
  92. class MoviesList {
  93.  
  94. public Set<Movie> movies;
  95.  
  96. public MoviesList() {
  97. this.movies = new TreeSet<>() ;
  98. }
  99.  
  100. public void addMovie(String title, int[] ratings) {
  101. Movie movie = new Movie(title, ratings);
  102. this.movies.add(movie);
  103. }
  104.  
  105. public float ratingForAllMovies() {
  106. float allRatings = 0;
  107. for (Movie movie : movies) {
  108. allRatings += movie.ratings.length;
  109. }
  110. return allRatings;
  111. }
  112.  
  113. public List<Movie> top10ByAvgRating() {
  114.  
  115. List<Movie> tempList = new ArrayList<Movie>();
  116. tempList.addAll(movies);
  117. List<Movie> tenBest = new ArrayList<>(10);
  118.  
  119. int counter = 0;
  120. while (counter < 10) {
  121. Movie bestMovie = tempList.get(0);
  122. for (Movie m : tempList) {
  123. if (m.average > bestMovie.average) {
  124. bestMovie = m;
  125. }
  126. }
  127. tenBest.add(bestMovie);
  128. tempList.remove(bestMovie);
  129. counter++;
  130. }
  131. return tenBest;
  132. }
  133.  
  134. public List<Movie> top10ByRatingCoef() {
  135. //просечен ретјтинг на филмот x вкупно број на рејтинзи на филмот / максимален број на рејтинзи
  136. // (од сите филмови во листата)
  137. List<Movie> tempList = new ArrayList<Movie>();
  138. tempList.addAll(movies);
  139.  
  140. List<Movie> tenBest = new ArrayList<>(10);
  141. float ratingForAllMovies = ratingForAllMovies();
  142. int counter = 0;
  143. while (counter < 10) {
  144. Movie bestMovie = tempList.get(0);
  145. for (Movie m : tempList) {
  146. if((m.coefWithoutMax / ratingForAllMovies) > (bestMovie.coefWithoutMax/ ratingForAllMovies)){
  147. bestMovie = m;
  148. }
  149. }
  150. tenBest.add(bestMovie);
  151. tempList.remove(bestMovie);
  152. counter++;
  153. }
  154. return tenBest;
  155. }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement