0

I have an ArrayList of custom made Users. The list is already sorted by manager.My goal is to go through the list by manager and add each user to the body of an email depending on their expiration date.

A User is basically built like this from the database. All necessary accessors/mutators are present:

id|fName|lName|...|manager

Go through the users and notify the manager if the user is expiring:

To: Manager

Expiring in 10 days
<User>
<User>
Expiring in 30 days
<User>

StringBuilder body = new StringBuilder();
ArrayList<Users> contractors;
Date today = cal.getTime();
...
if(contractors != null && contractors.size() > 0){

 for(int i = 0; i < contractors.size(); i++){

  if(i+1 > contractors.size()){
   //do something to avoid outOfBounds and still access last item in the list
  }else{

   if (contractors.get(i+1).getManager() != null){ 
    if(manager.equals(contractors.get(i+1).getManager())){
     if(today.compareTo(contractor.getExpiration()){
       //build body of email
     }
    }
  }
  sendEmail(manager, body.toString());
 }else{
  //new manager
  body.equals(""); // reset email for next run
}
}

After the email is sent I want to move on to the next set of users based on manager. My problem is that I'm having trouble with the logic behind traversing the array by manager and then resetting everytime for each new manager. I'm thinking that I need another for loop?

What's the best way to do this? thanks

Edit

When implemented this way:

4 Answers4

0

Iterate the list of Users and add them to a Map keyed by manager with a Set of employees per manager. Something like,

if (contractors != null && contractors.size() > 0) {
    Map<String, Set<Users>> map = new HashMap<>();
    for (Users contractor : contractors) {
        String manager = contractor.getManager();
        if (manager == null) {
            manager = contractor.getName();
        }
        Set<Users> employees = map.get(manager);
        if (employees == null) {
            employees = new HashSet<>();
            map.put(manager, employees);
        }
        employees.add(contractor);
    } // now you can iterate the keySet and then each manager's employees like
    for (String manager : map.keySet()) {
        Set<Users> employees = map.get(manager);
        for (Users u : employees) {
            // ...  
        }
    }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You should go for a "foreach" loop on contractors, more info here

another example here

Community
  • 1
  • 1
0

I would do it something like this:

if (contractors != null) {
    String currentManager = null;

    for (User contractor : contractors) {
        String contractorManager = contractor.getManager();

        if (!contractorManager.equals(currentManager) {
            // a new manager
            if (currentManager != null) {
                sendEmail(body.toString());
                body.setLength(0);
            }
            currentManager = contractorManager;
        }
        //build body of email ...
    }

    // send the email for the last manager
    sendEmail(body.toString());
}
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • why are you doing `sendEmail(body.toString());` in the `currentManager != null`? – BarryDale2014 Dec 02 '14 at 16:11
  • `currentManager == null` is true at the beginning of the first iteration of the loop, consistent with the fact that at that point there isn't any "current" manager (and per that variable's initialization). You don't want to send e-mail for the zero contractors processed up to that point. After the first iteration, `currentManager` is never `null`. – John Bollinger Dec 02 '14 at 18:09
  • @John_Bollinger If implemented this way, the system sends an email for every user regardless. Instead of adding the appropriate user to each body for each manager and sending a single email with all the users. And then resetting – BarryDale2014 Dec 03 '14 at 16:30
  • @BarryDale2014, not so. An email is sent only `if (!contractorManager.equals(currentManager))` -- which happens only when the contractor just obtained from the list has a different manager than the previous one(s) -- and at the end, for the final group. This does depend for its correctness on the list being sorted by manager, but you said it already was. – John Bollinger Dec 04 '14 at 14:28
0

If they are already sorted, you can take this approach (I'm using "ManagerObject" to represent the return type of Users.getManager() - replace with the actual class name):

StringBuilder body = new StringBuilder();
ManagerObject currentManager = null;
for (Users contractor : contractors) {
  if (currentManager != null && !(contractor.getManager().equals(currentManager)) {
    sendEmail(body.toString());
    body.equals("");
  }
  currentManager = contractor.getManager();
  // Add stuff to the body for this contractor
}

If the call to Users.getManager() is computationally expensive for some reason, this can be rejiggered to only set the currentManager upon a change in value

rchang
  • 5,150
  • 1
  • 15
  • 25