programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# divide employees into two teams

Aishwary Srivastava
Greenhorn
Posts: 2
Hi,
I am new to Java and is someone can help me how I can start on this code please. I need to divide employees into two teams and each team will be performing various activities. Activities list I am reading through a file. PFB the code for that. I am stuck now how should divide employees into two teams and let both teams to perform various activities.

Activities are :
Cricket 2020 60min
Football 40min
Hockey 30min
Quiz 60min
Race 45min
Presentation 60min
Lunch
test

Regards,
Aish

Les Morgan
Rancher
Posts: 779
19
Dividing in two is a binary task, you should think along that line: you need a flag to show a state as you go through your loop;

or something like this:

Both of those give you a binary condition so you can make a choice.

Your activities I will assume are going to be assigned randomly, so that should suggest a random number:

Something along the lines of that.

Winston Gutkowski
Bartender
Posts: 10575
66
Aishwary Srivastava wrote:I am new to Java and is someone can help me how I can start on this code please. I need to divide employees into two teams and each team will be performing various activities.

Hi Aishwary, and welcome to JavaRanch.

First: when you post code, could you UseCodeTags. I've added them for you this time; see how much better it looks?

You've said that you need to divide your employees, but you haven't said how; and there are several ways you could do it.
Assuming your employees are in a List of some kind, you could:
1. Find out how many there are (size()), halve it to give you a "group size" G, then put the first G employees in the first group and the rest in the second.
2. Similar to (1), except shuffle() your employee List first. This will give you a more "random" selection.
Note: #2 will probably work faster if your List also implements RandomAccess - eg, an ArrayList (←click).

HIH

Winston

Liutauras Vilda
Sheriff
Posts: 4928
334
I'm sorry, a bit off topic, but do you have Employee and Team classes? If you haven't reach OO techiques in your course yet, forget it for now then

And you don't need to write testFile.toString() as you did, because testFile is a string already when it arrives to readActivities method.

Aishwary Srivastava
Greenhorn
Posts: 2
Thanks for the response guys.

Employees are not defined . There could be any number of the employees and will be divided in to various teams.
Activities start from 9am until lunch is served at 12 noon . Activities resume from 1pm and must finish in time for a presentation to be delivered by external speaker on “Staff Motivation" . The presentation can start no earlier than 4:00 and no later than 5:00 5. All activity lengths are either in minutes (not hours) or sprint (15 minutes) . Due to high spirit of the team there needs to be no gap between each activity.

So this I need to do, I hope this will be clear now.

Campbell Ritchie
Marshal
Posts: 56598
172
There has to be a Java8 way to do this without needing loops and explicit Lists. I am going to assume you have a Player class with a getGame method and a constructor which accepts the entire line from the file.
Note that you can use try with resources to obviate the necessity to write close(). And you can obtain a Stream from a buffered reader:-If you look for the BufferedReader#lines() method, you find it returns a Stream<String> which goes through all your input from the file line by line. That information is not in a form you can use yet. What you can do next is convert the Stream<String> to a Stream<Player>. You can do that simply by telling the JVM to use the individual Strings and pass them to a Player constructor. You can get that with the :: operator. Put the class name to the left of the :: and something to say use the constructor on the right. You can't simply write Player in case there is a method like that, so they decided to reuse the new keyword. The map method creates a new Stream of a different type.You an do all sorts of things with a Stream. Let's try collecting its elements into a Map. You need to get a Collector object, but sometimes the easiest way to get a Collector is to use a method of the Collectors class. I think the groupingBy() method produces such a Collector which will do what you want.If you tell the groupingBy method "Player::getGame", it will apply the getGame method to the Player objects in the Stream and produce a Map. That Map has a String (if getGame returns a String) as its “K” and collects all possible Player objects as its “V”. But you cannot have one “K” pointing to multiple “V”s. It therefore uses a List and that method returns a Map<String, List<Player>>.You can get that all to work without having to import Stream; you do have to import Collectors.Now you have a Map which you can use to find who plays which game and you are hitting the ground running

Campbell Ritchie
Marshal
Posts: 56598
172
You need two algorithms together. One to collect all the people who play games, give presentations, etc. You can append a duration of an activity to that.
The other is a scheduling algorithm to fit everything into the timetable.