-1

i have this situation : I am trying to do an excercise about the prenotation of a user for a cinema. A user can be only in one film at the moment, (but he can buy another ticket for the next show if he want). Every show has a start time and an end time ( so i can rapresent them with timestamp ). My question is: how can i add a controll that the user that is partecipating at one show cannot buy another ticket for another show at the same time? We can immagine a user with an unique id, and a cinema room with an Id. And a third object ( Show with an Id, startTime,EndTime). My question is not a lot about code implementation but much more about the logic.

A person can partecipate only one for a certain interval to a show.( he cannot partecipate at the same time in two different shows, cause he need to respect the startTime and the EndTime of the show). For some reason if the user try to buy another ticket for another show at the same place he cannot do it because he is suposed to follow a show at this interval.

lirio oliro
  • 21
  • 1
  • 7
  • 1
    Show us what you have done so far. Read [this](https://stackoverflow.com/help/mcve) – zyexal Dec 18 '17 at 11:07
  • 1
    It's not really clear what is it exactly that you want. The way you describe your idea is fine, but I don't see where you got stuck in it. Just write your code and come back when you have some specific problem we can discuss. – M. Prokhorov Dec 18 '17 at 12:27
  • Please search before asking and find a good answer faster than anyone can type one here. – Ole V.V. Dec 18 '17 at 16:27

2 Answers2

0

Implement something like a hook into your booking function. Before the booking will be done, a check if the user is allowed to book this specific show will be done. Only if this check if positive, the actual booking progress can be triggered. What ever this includes in your system.

Cause I haven't any idea how your project looks like (which database your using for example) the implementation of the hook could be very variable. It would be an advantage, if you could get the list of shows for the executing user ordered by time. This would allow a fix check if any already booked show overlaps with the new one.

A possible pseudocode implementation of this check could be:

if (usersShowList.stream().filter(show -> (show.startTime < newShow.startTime && show.endTime > newShow.startTime)).count() <= 0) {
...
}

It would be really usefull if you could specifiy your problem in more detail, in case you need further comments.

weilbith
  • 564
  • 4
  • 20
  • Your condition will only catch if the new show’s *start* time falls within the time of the old show. If the old one was 20:00 to 21:30 and the new one 19:00 to 21:00, you will not catch it. See https://stackoverflow.com/questions/17106670/how-to-check-a-timeperiod-is-overlapping-another-time-period-in-java. – Ole V.V. Dec 19 '17 at 13:16
0

I give u a simple example here for and old movie and a new one.For your purpose you need to go higher and iterate over all movies.

  Calendar cal=Calendar.getInstance();

    //I just add some comments to be clear about the logic.
    //E_new dateEndStartMovie
    //E_old date dataEndOldMovie
   //S_new dateStartMovie
   //S_old dataStartOldMovie

   Date dateStartMovie=new Date();
   cal.setTime(dateStartMovie);
   cal.add(Calendar.HOUR_OF_DAY, 2);
   Date dataEndMovie=cal.getTime();

   cal.add(Calendar.HOUR_OF_DAY, -3);
   Date dataStartOldMovie=cal.getTime();
   cal.add(Calendar.HOUR_OF_DAY, 2);
   Date dataEndOldMovie=cal.getTime();

    System.out.println("data start movie"+dateStartMovie);
    System.out.println("data end movie"+dataEndMovie);
    System.out.println("data start  old movie"+dataStartOldMovie);
    System.out.println("data end old  movie "+dataEndOldMovie);
   if (

           dateStartMovie.getTime() > dataStartOldMovie.getTime() //S_new > S_old

           && dateStartMovie.getTime() < dataEndOldMovie.getTime() //S_new < E_old
           ||
           (dataEndMovie.getTime() > dataStartOldMovie.getTime() //E_new > S_old
           && dataEndMovie.getTime() < dataEndOldMovie.getTime()) //E_new < E_old
           || 
           (dataEndMovie.getTime() > dataEndOldMovie.getTime() //E_new > E_old
           && dateStartMovie.getTime()<dataStartOldMovie.getTime())//S_new < S_old
        // i think that we are missing even S_new < S_old  and F_new > F_old ( i can have more film in a bigger interval so i could not send another ticket if in the big interval the person buy a ticket
        )
           {
       System.out.println("Error.Cannot buy ticket at this time. ");
   }
lirio oliro
  • 21
  • 1
  • 7
Uta Alexandru
  • 324
  • 4
  • 11
  • 1
    Let's just all make a conscious decision and never go back to `Calendar` and `java.util.Date` classes, unless we absolutely have to. – M. Prokhorov Dec 18 '17 at 12:25
  • @M.Prokhorov, you mean if an evil boss forces us to use Java 1.4 or older? :-) – Ole V.V. Dec 18 '17 at 16:29
  • 1
    @OleV.V., at this point he's not even evil, he's just oblivious and careless. The amount of security problems alone that got fixed in the meantime easily justifies the upgrade to at least Java7. – M. Prokhorov Dec 19 '17 at 11:39
  • Good point, @M.Prokhorov. What I was trying to imply was that I agree 100 % with your conscious decision, only I have a hard time imagining the situation where we absolutely have to use `Calendar` and `Date`. – Ole V.V. Dec 19 '17 at 11:42
  • 1
    @OleV.V. for example, parts of Spring API use `Date` still (at least several scheduling-related classes that I've seen recently, and those don't have any alternate setters for those properties). However, let's not grow this any further in this comment thread, we are going off-topic. – M. Prokhorov Dec 19 '17 at 11:46
  • 1
    Uta Alexandru, your formula isn’t quite correct. If the new movie has the exact same start and end times, you will not catch it. Same if the old movie was 19:00 to 20:00 and the new one 18:30 to 21:00. See https://stackoverflow.com/questions/17106670/how-to-check-a-timeperiod-is-overlapping-another-time-period-in-java. – Ole V.V. Dec 19 '17 at 13:13
  • yer you are right , i forgot a case.sorry for that .I edited .Sorry also about using Calendar and Date. – Uta Alexandru Dec 19 '17 at 14:21
  • Certainly closer, but I think you are overcomplicating it, and I think you still don’t catch if the times are exactly the same for both movies. The correct and not-too-complicated formula is in the link in my previous comment. – Ole V.V. Dec 19 '17 at 15:30