// 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 a bad version of the program, which uses "chaining." // Chaining is when each method just calls the next one, never returning to main. // // Look at how the methods in this program just end by calling the next method, // and how they increasingly accept more and more parameters, but never return. // Also notice how main becomes just a single line, a single call. // // The program works, but all of this is bad style. // The main method should be a concise summary of the program. // // 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 Movies2Bad { public static void main(String[] args) throws FileNotFoundException { getWord(); } // Asks the user for a word to search for in the movie titles. public static void getWord() throws FileNotFoundException { Scanner console = new Scanner(System.in); System.out.print("Search word? "); String searchWord = console.next(); drawBasic(searchWord); } // Creates our DrawingPanel and draws the initial ranking tick marks. public static void drawBasic(String searchWord) throws FileNotFoundException { DrawingPanel panel = new DrawingPanel(550, 550); Graphics g = panel.getGraphics(); for (int i = 0; i <= 10; i++) { g.drawString(i + ".0", 50*i, 20); g.drawLine(50*i, 20, 50*i, 30); } findMatch(searchWord, g); } // Finds and displays all matches from the file. public static void findMatch(String searchWord, Graphics g) throws FileNotFoundException { int matches = 0; Scanner input = new Scanner(new File("imdb.txt")); 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); matches++; } } System.out.println(matches + " matches."); } // 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); // display blue bar for this movie 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); } }