Monday, July 8, 2013

Day of the Week library

Some applications may need a Day Of The Week object. The application may be a business tool that may need to track something based on the day of the week, such as business hours. The application I am designing is using a bus schedule, which varies depending on if it's a weekday or on a weekend. A useful feature of such a class is: given today, what is tomorrow? It works the other way as well. If today is Tuesday, was yesterday a weekday? Is four days from today on a weekend?

DaysOfTheWeekContract.cs


namespace Library.Contracts {
public enum DaysOfTheWeek {
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
}
public enum DayClassification { WeekDay, WeekEnd }
public interface IDay {
DaysOfTheWeek DayOfTheWeek { get; }
DayClassification Classification { get; }
IDay Next { get; set; }
IDay Previous { get; set; }
}
public interface IWeek {
DayClassification Classification(DaysOfTheWeek day);
DaysOfTheWeek Tomorrow(DaysOfTheWeek today);
DaysOfTheWeek Yesterday(DaysOfTheWeek today);
}
}



DaysOfTheWeekImp.cs

using Library.Contracts;
namespace Library.Impl {
public sealed class Day : IDay {
public DaysOfTheWeek DayOfTheWeek { get; private set; }
public DayClassification Classification { get; private set; }
public Day(DaysOfTheWeek dayOfTheWeek, DayClassification classification) {
this.DayOfTheWeek = dayOfTheWeek;
this.Classification = classification;
}
public IDay Next { get; set; }
public IDay Previous { get; set; }
}


public sealed class Week : IWeek {
private IList<IDay> Days;
public Week() {
this.Days = new List<IDay>();
IDay sunday = new Day(DaysOfTheWeek.Sunday, DayClassification.WeekEnd);
IDay monday = new Day(DaysOfTheWeek.Monday, DayClassification.WeekDay);
IDay tuesday = new Day(DaysOfTheWeek.Tuesday, DayClassification.WeekDay);
IDay wednesday = new Day(DaysOfTheWeek.Wednesday, DayClassification.WeekDay);
IDay thursday = new Day(DaysOfTheWeek.Thursday, DayClassification.WeekDay);
IDay friday = new Day(DaysOfTheWeek.Friday, DayClassification.WeekDay);
IDay saturday = new Day(DaysOfTheWeek.Saturday, DayClassification.WeekEnd);
sunday.Previous = saturday;
sunday.Next = monday;
monday.Previous = sunday;
monday.Next = tuesday;
tuesday.Previous = monday;
tuesday.Next = wednesday;
wednesday.Previous = tuesday;
wednesday.Next = thursday;
thursday.Previous = wednesday;
thursday.Next = friday;
friday.Previous = thursday;
friday.Next = saturday;
saturday.Next = sunday;
saturday.Previous = friday;

this.Days.Add(sunday);
this.Days.Add(monday);
this.Days.Add(tuesday);
this.Days.Add(wednesday);
this.Days.Add(thursday);
this.Days.Add(friday);
this.Days.Add(saturday);
}
public DayClassification Classification(DaysOfTheWeek today) {
return this.Days[(int)(today)].Classification;
}
private int indexOfToday(DaysOfTheWeek today) {
return (int)(today);
}
public DaysOfTheWeek Tomorrow(DaysOfTheWeek today) {
return this.Days[(int)(today)].Next.DayOfTheWeek;
}
public DaysOfTheWeek Yesterday(DaysOfTheWeek today) {
return this.Days[(int)(today)].Previous.DayOfTheWeek;
}
}
}


Your code would look like this:


:
:
IWeek week = new Week();
DaysOfTheWeek today = DaysOfTheWeek.Wednesday; // munge this from DateTime
int count = 0;
while (week.Classification(today) != DayClassification.WeekEnd) {
today = week.Tomorrow(today);
count++;
}
// count contains the number of days until the weekend.


No comments:

Post a Comment