Advertisement
Guest User

Guest

a guest
Sep 26th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1. package group_project;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.text.ParseException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Date;
  11.  
  12. public class Guest extends Admin {
  13. String userOnline, dateTime;
  14.  
  15. public Guest() {
  16.  
  17. }
  18.  
  19.  
  20. public String getESDateUpdate() throws Exception {
  21. String query = "SELECT DateUpdate FROM extra_service WHERE ID = ?";
  22. PreparedStatement statement = getConnection().prepareStatement(query);
  23. statement.setInt(1, getId());
  24. ResultSet rs = statement.executeQuery();
  25. if (rs.next()) {
  26. esDateUpdate = rs.getString(1);
  27. }
  28. return esDateUpdate;
  29. }
  30.  
  31. public String getRoomDateUpdate() throws Exception {
  32. String query = "SELECT DateUpdate FROM room WHERE ID = ?";
  33. PreparedStatement statement = getConnection().prepareStatement(query);
  34. statement.setInt(1, getId());
  35. ResultSet rs = statement.executeQuery();
  36. if (rs.next()) {
  37. roomDateUpdate = rs.getString(1);
  38. }
  39. return roomDateUpdate;
  40. }
  41.  
  42.  
  43.  
  44. /*
  45. * @author Jasmin Bektic
  46. *
  47. * Method acting like LogIn form
  48. */
  49. public void logIn() throws Exception {
  50. Statement s = getConnection().createStatement();
  51. s.executeQuery("SELECT UserName, Password FROM information");
  52. ResultSet rs = s.getResultSet();
  53.  
  54. //username and pass input
  55. boolean boo = true;
  56. while (boo) {
  57. System.out.println("Enter username:");
  58. userName = input.next();
  59. System.out.println("Enter password:");
  60. password = input.next();
  61.  
  62. //Processing current data and comparing to data from database
  63. while (rs.next()) {
  64. String user = rs.getString("UserName");
  65. String pass = rs.getString("Password");
  66. if (userName.equals(user) && password.equals(pass)) {
  67. boo = false;
  68. break;
  69. }
  70. }
  71. //Supporting message, in case of input failure
  72. if (boo == true) System.out.println("Incorrect user name or password, try again:");
  73. }
  74. System.out.println();
  75.  
  76. //Add username in array
  77. PreparedStatement statement = getConnection().prepareStatement("SELECT UserName FROM information WHERE information.UserName = ?");
  78. statement.setString(1, userName);
  79. userOnline = (rs.getString(1));
  80. rs.close ();
  81. s.close ();
  82. setList(userOnline);
  83. }
  84.  
  85. /* Method acts like LogOut form */
  86. public void logOut() {
  87. list.remove(list.size() - 1);
  88. // ovdje ide metoda za glavni meni
  89. }
  90.  
  91. /*
  92. * @author Jasmin Bektic
  93. *
  94. * Method from which guest can order new extra services
  95. */
  96. public void orderExtraService() throws Exception {
  97. int[] extraService = new int[5];
  98. int sum = 0;
  99.  
  100. //Getting old extra service prices and sum
  101. String query = "SELECT Gym, Cinema, Restaurant, Pool, Sauna FROM extra_service WHERE ID = ?";
  102. PreparedStatement statement = getConnection().prepareStatement(query);
  103. statement.setInt(1, getId());
  104. ResultSet rs = statement.executeQuery();
  105. while (rs.next()) {
  106. for (int i = 0; i < 5; i++) {
  107. extraService[i] = rs.getInt(i + 1);
  108. sum += rs.getInt(i + 1);
  109. }
  110. }
  111.  
  112. //Updating extra service list
  113. System.out.println("1- Gym\n2- Cinema\n3- Restaurant\n4- Pool\n5- Sauna\n\n0- Finish my order");
  114. int index = 0;
  115. while (true) {
  116. System.out.println("Order service entering adequate number or finish your order:");
  117. index = input.nextInt();
  118. if (index == 0) break;
  119. extraService[index - 1] = this.extraService[index - 1];
  120. }
  121. int balance = balance(getESDateUpdate(), sum);
  122.  
  123. //Get old balance and add new one
  124. PreparedStatement st = getConnection().prepareStatement("SELECT Balance FROM extra_service WHERE ID = ?");
  125. st.setInt(1, getId());
  126. ResultSet result = st.executeQuery();
  127. if (result.next()) {
  128. balance += result.getInt("Balance");
  129. }
  130.  
  131. //Update query with specified ID
  132. String sql = "UPDATE extra_service SET Gym = ?, Cinema= ?, Restaurant= ?, Pool = ?, Sauna = ?, Balance = ?, DateUpdate = ? WHERE ID = ?";
  133. PreparedStatement s = getConnection().prepareStatement(sql);
  134. s.setInt(1, extraService[0]);
  135. s.setInt(2, extraService[1]);
  136. s.setInt(3, extraService[2]);
  137. s.setInt(4, extraService[3]);
  138. s.setInt(5, extraService[4]);
  139. s.setInt(6, balance);
  140. s.setString(7, dateTime);
  141. s.setInt(8, getId());
  142. s.executeUpdate();
  143. }
  144.  
  145. /*
  146. * @author Jasmin Bektic
  147. *
  148. * Method used for calculating total balance for certain number of days
  149. */
  150. public int balance(String s, int sum) throws Exception {
  151. //Current date and time
  152. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  153. Date date = new Date();
  154. String orderDate = df.format(date);
  155. Date d1 = df.parse(s);
  156. Date d2 = df.parse(orderDate);
  157.  
  158. //Number of days between current date and checkIn date (time is default, 12:00:00)
  159. long diff = d2.getTime() - d1.getTime();
  160. long diffDays = diff / (24 * 60 * 60 * 1000);
  161. dateTime = orderDate.substring(0, 10) + " 12:00:00";
  162. int balance = 0;
  163. if (diffDays == 0) {
  164. balance = sum;
  165. } else {
  166. balance = (int) (diffDays * sum);
  167. }
  168. return balance;
  169. }
  170.  
  171. /*
  172. * @author Jasmin Bektic
  173. *
  174. * Method from which guest can order new room
  175. */
  176. public void orderNewRoom() throws Exception {
  177. String newRoom = roomType();
  178. int[] room = new int[3];
  179. int sum = 0;
  180.  
  181. //Get current room price
  182. String query = "SELECT SingleRoom, DoubleRoom, Apartment FROM room WHERE ID = ?";
  183. PreparedStatement statement = getConnection().prepareStatement(query);
  184. statement.setInt(1, getId());
  185. ResultSet rs = statement.executeQuery();
  186. while (rs.next()) {
  187. for (int i = 0; i < 3; i++) {
  188. room[i] = rs.getInt(i + 1);
  189. sum += rs.getInt(i + 1);
  190. }
  191. }
  192. int balance = balance(getRoomDateUpdate(), sum);
  193.  
  194. //Get old balance and add new one
  195. PreparedStatement st = getConnection().prepareStatement("SELECT Balance FROM room WHERE ID = ?");
  196. st.setInt(1, getId());
  197. ResultSet result = st.executeQuery();
  198. if (result.next()) {
  199. balance += result.getInt("Balance");
  200. }
  201. switch (newRoom) {
  202. case "SingleRoom": room[0] = this.room[0];room[1] = 0;room[2] = 0; break;
  203. case "DoubleRoom": room[1] = this.room[1];room[0] = 0;room[2] = 0; break;
  204. case "Apartment": room[2] = this.room[2];room[0] = 0;room[1] = 0; break;
  205. }
  206.  
  207. //Update room price which guest use
  208. String sql = "UPDATE room SET SingleRoom = ?, DoubleRoom = ?, Apartment = ?, Balance = ?, DateUpdate = ? WHERE ID = ?";
  209. PreparedStatement s = getConnection().prepareStatement(sql);
  210. s.setInt(1, room[0]);
  211. s.setInt(2, room[1]);
  212. s.setInt(3, room[2]);
  213. s.setInt(4, balance);
  214. s.setString(5, dateTime);
  215. s.setInt(6, getId());
  216. s.executeUpdate();
  217. }
  218.  
  219. /*
  220. * @author Maja Vasilic
  221. *
  222. * Method copying information from "information" table to the "archive"
  223. * table and removes user from "information"
  224. * @throws Exception
  225. */
  226. public void userCheckOut(String name) throws Exception {
  227.  
  228. // establish connection to the database hotel invoking method
  229. Connection conn = getConnection();
  230.  
  231. try {
  232. // create statement variable
  233. Statement stSelect = conn.createStatement();
  234.  
  235. // query execution (find row with specified name)
  236. ResultSet result = stSelect.executeQuery("SELECT * FROM information WHERE FirstName = '"+ name + "'");
  237. System.out.println("Sending data to the archive.");
  238.  
  239. // create prepared statement to insert values in archive from information
  240. PreparedStatement stInsert = (PreparedStatement) conn.prepareStatement("INSERT INTO archive (FirstName, LastName,"+ " Gender, IdentityCard, Age) VALUES (?, ?, ?, ?, ?)");
  241.  
  242. // set values (get values from one table to another)
  243. stInsert.setString(1, result.getString(2));
  244. stInsert.setString(2, result.getString(3));
  245. stInsert.setString(3, result.getString(4));
  246. stInsert.setString(4, result.getString(5));
  247. stInsert.setInt(5, result.getInt(6));
  248.  
  249. // execute query to insert values in the archive to the information
  250. stInsert.executeQuery();
  251.  
  252. // how we can remove user from current table for users
  253. stSelect.executeQuery("DELETE FROM information WHERE FirstName = '"+ name + "'");
  254.  
  255. // print message
  256. System.out.println("User " + name + " is removed.");
  257. conn.close();
  258.  
  259. } catch (Exception e) {
  260. System.out.println(e);
  261. } finally {
  262. if (conn != null) {
  263. // close connection with the database
  264. conn.close();
  265. }
  266. }
  267. }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement