This discussion is archived
4 Replies Latest reply: Jan 23, 2011 1:50 PM by jschellSomeoneStoleMyAlias RSS

Expected Date  Calculation

832622 Newbie
Currently Being Moderated
Hi All,

Am facing one design issue, here my requirement.

Am developing a component which calculates expected date. Considering some days in a weeks as holiday always. Input to the component is an integer and some other object[from which I want to find which days in a week are non-working days, for example I want to convey Sunday & Saturday as non-working day].

Please suggest me how can I pass this message efficiently.

My thoughts are:
1. Passing a List<Integer> which holds day numbers[sun-sat will ranges 1-7, like that]. If 2 & 5 are present then it means Tuesday & Fridays are non-working days.
2. Passing a byte. [assuming first 7 bits represents days in week]

Thanks,
GK
  • 1. Re: Expected Date  Calculation
    tschodt Pro
    Currently Being Moderated
    [url http://download.oracle.com/javase/6/docs/api/java/util/EnumSet.html]EnumSet maybe?
  • 2. Re: Expected Date  Calculation
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    Joda-Time might help with this, but I haven't looked.

    http://joda-time.sourceforge.net/
    Considering some days in a weeks as holiday always
    At least in the US one must consider that that would require a data driven calendar that specifies holidays. For example last year December 23 was a holiday for me because Christmas fell on a Saturday.
  • 3. Re: Expected Date  Calculation
    jduprez Pro
    Currently Being Moderated
    Hello,
    Considering you posted that in the "Design and Patterns" section, you are concerned with the flexibility of the design with regards to requirements.
    Like jschell, I suspect it's unlikely that the only days off in your scheduling are fixed-days-of-week (but now, I m' French, so I may be biased :o).

    If you know that you have to support other kinds of day-off constraints, you could design an interface for objects that can tell (a method with a boolean result) whether a given day (specified as a full Date passed as an argument to the method) is off. That way, when you have to support one more "day-off" rule, you can just code a new implementation of the method.

    Supporting several rules is a matter of asking each of such classes, and combine the results. For example:
    public class Scheduler {
        List<DayOffRule> allRules;
    
        public boolean canWeWorkThatDay(Date date) {
            for (DayOffRule rule : allRules) {
                if (rule.isDayOff(date)) {
                    return false;
                }
            }
            return true;
        }
    
        ... populate rules...
    }
    
    public interface DayOffRule{
        public boolean isdayOff(Date date);
    }
    
    FixedDayOfWeekOffRule implements DayOffRule {
        public FixedDayOfWeekOffRule(DayEnum singleDayOfWeek) {...}
        public boolean isdayOff(Date date) {
            ...
        }
    }
    
    USCompensateChristmasDayOffRule implements DayOffRule {
        ...
    }
  • 4. Re: Expected Date  Calculation
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    jduprez wrote:
    If you know that you have to support other kinds of day-off constraints, you could design an interface for objects that can tell (a method with a boolean result) whether a given day (specified as a full Date passed as an argument to the method) is off. That way, when you have to support one more "day-off" rule, you can just code a new implementation of the method.
    It depends on the business but at least some places will not be able to code rules. It requires that a real person marks a calendar somehow and then that is consumed by the application.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points