Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

July 6, 2009

Zodiac Sign: ZodiacSigns Class

Filed under: C#,LINQ,OOP @ 4:53 pm

This entry details the implementation of the ZodiacSigns class from this example in C#:

public class ZodiacSigns : List<ZodiacSign>
{

}

Constructor

The following is the constructor defined in the ZodiacSigns class:

public ZodiacSigns()
{
    InitializeCollection();
}

This constructor ensures that the collection of zodiac signs is initialized when an instance of this class is created.

Methods

The following are the methods in the ZodiacSigns class:

public string FindSign(DateTime desiredDate)
{
    // Find the name of the Zodiak sign with the date within the ranges
    var query = from z in this
                from d in z.DateRanges
                where (desiredDate >= d.StartDate) &&
                       
(desiredDate <= d.EndDate)
                select z.Name;
    string name = query.FirstOrDefault();
    return name;
}

The FindSign method uses LINQ to find the date within the defined ranges. It returns the name of the appropriate sign.

private void InitializeCollection()
{
    // This could potentially read all of these from a file.
    // NOTE: This data may not be accurate
    this.Add(new ZodiacSign("Rat",
        new List<DateRange> {
            new DateRange(new DateTime(1996, 2,19),
                new DateTime(1997, 2,6)),
            new DateRange(new DateTime(2008, 2,7),
                new DateTime(2009, 2,25))}));
    this.Add(new ZodiacSign("Ox",
        new List<DateRange> {
            new DateRange(new DateTime(1997, 2,7),
                new DateTime(1998, 2,27)),
            new DateRange(new DateTime(2009, 2,26),
                new DateTime(2010, 2,13))}));
    this.Add(new ZodiacSign("Tiger",
        new List<DateRange> {
            new DateRange(new DateTime(1998, 2,28),
                new DateTime(1999, 2,15)),
            new DateRange(new DateTime(2010, 2,14),
                new DateTime(2011, 2,2))}));
    this.Add(new ZodiacSign("Rabbit",
        new List<DateRange> {
            new DateRange(new DateTime(1999, 2,16),
                new DateTime(2000, 2,4)),
            new DateRange(new DateTime(2011, 2,3),
                new DateTime(2012, 1,22))}));
    this.Add(new ZodiacSign("Drago",
        new List<DateRange> {
            new DateRange(new DateTime(2000, 2,5),
                new DateTime(2001, 2,23)),
            new DateRange(new DateTime(2012, 1,23),
                new DateTime(2013, 2,9))}));
    this.Add(new ZodiacSign("Snake",
        new List<DateRange> {
            new DateRange(new DateTime(2001, 1,24),
                new DateTime(2002, 2,11)),
            new DateRange(new DateTime(2013, 2,10),
                new DateTime(2014, 1,30))}));
    this.Add(new ZodiacSign("Horse",
        new List<DateRange> {
            new DateRange(new DateTime(2002, 2,12),
                new DateTime(2003, 1,31)),
            new DateRange(new DateTime(2014, 1,31),
                new DateTime(2015, 2,18))}));
    this.Add(new ZodiacSign("Sheep",
        new List<DateRange> {
            new DateRange(new DateTime(2003, 2,1),
                new DateTime(2004, 2,21)),
            new DateRange(new DateTime(2015, 2,19),
                new DateTime(2016, 2,7))}));
    this.Add(new ZodiacSign("Monkey",
        new List<DateRange> {
            new DateRange(new DateTime(2004, 1, 22),
                new DateTime(2005, 2, 8)),
            new DateRange(new DateTime(2016, 2, 8),
                new DateTime(2017, 1, 27))}));
    this.Add(new ZodiacSign("Rooster",
        new List<DateRange> {
            new DateRange(new DateTime(2005, 2, 9),
                new DateTime(2006, 1, 28)),
            new DateRange(new DateTime(2017, 1, 28),
                new DateTime(2018, 2, 15))}));
    this.Add(new ZodiacSign("Dog",
        new List<DateRange> {
            new DateRange(new DateTime(2006, 1, 29),
                new DateTime(2007, 2, 17)),
            new DateRange(new DateTime(2018, 2, 16),
                new DateTime(2019, 2, 4))}));
    this.Add(new ZodiacSign("Pig",
        new List<DateRange> {
            new DateRange(new DateTime(2007, 2, 18),
                new DateTime(2008, 2, 6)),
            new DateRange(new DateTime(2019, 2, 5),
                new DateTime(2020, 2, 21))}));
}

The InitializeCollection method does exactly what it sounds like … creating the set of date ranges associated with each zodiac sign. This code takes advantage of the list initializers now available in C#.

Enjoy!

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2020 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs