These exercises are intended to bring together a lot of what we have covered so far in our lessons. Make sure you ask for help on the scala-school channel if you get stuck!
There will be a part-2 of these exercises that will look at expanding our domain to include Books and Music, which will explore traits and how we can use them.
Remember that there are the recap worksheets that you can use as references and cheat sheets whilst attempting the exercises.
Flying in the face of successful subscription-based services, let's start mapping out the domain of an entertainment website where people can rent items of interest, we can call it Guflix and we can start with Films.
Model a Film
with a case class. It should have a title
, director
, runtime
(in minutes), releaseDate
, rating
, price
(in £), and genre
.
Hints:
-
Think about what types these fields should be. You could use a
String
for yourreleaseDate
field, but you could try importingjava.util.LocalDate
and using that instead. -
The names of
case classes
andobjects
are always TitleCase whereas fields are always camelCase. -
Once you've defined your
case class
, try creating a few instances with different values and storing them in separate variables or directly in aList[Film]
.
Create a Film
object
(as a convenient place to store functions) and define the following functions within it:
-
a function called
filmsWithinBudget
that takes in aList
ofFilm
s and abudget
and returns aList
ofFilms
that can be rented within that budget. -
a function called
withStandardiseDirectorName
that takes aFilm
and returns aFilm
where everything is the same apart from the director name, which been converted to a first initial followed by the surname e.g.Martin Scorsese
->M. Scorsese
Kathryn Bigelow
->K. Bigelow
The Coen Brothers
->The Coen Brothers
How might you handle the latter case? How might you create a copy of the inputFilm
and change the value of thedirector
field? -
a function called
standardiseDirectorNames
that takes aList
ofFilm
s and returns aList
ofFilm
where all of the director names have been standardised. Try to reuse what you have already written. -
a function called
findSimilar
with the following signature:
def findSimilar(mainFilm: Film, candidates: List[Film]): List[Film]
it should return a List
of Film
s that are 'similar' to the mainFilm
argument. You can decide what 'similar' means:
- maybe the runtimes are close and the genre is the same;
- maybe the director matches and rating is the same;
- maybe the
releaseDate
s are less than 3 months apart; or - maybe a mix of all of those!
Hints:
- Create some instances of your
Film
case class
and test that your functions are behaving correctly. In a future class we will look at writing tests properly! - If you want to do something generic with a type e.g. uppercase a letter, split a
String
into aList[String]
according to a delimiting character, the chances are that a method or methods will already exist for doing that. Have a look around at methods on that type either by using IntelliJ or by looking at the documentation e.g.String
: https://www.scala-lang.org/api/current/scala/collection/immutable/StringOps.html