gigz
gigz

Reputation: 942

Java Rotation Algorithm

I work in a small energy company and i was asked to create a method that would be called to determine which of our technicians would take home the company off hours phone.

we have 2 tables i could consider

the Technicians table

id           name
--------------------
001         George
002         James
003         Wesley
004         Horace
005         Jason
006         Nice

the Leaves table

id        leaveDate
--------------------
005       2020/12/29
005       2020/12/30
002       2021/01/05
002       2021/01/13
003       2021/01/13
005       2021/01/22

History table *not existing yet

id        techPhoneDate
--------------------
001       2021/01/04
003       2021/01/05

i could create a new table with the phone history to keep tabs on who historically were assigned to it, i just want to have something that would make sure the rotation is fair and would skip the person if on leave and would assign them on the next possible day

i already have code that would return an error if its a weekend, and if the history table already has a calculated technician for that day, it would just return that technician. it should also consider in calculation if a new technician would be added.

I initially thought it would be an easy method, but how could i go about finding who's in charge of the phone for today?

private List<Technicians> techs;
private List<PhoneHistory> phoneHistory;
private List<Leaves> leaves;


public RotationResponse getTechOnDuty(String rotationDate) {

        RotationResponse resp = new RotationResponse();

        try {

        resp.setRotationDate(rotationDate);
        //check if its a weekend
        resp = validateDate(resp);

        if (resp.getErrorMessage() != null) {
            return resp;
        }

        String techId = findTechIdForDate(rotationDate); // find in history if there is an existing tech already calculated

        if (StringUtils.isNotBlank(techId)){
            resp.setTechnician(findTechById(techId));
        } else {
            resp.setTechnician(findTechById(getTechPhoneRotationForToday(rotationDate)));
        }


        return resp;

        } catch (Exception e) {
            return resp.setErrorMessage(e.getMessage());
        }


    }


private String getTechPhoneRotationForToday(String rotationDate){
// TODO
}

Upvotes: 0

Views: 102

Answers (1)

Reto H&#246;hener
Reto H&#246;hener

Reputation: 5808

Maybe:

  • keep a table with team join and leave dates for each technician (when did they start working for the company)
  • define a period to analyze for making a decision, e.g. the past year
  • iterate through that period and for each workday:
    • count the # of technicians in the team on that day
    • calculate the proportional phone 'obligation' as 1 / # of technicians
    • sum this obligation for each technician
  • deduct 1 from that 'obligation sum' for each day where the technician had phone duty
  • then assign for the next day the technician who is not on leave and has the highest obligation
  • probably also avoid consecutive duty days: use the person with the next highest obligation

determining whether a day was a workday is itself probably challenging

Upvotes: 1

Related Questions