Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Hotel {
  5. private int numOfUsedRooms;
  6. private Room[][] allRooms;
  7. private int numOfFloors;
  8. private int totalRooms;
  9. private int numOfRooms;
  10.  
  11. public Hotel(int floors, int roomsPerFloor) {
  12. this.allRooms = new Room[floors][roomsPerFloor];
  13. this.numOfFloors = floors;
  14. this.numOfRooms = roomsPerFloor;
  15. this.numOfUsedRooms = 0;
  16. for (int i = 0; i < floors; i++) {
  17. for (int j = 0; j < roomsPerFloor; j++) {
  18. int numOfBeds = (int) ((Math.random() * 3) + 1);
  19. this.allRooms[i][j] = new Room(numOfBeds);
  20. this.totalRooms++;
  21. }
  22. }
  23. }
  24.  
  25. public Hotel(String fileName) throws FileNotFoundException {
  26. File f = new File(fileName);
  27. Scanner sc = new Scanner(f);
  28. this.numOfUsedRooms = sc.nextInt();
  29. this.numOfFloors = sc.nextInt();
  30. this.numOfRooms = sc.nextInt();
  31. this.allRooms = new Room[this.numOfFloors][this.numOfRooms];
  32. for (int i = 0; i < this.numOfFloors; i++) {
  33. for (int j = 0; j < this.numOfRooms; j++) {
  34. int numOfBeds = sc.nextInt();
  35. this.allRooms[i][j] = new Room(numOfBeds);
  36. // this.allRooms[i][j].setNumOfGuests(sc.nextInt());
  37. }
  38. }
  39. for (int i = 0; i < this.numOfFloors; i++) {
  40. int roomNum = sc.nextInt();
  41. this.allRooms[roomNum / 100][roomNum % 10] = new Room(sc);
  42. }
  43. sc.close();
  44.  
  45. }
  46.  
  47. public Hotel(Scanner sc) throws FileNotFoundException {
  48. this.numOfUsedRooms = sc.nextInt();
  49. this.numOfFloors = sc.nextInt();
  50. this.numOfRooms = sc.nextInt();
  51. this.allRooms = new Room[this.numOfFloors][this.numOfRooms];
  52. for (int i = 0; i < this.numOfFloors; i++) {
  53. for (int j = 0; j < this.numOfRooms; j++) {
  54. int numOfBeds = sc.nextInt();
  55. this.allRooms[i][j] = new Room(numOfBeds);
  56. }
  57. }
  58. for (int i = 0; i < this.numOfUsedRooms; i++) {
  59. int roomNum = sc.nextInt();
  60. allRooms[roomNum / 100][roomNum % 100] = new Room(sc);
  61. }
  62. }
  63.  
  64. public String guestLookUp(int passportNumber) {
  65. for (int i = 0; i < this.numOfFloors; i++) {
  66. for (int j = 0; j < this.numOfRooms; j++) {
  67. Room currentRoom = allRooms[i][j];
  68. if (currentRoom.getNumOfGuests() != 0) {
  69. for (int k = 0; k < currentRoom.getNumOfGuests(); k++) {
  70. if (currentRoom.getGuests()[k].getPassportNumber() == passportNumber) {
  71. return ("Guest found, resides in room: " + ((i * 100) + j) + ".");
  72. }
  73. }
  74. }
  75. }
  76. }
  77. return "Sorry, guest was not found in hotel registry.";
  78. }
  79.  
  80. public void addGuestGroup(Guest[] group) {
  81. boolean freeRoomFound = false;
  82. int floor = 0;
  83. int freeRoom = 0;
  84. for (; floor < this.numOfFloors; floor++) {
  85. freeRoom = freeRoomFinder(floor, group.length);
  86. if (freeRoom != 0) {
  87. freeRoomFound = true;
  88. break;
  89. }
  90. }
  91. if (freeRoomFound == true) {
  92. for (int i = 0; i < group.length; i++) {
  93. this.allRooms[floor][freeRoom].addGuest(group[i]);
  94. }
  95. this.numOfUsedRooms++;
  96. if (floor == 0) {
  97. System.out.println("Guests were assigned to room: 0" + freeRoom);
  98. } else {
  99. System.out.println("Guests were assigned to room: " + ((floor * 100) + freeRoom));
  100. }
  101. } else {
  102. System.out.println("No free rooms were found for the guests.");
  103. }
  104. }
  105.  
  106. public int freeRoomFinder(int floor, int numOfBedsRequired) {
  107. for (int i = 0; i < this.totalRooms; i++) {
  108. Room currentRoom = this.allRooms[floor][i];
  109. if ((currentRoom.getNumOfBeds() - currentRoom.getNumOfGuests()) >= numOfBedsRequired) {
  110. return i;
  111. }
  112. }
  113. return 0;
  114. }
  115.  
  116. public int numOfFreeRooms(int floor) {
  117. int counter = 0;
  118. for (int i = 0; i < this.numOfRooms; i++) {
  119. if (this.allRooms[floor][i].getNumOfGuests() == 0) {
  120. counter++;
  121. }
  122. }
  123. return counter;
  124. }
  125.  
  126. public int floorWithMaxFreeRooms() {
  127. int maxedFloor = 0;
  128. for (int i = 0; i < this.numOfFloors - 1; i++) {
  129. if (this.numOfFreeRooms(i) < this.numOfFreeRooms(i + 1)) {
  130. maxedFloor = i + 1;
  131. }
  132. }
  133. return maxedFloor;
  134. }
  135.  
  136. public void displayStats() {
  137. for (int i = 0; i < this.numOfFloors; i++) {
  138. int counter = 0;
  139. System.out.println("\nFloor " + i + ".");
  140. for (int j = 0; j < this.numOfRooms; j++) {
  141. if (allRooms[i][j].getNumOfGuests() != 0) {
  142. System.out.println("\nRoom #" + j + ":");
  143. counter++;
  144. for (int k = 0; k < allRooms[i][j].getNumOfGuests(); k++) {
  145. allRooms[i][j].displayGuest(allRooms[i][j].getGuests()[k]);
  146. }
  147. }
  148. }
  149. if (counter == 0) {
  150. System.out.println("Floor is empty.\n");
  151. }
  152. }
  153. }
  154.  
  155. public void saveHotel(PrintWriter pw) throws FileNotFoundException {
  156. pw.println(this.numOfUsedRooms);
  157. pw.println(this.numOfFloors);
  158. pw.println(this.numOfRooms);
  159.  
  160. for (int i = 0; i < this.numOfFloors; i++) {
  161. for (int j = 0; j < this.numOfRooms; j++) {
  162. pw.println(this.allRooms[i][j].getNumOfBeds());
  163. }
  164. }
  165.  
  166. for (int i = 0; i < this.numOfFloors; i++) {
  167. for (int j = 0; j < this.numOfRooms; j++) {
  168. if (this.allRooms[i][j].getNumOfGuests() != 0) {
  169. int num = (i * 100) + j;
  170. pw.println(num);
  171. this.allRooms[i][j].saveRoom(pw);
  172. }
  173. }
  174. }
  175.  
  176. pw.close();
  177.  
  178. }
  179.  
  180. public void saveHotel(String fileName) throws FileNotFoundException {
  181. File f = new File(fileName);
  182. PrintWriter pw = new PrintWriter(f);
  183. pw.println(this.numOfUsedRooms);
  184. pw.println(this.numOfFloors);
  185. pw.println(this.numOfRooms);
  186. for (int i = 0; i < this.numOfFloors; i++) {
  187. for (int j = 0; j < this.numOfRooms; j++) {
  188. pw.println(this.allRooms[i][j].getNumOfBeds());
  189. }
  190. }
  191. for (int i = 0; i < this.numOfFloors; i++) {
  192. for (int j = 0; j < this.numOfRooms; j++) {
  193. if (allRooms[i][j].getNumOfGuests() != 0) {
  194. int num = (i * 100) + j;
  195. pw.println(num);
  196. allRooms[i][j].saveRoom(pw);
  197. }
  198. }
  199. }
  200. pw.close();
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement