- COMP.CS.140
- 8. Generics
- 8.5 ⌛⌛ Movie data as a stream
⌛⌛ Movie data as a stream¶
The exercise is returned as a Maven project. Place the pom.xml file
in the round8/streams directory of your local repository
and create to this directory the src/main/java subdirectory. Create class files
Movie.java and MovieAnalytics.java and attach them to the
fi.tuni.prog3.streams package. Your files should be in the
round8/streams/src/main/java/fi/tuni/prog3/streams directory. The test material is available in the
corresponding directory in the remote materials repository.
This task concerns basic use of Java streams. The goal is to implement a class MovieAnalytics
that offers functions for reading movie data from a file and for processing the data with Java
streams. You also need to implement a simple class Movie for representing the movie data.
Implement these two classes as follows:
The class
Movieshould have the following public features:Constructor
Movie(String title, int releaseYear, int duration, String genre, double score, String director)that initialises the object to hold the movie data provided by the constructor parameters: title, release year, duration, genre, rating score and director.Getter functions for all data of the movie:
getTitle(),getReleaseYear(), and so on.
The class
MovieAnalyticsshould have the following public features:Constructor
MovieAnalytics()that initialises an empty container for the movies. For example, a list containingMovieelements will suffice for this task.Static member function
Consumer<Movie> showInfo()that returns an object that implements the interfaceConsumer<Movie>and whose functionaccept(Movie t)prints out information of the movietin a single line of the form"title (By director, releaseYear)". The line ends into a new line. See the example output files for details. Please, note that it is enough to return a lambda that implements theConsumerinterface. For example,return p -> System.out.println(...);, where...is intentionally censored part of the statement.Member function
void populateWithData(String fileName)that reads movie data from the file spedicifed by the parameter.The file is expected to contain lines of the form
"title;releaseYear;duration;genre;score;director". That is, each line describes one movie and includes the same set of information that is passed via parameters to the constructor of theMovieclass.Straight-forward line splitting by semicolons with the
splitfunction of theStringclass will work fine here. You just need to convert some parts into suitable numbers.
Member function
Stream<Movie> moviesAfter(int year)that returns a stream that lists all movies released in the yearyearor later.Member function
Stream<Movie> moviesBefore(int year)that returns a stream that lists all movies released in the yearyearor earlier.Member function
Stream<Movie> moviesBetween(int yearA, int yearB)that returns a stream that lists all movies released between the yearsyearAandyearB, includingyearAandyearB.Member function
Stream<Movie> moviesByDirector(String director)that returns a stream that lists all movies directed bydirector.
The last four functions must return a stream that lists the movies in ascending order of release
year. Movies with the same release year are ordered by title using the default order of
the String objects.
A note about returning a stream: Return a stream that has been set to perform intermediate, but no
terminal operations. For example, a stream that lists the unique items of the array
int[] ia = [1, 2, 5, 4, 2, 5, 4] in sorted order could be created and returned as
return Arrays.stream(ia).distinct().sorted();. The recipient of this stream could then read the
values 1, 2, 4 and 5 from the stream.
The automatic tests, and the ones given below, assume that you make the following definitions
in your pom.xml project file:
The value of
artifactIdisstreams.The value of
versionis1.0.The values of the
maven.compiler.sourceandmaven.compiler.targetelements are17or lower. The grader uses Java 17, so any newer versions won’t work.A Onejar plugin definition where the value of
mainClassisMovieTestwhich 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 MovieTest.java,
the movie data files input1.txt, input1.txt and input1.txt and the example outputs given
in the files output1.txt, output2.txt and output3.txt.
Place MovieTest.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 MovieTest.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/streams-1.0.one-jar.jar input1.txt, java -jar target/streams-1.0.one-jar.jar input2.txt and
java -jar target/streams-1.0.one-jar.jar input3.txt 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.