简体   繁体   中英

compare elements of same array java

I am working a recurring appointment functionality for a weekly recurrence. I have a start date, end date, recurrence start date, recurrence end date and selected Days( Mon, Tue, Wed, Thu ...Sun)

Here is an example

Start Date: 15th July 2015
End Date: 18th July 2015

Recurrence Start Date: 20th July 2015
Recurrence End Date: 20th August 2015

Recurrence frequency = weekly

Selected Days(Array storing int values for days_of_week with Sun as 1 and Sat as 7) = Mon, Wed, Sun 

As per the requirements I need to create an appointment like this :-

Appointment 1 - 20th July 2015 (Mon) - 23rd July 2015(Thu)
............Appointment 2 - 22nd July 2015 (Wed)  - 25th July 2015(Sat)
............Appointment 3 - 26th July 2015 (Sun) - 29th July 2015(Wed)
............Appointment 4 - 27th July 2015 (Mon) - 30th July 2015(Thu)

But as you can see there are overlaps which I need to prevent. I was trying to develop an algorithm to prevent this overlapping without actually knowing the actual days.

So basically the difference in no. days between start date and end date must be greater than the difference between two consecutive indexes of the array.

I get into problems since Sun - Wed ( 1 - 4) would give me a negative number so comparison which will be less than days difference between end date and start date ( end date - start date) .

This is what I have done so far :-

                        Calendar e = Calendar.getInstance();
                        Calendar f = Calendar.getInstance();
                        e.setTime(sStartDate);
                        f.setTime(estSignIn);

                        long diffInDays = ((estSignIn.getTime() - sStartDate.getTime()) 
                                / (1000 * 60 * 60 * 24) );


                        for(int j=0; j < localSelectedDays.length - 1 ; j++)
                        {   
                            e.set(Calendar.DAY_OF_WEEK, localSelectedDays[j]);
                            f.set(Calendar.DAY_OF_WEEK, localSelectedDays[j +1]);
                             int x = e.get(Calendar.DAY_OF_WEEK);
                             int y = f.get(Calendar.DAY_OF_WEEK);

                                 if ((y - x) <= diffInDays)
                                  { 
                                    System.out.println("ERROR" + "Y:" + y + "x" + x);
                                  }
                        }

You can use Math.abs() if you just want the difference in days.

I personally would not use the index numbers as part of the algorithm since one day you might want to change the data structure, but that is subjective.

But as you can see there are overlaps which I need to prevent. I was trying to develop an algorithm to prevent this overlapping without actually knowing the actual days

I've written some code on your requirement. In my opinion use the Calendar or Date class after & before methods to compare the input dates rather than comparing them using arrays.

Create a class AppointmentDetails which will store the appointment details. You can add extra properties in it like the person's name etc.

public class AppointmentDetails {

    private Calendar startDate;
    private Calendar endDate;
   //getters & setters
     public String toString()
      {
       return "startDate "+ this.getStartDate() + "endDate " + this.getEndDate();
      }

In the client class store the appointment details in a Hashmap

public class AppointmentClient {


    public static void main(String[] arg)
    {
        System.out.println("Get the appointment");
        //Sample data input for the first appointment
        Calendar startDate = new GregorianCalendar(2015, 1, 6);
        Calendar endDate = new GregorianCalendar(2015, 1, 9);

        AppointmentDetails details = new AppointmentDetails();
        details.setStartDate(startDate);
        details.setEndDate(endDate);

        //Details added to the hashmap with key as appointment & value as the class holding appointment details
        Map<String,AppointmentDetails> map = new    HashMap<String,AppointmentDetails>();
        map.put("Appointment 1", details);

        //Dates for new appointment
        Calendar startDate2 = new GregorianCalendar(2015, 1, 7);
        Calendar endDate2 = new GregorianCalendar(2015, 1, 9);

            //logic for validating if the appointment on the input date is already booked
            for(Map.Entry<String, AppointmentDetails> appDtl:map.entrySet())
            {
                System.out.println("Inside loop");
                //Every time the loop iterates it will return the respective appointment
                AppointmentDetails apptDetail = appDtl.getValue();
                System.out.println(apptDetail);
                if(((apptDetail.getStartDate().equals(startDate2))||(apptDetail.getEndDate().equals(endDate2)))|| 
                        (apptDetail.getStartDate().after(startDate2))&&(apptDetail.getEndDate().before(endDate2)))
                {
                     System.out.println("Your appointment is overlapping with the existing appointments");
                     break;
                }
            }
         }
      }

PS This is just a basic code, you can further customize it for efficiency & further logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM