⌛⌛ Movie data as a stream #2

The exercise is returned as a Maven project. Place the pom.xml file in the round8/streams2 directory of your local repository and create to this directory the src/main/java subdirectory. Create class files Movie.java and MovieAnalytics2.java and attach them to the fi.tuni.prog3.streams2 package. Your files should be in the round8/streams2/src/main/java/fi/tuni/prog3/streams2 directory. The test material is available in the corresponding directory in the remote materials repository.

This task concerns using the Java streams with tools from the Collectors class. The goal is to implement the MovieAnalytics2 class for producing grouped information about movie data by using the groupingBy function of the Collectors class. This task uses the Movie class from the previous task to represent movie data.

The MovieAnalytics2 class should have the following public features:

  • Constructor MovieAnalytics2() that initializes an empty movie database. Just like in the previous exercise,

Movie list is a sufficient data structure here.

  • Member function void populateWithData(String fileName) that reads movie data from the file specifed by the parameter. The function behaves as the corresponding function in previous task, but it must be implemented differently (see below).

  • Member function void printCountByDirector(int n) that prints out the n directors that have directed the most movies, in descending order of the number of movies. Directors with equally many movies are ordered by their names using the default order of the String objects. Each director is printed in the form "director: x movies", where x is the number of movies directed by that particular director. The output is printed as a single line that ends in a new line character.

  • Member function void printAverageDurationByGenre() that prints out all movie genres of the database in ascending order of the average duration of the movies in that genre. Genres with the same average duration are ordered by the name of the genre using the default order of the String objects. Each genre is printed in the form "genre: average", where average is the average duration of the movies in that genre, using two decimals of precision. The output is printed as a single line that ends in a new line character.

  • Member function void printAverageScoreByGenre() that prints out all movie genres of the database in descending order of the average rating scores of movies in that genre. Genres with the same average rating are ordered by genre name using the default order of the String objects. Each genre is printed in the form "genre: average", where average is the average rating score of movies in that genre, using two decimals of precision. The output is printed as a single line that ends in a new line character.

You should use streams in this task. To this end, the grading system seeks to enforce a rule that the file MovieAnalytics2.java cannot contain any for or while loops. Since this restriction applies also to the reading of the file contents, implement also that part using a stream. Use the lines() function of the BufferedReader class to create a stream that reads input line by line.

A note about the grader’s so-called “loop detection”

The grader uses a very crude method for detecting loops: It checks that the source code does not contain the keywords “for” or “while”. The detection is overly eager in the sense that it matches also words like “form” that begin with the forbidden word “for”. These keywords are allowed, however, if they are directly preceded by a dot, for example .forEach and .format are allowed. Therefore, do not use variable or function names or even words in comments that begin with “for” or “while”.

The automatic tests, and the ones given below, assume that you make the following definitions in your pom.xml project file:

  • The value of artifactId is streams2.

  • The value of version is 1.0.

  • The values of the maven.compiler.source and maven.compiler.target elements are 17 or lower. The grader uses Java 17, so any newer versions won’t work.

  • A Onejar plugin definition where the value of mainClass is MovieTest2 which is the name of the given test class (see below).

Testing

You may test your implementation by using the test program given in the file MovieTest2.java, the movie data files input1.txt, input1.txt and input1.txt and the example output given in the files output1.txt, output2.txt and output3.txt.

Place MovieTest2.java into the root of the src/main/java subdirectory of your Maven project, and the other files into the root directory of your Maven project, that is, where the pom.xml is. Note that MovieTest2.java does not include a package definition and therefore is not placed into a deeper subdirectory.

After this you can compile the program with mvn package and run the tests as java -jar target/streams2-1.0.one-jar.jar input1.txt 10, java -jar target/streams2-1.0.one-jar.jar input2.txt 15 and java -jar target/streams2-1.0.one-jar.jar input3.txt 20 in the root directory of the project. The expected outputs of these tests are given in the files output1.txt, output2.txt and output3.txt.

A+ presents the exercise submission form here.