Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. public void bookingDetails() {
  2.         boolean valid = false;
  3.         String familyName;
  4.         String givenName;
  5.         String input;
  6.         LocalDateTime bookingSlot = LocalDateTime.now();
  7.         int reqWorkStations;
  8.         int reqBreakoutSeats;
  9.  
  10.         do{
  11.             do{
  12.                 System.out.println("Enter your family name: ");
  13.                 valid = false;
  14.                 familyName = in.nextLine();
  15.                 if(familyName.matches("[A-Z][a-z]*" )){
  16.                     valid = true;
  17.                 }else {
  18.                     valid = false;
  19.                     System.out.println("Invalid family name, your name must start with a capital letter. Please try again: ");
  20.                 }
  21.             }while(!valid);
  22.  
  23.             System.out.println("Enter your given name: ");
  24.             do{
  25.                 valid = false;
  26.                 givenName = in.nextLine();
  27.  
  28.                 if(givenName.matches("[A-Z][a-z]*" )){
  29.                     valid = true;
  30.                 }else {
  31.                     valid = false;
  32.                     System.out.println("Invalid given name, your name must start with a capital letter. Please try again: ");
  33.                 }
  34.             }while(!valid);
  35.             if(!clientExists(familyName, givenName)){
  36.                 System.out.println("Client does not exist.");
  37.                 return;
  38.             }
  39.         }while(!valid);
  40.  
  41.         System.out.println("Enter the date you want to book the room for in the format YYYY-MM-DD :");
  42.         do{
  43.             valid = false;
  44.             input = in.nextLine();
  45.             if(input.matches("\\d{4}-\\d{2}-\\d{2}")){
  46.                 try{
  47.                     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); //document said use YYYY-MM-DD but for some reason that wouldnt work, but yyyy-MM-dd works and thats just the same format with different syntax so whatever should be fine
  48.                     format.setLenient(false); //if a date is entered that doesnt exist throw an exception
  49.                     Date date = format.parse(input); //date is equal to the input parsed to the format above
  50.                     bookingSlot = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); //convert date to local date time
  51.  
  52.                     if(bookingSlot.isBefore(LocalDate.now().atTime(0, 0,0))){
  53.                         System.out.println("You cannot enter a date in the past. Please enter a future date: ");
  54.                         valid = false;
  55.                     }else{
  56.                         valid = true;
  57.                     }
  58.                 }catch (Exception e){ //catch the exception
  59.                     e.printStackTrace();//if the exception is thrown it is due to illegal date entered
  60.                 }
  61.             }else{
  62.                 System.out.println("Incorrect format, please try again: ");
  63.             }
  64.         }while(!valid);
  65.  
  66.         System.out.println("Enter the time you want to book the room for in the format HH:MM");
  67.         do{
  68.             valid = false;
  69.             input = in.nextLine();
  70.             if(input.matches("^(0[0-9]|1[0-9]|2[0-3]|[0-9]):[0-5][0-9]$")) {
  71.  
  72.                 if (bookingSlot.withHour(Integer.parseInt(input.substring(0, 2))).withMinute(Integer.parseInt(input.substring(3, 5))).isBefore(LocalDateTime.now())) {
  73.                     System.out.println("You cannot enter a time in the past to book a room, please enter a valid time: ");
  74.                     valid = false;
  75.                 } else {
  76.                     valid = true;
  77.                 }
  78.  
  79.             }else{
  80.                 System.out.println("Invalid time entered, please enter a time in the format of HH:MM: ");
  81.                 valid = false;
  82.             }
  83.         }while(!valid);
  84.  
  85.         System.out.println("How many work stations do you require: ");
  86.         do{
  87.             valid = false;
  88.             try(reqWorkStations = in.nextInt()){
  89.  
  90.             }
  91.  
  92.         }while(!valid);
  93.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement