Advertisement
Guest User

Untitled

a guest
Jun 20th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.75 KB | None | 0 0
  1. object IMDBAnalyzer extends App {
  2.  
  3.   import scala.io.Source
  4.   import scala.collection.SortedSet
  5.  
  6.   type MovieList = List[Movie]
  7.  
  8.   def selectMovieOption(): Unit = readLine("Please select an option [1-4] or [q] to exit!\n>>> ") match {
  9.     case "1" => listMoviesByRating()
  10.     case "2" => listMoviesByVotes()
  11.     case "3" => listMoviesByYear()
  12.     case "4" => listEpisodesOfSeries()
  13.     case "q" => println("Bye!")
  14.     case default =>
  15.       println(s"Your selection ($default) was not valid! Valid inputs may be [1-4] or [q]")
  16.       selectMovieOption()
  17.   }
  18.  
  19.   class Movie(val rating: Double, val votes: Int, val title: String, val year: String) {
  20.     override def toString() = s"Movie: title, year"
  21.   }
  22.  
  23.   class Serie(rating: Double, votes: Int, title: String, year: String, val episodes: String) extends Movie(rating, votes, title, year) {
  24.     override def toString() = s"Serie: title, year, episodes"
  25.   }
  26.  
  27.   def listMoviesByRating() {
  28.     println("\nPrint top rated movies (no TV serials)")
  29.     val numberOfResults = numberInput("Please enter the number of results you are interested in:\n>>>")
  30.     val Movies = moviesToMovieList("ratings.list")
  31.     val moviesList1 = (for (movie <- Movies if (movie.isInstanceOf[Movie]) && movie.votes > 25000) yield movie.asInstanceOf[Movie]).sortBy(_.rating).take(numberOfResults)
  32.     printOutput(moviesList1)
  33.   }
  34.  
  35.   def listMoviesByVotes() {
  36.     println("\nPrint top rated movies sorted by votes (no TV serials)")
  37.     val numberOfResults = numberInput("Please enter the number of results you are interested in:\n>>>")
  38.     val Movies = moviesToMovieList("ratings.list")
  39.     val moviesList1 = (for (movie <- Movies if (movie.isInstanceOf[Movie]) && movie.votes > 25000) yield movie.asInstanceOf[Movie]).sortBy(_.votes).take(numberOfResults)
  40.     printOutput(moviesList1)
  41.   }
  42.  
  43.   def listMoviesByYear() {
  44.     println("\nPrint top rated movies sorted by year (no TV serials)")
  45.     val numberOfResults = numberInput("Please enter the number of results you are interested in:\n>>>")
  46.     val Movies = moviesToMovieList("ratings.list")
  47.     val moviesList1 = (for (movie <- Movies if (movie.isInstanceOf[Movie]) && movie.votes > 25000) yield movie.asInstanceOf[Movie]).sortBy(_.year).take(numberOfResults)
  48.     printOutput(moviesList1)
  49.   }
  50.  
  51.   def listEpisodesOfSeries() {}
  52.  
  53.   def moviesToMovieList(filename: String): List[Movie] = {
  54.  
  55.     val moviePattern = """\s+?([\d.*]{10})\s+?(\d+?)\s+?(\d{1,2}\.\d)\s+?(.*?)\s\((\S{4})/?[XVI]{0,4}\)(\s\([TVG]{1,2}\))?$""".r
  56.  
  57.     val lineIterator = Source.fromURL(getClass().getResource(filename)).getLines
  58.  
  59.     lineIterator.foldLeft(List[Movie]())((MovieList, currentLine) => {
  60.       currentLine match {
  61.         case moviePattern(rating, votes, title, year) =>
  62.           new Movie(rating.toDouble, votes.toInt, title, year) :: MovieList
  63.  
  64.         case default =>
  65.           println(s"Invalid entry found $default")
  66.           return MovieList
  67.       }
  68.     })
  69.   }
  70.  
  71.   def numberInput(number: String): Int = {
  72.     try {
  73.       val valNum = readLine(number)
  74.       valNum.toInt
  75.       return valNum.toInt
  76.     } catch {
  77.       case e: NumberFormatException =>
  78.         println("Your input was incorrect and could not be converted to a number")
  79.         return (numberInput(number))
  80.     }
  81.   }
  82.  
  83.   def printOutput(movieList: List[Movie]) = {
  84.     if (movieList.length > 0) {
  85.       val formtLength = movieList.map(_.title.length).max + 9
  86.       val printPattern = "%-" + formtLength + "s"
  87.  
  88.       println(s"\n$printPattern %-9s %-9s %-9s".format("Title", "Year", "Votes", "Rating"))
  89.       for (movie <- movieList) println(s"$printPattern %-9s %-9.0f %-9.1f".format(movie.title.trim, movie.year, movie.votes, movie.rating))
  90.     } else println("Sorry - no matches found!")
  91.  
  92.   }
  93.  
  94.   selectMovieOption()
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement