-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovieAppMain.java
More file actions
329 lines (282 loc) · 12.4 KB
/
MovieAppMain.java
File metadata and controls
329 lines (282 loc) · 12.4 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* @authour: muteeba jamal <a href="mailto:muteeba.jamal@ucalgary.ca">
* muteeba.jamal@ucalgary.ca</a>
* @authour: shahzill naveed <a href="mailto:shahzill.naveed@ucalgary.ca">
* shahzill.naveed@ucalgary.ca</a>
* @authour: michele pham <a href="mailto:michele.pham@ucalgary.ca">
* michele.pham@ucalgary.ca</a>
* @authour: samira khan <a href="mailto:samira.khan@ucalgary.ca">
* samira.khan@ucalgary.ca</a>
* @version 1.0
* @since 1.0
*/
import java.io.FileNotFoundException;
import java.sql.*;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.*;
public class MovieAppMain{
private static GUI gui;
public final String DBURL;
public final String USERNAME;
public final String PASSWORD;
private static MovieAppMain myJDBC;
private static Connection dbConnect;
private ResultSet results;
// this is a linked list of all the movies in the database
private static LinkedList<Movie> movies = new LinkedList<Movie>();
private static String movieFound = "no";
// this is a linked list of all the theatres in the database
private static LinkedList<Theatre> theatres = new LinkedList<Theatre>();
private static LinkedList<RegisteredUser> regUsers = new LinkedList<RegisteredUser>();
////////////////////////////////////////////////////////////////////
/*
* Start of Methods required to access and manipulate the SQL Databse.
*/
////////////////////////////////////////////////////////////////////
public MovieAppMain(String url, String user, String pw){
// Database URL
this.DBURL = url;
// Database credentials
this.USERNAME = user;
this.PASSWORD = pw;
}
//Must create a connection to the database, no arguments, no return value
public void initializeConnection(){
try{
dbConnect = DriverManager.getConnection(getDburl(),getUsername(),getPassword());
}
catch(SQLException e){
e.printStackTrace();
}
}
String getUsername(){
return this.USERNAME;
}
String getDburl(){
return this.DBURL;
}
String getPassword(){
return this.PASSWORD;
}
/* this method stores all our data from the database into the linked lists */
public String storeData(String tableName){
StringBuilder comp = new StringBuilder();
if(tableName.equals("projectData")){
try{
Statement statement = dbConnect.createStatement();
results = statement.executeQuery("SELECT Theatre, Movie, dateM, dayM, timeM, Seats, memberOnly, Item from projectData");
int i = 0;
boolean flag = false;
while(results.next()){
// a movie object is created and added to the linked list
Movie mov = new Movie(results.getString("Item"), results.getString("Theatre"), results.getString("Movie"),
results.getString("dateM"), results.getString("dayM"),
results.getString("timeM"), results.getString("Seats"), results.getBoolean("memberOnly"));
movies.add(mov);
// if its not the first item in the database then we will traverse through the linked list of theatres
if (i != 0){
for (int x = 0; x<theatres.size(); x++) {
// if a theatre is already found to be in the linked list, then the movie object will be appended to it
// i did it this way because it makes sense to have multiple movie objects since each will have different timings but,
// it doesnt make sense to have multiple theatre objects --- 1 theatre has 0..* movies
if (theatres.get(x).getName().contains(results.getString("Theatre"))) {
theatres.get(x).addMovie(mov);
flag = true;
break;
}
}
// if the flag is false this means that the theatre isnt already in the list so we go ahead and add it
if (flag == false){
Theatre th = new Theatre(results.getString("Theatre"));
th.addMovie(mov);
theatres.add(th);
}
}
// this is for the first item in the database
else {
Theatre th = new Theatre(results.getString("Theatre"));
th.addMovie(mov);
theatres.add(th);
}
// flag is reset to false
flag = false;
// set to 1 after the first item is added, so consecutive runs will go through the first if statement
i = 1;
}
statement.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
String str = comp.toString();
return str.trim();
}
else if(tableName.equals("userData")){
try{
Statement statement = dbConnect.createStatement();
results = statement.executeQuery("SELECT Email, Password, CNumber, Cvv, Expiry, FeePaid from userData");
int i = 0;
boolean flag = false;
while(results.next()){
// a movie object is created and added to the linked list
RegisteredUser reg = new RegisteredUser(new RegisteredPayment(),results.getString("Email"),results.getString("Password"),
results.getString("CNumber"), results.getString("Cvv"), results.getString("Expiry"), results.getBoolean("FeePaid"));
regUsers.add(reg);
}
statement.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
String str = comp.toString();
return str.trim();
}
else{
String str = "Wrong input";
return str;
}
}
public static void registerUser(String email, String password, String cardNumber, String expiry, String cvv){
try {
int id = regUsers.size()+1;
Statement stmt = dbConnect.createStatement();
String str = "INSERT INTO userData " +
//"VALUES (5,'shah@gmail.com', 'shah', '350519263647', '333', '06/25', 0)";
"VALUES ("+id+",'" + email +"','"+password+"','"+cardNumber+"','"+cvv+"','"+expiry+"',"+0+")";
stmt.executeUpdate(str);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void addMovie(String Theatre, String Movie, String dateM, String dayM, String timeM, String seats, boolean exclusive){
try {
Statement stmt = dbConnect.createStatement();
// String str = "INSERT INTO projectData (Theatre, Movie, dateM, dayM, timeM, Seats, memberOnly) VALUES ("+ id+", " +Theatre +", " +Movie +", "+dateM+","+dayM+","+timeM+","+seats+","+exclusive+");";
int idEnter = movies.size()+1;
int excl;
if (exclusive) excl = 1;
else excl = 0;
String str = "INSERT INTO projectData VALUES ("+ idEnter+",' " +Theatre +"',' " +Movie +"',' "+dateM+"','"+dayM+"','"+timeM+"','"+seats+"','"+excl+"')";
stmt.executeUpdate(str);
theatres.clear();
movies.clear();
System.out.println(myJDBC.storeData("projectData"));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
////////////////////////////////////////////////////////////////////
/*
* End of Methods required to access and manipulate the SQL Databse.
*/
////////////////////////////////////////////////////////////////////
// this is the search function used to search for a movie
/*public static String search(String movieName){
System.out.println();
String details = "Search results for "+ movieName;
// traverses through the theatres list and searches each theatre for a movie
// you can see how the display happens in the theatre/movie classes
for(int i=0; i<theatres.size(); i++){
details += theatres.get(i).searchMovies(movieName);
}
return details;
}*/
public static LinkedList<Theatre> getThreatres(){
return theatres;
}
// this is the search function used to search for a movie
public static LinkedList<Movie> search(String movieName, RegisteredUser exclusive){
LinkedList<Movie> allFound = new LinkedList<Movie>();
String details = "Search results for "+ movieName;
// traverses through the theatres list and searches each theatre for a movie
// you can see how the display happens in the theatre/movie classes
int exists = 0;
for(int i=0; i<theatres.size(); i++){
if(exclusive == null){
allFound.addAll(theatres.get(i).searchMovies(movieName, "guest"));
}
else{
allFound.addAll(theatres.get(i).searchMovies(movieName, "registered"));
}
}
if(allFound.size() == 0){
details = "Movie not found. Please enter a valid movie name.";
movieFound = "no";
}
else{
movieFound = "yes";
}
// if(movieName == null){
// details = "Please enter a movie name.";
// }
// Theatre t = new Theatre(movieName);
// t.searchMovies(details);
// int exists = t.getExist();
// System.out.println("Whats exists: " + exists);
return allFound;
}
static LinkedList<Movie> getMovies(){
return movies;
}
public static String getFoundMovie(){
return movieFound;
}
public static String register(){
String done = "registered!!!";
return done;
}
public static Movie get(){
return movies.get(1);
}
public static RegisteredUser userSearch(String email, String password){
//System.out.println(email + " " + password);
regUsers.clear();
System.out.println(myJDBC.storeData("userData"));
for(int i = 0; i < regUsers.size(); i++){
if(regUsers.get(i).getEmail().equals(email) && regUsers.get(i).getPassword().equals(password)){
return regUsers.get(i);
}
}
return null;
}
public static boolean checkEmail(String email){
regUsers.clear();
System.out.println(myJDBC.storeData("userData"));
for(int i = 0; i < regUsers.size(); i++){
if(regUsers.get(i).getEmail().equals(email)){
return false;
}
}
return true;
}
public static void feePayment(String email){
try {
Statement stmt = dbConnect.createStatement();
String str = "Update userData set FeePaid = 1 Where Email = '" + email + "'";
stmt.executeUpdate(str);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str = "Update userData set FeePaid = 1 Where Email = '" + email + "'";
}
public static void main(String[] args) throws FileNotFoundException {
GUI gui = new GUI();
//Use the following account information: Username = user1, Password = ensf
myJDBC = new MovieAppMain("jdbc:mysql://localhost:3306/data_cinema","user1","ensf");
myJDBC.initializeConnection();
System.out.println("------------------------------");
System.out.println();
// Ticket.emailTicket("muteeba", "test", "String details");
System.out.println(myJDBC.storeData("projectData"));
System.out.println(myJDBC.storeData("userData"));
GUI.run();
}
}