Advertisement
Guest User

Hotel Reservation Program

a guest
May 11th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.38 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.FileReader;
  4. import java.util.Scanner;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Arrays;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import java.util.Collection;
  11. import java.util.Collections;
  12. import java.util.Iterator;
  13. import java.util.Random;
  14.  
  15. public class HotelProject {
  16.  
  17. ArrayList<Guest> guestList = new ArrayList();
  18. ArrayList<Room> roomList = new ArrayList();
  19. Room selectedRoom;
  20. boolean loggedIn = false;
  21. Guest g;
  22. public static void main(String[] args) {
  23. System.out.println("\n========================== WELCOME TO OUR RESERVATION SYSTEM ============================");
  24. HotelProject hp = new HotelProject();
  25. hp.readInGuests();
  26. System.out.println(" Our hotel currently has " + hp.guestList.size() + " members");
  27. hp.readInRooms();
  28. System.out.println(" We offer " + hp.roomList.size() + " types of room");
  29. System.out.println("=========================================================================================");
  30. hp.processHotelProject();
  31. }
  32.  
  33. public void processHotelProject() {
  34. while (true) {
  35. System.out.println("=========================================================================================");
  36. System.out.println("Please Log In To Begin");
  37. do {
  38. System.out.println("Username:");
  39. Scanner scanner = new Scanner(System.in);
  40. String username = scanner.next();
  41. System.out.println("Password:");
  42. String password = scanner.next();
  43. g = verifyLogin(username, password);
  44. } while (g == null);
  45. System.out.println("Successfully Logged in. Thank you.");
  46. System.out.println("To make sure you are not a robot please re-enter number below");
  47. Random rand = new Random();
  48. int randomNum = rand.nextInt((10000 - 1) + 1) + 1;
  49. System.out.println(randomNum);
  50. Scanner scanner = new Scanner(System.in);
  51. String random = scanner.next();
  52. if (random.equals(randomNum + "")) {
  53. System.out.println("Welcome " + g.getPrefix() + ". " + g.getLastName() + " Your balance is: $" + g.getBalance());
  54. do{
  55. int selection = processSelection();
  56. if(selection == 1){
  57. processBooking();
  58. } else {
  59. System.out.println("Thank you " + g.getPrefix() + ". " + g.getLastName() + " for using our system and Good Bye.");
  60. g = null;
  61. selectedRoom = null;
  62. break;
  63. }
  64.  
  65. } while(true);
  66.  
  67. } else {
  68. System.out.println("You are a robot! Bye Bye.");
  69. break;
  70. }
  71. }
  72.  
  73. }
  74.  
  75. public void processBooking(){
  76.  
  77. String selection = printAvailableOccupancies();
  78. if(selection.equals("x")){
  79. System.out.println("Thank you " + g.getPrefix() + ". " + g.getLastName() + " for using our system and Good Bye.");
  80. } else {
  81. selection = printAvailableRoomTypes(selection);
  82. if(selection.equals("x")){
  83. System.out.println("Thank you " + g.getPrefix() + ". " + g.getLastName() + " for using our system and Good Bye.");
  84. } else {
  85. // System.out.println("proceeding");
  86. boolean flag = confirmSelection();
  87. if(flag){
  88. g.setBalance(g.getBalance() - selectedRoom.getPrice());
  89. deductFromAvailable();
  90. System.out.println("Thank you for your booking. Your balance is now: $" + g.getBalance());
  91. do{
  92. System.out.println("Would you like to add Breakfast to your reservation at $25 per person?");
  93. System.out.println("This would add total of $" + (25*selectedRoom.getMaxAllowed()));
  94. System.out.println("select 1 to purchase");
  95. System.out.println("x to exit");
  96. Scanner scanner = new Scanner(System.in);
  97. selection = scanner.next();
  98. if (!selection.equals("x") && ! selection.equals("1")) {
  99. System.out.println("INVALID SELECTION PLEASE REENTER.");
  100. } else {
  101. if( selection.equals("1") ){
  102. g.setBalance(g.getBalance() - ((25*selectedRoom.getMaxAllowed())));
  103. }
  104. System.out.println("Thank you for your purchase. Your balance is now: $" + g.getBalance());
  105. break;
  106. }
  107.  
  108. }while(true);
  109.  
  110. } else {
  111. System.out.println("No reservation has been made. We are looking forward for your next reservations. Have a Great Day!");
  112. }
  113. }
  114. }
  115. }
  116.  
  117. public void deductFromAvailable(){
  118. for(int x = 0; x < roomList.size(); x++){
  119. Room availableRoom = roomList.get(x);
  120. if(availableRoom == selectedRoom){
  121. // System.out.println("found:" + availableRoom.getNumberOfRooms());
  122. availableRoom.setNumberOfRooms(availableRoom.getNumberOfRooms()-1);
  123. }
  124. }
  125. }
  126.  
  127. public boolean confirmSelection(){
  128. String selection;
  129. do{
  130. if(selectedRoom != null){
  131. System.out.println("The room you selected is $" + selectedRoom.getPrice() + " per night.");
  132. System.out.println("choose 1 to book it");
  133. System.out.println("choose x to exit");
  134. }
  135. Scanner scanner = new Scanner(System.in);
  136. selection = scanner.next();
  137. if (!selection.equals("x") && ! selection.equals("1")) {
  138. System.out.println("INVALID SELECTION PLEASE REENTER.");
  139. } else{
  140. break;
  141. }
  142.  
  143. } while (true);
  144.  
  145. return selection.equals("1");
  146. }
  147.  
  148. public String printAvailableRoomTypes(String str){
  149. int numOfOccupants = new Integer(str).intValue();
  150. String selection;
  151. do {
  152. HashMap hm = new HashMap();
  153. System.out.println("Choose type of room below:");
  154. for(int x = 0; x < roomList.size(); x++){
  155. Room room = roomList.get(x);
  156. if(room.getNumberOfRooms() > 0 && room.getMaxAllowed()==numOfOccupants){
  157. System.out.println(x+1 + " for " + room.getViewType() + " view( " + room.getNumberOfRooms() + " currently available)");
  158. hm.put(new Integer(x+1), room);
  159. }
  160. }
  161. System.out.println("x to exit");
  162. Scanner scanner = new Scanner(System.in);
  163. selection = scanner.next();
  164. if (!selection.equals("x") && ! hm.containsKey(new Integer(selection))) {
  165. System.out.println("INVALID SELECTION PLEASE REENTER.");
  166.  
  167. } else{
  168. if(!selection.equals("x")){
  169. selectedRoom = (Room)hm.get(new Integer(selection));
  170. }
  171. break;
  172. }
  173.  
  174. } while( true);
  175.  
  176. return selection;
  177.  
  178. }
  179.  
  180. public String printAvailableOccupancies(){
  181. String selection;
  182. Map<Integer, Integer> map = new HashMap<Integer, Integer>();
  183.  
  184. for(int x = 0; x < roomList.size(); x++){
  185.  
  186. if(roomList.get(x).getNumberOfRooms() > 0){
  187. map.put(new Integer((roomList.get(x)).getMaxAllowed()).intValue(), new Integer((roomList.get(x)).getMaxAllowed()).intValue());
  188. }
  189.  
  190. }
  191.  
  192. Collection<Integer> unsorted = map.values();
  193.  
  194. Object[] intsArr = unsorted.toArray();
  195. do {
  196. System.out.print("Currently we offer rooms for ");
  197. for (int x = 0; x < intsArr.length; x++) {
  198. System.out.print(((Integer) intsArr[x]).intValue());
  199. if (x < intsArr.length - 2) {
  200. System.out.print(", ");
  201. } else if (x < intsArr.length - 1) {
  202. System.out.print(" or ");
  203. }
  204. }
  205. System.out.println(" people. Please type");
  206. for (int x = 0; x < intsArr.length; x++) {
  207. System.out.println(((Integer) intsArr[x]).intValue() + " to choose room for " + ((Integer) intsArr[x]).intValue());
  208.  
  209. }
  210. System.out.println("x to exit");
  211. Scanner scanner = new Scanner(System.in);
  212. selection = scanner.next();
  213. if (!selection.equals("x") && !checkArrayForSelection(selection, intsArr)) {
  214. System.out.println("INVALID SELECTION PLEASE REENTER.");
  215. } else{
  216. break;
  217. }
  218. } while (true);
  219. return selection;
  220. }
  221.  
  222. public boolean checkArrayForSelection(String str, Object[] intsArr ){
  223. for(int x = 0; x < intsArr.length; x++){
  224. int fromArr = ((Integer)intsArr[x]).intValue();
  225. if(str.equals(fromArr + "")){
  226. return true;
  227. }
  228. }
  229. return false;
  230. }
  231.  
  232.  
  233.  
  234. public int processSelection(){
  235. while ( true ) {
  236. printOptions();
  237. if( getNumberOfRoomsAvailable() > 0){
  238. Scanner scanner = new Scanner(System.in);
  239. String selection = scanner.next();
  240. if (selection.equals("1") || selection.equals("2")) {
  241. return new Integer(selection).intValue();
  242. } else {
  243. System.out.println("We do not recognize this selection please try again");
  244. }
  245. } else {
  246. break;
  247. }
  248.  
  249. }
  250. return 2;
  251. }
  252.  
  253. public void printOptions(){
  254. if( getNumberOfRoomsAvailable() > 0){
  255. System.out.println("-----------------------------------------------------------------------------------------");
  256. //System.out.println("=========================================================================================");
  257. System.out.println("Please choose from the options below:");
  258. System.out.println("Type 1 to book a room");
  259. System.out.println("Type 2 to exit");
  260. } else {
  261. System.out.println("We are currently completely booked. Please check back soon! Good Bye.");
  262. }
  263.  
  264. }
  265.  
  266. public int getNumberOfRoomsAvailable(){
  267. int available = 0;
  268. for(int x = 0; x < roomList.size(); x++){
  269. available += roomList.get(x).getNumberOfRooms();
  270. }
  271. return available;
  272. }
  273.  
  274. public void readInGuests() {
  275.  
  276. try {
  277.  
  278. FileReader fr = new FileReader("./guests.txt");
  279. BufferedReader br = new BufferedReader(fr);
  280. String line;
  281. while ( ( line = br.readLine()) != null) {
  282.  
  283.  
  284. List<String> fields = Arrays.asList(line.split(","));
  285. Guest g = new Guest();
  286. g.setFirstName(fields.get(0));
  287. g.setLastName(fields.get(1));
  288. g.setUsername(fields.get(2));
  289. g.setPassword(fields.get(3));
  290. g.setPrefix(fields.get(4));
  291. g.setBalance(new Double(fields.get(5)).doubleValue());
  292.  
  293. guestList.add(g);
  294. }
  295. } catch(Exception e){
  296. System.out.println("Exception:" + e.toString());
  297. }
  298. }
  299.  
  300. public void readInRooms() {
  301.  
  302. try {
  303.  
  304. FileReader fr = new FileReader("./rooms.txt");
  305. BufferedReader br = new BufferedReader(fr);
  306. String line;
  307. while ( ( line = br.readLine()) != null) {
  308.  
  309.  
  310. List<String> fields = Arrays.asList(line.split(","));
  311. Room r = new Room();
  312. r.setMaxAllowed(new Integer(fields.get(0)).intValue());
  313. r.setNumberOfRooms(new Integer(fields.get(1)).intValue());
  314. r.setViewType(fields.get(2));
  315. r.setPrice(new Double(fields.get(3)).doubleValue());
  316.  
  317. roomList.add(r);
  318. }
  319. } catch(Exception e){
  320. System.out.println("Exception:" + e.toString());
  321. }
  322. }
  323.  
  324. public Guest verifyLogin(String username, String password) {
  325. for(int x = 0; x < guestList.size(); x++){
  326. Guest guest = guestList.get(x);
  327. if (username.equals(guest.getUsername()) && password.equals(guest.getPassword())) {
  328. return guest;
  329. }
  330. }
  331. System.out.println("Username/Password incorrect please try again");
  332. return null;
  333.  
  334. }
  335.  
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement