Advertisement
Guest User

Untitled

a guest
Feb 14th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.21 KB | None | 0 0
  1. package datamodel;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. /**
  8. * Database is a class that specifies the interface to the movie database. Uses
  9. * JDBC and the MySQL Connector/J driver.
  10. */
  11. public class Database {
  12. /**
  13. * The database connection.
  14. */
  15. private Connection conn;
  16.  
  17. /**
  18. * Create the database interface object. Connection to the database is
  19. * performed later.
  20. */
  21. public Database() {
  22. conn = null;
  23. }
  24.  
  25. /**
  26. * Open a connection to the database, using the specified user name and
  27. * password.
  28. *
  29. * @param userName
  30. * The user name.
  31. * @param password
  32. * The user's password.
  33. * @return true if the connection succeeded, false if the supplied user name
  34. * and password were not recognized. Returns false also if the JDBC
  35. * driver isn't found.
  36. */
  37. public boolean openConnection(String userName, String password) {
  38. try {
  39. Class.forName("com.mysql.jdbc.Driver");
  40. conn = DriverManager.getConnection("jdbc:mysql://puccini.cs.lth.se/" + userName, userName, password);
  41. } catch (SQLException e) {
  42. System.err.println(e);
  43. e.printStackTrace();
  44. return false;
  45. } catch (ClassNotFoundException e) {
  46. System.err.println(e);
  47. e.printStackTrace();
  48. return false;
  49. }
  50. return true;
  51. }
  52.  
  53. /**
  54. * Close the connection to the database.
  55. */
  56. public void closeConnection() {
  57. try {
  58. if (conn != null)
  59. conn.close();
  60. } catch (SQLException e) {
  61. e.printStackTrace();
  62. }
  63. conn = null;
  64.  
  65. System.err.println("Database connection closed.");
  66. }
  67.  
  68. /**
  69. * Check if the connection to the database has been established
  70. *
  71. * @return true if the connection has been established
  72. */
  73. public boolean isConnected() {
  74. return conn != null;
  75. }
  76.  
  77. public Show getShowData(String movie, String date) {
  78. Integer mFreeSeats = 0;
  79. String theater = "";
  80. try (Statement s = conn.createStatement()) {
  81. String sql = "SELECT date,tickets, theaterName FROM Shows";
  82. ResultSet rs = s.executeQuery(sql);
  83. while (rs.next()) {
  84. if (rs.getString("date").equals(date)) {
  85. mFreeSeats = rs.getInt("tickets");
  86. }
  87. if (rs.getString("date").equals(date)) {
  88. theater = rs.getString("theaterName");
  89. }
  90.  
  91. }
  92.  
  93. } catch (SQLException e) {
  94. e.printStackTrace();
  95. }
  96. return new Show(movie, date, theater, mFreeSeats);
  97. }
  98.  
  99. public boolean checkIfUserExist(String username) {
  100.  
  101. try (Statement s = conn.createStatement()) {
  102. String sql = "SELECT username FROM Users WHERE Users.username = '" + username + "'";
  103. ResultSet rs = s.executeQuery(sql);
  104.  
  105. if (rs.next() == false) {
  106. return false;
  107. }
  108. } catch (SQLException e) {
  109. e.printStackTrace();
  110. }
  111. return true;
  112. }
  113.  
  114. public List<String> addMovies() {
  115. List<String> movies = new ArrayList<String>();
  116. try (Statement s = conn.createStatement()) {
  117. String sql = "SELECT showName from Shows";
  118. ResultSet rs = s.executeQuery(sql);
  119.  
  120. while (rs.next()) {
  121. if (!movies.contains(rs.getString("showName"))) {
  122. movies.add(rs.getString("showName"));
  123. }
  124. }
  125. } catch (SQLException e) {
  126. e.printStackTrace();
  127. }
  128.  
  129. return movies;
  130.  
  131. }
  132.  
  133. public List<String> addDates(String movie) {
  134. List<String> dates = new ArrayList<String>();
  135. try (Statement s = conn.createStatement()) {
  136. String sql = "SELECT showName, date FROM Shows";
  137. ResultSet rs = s.executeQuery(sql);
  138.  
  139. while (rs.next()) {
  140. if (rs.getString("showName").equals(movie)) {
  141. dates.add(rs.getString("date"));
  142. }
  143. }
  144.  
  145. } catch (SQLException e) {
  146. e.printStackTrace();
  147. }
  148. return dates;
  149. }
  150.  
  151. public int bookTicket(String movie, String date, String userName) {
  152.  
  153. int resNbr = 0;
  154.  
  155. try (Statement s = conn.createStatement()) {
  156. String sql = "SELECT tickets from Shows where showName = '" + movie + "'";
  157. ResultSet rs = s.executeQuery(sql);
  158. rs.next();
  159. if (rs.getInt("tickets") == 0) {
  160. resNbr = -1;
  161. } else {
  162.  
  163. sql = "insert into Tickets values(null, '" + userName + "', '" + movie + "', '" + date + "')";
  164. s.executeUpdate(sql);
  165. sql = "UPDATE Shows SET tickets = tickets -1 WHERE showName = '" + movie + "'";
  166. s.executeUpdate(sql);
  167. sql = "Select resNbr from Tickets where username = '" + userName + "' ORDER BY resNbr DESC";
  168. rs = s.executeQuery(sql);
  169. rs.next();
  170. resNbr = rs.getInt("resNbr");
  171.  
  172. }
  173.  
  174. } catch (SQLException e) {
  175. e.printStackTrace();
  176. }
  177.  
  178. return resNbr;
  179.  
  180. }
  181.  
  182. }
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196. ------------------------------------------
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204. package gui;
  205.  
  206. import javafx.collections.FXCollections;
  207. import javafx.fxml.FXML;
  208. import javafx.scene.control.Label;
  209. import javafx.scene.control.ListView;
  210. import javafx.scene.control.Alert.AlertType;
  211. import javafx.scene.control.Alert;
  212. import javafx.scene.control.Button;
  213. import javafx.scene.text.Text;
  214.  
  215. import java.util.List;
  216.  
  217. import datamodel.CurrentUser;
  218. import datamodel.Database;
  219. import datamodel.Show;
  220.  
  221. import java.sql.SQLException;
  222. import java.util.ArrayList;
  223.  
  224.  
  225. public class BookingTab {
  226. // top context message
  227. @FXML private Text topContext;
  228. // bottom message
  229. @FXML private Text bookMsg;
  230.  
  231. // table references
  232. @FXML private ListView<String> moviesList;
  233. @FXML private ListView<String> datesList;
  234.  
  235. // show info references
  236. @FXML private Label showTitle;
  237. @FXML private Label showDate;
  238. @FXML private Label showVenue;
  239. @FXML private Label showFreeSeats;
  240.  
  241. // booking button
  242. @FXML private Button bookTicket;
  243.  
  244. private Database db;
  245. private Show crtShow = new Show();
  246.  
  247. public void initialize() {
  248. System.out.println("Initializing BookingTab");
  249.  
  250.  
  251. // set up listeners for the movie list selection
  252. moviesList.getSelectionModel().selectedItemProperty().addListener(
  253. (obs, oldV, newV) -> {
  254. // need to update the date list according to the selected movie
  255. // update also the details on the right panel
  256. String movie = newV;
  257. fillDatesList(newV);
  258. fillShow(movie,null);
  259. });
  260.  
  261.  
  262.  
  263. // set up listeners for the date list selection
  264. datesList.getSelectionModel().selectedItemProperty().addListener(
  265. (obs, oldV, newV) -> {
  266. // need to update the details according to the selected date
  267. String movie = moviesList.getSelectionModel().getSelectedItem();
  268. String date = newV;
  269. fillShow(movie, date);
  270. });
  271.  
  272. // set up booking button listener
  273. // one can either use this method (setup a handler in initialize)
  274. // or directly give a handler name in the fxml, as in the LoginTab class
  275. bookTicket.setOnAction(
  276. (event) -> {
  277. String movie = moviesList.getSelectionModel().getSelectedItem();
  278. String date = datesList.getSelectionModel().getSelectedItem();
  279. String userName = CurrentUser.instance().getCurrentUserId();
  280.  
  281. int resNbr = db.bookTicket(movie,date,userName);
  282. if(resNbr == -1){
  283. Alert alert = new Alert(AlertType.ERROR);
  284. alert.setTitle("Booking failure");
  285. alert.setHeaderText(null);
  286. alert.setContentText("Failed to complete booking, no available seats");
  287. alert.showAndWait();
  288.  
  289. }else{
  290. fillShow(movie,date);
  291. report("Success");
  292. Alert alert = new Alert(AlertType.INFORMATION);
  293. alert.setTitle("Ticket booked");
  294. alert.setHeaderText(null);
  295. alert.setContentText("You have successfully booked a ticket with the reservation number " + resNbr);
  296. alert.showAndWait();
  297. }
  298.  
  299.  
  300.  
  301. });
  302.  
  303. report("Ready.");
  304. }
  305.  
  306. // helpers
  307. // updates user display
  308. private void fillStatus(String usr) {
  309. if(usr.isEmpty()) topContext.setText("You must log in as a known user!");
  310. else topContext.setText("Currently logged in as " + usr);
  311. }
  312.  
  313. private void report(String msg) {
  314. bookMsg.setText(msg);
  315. }
  316.  
  317. public void setDatabase(Database db) {
  318. this.db = db;
  319. }
  320.  
  321.  
  322.  
  323. private void fillNamesList() {
  324. List<String> allmovies = new ArrayList<String>();
  325. allmovies = db.addMovies();
  326. moviesList.setItems(FXCollections.observableList(allmovies));
  327. // remove any selection
  328. moviesList.getSelectionModel().clearSelection();
  329. }
  330.  
  331.  
  332.  
  333. private void fillDatesList(String m) {
  334. List<String> alldates = new ArrayList<String>();
  335. if(m!=null) {
  336.  
  337. alldates = db.addDates(m);
  338. }
  339. datesList.setItems(FXCollections.observableList(alldates));
  340. // remove any selection
  341. datesList.getSelectionModel().clearSelection();
  342. }
  343.  
  344.  
  345.  
  346.  
  347. private void fillShow(String movie, String date) {
  348. if(movie==null) // no movie selected
  349. crtShow = new Show();
  350. else if(date==null) // no date selected yet
  351. crtShow = new Show(movie);
  352. else{
  353.  
  354. crtShow = db.getShowData(movie, date);
  355.  
  356. }
  357.  
  358. showTitle.setText(crtShow.getTitle());
  359. showDate.setText(crtShow.getDate());
  360. showVenue.setText(crtShow.getVenue());
  361. if(crtShow.getSeats() >= 0) showFreeSeats.setText(crtShow.getSeats().toString());
  362. else showFreeSeats.setText("-");
  363. }
  364.  
  365.  
  366.  
  367.  
  368. // called in case the user logged in changed
  369. public void userChanged() {
  370. fillStatus(CurrentUser.instance().getCurrentUserId());
  371. fillNamesList();
  372. fillDatesList(null);
  373. fillShow(null,null);
  374. }
  375.  
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement