From 87cb244f7132bc656b00c726d5fe29d61ceacaa1 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Wed, 7 Feb 2024 10:18:03 +0100 Subject: [PATCH] Almost done --- src/main/java/datamodel/Database.java | 55 +++++++++++++++++++++++++- src/main/java/gui/BookingTab.java | 5 ++- src/main/java/gui/ReservationsTab.java | 6 ++- src/main/resources/migration.sql | 12 +++--- 4 files changed, 69 insertions(+), 9 deletions(-) diff --git a/src/main/java/datamodel/Database.java b/src/main/java/datamodel/Database.java index 163e336..75681e2 100755 --- a/src/main/java/datamodel/Database.java +++ b/src/main/java/datamodel/Database.java @@ -190,5 +190,58 @@ public class Database { 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 getReservations(String username) { + ArrayList reservations = new ArrayList(); + + 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; + } } diff --git a/src/main/java/gui/BookingTab.java b/src/main/java/gui/BookingTab.java index 276f547..5207512 100755 --- a/src/main/java/gui/BookingTab.java +++ b/src/main/java/gui/BookingTab.java @@ -11,6 +11,7 @@ import java.util.List; import datamodel.CurrentUser; import datamodel.Database; +import datamodel.Reservation; import datamodel.Show; import java.util.ArrayList; @@ -67,10 +68,12 @@ public class BookingTab { (event) -> { String movie = moviesList.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 --- */ /* --- do not forget to report booking number! --- */ /* --- 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."); diff --git a/src/main/java/gui/ReservationsTab.java b/src/main/java/gui/ReservationsTab.java index d6abb90..98ecef1 100644 --- a/src/main/java/gui/ReservationsTab.java +++ b/src/main/java/gui/ReservationsTab.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.List; import datamodel.Reservation; +import datamodel.CurrentUser; import datamodel.Database; import javafx.fxml.FXML; import javafx.scene.control.TableColumn; @@ -57,8 +58,11 @@ public class ReservationsTab { public void updateList() { /* --- TODO: replace with own code using the database object instead --- */ System.out.println("Update booking list called."); + + // Get the list of bookings from the database + List bookings = db.getReservations(CurrentUser.instance().getCurrentUserId()); - List bookings = Arrays.asList(new Reservation(1, "Star Wars", "2019-12-30", "Bio Söder")); + // List bookings = Arrays.asList(new Reservation(1, "Star Wars", "2019-12-30", "Bio Söder")); tableReservations.getItems().setAll(bookings); } } diff --git a/src/main/resources/migration.sql b/src/main/resources/migration.sql index aadf70d..0b1b9b5 100644 --- a/src/main/resources/migration.sql +++ b/src/main/resources/migration.sql @@ -35,9 +35,9 @@ CREATE TABLE IF NOT EXISTS Showings ( CREATE TABLE IF NOT EXISTS Reservations ( reservation_id INTEGER PRIMARY KEY, - username VARCHAR(50) REFERENCES Users(id), + user_id INTEGER REFERENCES Users(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); @@ -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 (2, 2, '2024-01-10'); -INSERT INTO Reservations (username, showing_id) VALUES (1, 1); -INSERT INTO Reservations (username, showing_id) VALUES (2, 2); -INSERT INTO Reservations (username, showing_id) VALUES (3, 3); -INSERT INTO Reservations (username, showing_id) VALUES (1, 4); \ No newline at end of file +INSERT INTO Reservations (user_id, showing_id) VALUES (1, 1); +INSERT INTO Reservations (user_id, showing_id) VALUES (2, 2); +INSERT INTO Reservations (user_id, showing_id) VALUES (3, 3); +INSERT INTO Reservations (user_id, showing_id) VALUES (1, 4); \ No newline at end of file