views:

116

answers:

3
Aries          March 21 to April 20.
Taurus         April 21 to May 20.
Gemini         May 21 to June 21.

I need to print the Astrological sign of a user by getting the user's birth month and the date as inputs.how can i get the date range??.

EX: March 21 to April 20

A: 

Create a class that describes a star sign...

class StarSign
{
    public string Name {get;set;}

    public DateTime StartDate {get;set;}

    public DateTime EndDate {get;set;}

    public bool CoversDate(DateTime birthday)
    {
        return birthday <= this.EndDate && birthday >= this.StartDate;
    }
}
Kirk Broadhurst
And how well will this work for Capricorn? December 22 -January 19??
Hightechrider
I'm not very much clear on your answer.can u please explain bit more.
chamara
@`Kirk Broadhurst` - This doesn't answer the question as stated. I read it as requiring a function with this signature: `StarSign GetStarSign(int birthMonth, int birthDay)`. You solution doesn't take in to account that `DateTime` has a year and also a time component. If I passed in `#5/20/2010 11:23#` in to your `CoversDate` method I would not get any matching `StarSign`, even ignoring the issue with year.
Enigmativity
Sheesh it's not ready to drop into your solution, it's just an example of a class that describes a star sign.
Kirk Broadhurst
@Hightechrider It will work for Capricorn once it has some handling for years; and it needs to accept `day` and `month` parameters. The point of the answer is that he could use a class to describe a star sign, and it's a 1 minute pull together of a class that starts to do that.
Kirk Broadhurst
+5  A: 

You don't actually need to construct a datetime range to solve this. A simple switch statement based on the month with a simple if statement for each month that returns one of two star signs will suffice.

   e.g
     switch (month)
     {
       case 1:
          if (day <20) return "Capricorn"; else return "Aquarius";
          break;
       case 2:
          ...
Hightechrider
yap...thanks 4 da answer
chamara
A: 

I know I missed the boat on getting the accepted answer, but after giving Kirk Broadhurst a lashing I thought I better provide my own answer.

My reading of the question was that chamara wanted something like this:

var birthDate = new DateTime(1923, 4, 20);
var starSign = StarSigns.GetFor(birthDate);

Console.WriteLine(starSign); // Taurus (April 20 - May 20)
Console.WriteLine(starSign.GetStartDate(2010)); // 2010/04/20 00:00:00
Console.WriteLine(starSign.GetEndDate(2010)); // 2010/05/20 23:59:59

var starSign1 = StarSigns.GetFor(10, 22);
var starSign2 = StarSigns.GetFor(10, 23);

Console.WriteLine(starSign1); // Libra (September 23 - October 22)
Console.WriteLine(starSign2); // Scorpio (October 23 - November 21)

So here are my classes:

public static class StarSigns
{
    private static StarSign[] _starSigns;

    static StarSigns()
    {
        var names = new[]
        {
            "Aquarius", "Pisces", "Aries", "Taurus",
            "Gemini", "Cancer", "Leo", "Virgo",
            "Libra", "Scorpio", "Sagittarius", "Capricorn", 
        };

        var days = new[]
        {
            20, 18, 20, 20,
            21, 21, 22, 23,
            23, 23, 22, 22,
        };

        _starSigns = (from i in Enumerable.Range(0, 12)
                      let name = names[i]
                      let startMonth = i + 1
                      let startDay = days[i]
                      let endDay = days[(i + 1) % 12] - 1
                      select new StarSign(name, startMonth, startDay, endDay)).ToArray();
    }

    public static StarSign GetFor(DateTime birthDate)
    {
        return (from starSign in _starSigns
                let startDate = starSign.GetStartDate(birthDate.Year)
                let endDate = starSign.GetEndDate(birthDate.Year)
                where startDate <= birthDate
                where endDate >= birthDate
                select starSign).Single();
    }

    public static StarSign GetFor(int birthMonth, int birthDay)
    {
        return GetFor(new DateTime(2010, birthMonth, birthDay));
    }

}

public class StarSign
{
    public StarSign(string name, int startMonth, int startDay, int endDay)
    {
        this.Name = name;
        this.StartMonth = startMonth;
        this.StartDay = startDay;
        this.EndDay = endDay;
    }

    public string Name { get; private set; }

    public int StartDay { get; private set; }
    public int EndDay { get; private set; }

    public int StartMonth { get; private set; }

    public int EndMonth
    {
        get
        {
            return this.StartMonth + 1;
        }
    }

    public DateTime GetStartDate(int year)
    {
        return new DateTime(year, this.StartMonth, this.StartDay);
    }

    public DateTime GetEndDate(int year)
    {
        var nextStart = new DateTime(year, (this.StartMonth % 12) + 1, this.EndDay + 1);
        return nextStart.Subtract(TimeSpan.FromTicks(1));
    }

    public string Period
    {
        get
        {
            var startDate = this.GetStartDate(2010);
            var endDate = this.GetEndDate(2010);
            var template = "{0:MMMM dd} - {1:MMMM dd}";
            return String.Format(template, startDate, endDate);
        }
    }

    public override string ToString()
    {
        return String.Format("{0} ({1})", this.Name, this.Period);
    }
}

Enjoy!

Enigmativity
Remember the KISS principle.
jmservera
@`jmservera` - Yes, the API is pretty darn simple. The implementation is minimal, but robust. It would be hard to abuse this class too much. So, I agree that it is an excellent example of KISS in action.
Enigmativity