- COMP.CS.140
- 7. Generics
- 7.5 ⌛⌛ Movie data vs stream
⌛⌛ Movie data vs stream¶
Place your code into files named Movie.java and MovieAnalytics.java in the directory
Round7/streams. Also remember to pull task material from student_template_project
.
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 then processing the data with Java
streams. You also need to implement a simple class Movie
for representing 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 initializes the object to holds the information provided by the constructor parameters: movie title, release year, duration, genre, rating score and director.Getter functions for all these: e.g.
getTitle()
,getReleaseYear()
, and so on.
The class
MovieAnalytics
should have the following public features:Constructor
MovieAnalytics()
that initializes an empty movie database. E.g. a simpleMovie
list 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 the form “title (By director, releaseYear)\\n
” (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 form “
title;releaseYear;duration;genre;score;director
”. That is, each line describes one movie and includes the same set of information that the constructor ofMovie
takes as parameters.A straight-forward split operation using a semicolon as a separator will work fine. Then 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
(including alsoyearA
andyearB
).Member function
Stream<Movie> moviesByDirector(String director)``that returns a stream that lists all movies directed by ``director
.
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 orderd by title (using the natural String
order).
A note about returning a stream: return a stream that has been set to perform intermediate (but no
terminal) operations. E.g 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
and
the example output 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 e.g. 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.