Almost done
This commit is contained in:
		
							parent
							
								
									a16633680c
								
							
						
					
					
						commit
						87cb244f71
					
				
					 4 changed files with 69 additions and 9 deletions
				
			
		|  | @ -190,5 +190,58 @@ public class Database { | ||||||
|         return false; |         return false; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /* --- TODO: insert more own code here --- */ |     public Reservation addReservation(String username, String movie, String date) { | ||||||
|  |         if(!isConnected()) { | ||||||
|  |             System.err.println("addReservation: no connection to database"); | ||||||
|  |             return null; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         System.out.println("Adding reservation for " + username + " for " + movie + " on " + date); | ||||||
|  | 
 | ||||||
|  |         try { | ||||||
|  |             Statement stmt = conn.createStatement(); | ||||||
|  |             ResultSet rs = stmt.executeQuery("SELECT * FROM Users WHERE username = '" + username + "'"); | ||||||
|  |             if (rs.next()) { | ||||||
|  |                 int user_id = rs.getInt("id"); | ||||||
|  |                 // rs = stmt.executeQuery("SELECT * FROM Showings JOIN Movies ON Showings.movie_id = Movies.movie_id WHERE name = '" + movie + "' AND show_date = '" + date + "'"); | ||||||
|  |                 rs = stmt.executeQuery("SELECT * FROM Showings JOIN Movies ON Showings.movie_id = Movies.movie_id WHERE name = '" + movie + "' AND show_date = '" + date + "'"); | ||||||
|  |                 if (rs.next()) { | ||||||
|  |                     System.out.println("Adding reservation"); | ||||||
|  |                     int showing_id = rs.getInt("showing_id"); | ||||||
|  |                     String theater = rs.getString("theater_id"); | ||||||
|  |                     stmt.executeUpdate("INSERT INTO Reservations (user_id, showing_id) VALUES (" + user_id + ", " + showing_id + ")"); | ||||||
|  |                     System.out.println("Reservation added"); | ||||||
|  |                     int bookingId = stmt.getGeneratedKeys().getInt(1); | ||||||
|  |                     return new Reservation(bookingId, movie, date, theater); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             else { | ||||||
|  |                 System.err.println(String.format("addReservation: user %s not found", username)); | ||||||
|  |             } | ||||||
|  |         } catch (SQLException e) { | ||||||
|  |             e.printStackTrace(); | ||||||
|  |         } | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public ArrayList<Reservation> getReservations(String username) { | ||||||
|  |         ArrayList<Reservation> reservations = new ArrayList<Reservation>(); | ||||||
|  | 
 | ||||||
|  |         if(!isConnected()) { | ||||||
|  |             System.err.println("getReservations: no connection to database"); | ||||||
|  |             return reservations; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         try { | ||||||
|  |             Statement stmt = conn.createStatement(); | ||||||
|  |             ResultSet rs = stmt.executeQuery("SELECT *, Theaters.name AS theater_name FROM Reservations JOIN Showings ON Reservations.showing_id = Showings.showing_id JOIN Movies ON Showings.movie_id = Movies.movie_id JOIN Theaters ON Showings.theater_id = Theaters.theater_id JOIN users ON Reservations.user_id = Users.id WHERE username = '" + username + "'"); | ||||||
|  |             while (rs.next()) { | ||||||
|  |                 reservations.add(new Reservation(rs.getInt("reservation_id"), rs.getString("name"), rs.getString("show_date"), rs.getString("theater_name"))); | ||||||
|  |             } | ||||||
|  |         } catch (SQLException e) { | ||||||
|  |             e.printStackTrace(); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         return reservations; | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -11,6 +11,7 @@ import java.util.List; | ||||||
| 
 | 
 | ||||||
| import datamodel.CurrentUser; | import datamodel.CurrentUser; | ||||||
| import datamodel.Database; | import datamodel.Database; | ||||||
|  | import datamodel.Reservation; | ||||||
| import datamodel.Show; | import datamodel.Show; | ||||||
| 
 | 
 | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
|  | @ -67,10 +68,12 @@ public class BookingTab { | ||||||
| 				(event) -> { | 				(event) -> { | ||||||
| 					String movie = moviesList.getSelectionModel().getSelectedItem(); | 					String movie = moviesList.getSelectionModel().getSelectedItem(); | ||||||
| 					String date = datesList.getSelectionModel().getSelectedItem(); | 					String date = datesList.getSelectionModel().getSelectedItem(); | ||||||
|  | 					String user = CurrentUser.instance().getCurrentUserId(); | ||||||
|  | 					Reservation r = db.addReservation(user, movie, date); | ||||||
| 					/* --- TODO: should attempt to book a ticket via the database --- */ | 					/* --- TODO: should attempt to book a ticket via the database --- */ | ||||||
| 					/* --- do not forget to report booking number! --- */ | 					/* --- do not forget to report booking number! --- */ | ||||||
| 					/* --- update the displayed details (free seats) --- */ | 					/* --- update the displayed details (free seats) --- */ | ||||||
| 					report("Booked one ticket to "+movie+" on "+date); | 					report(String.format("Ticket booked for %s on %s. Reservation number: %d", movie, date, r.getBookingId())); | ||||||
| 				}); | 				}); | ||||||
| 		 | 		 | ||||||
| 		report("Ready."); | 		report("Ready."); | ||||||
|  |  | ||||||
|  | @ -4,6 +4,7 @@ import java.util.Arrays; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| 
 | 
 | ||||||
| import datamodel.Reservation; | import datamodel.Reservation; | ||||||
|  | import datamodel.CurrentUser; | ||||||
| import datamodel.Database; | import datamodel.Database; | ||||||
| import javafx.fxml.FXML; | import javafx.fxml.FXML; | ||||||
| import javafx.scene.control.TableColumn; | import javafx.scene.control.TableColumn; | ||||||
|  | @ -57,8 +58,11 @@ public class ReservationsTab { | ||||||
| 	public void updateList() { | 	public void updateList() { | ||||||
| 		/* --- TODO: replace with own code using the database object instead --- */ | 		/* --- TODO: replace with own code using the database object instead --- */ | ||||||
| 		System.out.println("Update booking list called."); | 		System.out.println("Update booking list called."); | ||||||
|  | 
 | ||||||
|  | 		// Get the list of bookings from the database | ||||||
|  | 		List<Reservation> bookings = db.getReservations(CurrentUser.instance().getCurrentUserId()); | ||||||
| 		 | 		 | ||||||
| 		List<Reservation> bookings = Arrays.asList(new Reservation(1, "Star Wars", "2019-12-30", "Bio Söder")); | 		// List<Reservation> bookings = Arrays.asList(new Reservation(1, "Star Wars", "2019-12-30", "Bio Söder")); | ||||||
| 		tableReservations.getItems().setAll(bookings); | 		tableReservations.getItems().setAll(bookings); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -35,9 +35,9 @@ CREATE TABLE IF NOT EXISTS Showings ( | ||||||
| 
 | 
 | ||||||
| CREATE TABLE IF NOT EXISTS Reservations ( | CREATE TABLE IF NOT EXISTS Reservations ( | ||||||
|     reservation_id INTEGER PRIMARY KEY, |     reservation_id INTEGER PRIMARY KEY, | ||||||
|     username VARCHAR(50) REFERENCES Users(id), |     user_id INTEGER REFERENCES Users(id), | ||||||
|     showing_id INTEGER REFERENCES Showings(showing_id), |     showing_id INTEGER REFERENCES Showings(showing_id), | ||||||
|     reservation_number VARCHAR(50) UNIQUE NOT NULL DEFAULT (hex(randomblob(2))) -- 4 characters |     reservation_number VARCHAR(50) UNIQUE DEFAULT (hex(randomblob(2))) -- 4 characters | ||||||
| ); | ); | ||||||
| 
 | 
 | ||||||
| INSERT INTO Theaters (name, total_seats) VALUES ('Theater 1', 100); | INSERT INTO Theaters (name, total_seats) VALUES ('Theater 1', 100); | ||||||
|  | @ -57,7 +57,7 @@ INSERT INTO Showings (movie_id, theater_id, show_date) VALUES (3, 1, '2024-08-12 | ||||||
| INSERT INTO Showings (movie_id, theater_id, show_date) VALUES (1, 2, '2024-03-04'); | INSERT INTO Showings (movie_id, theater_id, show_date) VALUES (1, 2, '2024-03-04'); | ||||||
| INSERT INTO Showings (movie_id, theater_id, show_date) VALUES (2, 2, '2024-01-10'); | INSERT INTO Showings (movie_id, theater_id, show_date) VALUES (2, 2, '2024-01-10'); | ||||||
| 
 | 
 | ||||||
| INSERT INTO Reservations (username, showing_id) VALUES (1, 1); | INSERT INTO Reservations (user_id, showing_id) VALUES (1, 1); | ||||||
| INSERT INTO Reservations (username, showing_id) VALUES (2, 2); | INSERT INTO Reservations (user_id, showing_id) VALUES (2, 2); | ||||||
| INSERT INTO Reservations (username, showing_id) VALUES (3, 3); | INSERT INTO Reservations (user_id, showing_id) VALUES (3, 3); | ||||||
| INSERT INTO Reservations (username, showing_id) VALUES (1, 4); | INSERT INTO Reservations (user_id, showing_id) VALUES (1, 4); | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Imbus
						Imbus