// Marty Stepp, CSE 142, Autumn 2008 // This program reads a file containing the IMDb top 250 all-time movies // and displays all movies matching a search phrase. // // This is the good version of the program, using methods without chaining. // Notice that main is a better summary of the program, and it creates all the // major objects used in the program such as the DrawingPanel and Scanners. // // input: // 1 9.1 243153 The Godfather (1972) // // output: // Search word? part // Rank Votes Rating Title // 2 139085 9.0 The Godfather: Part II (1974) // 40 129172 8.5 The Departed (2006) // 95 20401 8.2 The Apartment (1960) // 192 30587 8.0 Spartacus (1960) // 4 matches. import java.awt.*; // for Graphics import java.io.*; // for File import java.util.*; // for Scanner public class Movies2Good { // "main is a concise summary" public static void main(String[] args) throws FileNotFoundException { String searchWord = getWord(); // create initial DrawingPanel and tick marks DrawingPanel panel = new DrawingPanel(550, 550); Graphics g = panel.getGraphics(); draw(g); // find all matches in the file Scanner input = new Scanner(new File("imdb.txt")); int matches = 0; while (findMatch(searchWord, input, g, matches) == true) { matches++; } System.out.println(matches + " matches."); } // Asks the user for a word to search for in movie titles and returns the word. public static String getWord() { Scanner console = new Scanner(System.in); System.out.print("Search word? "); String searchWord = console.next(); return searchWord; } // Draws the initial ranking tick marks on our DrawingPanel. public static void draw(Graphics g) { // 1) create DrawingPanel in main, pass g to draw method // 2) return Graphics g for (int i = 0; i <= 10; i++) { g.drawString(i + ".0", 50*i, 20); g.drawLine(50*i, 20, 50*i, 30); } } // Finds and displays one match from the file. // Returns true if a match is found, false if not. public static boolean findMatch(String searchWord, Scanner input, Graphics g, int matches) { while (input.hasNextLine()) { // "1 9.1 243153 The Godfather (1972)" String line = input.nextLine(); Scanner lineScanner = new Scanner(line); // break apart the line into tokens int rank = lineScanner.nextInt(); double rating = lineScanner.nextDouble(); int votes = lineScanner.nextInt(); String title = lineScanner.nextLine(); if (title.toLowerCase().contains(searchWord.toLowerCase())) { // found a match! drawMatch(g, rank, votes, rating, title, matches); return true; } } // reached end of file without finding a match return false; } // Displays a single matching movie on the screen. public static void drawMatch(Graphics g, int rank, int votes, double rating, String title, int matches) { // Rank Votes Rating Title // 2 139085 9.0 The Godfather: Part II (1974) System.out.println(rank + "\t" + votes + "\t" + rating + "\t" + title); g.setColor(Color.BLACK); g.drawString(title, 0, 70 + 100*matches); g.setColor(Color.BLUE); g.fillRect(0, 70 + 100*matches, (int) (50 * rating), 50); } }