Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package project3;
- import java.io.FileNotFoundException;
- import java.io.File;
- import java.util.Scanner;
- public class movieDB {
- public static void main(String[] args) {
- System.out.println("Welcome to the Java loops tutorial!");
- int i = 0;
- displayMenu(i);
- } //end main
- //method used to display the menu driven interface to the user
- //input: int
- //output: none
- public static void displayMenu(int i) {
- Scanner fromKb = new Scanner(System.in);
- int choice;
- System.out.print("\033[H\033[2J");
- while (true) {
- //new method
- System.out.println("\n1: display all movies");
- System.out.println("2: display shortest movie");
- System.out.println("3: display longest movie");
- System.out.println("4: display oldest movie");
- System.out.println("5: display newest movie");
- System.out.println("6: display movies by rating");
- System.out.println("7: quit the program");
- System.out.println("Choice: ");
- choice = fromKb.nextInt();
- System.out.print("\033[H\033[2J");
- if (choice == 7)
- break;
- options(choice, i, fromKb);
- } //end while
- } //end displayMenu
- //method that calls specific loop pattern methods based on user input
- //input: int, int, Scanner
- //output: none
- public static void options(int c, int i, Scanner kb) {
- if (c == 1) display_all(i);
- else if (c == 2) display_shortest(i);
- else if (c == 3) display_longest(i);
- else if (c == 4) display_oldest(i);
- else if (c == 5) display_newest(i);
- else if (c == 6) {
- System.out.print("Display movies with what rating?");
- String match = kb.next();
- search(match);
- } //end if
- else System.out.println("Invalid input, please try again.");
- } //end options
- //method used to display each separate line in a String array
- //input: int
- //output: none
- public static void display_all(int i) {
- String fileName = "movies.txt";
- String[] lines = new String[3];
- readFile(lines, fileName);
- //print values stored in String[] lines:
- System.out.println("line 1: " + lines[0]);
- System.out.println("line 2: " + lines[1]);
- System.out.println("line 3: " + lines[2]);
- } //end display_all
- //method used to display the shortest movie in the array
- //input: int
- //output: none
- public static void display_shortest(int i) {
- String fileName = "movies.txt";
- String[] lines = new String[3];
- readFile(lines, fileName);
- String[] value1 = lines[0].split(",");
- String runtime1 = value1[3];
- int rt1 = Integer.parseInt(runtime1);
- String[] value2 = lines[1].split(",");
- String runtime2 = value2[3];
- int rt2 = Integer.parseInt(runtime2);
- String[] value3 = lines[2].split(",");
- String runtime3 = value3[3];
- int rt3 = Integer.parseInt(runtime3);
- int[] runtime_as_array = {
- rt1,
- rt2,
- rt3
- };
- int min = runtime_as_array[0];
- for (int j = 1; j < runtime_as_array.length; j++) {
- if (runtime_as_array[j] < min)
- min = runtime_as_array[j];
- } //end for
- String match = Integer.toString(min);
- search(match);
- } //end display_shortest
- //method used to display the longest movie in the array
- //input: int
- //output: none
- public static void display_longest(int i) {
- String fileName = "movies.txt";
- String[] lines = new String[3];
- readFile(lines, fileName);
- String[] value1 = lines[0].split(",");
- String runtime1 = value1[3];
- int rt1 = Integer.parseInt(runtime1);
- String[] value2 = lines[1].split(",");
- String runtime2 = value2[3];
- int rt2 = Integer.parseInt(runtime2);
- String[] value3 = lines[2].split(",");
- String runtime3 = value3[3];
- int rt3 = Integer.parseInt(runtime3);
- int[] runtime_as_array = {
- rt1,
- rt2,
- rt3
- };
- int max = runtime_as_array[0];
- for (int j = 1; j < runtime_as_array.length; j++) {
- if (runtime_as_array[j] > max)
- max = runtime_as_array[j];
- } //end for
- String match = Integer.toString(max);
- search(match);
- } //end display_longest
- //method used to display the oldest movie in the array
- //input: int
- //output: none
- public static void display_oldest(int i) {
- String fileName = "movies.txt";
- String[] lines = new String[3];
- readFile(lines, fileName);
- String[] value1 = lines[0].split(",");
- String release1 = value1[2];
- int rl1 = Integer.parseInt(release1);
- String[] value2 = lines[1].split(",");
- String release12 = value2[2];
- int rl2 = Integer.parseInt(release12);
- String[] value3 = lines[2].split(",");
- String release13 = value3[2];
- int rl3 = Integer.parseInt(release13);
- int[] runtime_as_array = {
- rl1,
- rl2,
- rl3
- };
- int min = runtime_as_array[0];
- for (int j = 1; j < runtime_as_array.length; j++) {
- if (runtime_as_array[j] < min)
- min = runtime_as_array[j];
- } //end for
- String match = Integer.toString(min);
- search(match);
- } //end display_oldest
- //method used to display the newest movie in the array
- //input: int
- //output: none
- public static void display_newest(int i) {
- String fileName = "movies.txt";
- String[] lines = new String[3];
- readFile(lines, fileName);
- String[] value1 = lines[0].split(",");
- String release1 = value1[2];
- int rl1 = Integer.parseInt(release1);
- String[] value2 = lines[1].split(",");
- String release12 = value2[2];
- int rl2 = Integer.parseInt(release12);
- String[] value3 = lines[2].split(",");
- String release13 = value3[2];
- int rl3 = Integer.parseInt(release13);
- int[] runtime_as_array = {
- rl1,
- rl2,
- rl3
- };
- int max = runtime_as_array[0];
- for (int j = 1; j < runtime_as_array.length; j++) {
- if (runtime_as_array[j] > max)
- max = runtime_as_array[j];
- } //end for
- String match = Integer.toString(max);
- search(match);
- } //end display_newest
- //method used for finding and reading a file based on its name
- //input: String[], String
- //output: none
- public static void readFile(String[] l, String f) {
- int counter = 0;
- try {
- Scanner fromFile = new Scanner(new File(f));
- while (fromFile.hasNextLine()) {
- l[counter] = fromFile.nextLine();
- counter++;
- } //end while
- } //end try block
- catch (FileNotFoundException e) {
- System.out.println("I'm sorry, Dave. I'm afraid I can't do that.");
- System.out.println("File not found.");
- } //end catch block
- } //end readFile
- //method used to search the match for an element in a file
- //input: String
- //output: none
- public static void search(String match) {
- File file = new File("movies.txt");
- Scanner in = null;
- try { in = new Scanner(file);
- boolean found = false;
- while ( in .hasNext()) {
- String line = in .nextLine();
- if (line.contains(match)) {
- System.out.println(line);
- found = true;
- }
- } //end while
- System.out.println("Movie was found: " + found);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } //end try and catch block
- } //end search
- } //end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement