Guest User

Untitled

a guest
Apr 8th, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.ArrayList;
  3.  
  4. public class HotelDAO {
  5. public static String hotelid ="G";
  6. public static String hoteltype="H";
  7. public static int hotelrating = 4;
  8. public static String loc = "R";
  9. public static int availableH = 1;
  10. public static int gym = 1;
  11. public static int pool = 0;
  12. public static int breakfast = 1;
  13. public static int lprice = 1;
  14. public static int hprice = 5;
  15. public static int Rbeds = 1;
  16. public static int Rwifi = 1;
  17. public static int RLprice = 100;
  18. public static int RHprice = 300;
  19. public static int availableR = 1;
  20. public static int rID;
  21.  
  22. public static ArrayList<Hotel> getAllHotels() throws SQLException{
  23. Connection con = DriverManager.getConnection("jdbc:sqlite:db/hotel.db");
  24. Statement stm = con.createStatement();
  25. ResultSet rs = stm.executeQuery("Select * from hotels");
  26. ArrayList<Hotel> hotelList = new ArrayList<>();
  27. while(rs.next()){
  28. Hotel hot = new Hotel();
  29. hot.setName(rs.getString("name"));
  30. hot.setType(rs.getString("type"));
  31. hot.setRating(rs.getInt("rating"));
  32. hot.setLocation(rs.getString("location"));
  33. hot.setNoRooms(rs.getInt("noRooms"));
  34. hot.setGym(rs.getInt("gym"));
  35. hot.setPool(rs.getInt("pool"));
  36. hot.setBreakfast(rs.getInt("breakfast"));
  37. hot.setPricerange(rs.getInt("pricerange"));
  38. hotelList.add(hot);
  39. }
  40. return hotelList;
  41. }
  42. public static ArrayList<Hotel> getFilteredHotels()throws SQLException{
  43. if(hotelrating>5 || hotelrating < 1)
  44. {
  45. hotelrating = 0;
  46. }
  47. if(availableH < 1)
  48. {
  49. availableH = -1;
  50. }
  51. Connection con = DriverManager.getConnection("jdbc:sqlite:db/hotel.db");
  52. ArrayList<Hotel> filteredHList = new ArrayList<>();
  53. String sql = "Select * from hotels where name Like ? AND type Like ? AND rating = ? AND location Like ? AND noRooms >= ? AND gym = ? AND pool = ? AND breakfast = ? AND pricerange >= ? AND pricerange <= ? ";
  54. PreparedStatement pst = null;
  55. ResultSet rs = null;
  56. try{
  57. pst = con.prepareStatement(sql);
  58. pst.setString(1, hotelid+"%");
  59. pst.setString(2, hoteltype+"%");
  60. pst.setInt(3, hotelrating);
  61. pst.setString(4, loc+"%");
  62. pst.setInt(5, availableH);
  63. pst.setInt(6, gym);
  64. pst.setInt(7, pool);
  65. pst.setInt(8, breakfast);
  66. pst.setInt(9, lprice);
  67. pst.setInt(10,hprice);
  68. rs = pst.executeQuery();
  69. while(rs.next()){
  70. Hotel hot = new Hotel();
  71. hot.setName(rs.getString("name"));
  72. hot.setType(rs.getString("type"));
  73. hot.setRating(rs.getInt("rating"));
  74. hot.setLocation(rs.getString("location"));
  75. hot.setNoRooms(rs.getInt("noRooms"));
  76. hot.setGym(rs.getInt("gym"));
  77. hot.setPool(rs.getInt("pool"));
  78. hot.setBreakfast(rs.getInt("breakfast"));
  79. hot.setPricerange(rs.getInt("pricerange"));
  80. filteredHList.add(hot);
  81. }
  82. }
  83. catch (SQLException e){
  84. e.printStackTrace();
  85. }
  86. return filteredHList;
  87. }
  88.  
  89. public static ArrayList<Room> getFilteredRooms() throws SQLException{
  90. Connection con = DriverManager.getConnection("jdbc:sqlite:db/hotel.db");
  91. ArrayList<Room> filteredRList = new ArrayList<>();
  92. String sql = "Select * from rooms where hotelId like (Select name from hotels where name LIKE ? AND type Like ? AND rating = ? AND location Like ? AND noRooms >= ? AND gym = ? AND pool = ? AND breakfast = ? AND pricerange >= ? AND pricerange <= ?) AND beds = ? AND wifi = ? AND price >= ? AND price <= ? AND availableR = ? ";
  93. PreparedStatement pst = null;
  94. ResultSet rs = null;
  95. try{
  96. pst = con.prepareStatement(sql);
  97. pst.setString(1, hotelid+"%");
  98. pst.setString(2, hoteltype+"%");
  99. pst.setInt(3, hotelrating);
  100. pst.setString(4, loc+"%");
  101. pst.setInt(5, availableH);
  102. pst.setInt(6, gym);
  103. pst.setInt(7, pool);
  104. pst.setInt(8, breakfast);
  105. pst.setInt(9, lprice);
  106. pst.setInt(10,hprice);
  107. pst.setInt(11, Rbeds);
  108. pst.setInt(12, Rwifi);
  109. pst.setInt(13, RLprice);
  110. pst.setInt(14, RHprice);
  111. pst.setInt(15, availableR);
  112. rs = pst.executeQuery();
  113. while (rs.next()){
  114. Room ro = new Room();
  115. ro.setHotelId(rs.getString("hotelId"));
  116. ro.setBeds(rs.getInt("beds"));
  117. ro.setWifi(rs.getInt("wifi"));
  118. ro.setPrice(rs.getInt("price"));
  119. ro.setAvailableR(rs.getInt("availableR"));
  120. ro.setRoomId(rs.getInt("roomId"));
  121. filteredRList.add(ro);
  122. }
  123. }
  124. catch (SQLException e){
  125. e.printStackTrace();
  126. }
  127. return filteredRList;
  128. }
  129.  
  130. public static void bokaHandler(int rId, String hId) throws SQLException{
  131. Connection con = DriverManager.getConnection("jdbc:sqlite:db/hotel.db");
  132. String sql = "UPDATE rooms SET availableR = 0 WHERE roomId = ? AND hotelId = ?";
  133. PreparedStatement pst = null;
  134. try {
  135. pst = con.prepareStatement(sql);
  136. pst.setInt(1, rId);
  137. pst.setString(2, hId);
  138. } catch (SQLException e) {
  139. e.printStackTrace();
  140. }
  141. minnkaHotel(hId);
  142. }
  143.  
  144. public static void afbokaHandler(int rId, String hId) throws SQLException{
  145. Connection con = DriverManager.getConnection("jdbc:sqlite:db/hotel.db");
  146. String sql = "UPDATE rooms SET availableR = 1 WHERE roomId = ? AND hotelId = ?";
  147. PreparedStatement pst = null;
  148. try {
  149. pst = con.prepareStatement(sql);
  150. pst.setInt(1, rId);
  151. pst.setString(2, hId);
  152. } catch (SQLException e) {
  153. e.printStackTrace();
  154. }
  155. }
  156.  
  157. public static void minnkaHotel(String hId) throws SQLException{
  158. Connection con = DriverManager.getConnection("jdbc:sqlite:db/hotel.db");
  159. String sql = "UPDATE hotels SET noRooms = noRooms - 1 WHERE name = ?";
  160. PreparedStatement pst = null;
  161. try {
  162. pst = con.prepareStatement(sql);
  163. pst.setString(1, hId);
  164. } catch (SQLException e) {
  165. e.printStackTrace();
  166. }
  167. }
  168.  
  169.  
  170. }
Add Comment
Please, Sign In to add comment