-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMovieSearch.java
124 lines (112 loc) · 5.2 KB
/
MovieSearch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.Scanner;
public class MovieSearch {
// Variable for outputting a line for the class when operating the functions
private static String movieLine = "****************";
// Private Method for Returning and Creating a Buffered Writer to add each search to a file
private static BufferedWriter movieSearchFile() throws IOException{
return new BufferedWriter(new FileWriter("MovieSearchFile.txt", true));
}
// Allows the user to search for a movie again using a loop
// If user enters yes - the search function runs again
// Otherwise it returns them to the menu
private static void mSearchAgain() {
System.out.println(movieLine);
System.out.println("Would you Like to Search Again? Y/N");
System.out.println(movieLine);
boolean mResearchLoop = true;
while (mResearchLoop)
{
Scanner mResearchScanner = mReSearchScanner();
String mResearch = mResearchScanner.nextLine().toLowerCase();
if (mResearch.equals("y"))
{
System.out.println("Searching Again");
mResearchLoop = false;
try {
mSearch();
}
catch (IOException mioe)
{
System.out.println("Error Occurred");
Moviedb.menu();
}
}
else if (mResearch.equals("n"))
{
System.out.println("Returning to Menu");
mResearchLoop = false;
Moviedb.menu();
}
else {
System.out.println("Please Enter Y or N");
}
}
}
// Method that searches for a MovieID from input
// Checks if the ID is in the Map and returns it using the integer as a key
// Otherwise prints that it couldn't be found
// Moves to the Re-Search function once complete
public static void mSearch() throws IOException{
Map <Integer, Movies> moviesMap = Movies.buildMovieSearch();
Scanner mSearch = mSearchScanner();
BufferedWriter movieFileWriter = movieSearchFile();
boolean mSearchLoop = true;
while (mSearchLoop)
{
try {
System.out.println(movieLine);
System.out.println("Enter a MovieID: ");
System.out.println(movieLine);
int mSearchId = mSearch.nextInt();
if (moviesMap.containsKey(mSearchId))
{
mSearchLoop = false;
System.out.println("Movie Id Found");
System.out.println(movieLine);
System.out.println("Movie Id: " + moviesMap.get(mSearchId).getID());
System.out.println("Movie Name: " + moviesMap.get(mSearchId).getName());
System.out.println("Movie Date: " + moviesMap.get(mSearchId).getDate());
System.out.println("Movie Url: " + moviesMap.get(mSearchId).getUrl());
System.out.println("Movie Genre: " + moviesMap.get(mSearchId).getGenre());
movieFileWriter.append("You Searched for: " + mSearchId + "\n" + "Movie Id: " + moviesMap.get(mSearchId).getID() + "\n" + "Movie Name: " + moviesMap.get(mSearchId).getName() + "\n" + "Movie Date: " + moviesMap.get(mSearchId).getDate() + "\n" + "Movie Url: " + moviesMap.get(mSearchId).getUrl() + "\n" + "Movie Genre: " + moviesMap.get(mSearchId).getGenre() + "\n");
movieFileWriter.append("===========================================================================" + "\n");
movieFileWriter.close();
}
else {
mSearchLoop = false;
System.out.println("Movie Id Not Found" );
}
mSearchAgain();
}
catch (InputMismatchException mSearchIME)
{
System.out.println("Enter A valid MovieId");
mSearch.next();
}
}
}
public static void ratingsMovies(Integer userMovieID) throws IOException{
Map<Integer, Movies> movieMap = Movies.buildMovieSearch();
System.out.println("Movie Id: " + movieMap.get(userMovieID).getID());
System.out.println("Movie Name: " + movieMap.get(userMovieID).getName());
System.out.println("Movie Date: " + movieMap.get(userMovieID).getDate());
System.out.println("Movie URL: " + movieMap.get(userMovieID).getUrl());
System.out.println("Movie Genre: " + movieMap.get(userMovieID).getGenre());
}
public static String twoUserMovies(Integer twoUserMovieId) throws IOException {
Map<Integer, Movies> movieMap = Movies.buildMovieSearch();
return movieMap.get(twoUserMovieId).getName();
}
// Methods to create new Scanners for Search and ReSearch
private static Scanner mSearchScanner() {
return new Scanner(System.in);
}
private static Scanner mReSearchScanner() {
return new Scanner(System.in);
}
}