Advertisement
Guest User

Untitled

a guest
Sep 24th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. package project3;
  2. import java.io.FileNotFoundException;
  3. import java.io.File;
  4. import java.util.Scanner;
  5.  
  6. public class movieDB {
  7. public static void main(String[] args) {
  8. System.out.println("Welcome to the Java loops tutorial!");
  9. int i = 0;
  10. displayMenu(i);
  11. } //end main
  12.  
  13. //method used to display the menu driven interface to the user
  14. //input: int
  15. //output: none
  16. public static void displayMenu(int i) {
  17. Scanner fromKb = new Scanner(System.in);
  18. int choice;
  19. System.out.print("\033[H\033[2J");
  20. while (true) {
  21. //new method
  22. System.out.println("\n1: display all movies");
  23. System.out.println("2: display shortest movie");
  24. System.out.println("3: display longest movie");
  25. System.out.println("4: display oldest movie");
  26. System.out.println("5: display newest movie");
  27. System.out.println("6: display movies by rating");
  28. System.out.println("7: quit the program");
  29. System.out.println("Choice: ");
  30. choice = fromKb.nextInt();
  31. System.out.print("\033[H\033[2J");
  32. if (choice == 7)
  33. break;
  34. options(choice, i, fromKb);
  35.  
  36. } //end while
  37. } //end displayMenu
  38.  
  39. //method that calls specific loop pattern methods based on user input
  40. //input: int, int, Scanner
  41. //output: none
  42. public static void options(int c, int i, Scanner kb) {
  43.  
  44. if (c == 1) display_all(i);
  45. else if (c == 2) display_shortest(i);
  46. else if (c == 3) display_longest(i);
  47. else if (c == 4) display_oldest(i);
  48. else if (c == 5) display_newest(i);
  49. else if (c == 6) {
  50. System.out.print("Display movies with what rating?");
  51. String match = kb.next();
  52. search(match);
  53. } //end if
  54. else System.out.println("Invalid input, please try again.");
  55. } //end options
  56.  
  57. //method used to display each separate line in a String array
  58. //input: int
  59. //output: none
  60. public static void display_all(int i) {
  61.  
  62. String fileName = "movies.txt";
  63. String[] lines = new String[3];
  64. readFile(lines, fileName);
  65. //print values stored in String[] lines:
  66. System.out.println("line 1: " + lines[0]);
  67. System.out.println("line 2: " + lines[1]);
  68. System.out.println("line 3: " + lines[2]);
  69. } //end display_all
  70.  
  71. //method used to display the shortest movie in the array
  72. //input: int
  73. //output: none
  74. public static void display_shortest(int i) {
  75.  
  76. String fileName = "movies.txt";
  77. String[] lines = new String[3];
  78. readFile(lines, fileName);
  79. String[] value1 = lines[0].split(",");
  80. String runtime1 = value1[3];
  81. int rt1 = Integer.parseInt(runtime1);
  82. String[] value2 = lines[1].split(",");
  83. String runtime2 = value2[3];
  84. int rt2 = Integer.parseInt(runtime2);
  85. String[] value3 = lines[2].split(",");
  86. String runtime3 = value3[3];
  87. int rt3 = Integer.parseInt(runtime3);
  88.  
  89. int[] runtime_as_array = {
  90. rt1,
  91. rt2,
  92. rt3
  93. };
  94. int min = runtime_as_array[0];
  95. for (int j = 1; j < runtime_as_array.length; j++) {
  96. if (runtime_as_array[j] < min)
  97. min = runtime_as_array[j];
  98. } //end for
  99. String match = Integer.toString(min);
  100. search(match);
  101. } //end display_shortest
  102.  
  103. //method used to display the longest movie in the array
  104. //input: int
  105. //output: none
  106. public static void display_longest(int i) {
  107.  
  108. String fileName = "movies.txt";
  109. String[] lines = new String[3];
  110. readFile(lines, fileName);
  111. String[] value1 = lines[0].split(",");
  112. String runtime1 = value1[3];
  113. int rt1 = Integer.parseInt(runtime1);
  114. String[] value2 = lines[1].split(",");
  115. String runtime2 = value2[3];
  116. int rt2 = Integer.parseInt(runtime2);
  117. String[] value3 = lines[2].split(",");
  118. String runtime3 = value3[3];
  119. int rt3 = Integer.parseInt(runtime3);
  120.  
  121. int[] runtime_as_array = {
  122. rt1,
  123. rt2,
  124. rt3
  125. };
  126. int max = runtime_as_array[0];
  127. for (int j = 1; j < runtime_as_array.length; j++) {
  128. if (runtime_as_array[j] > max)
  129. max = runtime_as_array[j];
  130. } //end for
  131. String match = Integer.toString(max);
  132. search(match);
  133. } //end display_longest
  134.  
  135. //method used to display the oldest movie in the array
  136. //input: int
  137. //output: none
  138. public static void display_oldest(int i) {
  139.  
  140. String fileName = "movies.txt";
  141. String[] lines = new String[3];
  142. readFile(lines, fileName);
  143. String[] value1 = lines[0].split(",");
  144. String release1 = value1[2];
  145. int rl1 = Integer.parseInt(release1);
  146. String[] value2 = lines[1].split(",");
  147. String release12 = value2[2];
  148. int rl2 = Integer.parseInt(release12);
  149. String[] value3 = lines[2].split(",");
  150. String release13 = value3[2];
  151. int rl3 = Integer.parseInt(release13);
  152.  
  153. int[] runtime_as_array = {
  154. rl1,
  155. rl2,
  156. rl3
  157. };
  158. int min = runtime_as_array[0];
  159. for (int j = 1; j < runtime_as_array.length; j++) {
  160. if (runtime_as_array[j] < min)
  161. min = runtime_as_array[j];
  162. } //end for
  163. String match = Integer.toString(min);
  164. search(match);
  165. } //end display_oldest
  166.  
  167. //method used to display the newest movie in the array
  168. //input: int
  169. //output: none
  170. public static void display_newest(int i) {
  171.  
  172. String fileName = "movies.txt";
  173. String[] lines = new String[3];
  174. readFile(lines, fileName);
  175. String[] value1 = lines[0].split(",");
  176. String release1 = value1[2];
  177. int rl1 = Integer.parseInt(release1);
  178. String[] value2 = lines[1].split(",");
  179. String release12 = value2[2];
  180. int rl2 = Integer.parseInt(release12);
  181. String[] value3 = lines[2].split(",");
  182. String release13 = value3[2];
  183. int rl3 = Integer.parseInt(release13);
  184.  
  185. int[] runtime_as_array = {
  186. rl1,
  187. rl2,
  188. rl3
  189. };
  190. int max = runtime_as_array[0];
  191. for (int j = 1; j < runtime_as_array.length; j++) {
  192. if (runtime_as_array[j] > max)
  193. max = runtime_as_array[j];
  194. } //end for
  195. String match = Integer.toString(max);
  196. search(match);
  197. } //end display_newest
  198.  
  199. //method used for finding and reading a file based on its name
  200. //input: String[], String
  201. //output: none
  202. public static void readFile(String[] l, String f) {
  203. int counter = 0;
  204. try {
  205. Scanner fromFile = new Scanner(new File(f));
  206. while (fromFile.hasNextLine()) {
  207. l[counter] = fromFile.nextLine();
  208. counter++;
  209. } //end while
  210. } //end try block
  211. catch (FileNotFoundException e) {
  212. System.out.println("I'm sorry, Dave. I'm afraid I can't do that.");
  213. System.out.println("File not found.");
  214. } //end catch block
  215. } //end readFile
  216.  
  217. //method used to search the match for an element in a file
  218. //input: String
  219. //output: none
  220. public static void search(String match) {
  221.  
  222. File file = new File("movies.txt");
  223. Scanner in = null;
  224. try { in = new Scanner(file);
  225. boolean found = false;
  226. while ( in .hasNext()) {
  227. String line = in .nextLine();
  228. if (line.contains(match)) {
  229. System.out.println(line);
  230. found = true;
  231. }
  232. } //end while
  233. System.out.println("Movie was found: " + found);
  234. } catch (FileNotFoundException e) {
  235. e.printStackTrace();
  236. } //end try and catch block
  237.  
  238. } //end search
  239. } //end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement