Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 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.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10. import java.util.Scanner;
  11.  
  12. public class Admin {
  13. public int ID, age, roomNum, balance;
  14. public String firstName, lastName, gender, identityCard, userName, password, roomType;
  15. public String checkInTime, checkOutTime, service, loggedIn;
  16. Scanner input = new Scanner(System.in);
  17.  
  18. public Admin() {
  19.  
  20. }
  21.  
  22.  
  23. /* @author Jasmin Bektic
  24. *
  25. * Method confirms connection */
  26. public Connection getConnection() {
  27. try {
  28. String driver = "com.mysql.jdbc.Driver";
  29. String url = "jdbc:mysql://localhost:3306/hotel";
  30. String user = "root";
  31. String pass = "";
  32. Class.forName(driver);
  33.  
  34. //Connecting with provided url,userName and pass
  35. Connection con = DriverManager.getConnection(url, user, pass);
  36. return con;
  37. } catch (Exception e) { //Catch errors
  38. System.out.println(e);
  39. }
  40. return null;
  41. }
  42.  
  43. public void checkGuest() {
  44.  
  45. }
  46.  
  47. public void enterInfo() throws SQLException {
  48. firstName = "John";
  49. lastName = "Snow";
  50. gender = "m";
  51. identityCard = "123456";
  52. age = 29;
  53. roomNum = checkAvailableRoom();
  54. roomType = roomType();
  55. checkInTime = "14:05";
  56. userName = checkAvailableUserName();
  57. password = "123456";
  58.  
  59. Connection con = getConnection();
  60. String sql = "INSERT INTO information (FirstName, LastName, Gender, IdentityCard, Age, RoomNumber, RoomType, CheckInTime, UserName, Password) VALUES ('"+firstName+"', '"+lastName+"', '"+gender+"', '"+identityCard+"', "+age+", "+roomNum+",'"+roomType+"','"+checkInTime+"','"+userName+"','"+password+"')";
  61. PreparedStatement statement = con.prepareStatement(sql);
  62.  
  63. statement.executeUpdate();
  64. }
  65.  
  66. /* @author Davor Sadikovic
  67. *
  68. * Method for checking available rooms */
  69. public int checkAvailableRoom() throws SQLException {
  70. Statement s = getConnection().createStatement ();
  71. s.executeQuery("SELECT RoomNumber FROM information");
  72. ResultSet rs = s.getResultSet();
  73. System.out.print("Occupied room numbers: ");
  74.  
  75. //Listing occupied rooms
  76. while (rs.next()) {
  77. int room = rs.getInt("RoomNumber");
  78. System.out.print(room + " ");
  79. }
  80. System.out.println();
  81. rs.close ();
  82. s.close ();
  83.  
  84. //Assign room number for a guest
  85. System.out.println("Enter room number for a guest:");
  86. int num = input.nextInt();
  87. return num;
  88. }
  89.  
  90. /* @author Jasmin Bektic
  91. *
  92. * Assign room type for guest */
  93. public String roomType() {
  94. System.out.println("\nChoose option:\n1- Single Room\n2- Double Room\n3- Apartment");
  95. int option = input.nextInt();
  96. String s = "";
  97. switch (option) {
  98. case 1: s = "Single Room"; break;
  99. case 2: s = "Double Room"; break;
  100. case 3: s = "Apartment"; break;
  101. }
  102. return s;
  103. }
  104.  
  105. /* @author Jasmin Bektic
  106. *
  107. * Method checking available user name */
  108. public String checkAvailableUserName() throws SQLException {
  109. ArrayList<String> list = new ArrayList<>();
  110. Statement s = getConnection().createStatement ();
  111. s.executeQuery("SELECT UserName FROM information");
  112. ResultSet rs = s.getResultSet();
  113. System.out.println("Assign user name:");
  114. String user = input.next();
  115.  
  116. //Transfering user names from database to array
  117. while (rs.next()) {
  118. String str = rs.getString("UserName");
  119. list.add(str);
  120. }
  121. rs.close ();
  122. s.close ();
  123.  
  124. //Compare user name to names from array
  125. for (int i = 0; i < list.size(); i++) {
  126. if (user.equals(list.get(i))) {
  127. System.out.println("User name already exist, try another one:");
  128. user = input.next();
  129. i = 0;
  130. }
  131. }
  132. return user;
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement