Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void bookingDetails() {
- boolean valid = false;
- String familyName;
- String givenName;
- String input;
- LocalDateTime bookingSlot = LocalDateTime.now();
- int reqWorkStations;
- int reqBreakoutSeats;
- do{
- do{
- System.out.println("Enter your family name: ");
- valid = false;
- familyName = in.nextLine();
- if(familyName.matches("[A-Z][a-z]*" )){
- valid = true;
- }else {
- valid = false;
- System.out.println("Invalid family name, your name must start with a capital letter. Please try again: ");
- }
- }while(!valid);
- System.out.println("Enter your given name: ");
- do{
- valid = false;
- givenName = in.nextLine();
- if(givenName.matches("[A-Z][a-z]*" )){
- valid = true;
- }else {
- valid = false;
- System.out.println("Invalid given name, your name must start with a capital letter. Please try again: ");
- }
- }while(!valid);
- if(!clientExists(familyName, givenName)){
- System.out.println("Client does not exist.");
- return;
- }
- }while(!valid);
- System.out.println("Enter the date you want to book the room for in the format YYYY-MM-DD :");
- do{
- valid = false;
- input = in.nextLine();
- if(input.matches("\\d{4}-\\d{2}-\\d{2}")){
- try{
- 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
- format.setLenient(false); //if a date is entered that doesnt exist throw an exception
- Date date = format.parse(input); //date is equal to the input parsed to the format above
- bookingSlot = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); //convert date to local date time
- if(bookingSlot.isBefore(LocalDate.now().atTime(0, 0,0))){
- System.out.println("You cannot enter a date in the past. Please enter a future date: ");
- valid = false;
- }else{
- valid = true;
- }
- }catch (Exception e){ //catch the exception
- e.printStackTrace();//if the exception is thrown it is due to illegal date entered
- }
- }else{
- System.out.println("Incorrect format, please try again: ");
- }
- }while(!valid);
- System.out.println("Enter the time you want to book the room for in the format HH:MM");
- do{
- valid = false;
- input = in.nextLine();
- if(input.matches("^(0[0-9]|1[0-9]|2[0-3]|[0-9]):[0-5][0-9]$")) {
- if (bookingSlot.withHour(Integer.parseInt(input.substring(0, 2))).withMinute(Integer.parseInt(input.substring(3, 5))).isBefore(LocalDateTime.now())) {
- System.out.println("You cannot enter a time in the past to book a room, please enter a valid time: ");
- valid = false;
- } else {
- valid = true;
- }
- }else{
- System.out.println("Invalid time entered, please enter a time in the format of HH:MM: ");
- valid = false;
- }
- }while(!valid);
- System.out.println("How many work stations do you require: ");
- do{
- valid = false;
- try(reqWorkStations = in.nextInt()){
- }
- }while(!valid);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement