Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object IMDBAnalyzer extends App {
- import scala.io.Source
- import scala.collection.SortedSet
- type MovieList = List[Movie]
- def selectMovieOption(): Unit = readLine("Please select an option [1-4] or [q] to exit!\n>>> ") match {
- case "1" => listMoviesByRating()
- case "2" => listMoviesByVotes()
- case "3" => listMoviesByYear()
- case "4" => listEpisodesOfSeries()
- case "q" => println("Bye!")
- case default =>
- println(s"Your selection ($default) was not valid! Valid inputs may be [1-4] or [q]")
- selectMovieOption()
- }
- class Movie(val rating: Double, val votes: Int, val title: String, val year: String) {
- override def toString() = s"Movie: title, year"
- }
- class Serie(rating: Double, votes: Int, title: String, year: String, val episodes: String) extends Movie(rating, votes, title, year) {
- override def toString() = s"Serie: title, year, episodes"
- }
- def listMoviesByRating() {
- println("\nPrint top rated movies (no TV serials)")
- val numberOfResults = numberInput("Please enter the number of results you are interested in:\n>>>")
- val Movies = moviesToMovieList("ratings.list")
- val moviesList1 = (for (movie <- Movies if (movie.isInstanceOf[Movie]) && movie.votes > 25000) yield movie.asInstanceOf[Movie]).sortBy(_.rating).take(numberOfResults)
- printOutput(moviesList1)
- }
- def listMoviesByVotes() {
- println("\nPrint top rated movies sorted by votes (no TV serials)")
- val numberOfResults = numberInput("Please enter the number of results you are interested in:\n>>>")
- val Movies = moviesToMovieList("ratings.list")
- val moviesList1 = (for (movie <- Movies if (movie.isInstanceOf[Movie]) && movie.votes > 25000) yield movie.asInstanceOf[Movie]).sortBy(_.votes).take(numberOfResults)
- printOutput(moviesList1)
- }
- def listMoviesByYear() {
- println("\nPrint top rated movies sorted by year (no TV serials)")
- val numberOfResults = numberInput("Please enter the number of results you are interested in:\n>>>")
- val Movies = moviesToMovieList("ratings.list")
- val moviesList1 = (for (movie <- Movies if (movie.isInstanceOf[Movie]) && movie.votes > 25000) yield movie.asInstanceOf[Movie]).sortBy(_.year).take(numberOfResults)
- printOutput(moviesList1)
- }
- def listEpisodesOfSeries() {}
- def moviesToMovieList(filename: String): List[Movie] = {
- val moviePattern = """\s+?([\d.*]{10})\s+?(\d+?)\s+?(\d{1,2}\.\d)\s+?(.*?)\s\((\S{4})/?[XVI]{0,4}\)(\s\([TVG]{1,2}\))?$""".r
- val lineIterator = Source.fromURL(getClass().getResource(filename)).getLines
- lineIterator.foldLeft(List[Movie]())((MovieList, currentLine) => {
- currentLine match {
- case moviePattern(rating, votes, title, year) =>
- new Movie(rating.toDouble, votes.toInt, title, year) :: MovieList
- case default =>
- println(s"Invalid entry found $default")
- return MovieList
- }
- })
- }
- def numberInput(number: String): Int = {
- try {
- val valNum = readLine(number)
- valNum.toInt
- return valNum.toInt
- } catch {
- case e: NumberFormatException =>
- println("Your input was incorrect and could not be converted to a number")
- return (numberInput(number))
- }
- }
- def printOutput(movieList: List[Movie]) = {
- if (movieList.length > 0) {
- val formtLength = movieList.map(_.title.length).max + 9
- val printPattern = "%-" + formtLength + "s"
- println(s"\n$printPattern %-9s %-9s %-9s".format("Title", "Year", "Votes", "Rating"))
- for (movie <- movieList) println(s"$printPattern %-9s %-9.0f %-9.1f".format(movie.title.trim, movie.year, movie.votes, movie.rating))
- } else println("Sorry - no matches found!")
- }
- selectMovieOption()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement