Fixes for seat amount
This commit is contained in:
parent
87cb244f71
commit
ebc6ec7896
3 changed files with 43 additions and 23 deletions
|
@ -99,32 +99,39 @@ public class Database {
|
|||
}
|
||||
|
||||
// Gets the Show object with the given title and date
|
||||
public Show getShowData(String mTitle, String mDate) {
|
||||
Integer mFreeSeats = 42;
|
||||
String mVenue = "Kino 2";
|
||||
public Show getShowData(String movie, String date) {
|
||||
if (!isConnected()) {
|
||||
System.err.println("getShowData: no connection to database");
|
||||
return null;
|
||||
}
|
||||
|
||||
if(!isConnected()) {
|
||||
System.err.println("getShowData: no connection to database");
|
||||
return null;
|
||||
try {
|
||||
// Using prepared statements here to avoid injection, but this is never done anywhere else in this file
|
||||
String query = "SELECT m.name AS movie_title, s.show_date, t.name AS theater_name, t.total_seats - COUNT(r.reservation_id) AS free_seats " +
|
||||
"FROM Showings s " +
|
||||
"JOIN Theaters t ON s.theater_id = t.theater_id " +
|
||||
"JOIN Movies m ON s.movie_id = m.movie_id " +
|
||||
"LEFT JOIN Reservations r ON s.showing_id = r.showing_id " +
|
||||
"WHERE m.name = ? AND s.show_date = ? " +
|
||||
"GROUP BY s.show_date, t.theater_id";
|
||||
PreparedStatement pstmt = conn.prepareStatement(query);
|
||||
pstmt.setString(1, movie);
|
||||
pstmt.setString(2, date);
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
String title = rs.getString("movie_title");
|
||||
String showDate = rs.getString("show_date");
|
||||
String venue = rs.getString("theater_name");
|
||||
int freeSeats = rs.getInt("free_seats");
|
||||
|
||||
return new Show(title, showDate, venue, freeSeats);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
Statement stmt = conn.createStatement();
|
||||
// ResultSet rs = stmt.executeQuery("SELECT * FROM Showings WHERE title = '" + mTitle + "' AND date = '" + mDate + "'");
|
||||
ResultSet rs = stmt.executeQuery("SELECT *, Theaters.name AS theater_name FROM Showings JOIN Movies ON Showings.movie_id = Movies.movie_id JOIN Theaters ON Showings.theater_id = Theaters.theater_id");
|
||||
if (rs.next()) {
|
||||
mFreeSeats = rs.getInt("total_seats");
|
||||
mVenue = rs.getString("theater_name");
|
||||
} else {
|
||||
System.err.println("getShowData: no show found");
|
||||
return null;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return new Show(mTitle, mDate, mVenue, mFreeSeats);
|
||||
}
|
||||
|
||||
public ArrayList<Show> getAllShows() {
|
||||
ArrayList<Show> shows = new ArrayList<Show>();
|
||||
|
|
|
@ -69,7 +69,18 @@ public class BookingTab {
|
|||
String movie = moviesList.getSelectionModel().getSelectedItem();
|
||||
String date = datesList.getSelectionModel().getSelectedItem();
|
||||
String user = CurrentUser.instance().getCurrentUserId();
|
||||
if(movie==null || date==null || user.isEmpty()) {
|
||||
report("You must select a movie and a date, and be logged in!");
|
||||
return;
|
||||
}
|
||||
|
||||
if(crtShow.getSeats() <= 0) {
|
||||
report("No more free seats for this show!");
|
||||
return;
|
||||
}
|
||||
|
||||
Reservation r = db.addReservation(user, movie, date);
|
||||
fillShow(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) --- */
|
||||
|
|
|
@ -42,6 +42,7 @@ CREATE TABLE IF NOT EXISTS Reservations (
|
|||
|
||||
INSERT INTO Theaters (name, total_seats) VALUES ('Theater 1', 100);
|
||||
INSERT INTO Theaters (name, total_seats) VALUES ('Theater 2', 150);
|
||||
INSERT INTO Theaters (name, total_seats) VALUES ('Theater 3', 2);
|
||||
|
||||
INSERT INTO Movies (name) VALUES ('Movie 1');
|
||||
INSERT INTO Movies (name) VALUES ('Movie 2');
|
||||
|
@ -56,6 +57,7 @@ INSERT INTO Showings (movie_id, theater_id, show_date) VALUES (2, 1, '2024-02-24
|
|||
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 Showings (movie_id, theater_id, show_date) VALUES (2, 3, '2025-01-10');
|
||||
|
||||
INSERT INTO Reservations (user_id, showing_id) VALUES (1, 1);
|
||||
INSERT INTO Reservations (user_id, showing_id) VALUES (2, 2);
|
||||
|
|
Loading…
Reference in a new issue