/* CSE 142, Autumn 2007 (Marty Stepp) This program reads a file of IMDB's Top 250 movies and displays information about movies that match a search string typed by the user. */ import java.awt.*; import java.io.*; import java.util.*; public class Movies { public static void main(String[] args) throws FileNotFoundException { introduction(); String phrase = getWord(); Scanner input = new Scanner(new File("imdb.txt")); search(input, phrase); } // prints introductory text to the user public static void introduction() { System.out.println("This program will allow you to search the"); System.out.println("imdb top 250 movies for a particular word."); System.out.println(); } // Asks the user for their search phrase and returns it. public static String getWord() { System.out.print("Search word: "); Scanner console = new Scanner(System.in); String phrase = console.next(); phrase = phrase.toLowerCase(); System.out.println(); return phrase; } // Breaks apart each line, looking for lines that match the search phrase. // Prints information about all movies that match the phrase, // and draws a graph of the highest-ranked movie that matches the phrase. // // example line of input: // 8.9 113807 The Lord of the Rings: The Return of the King (2003) public static void search(Scanner input, String phrase) { System.out.println("Rank\tVotes\tRating\tTitle"); int matches = 0; Graphics g = createWindow(); int rank = 0; while (input.hasNextLine()) { String line = input.nextLine(); Scanner lineScan = new Scanner(line); rank++; double rating = lineScan.nextDouble(); // 8.9 int votes = lineScan.nextInt(); // 113807 String title = lineScan.nextLine(); // " The Lord of ..." String lcTitle = title.toLowerCase(); // try to case-insensitively match the phrase the user typed if (lcTitle.indexOf(phrase) >= 0) { matches++; System.out.println(rank + "\t" + votes + "\t" + rating + title); if (matches == 1) { // only draw first match drawBar(g, rating, votes, title); } } } System.out.println(); System.out.println(matches + " matches."); } // Creates drawing panel and all fixed graphics (not from search results) public static Graphics createWindow() { DrawingPanel panel = new DrawingPanel(600, 130); Graphics g = panel.getGraphics(); // draw tick marks for (int i = 0; i <= 10; i++) { // first tick mark at (20, 20); ticks 10px tall, 50px apart int x = 20 + i * 50; g.drawLine(x, 20, x, 30); g.drawString(i + ".0", x, 20); } return g; } // Draws one red bar representing a movie's votes and ranking. // The "matches" parameter determines the bar's y position. public static void drawBar(Graphics g, double rating, int votes, String title) { // draw red bar for movie. bar's top-left is at (20, 70); // 1px tall for each 5000 votes; 50px wide for each ratings point int w = (int) (rating * 50); int h = votes / 5000; g.setColor(Color.RED); g.fillRect(20, 70, w, h); g.setColor(Color.BLACK); g.drawString(title, 20, 70); g.drawString(votes + " votes", 20 + w, 70); } }