From c789c24af30015f956acce5e0310becc39acad9b Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Wed, 7 Feb 2024 08:42:55 +0100 Subject: [PATCH] Half finished, loads of queries implemented --- src/main/java/datamodel/Database.java | 88 ++++++++++++++++++++++++++- src/main/java/gui/BookingTab.java | 17 ++---- src/main/java/gui/LoginTab.java | 21 +++---- src/main/resources/application.css | 4 +- 4 files changed, 103 insertions(+), 27 deletions(-) diff --git a/src/main/java/datamodel/Database.java b/src/main/java/datamodel/Database.java index fd92fc5..163e336 100755 --- a/src/main/java/datamodel/Database.java +++ b/src/main/java/datamodel/Database.java @@ -1,6 +1,7 @@ package datamodel; import java.sql.*; +import java.util.ArrayList; /** * Database is a class that specifies the interface to the * movie database. Uses JDBC and the MySQL Connector/J driver. @@ -97,14 +98,97 @@ public class Database { return conn != null; } + // Gets the Show object with the given title and date public Show getShowData(String mTitle, String mDate) { Integer mFreeSeats = 42; String mVenue = "Kino 2"; - - /* --- TODO: add code for database query --- */ + + if(!isConnected()) { + System.err.println("getShowData: no connection to database"); + 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 getAllShows() { + ArrayList shows = new ArrayList(); + + if(!isConnected()) { + System.err.println("getAllShows: no connection to database"); + return shows; + } + + try { + Statement stmt = conn.createStatement(); + 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"); + while (rs.next()) { + String mTitle = rs.getString("name"); + String mDate = rs.getString("show_date"); + Integer mFreeSeats = rs.getInt("total_seats"); + String mVenue = rs.getString("theater_name"); + shows.add(new Show(mTitle, mDate, mVenue, mFreeSeats)); + } + } catch (SQLException e) { + e.printStackTrace(); + } + + return shows; + } + + public ArrayList getDatesForMovie(String mTitle) { + ArrayList dates = new ArrayList(); + + if(!isConnected()) { + System.err.println("getDatesForMovie: no connection to database"); + return dates; + } + + try { + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT show_date FROM Showings JOIN Movies ON Showings.movie_id = Movies.movie_id WHERE name = '" + mTitle + "'"); + while (rs.next()) { + dates.add(rs.getString("show_date")); + } + } catch (SQLException e) { + e.printStackTrace(); + } + + return dates; + } + + public boolean login(String username) { + if(!isConnected()) { + System.err.println("login: no connection to database"); + return false; + } + + try { + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * FROM users WHERE username = '" + username + "'"); + if (rs.next()) { + return true; + } + } catch (SQLException e) { + e.printStackTrace(); + } + return false; + } + /* --- TODO: insert more own code here --- */ } diff --git a/src/main/java/gui/BookingTab.java b/src/main/java/gui/BookingTab.java index 1fd41cc..276f547 100755 --- a/src/main/java/gui/BookingTab.java +++ b/src/main/java/gui/BookingTab.java @@ -92,14 +92,11 @@ public class BookingTab { } private void fillNamesList() { + System.out.println("Filling names list"); List allmovies = new ArrayList(); - - // query the database via db - /* --- TODO: replace with own code --- */ - allmovies.add("Pulp Fiction"); - allmovies.add("The Big Lebowski"); - allmovies.add("Whiplash"); - /* --- END TODO --- */ + + // Spooky functional voodoo + db.getAllShows().stream().map(Show::getTitle).distinct().forEach(allmovies::add); moviesList.setItems(FXCollections.observableList(allmovies)); // remove any selection @@ -109,11 +106,7 @@ public class BookingTab { private void fillDatesList(String m) { List alldates = new ArrayList(); if(m!=null) { - // query the database via db - /* --- TODO: replace with own code --- */ - alldates.add("2016-02-01"); - alldates.add("2016-01-15"); - /* --- END TODO --- */ + db.getDatesForMovie(m).forEach(alldates::add); } datesList.setItems(FXCollections.observableList(alldates)); // remove any selection diff --git a/src/main/java/gui/LoginTab.java b/src/main/java/gui/LoginTab.java index a1da2e7..05cbbac 100755 --- a/src/main/java/gui/LoginTab.java +++ b/src/main/java/gui/LoginTab.java @@ -31,22 +31,21 @@ public class LoginTab { } else { String uname = username.getText(); - /* --- TODO: add code to query the database credentials --- */ - // could be if(!db.login(uname)) alert... + // Check if the user exists in the database + if(!db.login(uname)) { + Alert alert = new Alert(AlertType.ERROR); + alert.setTitle("Login fail"); + alert.setHeaderText(null); + alert.setContentText("User "+uname+" not found!"); + alert.showAndWait(); + return; + } - // inform the user that there is no check against the database - Alert alert = new Alert(AlertType.INFORMATION); - alert.setTitle("Login fail"); - alert.setHeaderText(null); - alert.setContentText("No user check implemented yet!"); - alert.showAndWait(); - /* --- END TODO --- */ - // setting the user name CurrentUser.instance().loginAs(uname); // inform the user about logging in - actiontarget.setText("Sign in user "+uname); + actiontarget.setText(String.format("User %s logged in.", uname)); // inform booking tab of user change bookingTabCtrl.userChanged(); diff --git a/src/main/resources/application.css b/src/main/resources/application.css index d21e50b..70d1304 100755 --- a/src/main/resources/application.css +++ b/src/main/resources/application.css @@ -34,9 +34,9 @@ root { display: block; } -.root { +/* .root { -fx-background-image: url("background.jpg"); -} +} */ .label { -fx-font-size: 12px;