Compare commits
6 commits
aa1fdbeb2a
...
c789c24af3
Author | SHA1 | Date | |
---|---|---|---|
![]() |
c789c24af3 | ||
![]() |
2a829509fc | ||
![]() |
a1f691de6a | ||
![]() |
33c67ff4de | ||
![]() |
d434e8fb0f | ||
![]() |
9665dec8ae |
6 changed files with 189 additions and 27 deletions
|
@ -1,6 +1,7 @@
|
||||||
package datamodel;
|
package datamodel;
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
/**
|
/**
|
||||||
* Database is a class that specifies the interface to the
|
* Database is a class that specifies the interface to the
|
||||||
* movie database. Uses JDBC and the MySQL Connector/J driver.
|
* movie database. Uses JDBC and the MySQL Connector/J driver.
|
||||||
|
@ -50,6 +51,28 @@ public class Database {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reads a file from the resources folder and returns the content as a string
|
||||||
|
private String readResourceFile(String fileName) {
|
||||||
|
String content = "";
|
||||||
|
try {
|
||||||
|
content = new String(getClass().getResourceAsStream(fileName).readAllBytes());
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void runMigration() {
|
||||||
|
// This path needs to start with a /
|
||||||
|
String migration = readResourceFile("/migration.sql");
|
||||||
|
try {
|
||||||
|
Statement stmt = conn.createStatement();
|
||||||
|
stmt.executeUpdate(migration);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Close the connection to the database.
|
* Close the connection to the database.
|
||||||
*/
|
*/
|
||||||
|
@ -75,14 +98,97 @@ public class Database {
|
||||||
return conn != null;
|
return conn != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Gets the Show object with the given title and date
|
||||||
public Show getShowData(String mTitle, String mDate) {
|
public Show getShowData(String mTitle, String mDate) {
|
||||||
Integer mFreeSeats = 42;
|
Integer mFreeSeats = 42;
|
||||||
String mVenue = "Kino 2";
|
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);
|
return new Show(mTitle, mDate, mVenue, mFreeSeats);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ArrayList<Show> getAllShows() {
|
||||||
|
ArrayList<Show> shows = new ArrayList<Show>();
|
||||||
|
|
||||||
|
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<String> getDatesForMovie(String mTitle) {
|
||||||
|
ArrayList<String> dates = new ArrayList<String>();
|
||||||
|
|
||||||
|
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 --- */
|
/* --- TODO: insert more own code here --- */
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,14 +92,11 @@ public class BookingTab {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void fillNamesList() {
|
private void fillNamesList() {
|
||||||
|
System.out.println("Filling names list");
|
||||||
List<String> allmovies = new ArrayList<String>();
|
List<String> allmovies = new ArrayList<String>();
|
||||||
|
|
||||||
// query the database via db
|
// Spooky functional voodoo
|
||||||
/* --- TODO: replace with own code --- */
|
db.getAllShows().stream().map(Show::getTitle).distinct().forEach(allmovies::add);
|
||||||
allmovies.add("Pulp Fiction");
|
|
||||||
allmovies.add("The Big Lebowski");
|
|
||||||
allmovies.add("Whiplash");
|
|
||||||
/* --- END TODO --- */
|
|
||||||
|
|
||||||
moviesList.setItems(FXCollections.observableList(allmovies));
|
moviesList.setItems(FXCollections.observableList(allmovies));
|
||||||
// remove any selection
|
// remove any selection
|
||||||
|
@ -109,11 +106,7 @@ public class BookingTab {
|
||||||
private void fillDatesList(String m) {
|
private void fillDatesList(String m) {
|
||||||
List<String> alldates = new ArrayList<String>();
|
List<String> alldates = new ArrayList<String>();
|
||||||
if(m!=null) {
|
if(m!=null) {
|
||||||
// query the database via db
|
db.getDatesForMovie(m).forEach(alldates::add);
|
||||||
/* --- TODO: replace with own code --- */
|
|
||||||
alldates.add("2016-02-01");
|
|
||||||
alldates.add("2016-01-15");
|
|
||||||
/* --- END TODO --- */
|
|
||||||
}
|
}
|
||||||
datesList.setItems(FXCollections.observableList(alldates));
|
datesList.setItems(FXCollections.observableList(alldates));
|
||||||
// remove any selection
|
// remove any selection
|
||||||
|
|
|
@ -31,22 +31,21 @@ public class LoginTab {
|
||||||
} else {
|
} else {
|
||||||
String uname = username.getText();
|
String uname = username.getText();
|
||||||
|
|
||||||
/* --- TODO: add code to query the database credentials --- */
|
// Check if the user exists in the database
|
||||||
// could be if(!db.login(uname)) alert...
|
if(!db.login(uname)) {
|
||||||
|
Alert alert = new Alert(AlertType.ERROR);
|
||||||
// inform the user that there is no check against the database
|
alert.setTitle("Login fail");
|
||||||
Alert alert = new Alert(AlertType.INFORMATION);
|
alert.setHeaderText(null);
|
||||||
alert.setTitle("Login fail");
|
alert.setContentText("User "+uname+" not found!");
|
||||||
alert.setHeaderText(null);
|
alert.showAndWait();
|
||||||
alert.setContentText("No user check implemented yet!");
|
return;
|
||||||
alert.showAndWait();
|
}
|
||||||
/* --- END TODO --- */
|
|
||||||
|
|
||||||
// setting the user name
|
// setting the user name
|
||||||
CurrentUser.instance().loginAs(uname);
|
CurrentUser.instance().loginAs(uname);
|
||||||
|
|
||||||
// inform the user about logging in
|
// 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
|
// inform booking tab of user change
|
||||||
bookingTabCtrl.userChanged();
|
bookingTabCtrl.userChanged();
|
||||||
|
|
|
@ -48,6 +48,7 @@ public class MainApplication extends Application {
|
||||||
alert.setContentText("Could not connect to the database! Check console for details.");
|
alert.setContentText("Could not connect to the database! Check console for details.");
|
||||||
alert.showAndWait();
|
alert.showAndWait();
|
||||||
}
|
}
|
||||||
|
db.runMigration();
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,9 +34,9 @@ root {
|
||||||
display: block;
|
display: block;
|
||||||
}
|
}
|
||||||
|
|
||||||
.root {
|
/* .root {
|
||||||
-fx-background-image: url("background.jpg");
|
-fx-background-image: url("background.jpg");
|
||||||
}
|
} */
|
||||||
|
|
||||||
.label {
|
.label {
|
||||||
-fx-font-size: 12px;
|
-fx-font-size: 12px;
|
||||||
|
|
63
src/main/resources/migration.sql
Normal file
63
src/main/resources/migration.sql
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
PRAGMA foreign_keys = OFF;
|
||||||
|
|
||||||
|
DROP TABLE IF EXISTS Users;
|
||||||
|
DROP TABLE IF EXISTS Reservations;
|
||||||
|
DROP TABLE IF EXISTS Showings;
|
||||||
|
DROP TABLE IF EXISTS Movies;
|
||||||
|
DROP TABLE IF EXISTS Theaters;
|
||||||
|
|
||||||
|
PRAGMA foreign_keys = ON;
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS Users (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
username VARCHAR(50),
|
||||||
|
address VARCHAR(255),
|
||||||
|
telephone VARCHAR(20)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS Theaters (
|
||||||
|
theater_id INTEGER PRIMARY KEY,
|
||||||
|
name VARCHAR(100) UNIQUE,
|
||||||
|
total_seats INTEGER
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS Movies (
|
||||||
|
movie_id INTEGER PRIMARY KEY,
|
||||||
|
name VARCHAR(255) UNIQUE
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS Showings (
|
||||||
|
showing_id INTEGER PRIMARY KEY,
|
||||||
|
movie_id INTEGER REFERENCES Movies(movie_id),
|
||||||
|
theater_id INTEGER REFERENCES Theaters(theater_id),
|
||||||
|
show_date DATE
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS Reservations (
|
||||||
|
reservation_id INTEGER PRIMARY KEY,
|
||||||
|
username VARCHAR(50) REFERENCES Users(id),
|
||||||
|
showing_id INTEGER REFERENCES Showings(showing_id),
|
||||||
|
reservation_number VARCHAR(50) UNIQUE NOT NULL DEFAULT (hex(randomblob(2))) -- 4 characters
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO Theaters (name, total_seats) VALUES ('Theater 1', 100);
|
||||||
|
INSERT INTO Theaters (name, total_seats) VALUES ('Theater 2', 150);
|
||||||
|
|
||||||
|
INSERT INTO Movies (name) VALUES ('Movie 1');
|
||||||
|
INSERT INTO Movies (name) VALUES ('Movie 2');
|
||||||
|
INSERT INTO Movies (name) VALUES ('Movie 3');
|
||||||
|
|
||||||
|
INSERT INTO Users (username, address, telephone) VALUES ('user1', 'address1', '1234567890');
|
||||||
|
INSERT INTO Users (username, address, telephone) VALUES ('user2', 'address2', '1234567890');
|
||||||
|
INSERT INTO Users (username, address, telephone) VALUES ('user3', 'address3', '1234567890');
|
||||||
|
|
||||||
|
INSERT INTO Showings (movie_id, theater_id, show_date) VALUES (1, 1, '2024-04-01');
|
||||||
|
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 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);
|
Loading…
Reference in a new issue