Guest User

Admin

a guest
Sep 26th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. package group_project;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.ResultSetMetaData;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ArrayList;
  11. import java.util.Scanner;
  12.  
  13. public class Admin {
  14. public int id, age, roomNum;
  15. public String firstName, lastName, gender, identityCard, userName, password, roomType, checkInDate, dateUpdate;
  16. final int[] extraService = {10, 10, 20, 10, 10};
  17. final int[] room = {20, 40, 60};
  18. Scanner input = new Scanner(System.in);
  19. ArrayList<String> list = new ArrayList<>();
  20.  
  21.  
  22. public Admin() {
  23.  
  24. }
  25.  
  26. public String getCheckInDate() throws SQLException {
  27. String query = "SELECT CheckInDate FROM information WHERE information.UserName = ?";
  28. PreparedStatement statement = getConnection().prepareStatement(query);
  29. statement.setString(1, userName);
  30. ResultSet rs = statement.executeQuery();
  31. if (rs.next()) {
  32. checkInDate = rs.getString(1);
  33. }
  34. return checkInDate;
  35. }
  36.  
  37. public int getId() throws SQLException {
  38. String query = "SELECT ID FROM information WHERE information.UserName = ?";
  39. PreparedStatement s = getConnection().prepareStatement(query);
  40. s.setString(1, userName);
  41. ResultSet rs = s.executeQuery();
  42. if (rs.next()) {
  43. id = rs.getInt(1);
  44. }
  45. return id;
  46. }
  47.  
  48. public ArrayList<String> getList() {
  49. return list;
  50. }
  51.  
  52. public void setList(String s) {
  53. list.add(s);
  54. }
  55.  
  56.  
  57. /* @author Jasmin Bektic
  58. *
  59. * Method confirms connection */
  60. public Connection getConnection() {
  61. try {
  62. String driver = "com.mysql.jdbc.Driver";
  63. String url = "jdbc:mysql://localhost:3306/hotel";
  64. String user = "root";
  65. String pass = "";
  66. Class.forName(driver);
  67.  
  68. //Connecting with provided url,userName and pass
  69. Connection con = DriverManager.getConnection(url, user, pass);
  70. return con;
  71. } catch (Exception e) { //Catch errors
  72. System.out.println(e);
  73. }
  74. return null;
  75. }
  76.  
  77. public void checkGuest() {
  78.  
  79. }
  80.  
  81. public void enterInfo() throws SQLException {
  82. firstName = "John";
  83. lastName = "Snow";
  84. gender = "m";
  85. identityCard = "123456";
  86. age = 29;
  87. roomNum = checkAvailableRoom();
  88. roomType = roomType();
  89. checkInDate = "14:05";
  90. userName = checkAvailableUserName();
  91. password = "123456";
  92.  
  93. Connection con = getConnection();
  94. String sql = "INSERT INTO information (FirstName, LastName, Gender, IdentityCard, Age, RoomNumber, RoomType, CheckInTime, UserName, Password) VALUES ('"+firstName+"', '"+lastName+"', '"+gender+"', '"+identityCard+"', "+age+", "+roomNum+",'"+roomType+"','"+checkInDate+"','"+userName+"','"+password+"')";
  95. PreparedStatement statement = con.prepareStatement(sql);
  96.  
  97. statement.executeUpdate();
  98. }
  99.  
  100. /* @author Davor Sadikovic
  101. *
  102. * Method for checking available rooms */
  103. public int checkAvailableRoom() throws SQLException {
  104. Statement s = getConnection().createStatement();
  105. s.executeQuery("SELECT RoomNumber FROM information");
  106. ResultSet rs = s.getResultSet();
  107. System.out.print("Occupied room numbers: ");
  108.  
  109. //Listing occupied rooms
  110. while (rs.next()) {
  111. int room = rs.getInt("RoomNumber");
  112. System.out.print(room + " ");
  113. }
  114. System.out.println();
  115. rs.close ();
  116. s.close ();
  117.  
  118. //Assign room number for a guest
  119. System.out.println("Enter room number for a guest:");
  120. int num = input.nextInt();
  121. return num;
  122. }
  123.  
  124. /* @author Jasmin Bektic
  125. *
  126. * Assign room type for guest */
  127. public String roomType() {
  128. System.out.println("\nChoose option:\n1- Single Room\n2- Double Room\n3- Apartment");
  129. int option = input.nextInt();
  130. String s = "";
  131. switch (option) {
  132. case 1: s = "Single room"; break;
  133. case 2: s = "Double room"; break;
  134. case 3: s = "Apartment"; break;
  135. }
  136. return s;
  137. }
  138.  
  139. /* @author Jasmin Bektic
  140. *
  141. * Method checking available user name */
  142. public String checkAvailableUserName() throws SQLException {
  143. ArrayList<String> li = new ArrayList<>();
  144. Statement s = getConnection().createStatement();
  145. s.executeQuery("SELECT UserName FROM information");
  146. ResultSet rs = s.getResultSet();
  147. System.out.println("Assign username:");
  148. String user = input.next();
  149.  
  150. //Transfering user names from database to array
  151. while (rs.next()) {
  152. String str = rs.getString("UserName");
  153. li.add(str);
  154. }
  155. rs.close ();
  156. s.close ();
  157.  
  158. //Compare user name to names from array
  159. for (int i = 0; i < li.size(); i++) {
  160. if (user.equals(li.get(i))) {
  161. System.out.println("User name already exist, try another one:");
  162. user = input.next();
  163. i = 0;
  164. }
  165. }
  166. return user;
  167. }
  168.  
  169. /* @author Maja Vasilic
  170. *
  171. * providing information from database based on entry */
  172. public void searchDatabase(String s) throws Exception {
  173. try {
  174. // establish connection to the database invoking method
  175. Connection con = getConnection();
  176. // var query with SQL query
  177. String query = "SELECT * FROM hotel.information WHERE FirstName = ? OR IdentityCard= ? OR UserName = ?";
  178. // create statement
  179. PreparedStatement statement = con.prepareStatement(query);
  180.  
  181. // actual values are set to parameters
  182. statement.setString(1, s);
  183. statement.setString(2, s);
  184. statement.setString(3, s);
  185. //query execution
  186. ResultSet result = statement.executeQuery();
  187. ResultSetMetaData rsmd = result.getMetaData();
  188. int columnCount = rsmd.getColumnCount();
  189. String format = "%-20s";
  190.  
  191. // print column names
  192. for (int i = 1; i <= columnCount; i++) {
  193. System.out.printf(format, rsmd.getColumnLabel(i));
  194. }
  195. System.out.println();
  196. // print underline
  197. for (int i = 1; i <= columnCount * 20; i++) {
  198. System.out.print("_");
  199. }
  200. System.out.println();
  201. // print information from database
  202. while (result.next()) {
  203. for (int i = 1; i <= columnCount; i++) {
  204. System.out.printf(format, result.getString(i));
  205. }
  206. System.out.println();
  207. }
  208. } catch (Exception e) {
  209. System.out.println(e);
  210. }
  211. }
  212.  
  213. /* @author Jasmin Bektic
  214. *
  215. * Method checking for online users */
  216. public void checkOnlineStatus() {
  217. printStatus();
  218. while (true) {
  219. System.out.println();
  220. System.out.println("Log out user choosing No:");
  221. int num = input.nextInt();
  222.  
  223. //Option conditions and print status
  224. if (num > 0 && num < list.size() + 1) {
  225. list.remove(num - 1);
  226. printStatus();
  227. } else if (num == 0) {
  228. //metoda MENU
  229. } else if (num == list.size() + 1) {
  230. for (int i = list.size() - 1; i >= 0; i--) {
  231. list.remove(i);
  232. }
  233. printStatus();
  234. } else if (num == list.size() + 2) {
  235. System.exit(0);
  236. }
  237. }
  238. }
  239.  
  240. /* Online status print method */
  241. public void printStatus() {
  242. System.out.println("\nNo\tUserName");
  243. System.out.println("-----------------");
  244. for (int i = 1; i <= list.size(); i++) {
  245. System.out.println(i + "\t" + list.get(i - 1));
  246. }
  247. if (list.size() == 0) {
  248. System.out.println("No users online!");
  249. }
  250. System.out.println("-----------------");
  251. System.out.println("0- Return to menu");
  252. System.out.println((list.size() + 1) + "- Log out all users");
  253. System.out.println((list.size() + 2) + "- Shut down");
  254. }
  255. }
Add Comment
Please, Sign In to add comment