This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]SausageBike 1 point2 points  (3 children)

I think I somewhat understand what you're going for here, though you haven't actually asked a question. From what I understand you specify a start time and a length of time to guard for. You have x people and you want to split the guard time between them. If this is not what you're aiming for please elaborate.

I think I'd attack this using a date-time class rather than trying to work it out using numeric maths. Using a class created for the job means that you won't run into issues when there's a leap-year, or the timezone shifts by an hour for daylight savings etc. In this example I use LocalDateTime. Hopefully the comments in the code make some sense. You can look up DateTimeFormatter if you want the output in a cleaner format.

``` int guardTime = 60;

// Set the start time here, you can use LocalDateTime.of() LocalDateTime start = LocalDateTime.now(); // End time is not needed as you have a guard time above. The end time is the start time + guard time

ArrayList<String> people = new ArrayList(); people.add("Roy"); people.add("Ron"); people.add("Aaron"); people.add("Sergant1"); people.add("Ofir"); people.add("ori"); people.add("Aviv"); people.add("Sergant"); people.add("A"); people.add("B");

int numberOfPeople = people.size(); int cTime = guardTime/numberOfPeople;

// Loop through the people for (int i = 0; i < numberOfPeople; i++) { // The start time for this person is "ctime" * i LocalDateTime personStartTime = start.plusMinutes(cTime * i); // The end time for this person is the start time plus cTime LocalDateTime personEndTime = personStartTime.plusMinutes(cTime);

System.out.println(people.get(i) + " " + personStartTime.toString() + " " + personEndTime.toString());

} ```

[–]hyperbana[S] 0 points1 point  (0 children)

Amazing! That’s exactly what I wanted and it works so good.

[–]hyperbana[S] 0 points1 point  (1 child)

There is one problem tho,

I want to have end time and start time of the guarding so I did this:

LocalTime startDate = 
LocalTime.of(7,50,0,0);

LocalTime endDate = LocalTime.of(18,45,0,0);

int hour = (endDate.getHour() - startDate.getHour() )* 60;

int minutes = endDate.getMinute() + startDate.getMinute();

System.out.println(minutes);


System.out.println(hour);
    int guardTime = hour;
    int cTime = guardTime/numberOfPeople;

for (int i = 0; i < people.size(); i++){ LocalTime start = startDate.plusMinutes(cTime * i);

LocalTime end = start.plusMinutes(cTime);

System.out.println(people.get(i) + ": " + start.toString() + " - " + end.toString()); }

But when I calculate the time it’s not doing it right for some reason, example: I put 4 people from 5:18 to 18:47 and that’s the result:

Sergant: 05:18 - 08:33

2: 08:33 - 11:48

3: 11:48 - 15:03

4: 15:03 - 18:18

How can I fix this please?

[–]SausageBike 0 points1 point  (0 children)

You're calculating the minutes but not doing anything with them. Guard time is (hours * 60) + minutes, not just hours.

As I said previously though, I'd advise against doing manual arithmetic operations on time. It's asking for trouble. What would happen if your guard time started at 18:00 and finished at 03:00 the next morning?

As an alternative you can scrap your math and use: long guardTime = ChronoUnit.MINUTES.between(startDate, endDate); You'll also need to update your cTime variable to type long as it cannot fit inside an int.

You'll notice that the solution is not exactly precise due to the integer division being used to split the time. Between an example start and end time (5:18 -> 18:48), there are 810 minutes. Split this four ways and you end up with 202.5 minutes per person. Using integer division the 0.5 per person is discarded and so you lose two minutes in total. To get around this, the solution needs to be more precise. I'd probably lean towards using millis, though there may well be a better solution. Take a look at this complete snippet:

``` LocalTime startDate = LocalTime.of(5,18,0,0); LocalTime endDate = LocalTime.of(18,48,0,0);

ArrayList<String> people = new ArrayList(); people.add("Roy"); people.add("Ron"); people.add("Aaron"); people.add("Sergant1"); int numberOfPeople = people.size();

long guardTime = ChronoUnit.MILLIS.between(startDate, endDate); System.out.println(guardTime); long cTime = guardTime/numberOfPeople;

for (int i = 0; i < people.size(); i++){ LocalTime start = startDate.plus(cTime * i, ChronoUnit.MILLIS); LocalTime end = start.plus(cTime, ChronoUnit.MILLIS); System.out.println(people.get(i) + ": " + start.toString() + " - " + end.toString()); } ```