⌛⌛ Movie data as a stream¶
Place your code into files named Movie.java and MovieAnalytics.java in the round8/streams
directory of your local repository. 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
Movie
should 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
MovieAnalytics
should have the following public features:Constructor
MovieAnalytics()
that initialises an empty container for the movies. For example, a list containingMovie
elements 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 moviet
in a single line of the form"title (By director, releaseYear)"
. The line ends into a new line. See the example output files for details.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 theMovie
class.Straight-forward line splitting by semicolons with the
split
function of theString
class 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 yearyear
or later.Member function
Stream<Movie> moviesBefore(int year)
that returns a stream that lists all movies released in the yearyear
or earlier.Member function
Stream<Movie> moviesBetween(int yearA, int yearB)
that returns a stream that lists all movies released between the yearsyearA
andyearB
, includingyearA
andyearB
.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.
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
these files to the same directory with your code, compile the test program, for example, as
javac *.java
and run the tests as java MovieTest input1.txt
, java MovieTest input2.txt
and java MovieTest input3.txt
. 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.