How to work with time intervall in java [duplicate] - java

This question already has answers here:
How to check a timeperiod is overlapping another time period in java
(5 answers)
Closed 2 years ago.
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.

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.

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. ");
}

Related

How to auto increment on a specific child on firebase based another data

I am using a Firebase Realtime database. I want to increment a number every time I create a data. However, I want to base it from another data. Example Year is 2000 then it will 2000-1. Then another data is 2000 it will be 2000-2. Then if the year is 2001 it will be 2001-1 since it is a different year. I want to increment the numbers. However, I want the number to restart if it has another year.
idReg:
1
idYear:
2000
newDate:
September 16, 2000
etIdYear.setEnabled(true);
setNewIdYear = newDate.substring(newDate.length() - 4);
etIdYear.setText(setNewIdYear);
newIdYear = Integer.parseInt(setNewIdYear);
newIdReg = Integer.parseInt(setNewReg);
mCustom = new Custom(newIdYear, newIdReg, newDate);
key = mDatabase.child("Custom").push().getKey();
mDatabase.child(newDate).setValue(mCustom);
I am still new to this, thank you.

How to use SQLITE Recursion to check at least once daily event in android?

I am working on an android app. It has a database that stores some events(say, when the user presses a button, that special event is stored in the database) and timestamp. I want to know if the user has pressed that button daily at least once for 5 consecutive days.
I came across this stackoverflow answer that tells me to use recursion of SQLite. I tried to use the SQLite Recursion to 'loop for 5 days' but I am getting an error. I don't understand what I am doing wrong. Help.
Here is my code:
WITH RECURSIVE
recursiveDailyEvents (startTimeMillis, endTimeMillis, eventCount) AS
(
1612244382565, 1612330782565, (select unique count from event_tracking_table where event_tracking_table_event_id = 'post_image' and 1612244382565 <= event_tracking_table_timestamp and event_tracking_table_timestamp <= 1612330782565 )
UNION ALL startTimeMillis + 86400000, endTimeMillis + 86400000 FROM recursiveDailyEvents
Limit 5)
select * from recursiveDailyEvents;
);
This is the error from sqlite browser:
near "1612244382565": syntax error: WITH RECURSIVE
recursiveDailyEvents (startTimeMillis, endTimeMillis, eventCount) AS
(
1612244382565
But I was expecting a table with startTimeMillis, endTimeMillis, and a count (1 or 0).
What am I doing wrong? Or how should I write this recursion?
Edit
Here is some sample data
event_tracking_table_row_id| event_tracking_table_event_id| event_tracking_table_timestamp
1|app_open|1612169104224
2|post_image|1612169437373
3|post_image|1612169738068
4|app_open|1612170216320
5|post_image|1612170507935
6|app_open|1612689116738
7|post_image|1612689316673
8|post_video|1612689579697
9|post_video|1612689609683
10|app_open|1612689664683
... ... ...
Here, event_tracking_table_event_id is in millisecond.
Expected output
If I understand correctly, the recursion should generate a table of start time millisecond, end time millisecond, and eventCount between those time limits.
So today (2 February 20201) at some time, the epoch time was 1612244382565, and after 24 hours, the end time is 1612330782565, and so on.
1612244382565 , 1612330782565, 1 // 1st day start time, end time, event count
1612330782565 , 1612417182565, 0 // 2nd day start time, end time, event count
... ... // 5 rows for 5 consecutive days.
I am trying my best to be as clear as possible.
If you want the starting and ending time of each day and whether the button was clicked, you can do it with conditional aggregation:
SELECT date(event_tracking_table_timestamp / 1000, 'unixepoch', 'localtime') day,
MIN(event_tracking_table_timestamp) min_time,
MAX(event_tracking_table_timestamp) max_time,
MAX(event_tracking_table_event_id = 'post_image') event,
FROM event_tracking_table
GROUP BY day
If you want the number of times the button was clicked for each day:
SELECT date(event_tracking_table_timestamp / 1000, 'unixepoch', 'localtime') day,
MIN(event_tracking_table_timestamp) min_time,
MAX(event_tracking_table_timestamp) max_time,
SUM(event_tracking_table_event_id = 'post_image') event_count
FROM event_tracking_table
GROUP BY day
If you want the rows for the last 5 days, add a WHERE clause before GROUP BY day:
WHERE date(event_tracking_table_timestamp / 1000, 'unixepoch', 'localtime') >=
date('now', '-5 days', 'localtime')
See the demo.

How to use If else statement to create burst report for performance review?

I have a real industry problem with the employee work performance report. In the table, we have the data items as follows:
EmpName EmpDue ManagerDue Manager2Due ReviewStatus SubStatus
A 11/05/19 11/12/19 11/19/19 Not Started EmployeeAssessment
B 11/19/19 11/26/19 12/3/19 Not Started EmployeeAssessment
C 11/01/19 11/08/19 11/15/19 In Progress Manager Assessment
D 11/01/19 11/08/19 11/15/19 In Progress Manager2 Acknowledgement
E 10/3/19 10/10/19 10/17/19 Completed
So, if the Review Status is Not Started, and Sub Status is EmployeeAssessment, it means, the employee himself has not started to review himself.
Expected Result:
We would like to send out the reminder 7 days before the employee's due date. If the employee doesn't take any action and the due day passed, then we would like to send out the reminder every 3 days(which means 3, 6, 9...) after the due date.
Once the employee reviewed himself, then it comes to the next stage, which means the ReviewStatus will change to In Progress, and the sub status will be Manager Assessment. At this point, we would like the direct manager to review the employee. If the manager didn't do so and the manager due date passed, then we also would like to send out the reminder every 3 days.
After the employee and manager did the review, then it turns to the manager2 to review. the Review status will still be In Progress,the sub Status will be Manager2 Acknowledgement, the same reminder will send out to manager2.
After all the people completed, then the review status will become Completed.
Due to the real working platform, I don't know how to apply if-else statements in this case, especially for the 3 days rotation.
Check the mock solution below, feel free to ask questions:
# check the conditions below avery day at 00:00, take info from db and check it in your script
if ReviewStatus == "Not Started":
if curremt_date =< EmpDueDate:
if EmpDue - current_date == 7:
send_message()
else:
if (curremt_Date - EmpDueDate).daysnumber % 3 == 0:
send_message()
elif ReviewStatus == "Progress" and SubStatus == "Manager Assessment":
if curremt_date > ManagerDueDate:
if (curremt_Date - ManagerDueDate).daysnumber % 3 == 0:
send_message()
elif ReviewStatus == "Progress" and SubStatus == "Acknowledgement":
if curremt_date > Manager2DueDate:
if (curremt_Date - Manager2DueDate).daysnumber % 3 == 0:
send_message()
else:
raise Exception("Something Unexpected here !")

BigQuery WORM work-around for updated data

Using Google's "electric meter" example from a few years back, we would have:
MeterID (Datastore Key) | MeterDate (Date) | ReceivedDate (Date) | Reading (double)
Presuming we received updated info (Say, out of calibration/busted meter, etc.) and put in a new row with same MeterID and MeterDate, using a Window Function to grab the newest Received Date for each ID+MeterDate pair would only cost more if there is multiple records for that pair, right?
Sadly, we are flying without a SQL expert, but it seems like the query should look like:
SELECT
meterDate,
NTH_VALUE(reading, 1) OVER (PARTITION BY meterDate ORDER BY receivedDate DESC) AS reading
FROM [BogusBQ:TableID]
WHERE meterID = {ID}
AND meterDate BETWEEN {startDate} AND {endDate}
Am I missing anything else major here? Would adding 'AND NOT IS_NAN(reading)' cause the Window Function to return the next row, or nothing? (Then we could use NaN to signify "deleted".)
Your SQL looks good. Couple of advices:
- I would use FIRST_VALUE to be a bit more explicit, but otherwise should work.
- If you can - use NULL instead of NaN. Or better yet, add new BOOLEAN column to mark deleted rows.

versionone: get defects with project and change date filter from version one java sdk

I am looking to retrieve list of all defects that belongs to project A and that have modified in last 24 hours. I used the given below code to retrieve list of all defects that belongs to project(project is a string variable containing name of the project)
scope=defectType.getAttributeDefinition("Scope.Name");
FilterTerm toDoTerm = Query.term(scope);
toDoTerm.Equal(project);
Now i want to add one more condition, list only those defects that have been changed in last 24 hours.
I checked the versionone api but could not find any relevant java source code/example or function name.
How can i add conditions like "and" or "or" ?
You can use the GroupFilterTerm to concatenate filters (And or Or) like this:
FilterTerm toDoTerm = new FilterTerm(nameAttribute);
toDoTerm.equal(project);
FilterTerm dateFilter = new FilterTerm(dateAttribute);
Calendar c = Calendar.getInstance();
dateFilter.lessOrEqual(c.getTime());
GroupFilterTerm groupFilter = new AndFilterTerm(toDoTerm, dateFilter);
Collection<IAttributeDefinition> attributesToQuery = new LinkedList<IAttributeDefinition>();
attributesToQuery.add(nameAttribute);
attributesToQuery.add(dateAttribute);
query = new Query(storyAsset.getOid().getMomentless(), true);
query.getSelection().addAll(attributesToQuery);
query.setFilter(groupFilter);
In this example I am querying Defects that are in a specific project, and where the Defect ChangeDate (dateAttribute) value is less than or equal to today.
You´ll need to take into account how Java handles Date variables when you do the comparison.

Categories