// Allison Obourn
// CS& 141, Winter 2021
// Lecture 11

// Prompts the user for a word and then outputs all IMDB's Top 250 movies that contain 
// that word. Ignores case when checking for a match. Each movie that matches outputs in
// the format <rank> <votes> <rating> <name>.

import java.util.*;
import java.io.*;

public class Imdb {
   public static void main(String[] args) throws FileNotFoundException {
      String word = getWord();
      
      System.out.println("Rank Votes Rating Title");
      
      Scanner file = new Scanner(new File("imdb.txt"));
      String line = search(word, file);
      while(!line.equals("")) {
         printLine(line);
         line = search(word, file);
      }
   }
   
   // Prompts the user for a word and returns the user's response
   public static String getWord() {
      System.out.print("Search word? ");
      Scanner user = new Scanner(System.in);
      String word = user.next();
      word = word.toLowerCase();
      return word;  
   }
   
   // Finds and returns then next line in the scanner that contains the passed in word
   // returns an empty string if there are no lines that contain the word
   public static String search(String word, Scanner file) throws FileNotFoundException {
      while(file.hasNextLine()) {
         String line = file.nextLine();
         String lowerLine = line.toLowerCase();
         if(lowerLine.contains(word)) {
            return line;
         }
      }
      return "";
   }
   
   // takes a string assumed to contain an integer representing a ranking, a real number 
   // representing a rating, an integer representing a number of votes and a a name
   // Displays the information with rank first, then votes, then rating and then the name.
   public static void printLine(String line) {
      Scanner lineScan = new Scanner(line);
      int rank = lineScan.nextInt();
      double rating = lineScan.nextDouble();
      int votes = lineScan.nextInt();
      String name = "";
      while(lineScan.hasNext()) {
         name += lineScan.next() + " ";
      }
      System.out.println(rank + " " + votes + " " + rating + " " + name);
   
   }
}