// 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. // // Graphical version. import java.awt.*; import java.io.*; import java.util.*; public class Movies2 { public static void main(String[] args) throws FileNotFoundException { String phrase = getWord(); Scanner input = new Scanner(new File("imdb.txt")); System.out.println("Rank\tVotes\tRating\tTitle"); search(input, phrase); } // Asks the user for their search phrase and returns it. public static String getWord() { 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(); 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 each movie that matches the phrase. // // example line: "2 8.9 113807 The Lord of the Rings: The Return of the King (2003)" public static void search(Scanner input, String phrase) { int matches = 0; Graphics g = createWindow(); while (input.hasNextLine()) { String line = input.nextLine(); Scanner lineScan = new Scanner(line); int rank = lineScan.nextInt(); int votes = lineScan.nextInt(); double rating = lineScan.nextDouble(); String title = lineScan.nextLine(); // all the rest String lcTitle = title.toLowerCase(); if (lcTitle.indexOf(phrase) >= 0) { matches++; System.out.println(rank + "\t" + votes + "\t" + rating + title); drawBar(g, line, matches); } } System.out.println(); System.out.println(matches + " matches."); } // Creates a drawing panel and draws all fixed graphics // (graphics unaffected by the file search results) public static Graphics createWindow() { DrawingPanel panel = new DrawingPanel(600, 500); Graphics g = panel.getGraphics(); // draw tick marks for (int i = 0; i <= 10; i++) { // first tick mark's top-left corner is at (20, 20) // 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, String line, int matches) { Scanner lineScan = new Scanner(line); int rank = lineScan.nextInt(); int votes = lineScan.nextInt(); double rating = lineScan.nextDouble(); String title = lineScan.nextLine(); // all the rest // draw the red bar for that movie // first bar's top-left corner is at (20, 70) // 100px apart vertically // 1px tall for every 5000 votes earned // 50px wide for each ratings point int x = 20; int y = 70 + 100 * (matches - 1); int w = (int) (rating * 50); int h = votes / 5000; g.setColor(Color.RED); g.fillRect(x, y, w, h); g.setColor(Color.BLACK); g.drawString("#" + rank + ": " + title, x, y); g.drawString(votes + " votes", x + w, y); } }