简体   繁体   English

比较相同数组java的元素

[英]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) . 我遇到了问题,因为Sun - Wed ( 1 - 4)会给我一个负数,因此比较将小于结束日期和开始日期( 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. 如果只需要天数差异,则可以使用Math.abs()。

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. 我认为使用Calendar或Date类的afterbefore方法来比较输入日期,而不是使用数组比较它们。

Create a class AppointmentDetails which will store the appointment details. 创建一个类AppointmentDetails,它将存储约会详细信息。 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 在客户端类中,将约会详细信息存储在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. PS这只是一个基本代码,您可以进一步自定义它,以提高效率和逻辑性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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