• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

LocalDate objects how to check if a start and end Date overlaps with another?

 
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,

Right now I have a logic Problem where I am stuck.

Situation : I try to compare two existing date objects with a wanted date. It is for a fun Project dealing with the Situation to rent a car.

I have two Date objects - one for startdate to rent and one for enddate
I put this Dates as instance fields in an ArrayList
something Like :



Now I try to iter through that Date ArrayList and check if that new start date is already in there if yes the for loop should break

The Code I wrote looks like this:



Example I want to rent a car
start date 2019-06-12
end date 2019-06-15
But the car is of Course rented on several days

So if the car has a RENT_START date e.g 2019-06-11
and an existing RENT_END date e.g. 2019-06-14

I won't be able to rent it cause of the overlapping days 2019-06-12 until  2019-06-14  <--- this is exactly what I try to avoid by checking the car availiability  

Anybody who has a clue how i can solve this or an idea if there is already a method I can use ??

Sorry for my english, I tried to do my best  
 
Saloon Keeper
Posts: 10528
224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Aleana,

The problem is that you're not dividing your responsibilities properly. You're making whatever class is responsible for reservations also responsible for date manipulation. Even worse, you're doing it by keeping track of start and ending dates in parallel collections.

Instead, you should create a class that represents a range of dates:

Now, you can also easily create reservations:
 
Aleana Cologne
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aleana Cologne wrote:





Problem is solved  

It was easier as i thought  

Thanks anyway
 
Aleana Cologne
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steven,
that's awsome of you providing me that much Information.
In fact never thought doing it this way.  But sure I'll try it that way .
It Looks much more easy to maintain than my Code does, but to be honest I thought I give my students a small Project to practice coding with several classes using interfaces and Abstract class as well as inheritance. So I wrote a Little Task to get them started.

Because I thought oh it might be nice doing some practicing myself as well i started just from Scratch and then it became a selfplaying Situation where  this might be nice in there as well and the formely Little Project grows and grows ….
.. and yet i have a lot more ideas to realize in that Project so your Code will definetely be used in there as well .

Thank you so much
 
Stephan van Hulst
Saloon Keeper
Posts: 10528
224
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd still do well to refactor that code. It will be hard to maintain because:

  • You're using parallel lists. Instead, encapsulate parallel elements in objects and maintain a single list.
  • Use Java conventions for capitalization. Instance fields should be in camel-case, starting with a lower-case letter.
  • Your variable names are unreadable. Don't use names like fz, untilC, p or pf. Use descriptive names. Characters don't cost anything.
  • Put spaces after commas and semi-colons, not before, put spaces around operators, and put spaces before opening braces.
  • Put 'then'-clauses on a separate line from the if-condition.
  • If two 'else-if' statements have the same 'then'-clause, then just perform a union over their conditions with the || operator.
  • Use consistent indentation: Block statements with the same scope should be indented with the same whitespace.
  • Don't modify loop variables inside the loop. Instead of forcing i to the last index, use a break statement.
  • Use early returns instead of result variables: Get rid of the ok variable.
  • Don't declare variables before you use them.

  • If I rewrite your method to use proper formatting, it would look like this:

    I'm going to move declarations to to the proper scope, add early breaks and returns and 'or' conditions together:

    Let's use proper identifiers, and use the methods from LocalDate as they were intended:

    I hope you'll agree that this is much more readable. Now that I've cleaned it up a bit, I can immediately spot some bugs:

    On line 9, you're comparing a LocalDate to a List<LocalDate> using the equals() method. This will always return false, so in essence the numberOfDays parameter has absolutely no effect on the result.

    You're checking that the existing start date and the requested start date are equal, with is fine, but you're not checking if the existing start date is before the requested start date. Also, use LocalDate.isEqual(), not LocalDate.equals().

    If you encounter one pair of start and end dates that don't conflict with the requested date, isAvailable will be set to true. If later in the lists of dates there is a pair of dates that DO conflict with the requested date, lines 10 and 14 will break out of the loop, but the method will return true instead of false.

    There is no way that your method implementation can work. Here's a testcase that uses your original code:
     
    Aleana Cologne
    Greenhorn
    Posts: 11
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Steve,

    again it is absolutely great how much you try to help me and Point my nose to how Things could be done better.

    actually these  to Lists are inside a Vehicle Type that is inherit from several Vehicle objects ( Car and Transporter on the one side and a bycicle on the other) the abbreviations I used are definitely not easy to read because they make no sense in english ;) The Code is written in german
    ( Yes, I know all Code should be written in english  ) That's why I used fz for Fahrzeug in that way it makes sense but still you are Right to avoid this.

    The method works though even with my solution. But I will refactor the Code keeping an eye on your hints:


    •You're using parallel lists. Instead, encapsulate parallel elements in objects and maintain a single list.
    •Use Java conventions for capitalization. Instance fields should be in camel-case, starting with a lower-case letter.
    •Your variable names are unreadable. Don't use names like fz, untilC, p or pf. Use descriptive names. Characters don't cost anything.
    •Put spaces after commas and semi-colons, not before, put spaces around operators, and put spaces before opening braces.
    •Put 'then'-clauses on a separate line from the if-condition.
    •If two 'else-if' statements have the same 'then'-clause, then just perform a union over their conditions with the || operator.
    •Use consistent indentation: Block statements with the same scope should be indented with the same whitespace.
    •Don't modify loop variables inside the loop. Instead of forcing i to the last index, use a break statement.
    •Use early returns instead of result variables: Get rid of the ok variable.
    •Don't declare variables before you use them.


    and I aggree that the ok Variables are annoying, and your Information about early returns is very helpful, because I never tried them this way.

    I'll copy your code and will use it as a Code to look at how Things can be done more maintainable.

    Thank you so much for spendiing that much time solving my Problem.

     
    Marshal
    Posts: 65447
    248
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Aleana Cologne wrote:. . . Yes, I know all Code should be written in english . . .

    . . . but there is a risk of introducing new errors in the translating process, so it is often better to use copy'n'paste and keep the German/French/Spanish/Japanese/Tamil variable names.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 10528
    224
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Agreed, when posting code, post the actual original code, regardless of what language it's in.

    It's usually just best to write the original code in English, no matter how proud you are of your language. I started out writing code in Dutch and it got me into all sorts of troubles when asking people for help on message boards.

    Regardless, even when you write code in German, use variable names like fahrzeug, abholdatum and rueckgabedatum rather than fz, ahd or rgd.

    I'm really happy that as a teacher, you are performing research before giving your pupils an assignment. For that, have a cow!
     
    Campbell Ritchie
    Marshal
    Posts: 65447
    248
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:. . . when you write code in German, use variable names like fahrzeug, abholdatum and rueckgabedatum . . .

    There I was thinking, “That should read abholDatum and rueckGabeDatum,” but then I remembered that Deutsch allows almost infinite scope for concatenating words, so camelCase is alien to German grammar‍‍.
     
    Aleana Cologne
    Greenhorn
    Posts: 11
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    I'm really happy that as a teacher, you are performing research before giving your pupils an assignment. For that, have a cow!



    honestly I allways tell them while explaining variables to use speaking names  

    and I do aswell during lessons, but in Projects doing for myself i am a bit lazy    

    And I aggree that it is Always better to ask for help and not tell someone something it is like that only to avoid admitting not knowing something.

     
    I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!