Advertisement
Guest User

Hotel Reservation Program

a guest
May 10th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.util.Scanner;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Arrays;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import java.util.Collection;
  10. import java.util.Collections;
  11. import java.util.Iterator;
  12. import java.util.Random;
  13.  
  14. public class HotelProject {
  15.  
  16. ArrayList<Guest> guestList = new ArrayList();
  17. ArrayList<Room> roomList = new ArrayList();
  18. boolean loggedIn = false;
  19. Guest g;
  20. public static void main(String[] args) {
  21. System.out.println("\n========================== WELCOME TO OUR RESERVATION SYSTEM ============================");
  22. HotelProject hp = new HotelProject();
  23. hp.readInGuests();
  24. System.out.println(" Our hotel currently has " + hp.guestList.size() + " members");
  25. hp.readInRooms();
  26. System.out.println(" We offer " + hp.roomList.size() + " types of room");
  27. System.out.println("=========================================================================================");
  28. hp.processHotelProject();
  29. }
  30.  
  31. public void processHotelProject() {
  32. while (true) {
  33. System.out.println("Please Log In To Begin");
  34. do {
  35. System.out.println("Username:");
  36. Scanner scanner = new Scanner(System.in);
  37. String username = scanner.next();
  38. System.out.println("Password:");
  39. String password = scanner.next();
  40. g = verifyLogin(username, password);
  41. } while (g == null);
  42. System.out.println("Successfully Logged in. Thank you.");
  43. System.out.println("To make sure you are not a robot please re-enter number below");
  44. Random rand = new Random();
  45. int randomNum = rand.nextInt((10000 - 1) + 1) + 1;
  46. System.out.println(randomNum);
  47. Scanner scanner = new Scanner(System.in);
  48. String random = scanner.next();
  49. if (random.equals(randomNum + "")) {
  50. System.out.println("Thank you " + g.getPrefix() + ". " + g.getLastName() + " Your balance is: $" + g.getBalance());
  51. System.out.println("HERE ADD ");
  52. int selection = processSelection();
  53. if(selection == 1){
  54. processBooking();
  55. } else {
  56. System.out.println("Thank you " + g.getPrefix() + ". " + g.getLastName() + " for using our system and Good Bye.");
  57. g = null;
  58. }
  59. } else {
  60. System.out.println("You are a robot! Bye Bye.");
  61. break;
  62. }
  63. }
  64.  
  65. }
  66.  
  67. public void processBooking(){
  68. System.out.println("Currently we offer rooms for ");
  69. printAvailableOccupancies();
  70. }
  71.  
  72. public void printAvailableOccupancies(){
  73. Map<Integer, Integer> map = new HashMap<Integer, Integer>();
  74.  
  75. for(int x = 0; x < roomList.size(); x++){
  76. System.out.println("adding max allowed:" + (roomList.get(x)).getMaxAllowed());
  77. map.put(new Integer((roomList.get(x)).getMaxAllowed()).intValue(), new Integer((roomList.get(x)).getMaxAllowed()).intValue());
  78. }
  79. System.out.println("ended with:" + map.size());
  80. Collection<Integer> unsorted = map.values();
  81. System.out.println("as values:" + unsorted.size());
  82. Object[] intsArr = unsorted.toArray();
  83. //List<Integer> sorted = Collections.sort(unsorted);
  84. //Iterator it = unsorted.iterator();
  85. for(int x = 0; x < intsArr.length; x++){
  86. System.out.print(((Integer)intsArr[x]).intValue());
  87. if(x < intsArr.length-2) System.out.print(", ");
  88. else if( x == intsArr.length-1 )System.out.println(" or ");
  89. }
  90. }
  91.  
  92.  
  93.  
  94. public int processSelection(){
  95. while ( true ) {
  96. printOptions();
  97. Scanner scanner = new Scanner(System.in);
  98. String selection = scanner.next();
  99. if (selection.equals("1") || selection.equals("2")) {
  100. System.out.println("returning " + selection);
  101. return new Integer(selection).intValue();
  102. } else {
  103. System.out.println("We do not recognize this selection please try again");
  104. }
  105. }
  106. }
  107.  
  108. public void printOptions(){
  109. System.out.println("Please choose your selection:");
  110. System.out.println("Type 1 to book a room");
  111. System.out.println("Type 2 to exit");
  112. }
  113.  
  114. public void readInGuests() {
  115.  
  116. try {
  117.  
  118. FileReader fr = new FileReader("./guests.txt");
  119. BufferedReader br = new BufferedReader(fr);
  120. String line;
  121. while ( ( line = br.readLine()) != null) {
  122.  
  123.  
  124. List<String> fields = Arrays.asList(line.split(","));
  125. Guest g = new Guest();
  126. g.setFirstName(fields.get(0));
  127. g.setLastName(fields.get(1));
  128. g.setUsername(fields.get(2));
  129. g.setPassword(fields.get(3));
  130. g.setPrefix(fields.get(4));
  131. g.setBalance(new Double(fields.get(5)).doubleValue());
  132.  
  133. guestList.add(g);
  134. }
  135. } catch(Exception e){
  136. System.out.println("Exception:" + e.toString());
  137. }
  138. }
  139.  
  140. public void readInRooms() {
  141.  
  142. try {
  143.  
  144. FileReader fr = new FileReader("./rooms.txt");
  145. BufferedReader br = new BufferedReader(fr);
  146. String line;
  147. while ( ( line = br.readLine()) != null) {
  148.  
  149.  
  150. List<String> fields = Arrays.asList(line.split(","));
  151. Room r = new Room();
  152. r.setMaxAllowed(new Integer(fields.get(0)).intValue());
  153. r.setNumberOfRooms(new Integer(fields.get(1)).intValue());
  154. r.setViewType(fields.get(2));
  155. r.setPrice(new Double(fields.get(3)).doubleValue());
  156.  
  157. roomList.add(r);
  158. }
  159. } catch(Exception e){
  160. System.out.println("Exception:" + e.toString());
  161. }
  162. }
  163.  
  164. public Guest verifyLogin(String username, String password) {
  165. for(int x = 0; x < guestList.size(); x++){
  166. Guest guest = guestList.get(x);
  167. if (username.equals(guest.getUsername()) && password.equals(guest.getPassword())) {
  168. return guest;
  169. }
  170. }
  171. System.out.println("Username/Password incorrect please try again");
  172. return null;
  173.  
  174. }
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement